public override bool Execute(TransactionResultCapsule result)
        {
            long fee = CalcFee();

            try
            {
                ExchangeInjectContract ei_contract = this.contract.Unpack <ExchangeInjectContract>();
                AccountCapsule         account     = this.db_manager.Account.Get(ei_contract.OwnerAddress.ToByteArray());
                ExchangeCapsule        exchange    = this.db_manager.ExchangeFinal.Get(BitConverter.GetBytes(ei_contract.ExchangeId));

                byte[] first_token_id       = exchange.FirstTokenId.ToByteArray();
                byte[] second_token_id      = exchange.SecondTokenId.ToByteArray();
                long   first_token_balance  = exchange.FirstTokenBalance;
                long   second_token_balance = exchange.SecondTokenBalance;
                byte[] token_id             = ei_contract.TokenId.ToByteArray();
                long   token_quantity       = ei_contract.Quant;

                byte[] other_token_id       = null;
                long   other_token_quantity = 0;

                if (token_id.SequenceEqual(first_token_id))
                {
                    other_token_id       = second_token_id;
                    other_token_quantity = (long)Math.Floor((double)(second_token_balance * token_quantity) / first_token_balance);
                    exchange.SetBalance(first_token_balance + token_quantity, second_token_balance + other_token_quantity);
                }
                else
                {
                    other_token_id       = first_token_id;
                    other_token_quantity = (long)Math.Floor((double)(first_token_balance * token_quantity) / second_token_balance);
                    exchange.SetBalance(first_token_balance + other_token_quantity, second_token_balance + token_quantity);
                }

                long new_balance = account.Balance - CalcFee();
                account.Balance = new_balance;

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

                if (other_token_id.SequenceEqual(COMPARE_CHARICTOR))
                {
                    account.Balance = new_balance - other_token_quantity;
                }
                else
                {
                    account.ReduceAssetAmountV2(other_token_id, other_token_quantity, this.db_manager);
                }
                this.db_manager.Account.Put(account.CreateDatabaseKey(), account);
                this.db_manager.PutExchangeCapsule(exchange);

                result.ExchangeInjectAnotherAmount = other_token_quantity;
                result.SetStatus(fee, code.Sucess);
            }
            catch (ItemNotFoundException 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);
        }
Beispiel #2
0
 public async Task <TransactionExtention> ExchangeInject(ExchangeInjectContract contract)
 {
     return(await _grpcClient.ExchangeInjectAsync(contract));
 }