Example #1
0
        public override bool Execute(TransactionResultCapsule result)
        {
            long fee = CalcFee();

            try
            {
                ExchangeCreateContract exchange_create_contract = this.contract.Unpack <ExchangeCreateContract>();
                AccountCapsule         account = this.db_manager.Account.Get(exchange_create_contract.OwnerAddress.ToByteArray());

                byte[] first_token_id       = exchange_create_contract.FirstTokenId.ToByteArray();
                byte[] secodn_token_id      = exchange_create_contract.SecondTokenId.ToByteArray();
                long   first_token_balance  = exchange_create_contract.FirstTokenBalance;
                long   second_token_balance = exchange_create_contract.SecondTokenBalance;
                long   new_balance          = account.Balance - fee;

                account.Balance = new_balance;

                if (first_token_id.SequenceEqual(COMPARE_CHARICTOR))
                {
                    account.Balance = new_balance - first_token_balance;
                }
                else
                {
                    account.ReduceAssetAmountV2(first_token_id, first_token_balance, this.db_manager);
                }

                if (secodn_token_id.SequenceEqual(COMPARE_CHARICTOR))
                {
                    account.Balance = new_balance - second_token_balance;
                }
                else
                {
                    account.ReduceAssetAmountV2(secodn_token_id, second_token_balance, this.db_manager);
                }

                long id  = this.db_manager.DynamicProperties.GetLatestExchangeNum() + 1;
                long now = this.db_manager.GetHeadBlockTimestamp();
                if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0)
                {
                    ExchangeCapsule exchange = new ExchangeCapsule(
                        exchange_create_contract.OwnerAddress,
                        id,
                        now,
                        first_token_id,
                        secodn_token_id);

                    exchange.SetBalance(first_token_balance, second_token_balance);
                    this.db_manager.Exchange.Put(exchange.CreateDatabaseKey(), exchange);

                    if (!first_token_id.SequenceEqual(COMPARE_CHARICTOR))
                    {
                        string first_id = this.db_manager.AssetIssue.Get(first_token_id).Id;
                        first_token_id = Encoding.UTF8.GetBytes(first_id);
                    }
                    if (!secodn_token_id.SequenceEqual(COMPARE_CHARICTOR))
                    {
                        string second_id = this.db_manager.AssetIssue.Get(secodn_token_id).Id;
                        secodn_token_id = Encoding.UTF8.GetBytes(second_id);
                    }
                }

                ExchangeCapsule exchange_v2 = new ExchangeCapsule(
                    exchange_create_contract.OwnerAddress,
                    id,
                    now,
                    first_token_id,
                    secodn_token_id);

                exchange_v2.SetBalance(first_token_balance, second_token_balance);

                this.db_manager.ExchangeV2.Put(exchange_v2.CreateDatabaseKey(), exchange_v2);
                this.db_manager.Account.Put(account.CreateDatabaseKey(), account);
                this.db_manager.DynamicProperties.PutLatestExchangeNum(id);
                this.db_manager.AdjustBalance(this.db_manager.Account.GetBlackHole().CreateDatabaseKey(), fee);

                result.ExchangeId = id;
                result.SetStatus(fee, code.Sucess);
            }
            catch (BalanceInsufficientException e)
            {
                Logger.Debug(e.Message);
                result.SetStatus(fee, code.Failed);
                throw new ContractExeException(e.Message);
            }
            catch (InvalidProtocolBufferException e)
            {
                Logger.Debug(e.Message);
                result.SetStatus(fee, code.Failed);
                throw new ContractExeException(e.Message);
            }
            return(true);
        }