public override bool Execute(TransactionResultCapsule result) { long fee = CalcFee(); try { AccountCreateContract account_create_contract = contract.Unpack <AccountCreateContract>(); bool default_permission = this.db_manager.DynamicProperties.GetAllowMultiSign() == 1; AccountCapsule account = new AccountCapsule( account_create_contract, this.db_manager.GetHeadBlockTimestamp(), default_permission, this.db_manager); this.db_manager.Account.Put(account_create_contract.AccountAddress.ToByteArray(), account); this.db_manager.AdjustBalance(account_create_contract.OwnerAddress.ToByteArray(), -fee); this.db_manager.AdjustBalance(this.db_manager.Account.GetBlackHole().CreateDatabaseKey(), fee); 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(); SetAccountIdContract account_id_contract = null; try { account_id_contract = contract.Unpack <SetAccountIdContract>(); } catch (InvalidProtocolBufferException e) { Logger.Debug(e.Message); result.SetStatus(fee, code.Failed); throw new ContractExeException(e.Message); } byte[] owner_address = account_id_contract.OwnerAddress.ToByteArray(); AccountCapsule account = this.db_manager.Account.Get(owner_address); account.AccountId = ByteString.CopyFrom(account_id_contract.AccountId.ToByteArray()); this.db_manager.Account.Put(owner_address, account); this.db_manager.AccountIdIndex.Put(account); result.SetStatus(fee, code.Sucess); return(true); }
public override bool Execute(TransactionResultCapsule result) { long fee = CalcFee(); try { UpdateSettingContract us_contract = contract.Unpack <UpdateSettingContract>(); long new_percent = us_contract.ConsumeUserResourcePercent; byte[] contract_address = us_contract.ContractAddress.ToByteArray(); ContractCapsule deployed_contract = this.db_manager.Contract.Get(contract_address); deployed_contract.Instance.ConsumeUserResourcePercent = new_percent; this.db_manager.Contract.Put(contract_address, new ContractCapsule(deployed_contract.Instance)); result.SetStatus(fee, code.Sucess); } 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(); AccountPermissionUpdateContract apu_contract = null; try { apu_contract = contract.Unpack <AccountPermissionUpdateContract>(); byte[] owner_address = apu_contract.OwnerAddress.ToByteArray(); AccountCapsule account = this.db_manager.Account.Get(owner_address); account.UpdatePermissions(apu_contract.Owner, apu_contract.Witness, new List <Permission>(apu_contract.Actives)); this.db_manager.Account.Put(owner_address, account); this.db_manager.AdjustBalance(owner_address, -fee); this.db_manager.AdjustBalance(this.db_manager.Account.GetBlackHole().CreateDatabaseKey(), fee); 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 { ExchangeTransactionContract exchage_tx_contract = this.contract.Unpack <ExchangeTransactionContract>(); AccountCapsule account = this.db_manager.Account.Get(exchage_tx_contract.OwnerAddress.ToByteArray()); ExchangeCapsule exchange = this.db_manager.ExchangeFinal.Get(BitConverter.GetBytes(exchage_tx_contract.ExchangeId)); byte[] first_token_id = exchange.FirstTokenId.ToByteArray(); byte[] second_token_id = exchange.SecondTokenId.ToByteArray(); byte[] token_id = exchage_tx_contract.TokenId.ToByteArray(); long token_quantity = exchage_tx_contract.Quant; byte[] other_token_id = null; long other_token_quantity = exchange.Transaction(token_id, token_quantity); other_token_id = token_id.SequenceEqual(first_token_id) ? second_token_id : first_token_id; 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.AddAssetAmountV2(other_token_id, other_token_quantity, this.db_manager); } this.db_manager.Account.Put(account.CreateDatabaseKey(), account); this.db_manager.PutExchangeCapsule(exchange); result.ExchangeReceivedAmount = 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); }
public override bool Execute(TransactionResultCapsule result) { long fee = CalcFee(); try { TransferContract transfer_contract = this.contract.Unpack <TransferContract>(); long amount = transfer_contract.Amount; byte[] to_address = transfer_contract.ToAddress.ToByteArray(); byte[] owner_address = transfer_contract.OwnerAddress.ToByteArray(); AccountCapsule to_account = this.db_manager.Account.Get(to_address); if (to_account == null) { bool default_permission = this.db_manager.DynamicProperties.GetAllowMultiSign() == 1; to_account = new AccountCapsule(ByteString.CopyFrom(to_address), AccountType.Normal, this.db_manager.GetHeadBlockTimestamp(), default_permission, this.db_manager); this.db_manager.Account.Put(to_address, to_account); fee = fee + this.db_manager.DynamicProperties.GetCreateNewAccountFeeInSystemContract(); } this.db_manager.AdjustBalance(owner_address, -fee); this.db_manager.AdjustBalance(this.db_manager.Account.GetBlackHole().CreateDatabaseKey(), fee); result.SetStatus(fee, code.Sucess); this.db_manager.AdjustBalance(owner_address, -amount); this.db_manager.AdjustBalance(to_address, amount); } catch (BalanceInsufficientException e) { Logger.Debug(e.Message); result.SetStatus(fee, code.Failed); throw new ContractExeException(e.Message); } catch (ArithmeticException 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 { UnfreezeAssetContract unfreeze_asset_contract = contract.Unpack <UnfreezeAssetContract>(); byte[] owner_address = unfreeze_asset_contract.OwnerAddress.ToByteArray(); long unfreeze_asset = 0L; long now = this.db_manager.GetHeadBlockTimestamp(); AccountCapsule account = this.db_manager.Account.Get(owner_address); List <Frozen> frozen_list = new List <Frozen>(account.FrozenList); foreach (Account.Types.Frozen frozen in frozen_list) { if (frozen.ExpireTime <= now) { unfreeze_asset += frozen.FrozenBalance; account.FrozenList.Remove(frozen); } } if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0) { account.AddAssetAmountV2(account.AssetIssuedName.ToByteArray(), unfreeze_asset, this.db_manager); } else { account.AddAssetAmountV2(account.AssetIssuedID.ToByteArray(), unfreeze_asset, this.db_manager); } account.Instance.FrozenSupply.Clear(); account.Instance.FrozenSupply.AddRange(frozen_list); this.db_manager.Account.Put(owner_address, account); result.SetStatus(fee, code.Sucess); } catch (InvalidProtocolBufferException e) { Logger.Debug(e.Message); result.SetStatus(fee, code.Failed); throw new ContractExeException(e.Message); } catch (ArithmeticException 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 { UpdateAssetContract update_asset_contract = this.contract.Unpack <UpdateAssetContract>(); long new_limit = update_asset_contract.NewLimit; long new_public_limit = update_asset_contract.NewPublicLimit; byte[] owner_address = update_asset_contract.OwnerAddress.ToByteArray(); ByteString new_url = update_asset_contract.Url; ByteString new_description = update_asset_contract.Description; AccountCapsule account = this.db_manager.Account.Get(owner_address); AssetIssueCapsule asset_issue = null; AssetIssueCapsule asset_issue_v2 = null; asset_issue_v2 = this.db_manager.AssetIssueV2.Get(account.AssetIssuedID.ToByteArray()); asset_issue_v2.FreeAssetNetLimit = new_limit; asset_issue_v2.PublicFreeAssetNetLimit = new_public_limit; asset_issue_v2.Url = new_url; asset_issue_v2.Description = new_description; if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0) { asset_issue = this.db_manager.AssetIssue.Get(account.AssetIssuedName.ToByteArray()); asset_issue.FreeAssetNetLimit = new_limit; asset_issue.PublicFreeAssetNetLimit = new_public_limit; asset_issue.Url = new_url; asset_issue.Description = new_description; this.db_manager.AssetIssue.Put(asset_issue.CreateDatabaseKey(), asset_issue); this.db_manager.AssetIssueV2.Put(asset_issue_v2.CreateDatabaseKeyV2(), asset_issue_v2); } else { this.db_manager.AssetIssueV2.Put(asset_issue_v2.CreateDatabaseKeyV2(), asset_issue_v2); } result.SetStatus(fee, code.Sucess); } 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 { ProposalCreateContract proposal_create_contract = this.contract.Unpack <ProposalCreateContract>(); long id = Deposit == null? this.db_manager.DynamicProperties.GetLatestProposalNum() + 1 : Deposit.GetLatestProposalNum() + 1; ProposalCapsule proposal = new ProposalCapsule(proposal_create_contract.OwnerAddress, id); proposal.Parameters = new Dictionary <long, long>(proposal_create_contract.Parameters); long now = this.db_manager.GetHeadBlockTimestamp(); long maintenance_interval = Deposit == null? this.db_manager.DynamicProperties.GetMaintenanceTimeInterval() : Deposit.GetMaintenanceTimeInterval(); proposal.CreateTime = now; long current_maintenance_time = Deposit == null? this.db_manager.DynamicProperties.GetNextMaintenanceTime() : Deposit.GetNextMaintenanceTime(); long now3 = now + (int)Args.Instance.Block.ProposalExpireTime; long round = (now3 - current_maintenance_time) / maintenance_interval; long expiration_time = current_maintenance_time + (round + 1) * maintenance_interval; proposal.ExpirationTime = expiration_time; if (Deposit == null) { this.db_manager.Proposal.Put(proposal.CreateDatabaseKey(), proposal); this.db_manager.DynamicProperties.PutLatestProposalNum(id); } else { Deposit.PutProposalValue(proposal.CreateDatabaseKey(), proposal); Deposit.PutDynamicPropertiesWithLatestProposalNum(id); } result.SetStatus(fee, code.Sucess); } 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 { ParticipateAssetIssueContract asset_issue_contract = this.contract.Unpack <ParticipateAssetIssueContract>(); long cost = asset_issue_contract.Amount; //subtract from owner address byte[] owner_address = asset_issue_contract.OwnerAddress.ToByteArray(); AccountCapsule owner_account = this.db_manager.Account.Get(owner_address); owner_account.Balance = owner_account.Balance - cost - fee; byte[] key = asset_issue_contract.AssetName.ToByteArray(); AssetIssueCapsule asset_issue = this.db_manager.GetAssetIssueStoreFinal().Get(key); long exchange_amount = cost * asset_issue.Num; exchange_amount = (long)Math.Floor((double)(exchange_amount / asset_issue.TransactionNum)); owner_account.AddAssetAmountV2(key, exchange_amount, this.db_manager); byte[] to_address = asset_issue_contract.ToAddress.ToByteArray(); AccountCapsule to_account = this.db_manager.Account.Get(to_address); to_account.Balance = to_account.Balance + cost; if (!to_account.ReduceAssetAmountV2(key, exchange_amount, this.db_manager)) { throw new ContractExeException("reduceAssetAmount failed !"); } //write to db this.db_manager.Account.Put(owner_address, owner_account); this.db_manager.Account.Put(to_address, to_account); result.SetStatus(fee, code.Sucess); } catch (InvalidProtocolBufferException e) { Logger.Debug(e.Message); result.SetStatus(fee, code.Failed); throw new ContractExeException(e.Message); } catch (ArithmeticException 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 { ProposalApproveContract proposal_approve_contract = this.contract.Unpack <ProposalApproveContract>(); ProposalCapsule proposal = Deposit == null? this.db_manager.Proposal.Get(BitConverter.GetBytes(proposal_approve_contract.ProposalId)) : Deposit.GetProposalCapsule(BitConverter.GetBytes(proposal_approve_contract.ProposalId)); ByteString committee_address = proposal_approve_contract.OwnerAddress; if (proposal_approve_contract.IsAddApproval) { proposal.AddApproval(committee_address); } else { proposal.RemoveApproval(committee_address); } if (Deposit == null) { this.db_manager.Proposal.Put(proposal.CreateDatabaseKey(), proposal); } else { deposit.PutProposalValue(proposal.CreateDatabaseKey(), proposal); } 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); }
public override bool Execute(TransactionResultCapsule result) { long fee = CalcFee(); try { VoteWitnessContract contract = this.contract.Unpack <VoteWitnessContract>(); CountVoteAccount(contract, Deposit); result.SetStatus(fee, code.Sucess); } 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 { WitnessUpdateContract witness_update_contract = this.contract.Unpack <WitnessUpdateContract>(); UpdateWitness(witness_update_contract); result.SetStatus(fee, code.Sucess); } 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(); WithdrawBalanceContract withdraw_contract; try { withdraw_contract = contract.Unpack <WithdrawBalanceContract>(); } catch (InvalidProtocolBufferException e) { Logger.Debug(e.Message); result.SetStatus(fee, code.Failed); throw new ContractExeException(e.Message); } AccountCapsule account = (Deposit == null) ? this.db_manager.Account.Get(withdraw_contract.OwnerAddress.ToByteArray()) : Deposit.GetAccount(withdraw_contract.OwnerAddress.ToByteArray()); long now = this.db_manager.GetHeadBlockTimestamp(); account.Instance.Balance = account.Balance + account.Allowance; account.Allowance = 0; account.LatestWithdrawTime = now; if (Deposit == null) { this.db_manager.Account.Put(account.CreateDatabaseKey(), account); } else { Deposit.PutAccountValue(account.CreateDatabaseKey(), account); } result.WithdrawAmount = account.Allowance; result.SetStatus(fee, code.Sucess); return(true); }
public override bool Execute(TransactionResultCapsule result) { long fee = CalcFee(); try { ProposalDeleteContract proposal_delete_contract = this.contract.Unpack <ProposalDeleteContract>(); ProposalCapsule proposal = (Deposit == null) ? this.db_manager.Proposal.Get(BitConverter.GetBytes(proposal_delete_contract.ProposalId)) : Deposit.GetProposalCapsule(BitConverter.GetBytes(proposal_delete_contract.ProposalId)); proposal.State = Proposal.Types.State.Canceled; if (Deposit == null) { this.db_manager.Proposal.Put(proposal.CreateDatabaseKey(), proposal); } else { Deposit.PutProposalValue(proposal.CreateDatabaseKey(), proposal); } result.SetStatus(fee, code.Sucess); } catch (InvalidProtocolBufferException e) { Logger.Debug(e.Message); result.SetStatus(fee, code.Failed); throw new ContractExeException(e.Message); } catch (ItemNotFoundException 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 { ClearABIContract us_contract = contract.Unpack <ClearABIContract>(); byte[] contract_address = us_contract.ContractAddress.ToByteArray(); ContractCapsule deployed_contract = this.db_manager.Contract.Get(contract_address); deployed_contract.ClearABI(); this.db_manager.Contract.Put(contract_address, deployed_contract); result.SetStatus(fee, code.Sucess); } 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 { AssetIssueContract asset_issue_contract = contract.Unpack <AssetIssueContract>(); AssetIssueCapsule asset_issue = new AssetIssueCapsule(asset_issue_contract); AssetIssueCapsule asset_issue_v2 = new AssetIssueCapsule(asset_issue_contract); byte[] owner_address = asset_issue_contract.OwnerAddress.ToByteArray(); long token_id = this.db_manager.DynamicProperties.GetTokenIdNum(); token_id++; asset_issue.Id = token_id.ToString(); asset_issue_v2.Id = token_id.ToString(); this.db_manager.DynamicProperties.PutTokenIdNum(token_id); if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0) { asset_issue_v2.Percision = 0; this.db_manager.AssetIssue.Put(asset_issue.CreateDatabaseKey(), asset_issue); this.db_manager.AssetIssueV2.Put(asset_issue_v2.CreateDatabaseKeyV2(), asset_issue_v2); } else { this.db_manager.AssetIssueV2.Put(asset_issue_v2.CreateDatabaseKeyV2(), asset_issue_v2); } this.db_manager.AdjustBalance(owner_address, -fee); this.db_manager.AdjustBalance(this.db_manager.Account.GetBlackHole().Address.ToByteArray(), fee); AccountCapsule account = this.db_manager.Account.Get(owner_address); List <FrozenSupply> frozen_supplys = new List <FrozenSupply>(asset_issue_contract.FrozenSupply); long remain_supply = asset_issue_contract.TotalSupply; List <Frozen> frozens = new List <Frozen>(); long startTime = asset_issue_contract.StartTime; foreach (AssetIssueContract.Types.FrozenSupply supply in asset_issue_contract.FrozenSupply) { long expire_time = startTime + supply.FrozenDays * 86_400_000; Frozen frozen = new Frozen(); frozen.FrozenBalance = supply.FrozenAmount; frozen.ExpireTime = expire_time; frozens.Add(frozen); remain_supply -= supply.FrozenAmount; } if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0) { account.AddAsset(asset_issue.CreateDatabaseKey(), remain_supply); } account.AssetIssuedName = ByteString.CopyFrom(asset_issue.CreateDatabaseKey()); account.AssetIssuedID = ByteString.CopyFrom(asset_issue.CreateDatabaseKeyV2()); account.AddAssetV2(asset_issue_v2.CreateDatabaseKeyV2(), remain_supply); account.FrozenSupplyList.AddRange(frozens); this.db_manager.Account.Put(owner_address, account); result.AssetIssueID = token_id.ToString(); result.SetStatus(fee, code.Sucess); } catch (InvalidProtocolBufferException e) { Logger.Debug(e.Message); result.SetStatus(fee, code.Failed); throw new ContractExeException(e.Message); } catch (BalanceInsufficientException e) { Logger.Debug(e.Message); result.SetStatus(fee, code.Failed); throw new ContractExeException(e.Message); } catch (ArithmeticException 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(); FreezeBalanceContract freeze_balance_contract = null; try { freeze_balance_contract = contract.Unpack <FreezeBalanceContract>(); } catch (InvalidProtocolBufferException e) { Logger.Debug(e.Message); result.SetStatus(fee, code.Failed); throw new ContractExeException(e.Message); } AccountCapsule account = this.db_manager.Account.Get(freeze_balance_contract.OwnerAddress.ToByteArray()); long now = this.db_manager.GetHeadBlockTimestamp(); long duration = freeze_balance_contract.FrozenDuration * 86_400_000; long new_balance = account.Balance - freeze_balance_contract.FrozenBalance; long frozen_balance = freeze_balance_contract.FrozenBalance; long expire_time = now + duration; byte[] owner_address = freeze_balance_contract.OwnerAddress.ToByteArray(); byte[] receiver_address = freeze_balance_contract.ReceiverAddress.ToByteArray(); switch (freeze_balance_contract.Resource) { case ResourceCode.Bandwidth: { if (receiver_address != null && receiver_address.Length > 0 && this.db_manager.DynamicProperties.SupportDR()) { DelegateResource(owner_address, receiver_address, true, frozen_balance, expire_time); account.AddDelegatedFrozenBalanceForBandwidth(frozen_balance); } else { long newFrozenBalanceForBandwidth = frozen_balance + account.FrozenBalance; account.SetFrozenForBandwidth(newFrozenBalanceForBandwidth, expire_time); } this.db_manager.DynamicProperties.AddTotalNetWeight(frozen_balance / 1000_000L); } break; case ResourceCode.Energy: { if (receiver_address != null && receiver_address.Length > 0 && this.db_manager.DynamicProperties.SupportDR()) { DelegateResource(owner_address, receiver_address, false, frozen_balance, expire_time); account.AddDelegatedFrozenBalanceForEnergy(frozen_balance); } else { long new_energy = frozen_balance + account.AccountResource.FrozenBalanceForEnergy.FrozenBalance; account.SetFrozenForEnergy(new_energy, expire_time); } this.db_manager.DynamicProperties.AddTotalEnergyWeight(frozen_balance / 1000_000L); } break; } account.Balance = new_balance; this.db_manager.Account.Put(account.CreateDatabaseKey(), account); result.SetStatus(fee, code.Sucess); return(true); }
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(); UnfreezeBalanceContract unfreeze_balance_contract; try { unfreeze_balance_contract = contract.Unpack <UnfreezeBalanceContract>(); } catch (InvalidProtocolBufferException e) { Logger.Debug(e.Message); result.SetStatus(fee, code.Failed); throw new ContractExeException(e.Message); } byte[] owner_address = unfreeze_balance_contract.OwnerAddress.ToByteArray(); AccountCapsule account = this.db_manager.Account.Get(owner_address); long old_balance = account.Balance; long unfreeze_balance = 0L; byte[] receiver_address = unfreeze_balance_contract.ReceiverAddress.ToByteArray(); if (receiver_address != null && receiver_address.Length > 0 && this.db_manager.DynamicProperties.SupportDR()) { byte[] key = DelegatedResourceCapsule.CreateDatabaseKey(unfreeze_balance_contract.OwnerAddress.ToByteArray(), unfreeze_balance_contract.ReceiverAddress.ToByteArray()); DelegatedResourceCapsule delegated_resource = this.db_manager.DelegatedResource.Get(key); AccountCapsule receiver = this.db_manager.Account.Get(receiver_address); switch (unfreeze_balance_contract.Resource) { case ResourceCode.Bandwidth: { unfreeze_balance = delegated_resource.FrozenBalanceForBandwidth; delegated_resource.SetFrozenBalanceForBandwidth(0, 0); receiver.AddAcquiredDelegatedFrozenBalanceForBandwidth(-unfreeze_balance); account.AddDelegatedFrozenBalanceForBandwidth(-unfreeze_balance); } break; case ResourceCode.Energy: { unfreeze_balance = delegated_resource.FrozenBalanceForEnergy; delegated_resource.SetFrozenBalanceForEnergy(0, 0); receiver.AddAcquiredDelegatedFrozenBalanceForEnergy(-unfreeze_balance); account.AddDelegatedFrozenBalanceForEnergy(-unfreeze_balance); } break; default: break; } account.Balance = old_balance + unfreeze_balance; this.db_manager.Account.Put(receiver.CreateDatabaseKey(), receiver); if (delegated_resource.FrozenBalanceForBandwidth == 0 && delegated_resource.FrozenBalanceForEnergy == 0) { this.db_manager.DelegatedResource.Delete(key); DelegatedResourceAccountIndexCapsule delegate_account_index = this.db_manager.DelegateResourceAccountIndex.Get(owner_address); if (delegate_account_index != null) { List <ByteString> to_accounts = new List <ByteString>(delegate_account_index.ToAccounts); to_accounts.Remove(ByteString.CopyFrom(receiver_address)); delegate_account_index.ToAccounts = to_accounts; this.db_manager.DelegateResourceAccountIndex.Put(owner_address, delegate_account_index); } delegate_account_index = this.db_manager.DelegateResourceAccountIndex.Get(receiver_address); if (delegate_account_index != null) { List <ByteString> from_accounts = new List <ByteString>(delegate_account_index.FromAccounts); from_accounts.Remove(ByteString.CopyFrom(owner_address)); delegate_account_index.FromAccounts = from_accounts; this.db_manager.DelegateResourceAccountIndex.Put(receiver_address, delegate_account_index); } } else { this.db_manager.DelegatedResource.Put(key, delegated_resource); } } else { switch (unfreeze_balance_contract.Resource) { case ResourceCode.Bandwidth: { List <Frozen> frozens = new List <Frozen>(); frozens.AddRange(account.FrozenList); long now = this.db_manager.GetHeadBlockTimestamp(); foreach (Frozen frozen in frozens) { if (frozen.ExpireTime <= now) { unfreeze_balance += frozen.FrozenBalance; frozens.Remove(frozen); } } account.Balance = old_balance + unfreeze_balance; account.FrozenList.Clear(); account.FrozenList.AddRange(frozens); } break; case ResourceCode.Energy: { unfreeze_balance = account.AccountResource.FrozenBalanceForEnergy.FrozenBalance; account.AccountResource.FrozenBalanceForEnergy = new Frozen(); account.Balance = old_balance + unfreeze_balance; } break; default: break; } } switch (unfreeze_balance_contract.Resource) { case ResourceCode.Bandwidth: { this.db_manager.DynamicProperties.AddTotalNetWeight(-unfreeze_balance / 1000_000L); } break; case ResourceCode.Energy: { this.db_manager.DynamicProperties.AddTotalEnergyWeight(-unfreeze_balance / 1000_000L); } break; default: break; } VotesCapsule votes = null; if (!this.db_manager.Votes.Contains(owner_address)) { votes = new VotesCapsule(unfreeze_balance_contract.OwnerAddress, account.GetVotesList()); } else { votes = this.db_manager.Votes.Get(owner_address); } account.ClearVotes(); votes.ClearNewVotes(); this.db_manager.Account.Put(owner_address, account); this.db_manager.Votes.Put(owner_address, votes); result.UnfreezeAmount = unfreeze_balance; result.SetStatus(fee, code.Sucess); return(true); }
public abstract bool Execute(TransactionResultCapsule result);
public override bool Execute(TransactionResultCapsule result) { long fee = CalcFee(); try { ExchangeWithdrawContract ew_contract = this.contract.Unpack <ExchangeWithdrawContract>(); AccountCapsule account = this.db_manager.Account.Get(ew_contract.OwnerAddress.ToByteArray()); ExchangeCapsule exchange = this.db_manager.ExchangeFinal.Get(BitConverter.GetBytes(ew_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 = ew_contract.TokenId.ToByteArray(); long token_quantity = ew_contract.Quant; byte[] other_token_id = null; long other_token_quantity = 0; BigInteger first_balance = new BigInteger(first_token_balance); BigInteger second_balance = new BigInteger(second_token_balance); BigInteger quantity = new BigInteger(token_quantity); if (token_id.SequenceEqual(first_token_id)) { other_token_id = second_token_id; other_token_quantity = (long)BigInteger.Multiply(second_balance, quantity); other_token_quantity = (long)BigInteger.Divide(other_token_quantity, first_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)BigInteger.Multiply(first_balance, quantity); other_token_quantity = (long)BigInteger.Divide(other_token_quantity, second_balance); exchange.SetBalance(first_token_balance - other_token_quantity, second_token_balance - token_quantity); } long new_balance = account.Balance - CalcFee(); if (token_id.SequenceEqual(COMPARE_CHARICTOR)) { account.Balance = new_balance + token_quantity; } else { account.AddAssetAmountV2(token_id, token_quantity, this.db_manager); } if (other_token_id.SequenceEqual(COMPARE_CHARICTOR)) { account.Balance = new_balance + other_token_quantity; } else { account.AddAssetAmountV2(other_token_id, other_token_quantity, this.db_manager); } this.db_manager.Account.Put(account.CreateDatabaseKey(), account); this.db_manager.PutExchangeCapsule(exchange); result.ExchangeWithdrawAnotherAmount = 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); }