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); }
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); }