public static bool OnAssetIssueByName(JToken id, string method, JArray parameters, out JToken result) { result = new JObject(); if (parameters == null || parameters.Count != 1) { result = RpcMessage.CreateErrorResult(id, RpcMessage.INVALID_PARAMS, "Invalid parameters"); return(false); } try { byte[] name = Encoding.UTF8.GetBytes(parameters[0].ToString()); AssetIssueContract asset_issue_contract = RpcApiService.GetAssetIssueByName(name); result = JToken.FromObject(asset_issue_contract.ToByteArray()); } catch (ArgumentException e) { result = RpcMessage.CreateErrorResult(id, RpcMessage.INVALID_PARAMS, e.Message); return(false); } catch (System.Exception e) { result = RpcMessage.CreateErrorResult(id, RpcMessage.UNKNOWN_ERROR, e.Message); return(false); } return(true); }
public static AssetIssueContract GetAssetIssueByName(byte[] name) { if (name == null || name.Length == 0) { throw new ArgumentException("Invalid name"); } AssetIssueContract contract = null; if (Manager.Instance.DBManager.DynamicProperties.GetAllowSameTokenName() == 0) { AssetIssueCapsule asset_issue = Manager.Instance.DBManager.AssetIssue.Get(name); contract = asset_issue != null ? asset_issue.Instance : null; } else { ByteString asset_name = ByteString.CopyFrom(name); AssetIssueList asset_issue_list = new AssetIssueList(); foreach (var asset_issue in Manager.Instance.DBManager.AssetIssueV2.AllAssetIssues) { if (asset_issue.Name.Equals(asset_name)) { asset_issue_list.AssetIssue.Add(asset_issue.Instance); } } if (asset_issue_list.AssetIssue.Count > 1) { throw new NonUniqueObjectException("get more than one asset, please use " + RpcCommand.AssetIssue.AssetIssueById); } else { AssetIssueCapsule asset_issue = Manager.Instance.DBManager.AssetIssueV2.Get(asset_name.ToByteArray()); if (asset_name != null) { if (asset_issue_list.AssetIssue.Count > 0 && asset_issue_list.AssetIssue[0].Id.Equals(asset_issue.Instance.Id)) { contract = asset_issue.Instance; } else { asset_issue_list.AssetIssue.Add(asset_issue.Instance); if (asset_issue_list.AssetIssue.Count > 1) { throw new NonUniqueObjectException("get more than one asset, please use " + RpcCommand.AssetIssue.AssetIssueById); } contract = asset_issue_list.AssetIssue[0]; } } } } return(contract); }
public List <AssetIssueCapsule> GetAllAssetIssues() { List <AssetIssueCapsule> result = new List <AssetIssueCapsule>(); long block_num = 1; long latest_header_num = this.db_manager.DynamicProperties.GetLatestBlockHeaderNumber(); while (block_num <= latest_header_num) { if (block_num % 100000 == 0) { Logger.Info( string.Format("The number of block that have processed:{0}", block_num)); } try { BlockCapsule block = this.db_manager.GetBlockByNum(block_num); foreach (TransactionCapsule tx in block.Transactions) { if (tx.Instance.RawData.Contract[0].Type == Protocol.Transaction.Types.Contract.Types.ContractType.AssetIssueContract) { AssetIssueContract contract = tx.Instance.RawData.Contract[0].Parameter.Unpack <AssetIssueContract>(); AssetIssueCapsule asset_issue = new AssetIssueCapsule(contract); result.Add(this.db_manager.AssetIssue.Get(asset_issue.CreateDatabaseKey())); } } } catch (System.Exception e) { throw new System.Exception("Block not exists,num:" + block_num, e); } block_num++; } Logger.Info( string.Format("Total block:{0}", block_num)); if (this.db_manager.AssetIssue.AllAssetIssues.Count != result.Count) { throw new System.Exception("Asset num is wrong!"); } return(result); }
public static RpcApiResult AssetIssueByName(string name, out AssetIssueContract contract) { contract = null; JObject receive = SendCommand(RpcCommand.AssetIssue.AssetIssueByName, new JArray() { name }); if (receive.TryGetValue("error", out JToken value)) { return(new RpcApiResult(false, value["code"].ToObject <int>(), value["message"].ToObject <string>())); } contract = AssetIssueContract.Parser.ParseFrom(receive["result"].ToObject <byte[]>()); return(RpcApiResult.Success); }
public TransactionCapsule(AssetIssueContract contract) { CreateTransaction(contract, Transaction.Types.Contract.Types.ContractType.AssetIssueContract); }
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 Validate() { if (this.contract == null) { throw new ContractValidateException("No contract!"); } if (this.db_manager == null) { throw new ContractValidateException("No dbManager!"); } if (this.contract.Is(AssetIssueContract.Descriptor)) { AssetIssueContract asset_issue_contract = null; try { asset_issue_contract = this.contract.Unpack <AssetIssueContract>(); } catch (InvalidProtocolBufferException e) { Logger.Debug(e.Message); throw new ContractValidateException(e.Message); } byte[] owner_address = asset_issue_contract.OwnerAddress.ToByteArray(); if (!Wallet.IsValidAddress(owner_address)) { throw new ContractValidateException("Invalid ownerAddress"); } if (!TransactionUtil.ValidAssetName(asset_issue_contract.Name.ToByteArray())) { throw new ContractValidateException("Invalid assetName"); } if (this.db_manager.DynamicProperties.GetAllowSameTokenName() != 0) { string name = asset_issue_contract.Name.ToStringUtf8().ToLower(); if (name.Equals("tx")) { throw new ContractValidateException("assetName can't be tx"); } } int precision = asset_issue_contract.Precision; if (precision != 0 && this.db_manager.DynamicProperties.GetAllowSameTokenName() != 0) { if (precision < 0 || precision > 6) { throw new ContractValidateException("precision cannot exceed 6"); } } if ((!asset_issue_contract.Abbr.IsEmpty) && !TransactionUtil.ValidAssetName(asset_issue_contract.Abbr.ToByteArray())) { throw new ContractValidateException("Invalid abbreviation for token"); } if (!TransactionUtil.ValidUrl(asset_issue_contract.Url.ToByteArray())) { throw new ContractValidateException("Invalid url"); } if (!TransactionUtil.ValidAssetDescription(asset_issue_contract.Description.ToByteArray())) { throw new ContractValidateException("Invalid description"); } if (asset_issue_contract.StartTime == 0) { throw new ContractValidateException("Start time should be not empty"); } if (asset_issue_contract.EndTime == 0) { throw new ContractValidateException("End time should be not empty"); } if (asset_issue_contract.EndTime <= asset_issue_contract.StartTime) { throw new ContractValidateException("End time should be greater than start time"); } if (asset_issue_contract.StartTime <= this.db_manager.GetHeadBlockTimestamp()) { throw new ContractValidateException("Start time should be greater than HeadBlockTime"); } if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0 && this.db_manager.AssetIssue.Get(asset_issue_contract.Name.ToByteArray()) != null) { throw new ContractValidateException("Token exists"); } if (asset_issue_contract.TotalSupply <= 0) { throw new ContractValidateException("TotalSupply must greater than 0!"); } if (asset_issue_contract.TrxNum <= 0) { throw new ContractValidateException("TrxNum must greater than 0!"); } if (asset_issue_contract.Num <= 0) { throw new ContractValidateException("Num must greater than 0!"); } if (asset_issue_contract.PublicFreeAssetNetUsage != 0) { throw new ContractValidateException("PublicFreeAssetNetUsage must be 0!"); } if (asset_issue_contract.FrozenSupply.Count > this.db_manager.DynamicProperties.GetMaxFrozenSupplyNumber()) { throw new ContractValidateException("Frozen supply list length is too long"); } if (asset_issue_contract.FreeAssetNetLimit < 0 || asset_issue_contract.FreeAssetNetLimit >= this.db_manager.DynamicProperties.GetOneDayNetLimit()) { throw new ContractValidateException("Invalid FreeAssetNetLimit"); } if (asset_issue_contract.PublicFreeAssetNetLimit < 0 || asset_issue_contract.PublicFreeAssetNetLimit >= this.db_manager.DynamicProperties.GetOneDayNetLimit()) { throw new ContractValidateException("Invalid PublicFreeAssetNetLimit"); } long remain_supply = asset_issue_contract.TotalSupply; long min_frozen_time = this.db_manager.DynamicProperties.GetMinFrozenSupplyTime(); long max_frozen_time = this.db_manager.DynamicProperties.GetMaxFrozenSupplyTime(); foreach (AssetIssueContract.Types.FrozenSupply frozen in asset_issue_contract.FrozenSupply) { if (frozen.FrozenAmount <= 0) { throw new ContractValidateException("Frozen supply must be greater than 0!"); } if (frozen.FrozenAmount > remain_supply) { throw new ContractValidateException("Frozen supply cannot exceed total supply"); } if (!(frozen.FrozenDays >= min_frozen_time && frozen.FrozenDays <= max_frozen_time)) { throw new ContractValidateException( "frozenDuration must be less than " + max_frozen_time + " days " + "and more than " + min_frozen_time + " days"); } remain_supply -= frozen.FrozenAmount; } AccountCapsule account = this.db_manager.Account.Get(owner_address); if (account == null) { throw new ContractValidateException("Account not exists"); } if (!account.AssetIssuedName.IsEmpty) { throw new ContractValidateException("An account can only issue one asset"); } if (account.Balance < CalcFee()) { throw new ContractValidateException("No enough balance for fee!"); } } else { throw new ContractValidateException( "contract type error,expected type [AssetIssueContract],real type[" + contract.GetType().Name + "]"); } return(true); }
public static RpcApiResult CreateAssetIssueContract(byte[] owner_address, string name, string description, string url, int tx_num, int num, int precision, int vote_score, long total_supply, long free_limit, long public_free_limit, long start_time, long end_time, Dictionary <long, long> frozen_supply, out AssetIssueContract contract) { contract = new AssetIssueContract(); if (tx_num <= 0) { return(new RpcApiResult(false, RpcMessage.INVALID_PARAMS, "transaction count must be > 0")); } if (num <= 0) { return(new RpcApiResult(false, RpcMessage.INVALID_PARAMS, "count must be > 0")); } if (precision < 0) { return(new RpcApiResult(false, RpcMessage.INVALID_PARAMS, "precision must be >= 0")); } if (total_supply <= 0) { return(new RpcApiResult(false, RpcMessage.INVALID_PARAMS, "total supply must be > 0")); } if (free_limit < 0) { return(new RpcApiResult(false, RpcMessage.INVALID_PARAMS, "free net limit must be >= 0")); } if (public_free_limit < 0) { return(new RpcApiResult(false, RpcMessage.INVALID_PARAMS, "public free net limit must be >= 0")); } long now = Helper.CurrentTimeMillis(); if (start_time <= now) { return(new RpcApiResult(false, RpcMessage.INVALID_PARAMS, "start time must be <= now")); } if (start_time >= end_time) { return(new RpcApiResult(false, RpcMessage.INVALID_PARAMS, "start time mus be <= end time")); } contract.OwnerAddress = ByteString.CopyFrom(owner_address); contract.Name = ByteString.CopyFromUtf8(name); contract.Description = ByteString.CopyFromUtf8(description); contract.Url = ByteString.CopyFromUtf8(url); contract.TrxNum = tx_num; contract.Num = num; contract.Precision = precision; contract.VoteScore = vote_score; contract.TotalSupply = total_supply; contract.FreeAssetNetLimit = free_limit; contract.PublicFreeAssetNetLimit = public_free_limit; contract.StartTime = start_time; contract.EndTime = end_time; foreach (var frozen in frozen_supply) { AssetIssueContract.Types.FrozenSupply entry = new AssetIssueContract.Types.FrozenSupply(); entry.FrozenAmount = frozen.Key; entry.FrozenDays = frozen.Value; contract.FrozenSupply.Add(entry); } return(RpcApiResult.Success); }
public async Task <TransactionExtention> CreateAssetIssueAsync(AssetIssueContract contract) { return(await _grpcClient.CreateAssetIssue2Async(contract)); }
public async Task <Transaction> CreateAssetIssueAsync(AssetIssueContract contract, CancellationToken token = default) { var wallet = GetWallet(); return(await wallet.CreateAssetIssueAsync(contract, _configuration.GetCallOptions(token))); }