/// <summary> /// Retrieves the bytecode for the specified contract. /// </summary> /// <param name="contract"> /// The Hedera Network Address of the Contract. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// The bytecode for the specified contract instance. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> /// /// <remarks> /// For now this is marked internal because this method cannot be implemented /// in a safe maner that does not waste hBars. The cost of the query is variable /// and there is no public information on how to efficiently compute the cost prior /// to calling the method. So the query fee must be in excess of the actual cost. /// </remarks> internal async Task <ReadOnlyMemory <byte> > GetContractBytecodeAsync(Address contract, Action <IContext>?configure = null) { contract = RequireInputParameter.Contract(contract); await using var context = CreateChildContext(configure); var query = new Query { ContractGetBytecode = new ContractGetBytecodeQuery { Header = Transactions.CreateAskCostHeader(), ContractID = Protobuf.ToContractID(contract) } }; var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); long cost = (long)response.ContractGetBytecodeResponse.Header.Cost; if (cost > 0) { var transactionId = Transactions.GetOrCreateTransactionID(context); query.ContractGetBytecode.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId); response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); ValidateResult.ResponseHeader(transactionId, getResponseHeader(response)); } return(response.ContractGetBytecodeResponse.Bytecode.ToByteArray());
/// <summary> /// Internal implementation of the update Contract functionality. /// </summary> private async Task <TResult> UpdateContractImplementationAsync <TResult>(UpdateContractParams updateParameters, Action <IContext>?configure) where TResult : new() { updateParameters = RequireInputParameter.UpdateParameters(updateParameters); await using var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var signatory = Transactions.GatherSignatories(context, updateParameters.Signatory); var updateContractBody = new ContractUpdateTransactionBody { ContractID = Protobuf.ToContractID(updateParameters.Contract) }; if (updateParameters.Expiration.HasValue) { updateContractBody.ExpirationTime = Protobuf.ToTimestamp(updateParameters.Expiration.Value); } if (!(updateParameters.Administrator is null)) { updateContractBody.AdminKey = Protobuf.ToPublicKey(updateParameters.Administrator); } if (updateParameters.RenewPeriod.HasValue) { updateContractBody.AutoRenewPeriod = Protobuf.ToDuration(updateParameters.RenewPeriod.Value); } if (!(updateParameters.File is null)) { updateContractBody.FileID = Protobuf.ToFileId(updateParameters.File); } if (!string.IsNullOrWhiteSpace(updateParameters.Memo)) { updateContractBody.Memo = updateParameters.Memo; } var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateTransactionBody(context, transactionId); transactionBody.ContractUpdateInstance = updateContractBody; var request = await Transactions.SignTransactionAsync(transactionBody, signatory); var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode); ValidateResult.PreCheck(transactionId, precheck); var receipt = await GetReceiptAsync(context, transactionId); if (receipt.Status != ResponseCodeEnum.Success) { throw new TransactionException($"Unable to update Contract, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is TransactionRecord arec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, arec); } else if (result is TransactionReceipt arcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, arcpt); } return(result);
/// <summary> /// Retrieves the account records associated with an account that are presently /// held within the network. /// </summary> /// <param name="address"> /// The Hedera Network Address to retrieve associated records. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// A detailed description of the account. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> public async Task <TransactionRecord[]> GetAccountRecordsAsync(Address address, Action <IContext>?configure = null) { address = RequireInputParameter.Address(address); var context = CreateChildContext(configure); var gateway = RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var transfers = Transactions.CreateCryptoTransferList((payer, -context.FeeLimit), (gateway, context.FeeLimit)); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateCryptoTransferTransactionBody(context, transfers, transactionId, "Get Account Records"); var query = new Query { CryptoGetAccountRecords = new CryptoGetAccountRecordsQuery { Header = Transactions.SignQueryHeader(transactionBody, payer), AccountID = Protobuf.ToAccountID(address) } }; var response = await Transactions.ExecuteRequestWithRetryAsync(context, query, getRequestMethod, getResponseCode); ValidateResult.PreCheck(transactionId, getResponseCode(response)); return(response.CryptoGetAccountRecords.Records.Select(record => { var result = new TransactionRecord(); Protobuf.FillRecordProperties(record, result); return result; }).ToArray());
/// <summary> /// Retrieves the records associated with an contract that are presently /// held within the network. /// </summary> /// <param name="contract"> /// The Hedera Network Contract Address to retrieve associated records. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// A detailed description of the account. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> public async Task <TransactionRecord[]> GetContractRecordsAsync(Address contract, Action <IContext>?configure = null) { contract = RequireInputParameter.Contract(contract); await using var context = CreateChildContext(configure); var query = new Query { ContractGetRecords = new ContractGetRecordsQuery { Header = Transactions.CreateAskCostHeader(), ContractID = Protobuf.ToContractID(contract) } }; var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); long cost = (long)response.ContractGetRecordsResponse.Header.Cost; if (cost > 0) { var transactionId = Transactions.GetOrCreateTransactionID(context); query.ContractGetRecords.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId); response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); ValidateResult.ResponseHeader(transactionId, getResponseHeader(response)); } return(response.ContractGetRecordsResponse.Records.Select(record => { var result = new TransactionRecord(); Protobuf.FillRecordProperties(record, result); return result; }).ToArray());
/// <summary> /// Internal implementation of dissociate method. /// </summary> private async Task <TResult> DissociateTokenImplementationAsync <TResult>(TokenID[] tokens, Address account, Signatory?signatory, Action <IContext>?configure) where TResult : new() { account = RequireInputParameter.Account(account); await using var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var signatories = Transactions.GatherSignatories(context, signatory); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = new TransactionBody(context, transactionId); transactionBody.TokenDissociate = new TokenDissociateTransactionBody { Account = new AccountID(account) }; transactionBody.TokenDissociate.Tokens.AddRange(tokens); var receipt = await transactionBody.SignAndExecuteWithRetryAsync(signatories, context); if (receipt.Status != ResponseCodeEnum.Success) { throw new TransactionException($"Unable to Dissociate Token from Account, status: {receipt.Status}", transactionId.ToTxId(), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is TransactionRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); record.FillProperties(rec); } else if (result is TransactionReceipt rcpt) { receipt.FillProperties(transactionId, rcpt); } return(result); }
/// <summary> /// Retreives the accounts that are proxy staking to this account. /// </summary> /// <param name="address"> /// The Hedera Network Address to retrieve the stakers of. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// A dictionary mapping account addresses to the amount of stake. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> /// /// <remarks> /// Marked internal at this point because it is not yet implemented by the network /// </remarks> internal async Task <Dictionary <Address, long> > GetStakers(Address address, Action <IContext>?configure = null) { address = RequireInputParameter.Address(address); await using var context = CreateChildContext(configure); var query = new Query { CryptoGetProxyStakers = new CryptoGetStakersQuery { Header = Transactions.CreateAskCostHeader(), AccountID = Protobuf.ToAccountID(address) } }; var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); long cost = (long)response.CryptoGetProxyStakers.Header.Cost; if (cost > 0) { var transactionId = Transactions.GetOrCreateTransactionID(context); query.CryptoGetProxyStakers.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId); response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); ValidateResult.ResponseHeader(transactionId, getResponseHeader(response)); } return(response.CryptoGetProxyStakers.Stakers.ProxyStaker.ToDictionary(ps => Protobuf.FromAccountID(ps.AccountID), ps => ps.Amount));
/// <summary> /// Internal implementation of delete topic method. /// </summary> private async Task <TransactionReceipt> DeleteTopicImplementationAsync(Address topicToDelete, Signatory?signatory, Action <IContext>?configure) { topicToDelete = RequireInputParameter.AddressToDelete(topicToDelete); await using var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var signatories = Transactions.GatherSignatories(context, signatory); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateTransactionBody(context, transactionId); transactionBody.ConsensusDeleteTopic = new ConsensusDeleteTopicTransactionBody { TopicID = Protobuf.ToTopicID(topicToDelete) }; var request = await Transactions.SignTransactionAsync(transactionBody, signatories); var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode); ValidateResult.PreCheck(transactionId, precheck); var receipt = await GetReceiptAsync(context, transactionId); if (receipt.Status != ResponseCodeEnum.Success) { throw new TransactionException($"Unable to Delete Topic, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TransactionReceipt(); Protobuf.FillReceiptProperties(transactionId, receipt, result); return(result);
/// <summary> /// Public Constructor, a <code>Gateway</code> is immutable after creation. /// </summary> /// <param name="url"> /// The URL and port of the public Hedera Network access point. /// </param> /// <param name="shardNum"> /// Main Network Node Shard Number /// </param> /// <param name="realmNum"> /// Main Network Node Realm Number /// </param> /// <param name="accountNum"> /// Main Network Node Account Number /// </param> public Gateway(string url, long shardNum, long realmNum, long accountNum) { Url = RequireInputParameter.Url(url); ShardNum = RequireInputParameter.ShardNumber(shardNum); RealmNum = RequireInputParameter.RealmNumber(realmNum); AccountNum = RequireInputParameter.AcountNumber(accountNum); }
/// <summary> /// Calls a smart contract function locally on the gateway node. /// </summary> /// <remarks> /// This is performed locally on the gateway node. It cannot change the state of the contract instance /// (and so, cannot spend anything from the instance's cryptocurrency account). It will not have a /// consensus timestamp nor a record or a receipt. The response will contain the output returned /// by the function call. /// </remarks> /// <param name="queryParameters"> /// The parameters identifying the contract and function method to call. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// The results from the local contract query call. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> public async Task <ContractCallResult> QueryContractAsync(QueryContractParams queryParameters, Action <IContext>?configure = null) { queryParameters = RequireInputParameter.QueryParameters(queryParameters); await using var context = CreateChildContext(configure); var query = new Query { ContractCallLocal = new ContractCallLocalQuery { Header = Transactions.CreateAskCostHeader(), ContractID = Protobuf.ToContractID(queryParameters.Contract), Gas = queryParameters.Gas, FunctionParameters = Abi.EncodeFunctionWithArguments(queryParameters.FunctionName, queryParameters.FunctionArgs).ToByteString(), MaxResultSize = queryParameters.MaxAllowedReturnSize } }; var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); long cost = (long)response.ContractCallLocal.Header.Cost; if (cost > 0) { var transactionId = Transactions.GetOrCreateTransactionID(context); query.ContractCallLocal.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost + queryParameters.ReturnValueCharge, transactionId); response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); ValidateResult.ResponseHeader(transactionId, getResponseHeader(response)); } return(Protobuf.FromContractCallResult(response.ContractCallLocal.FunctionResult));
/// <summary> /// Set the period of time where the network will suspend will stop creating events /// and accepting transactions. This can be used to safely shut down /// the platform for maintenance and for upgrades if the file information is included. /// </summary> /// <param name="suspendParameters"> /// The details of the suspend request, includes the time to wait before suspension, /// the duration of the suspension and optionally to include an update file. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// A Submit Message Receipt indicating success. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> /// <exception cref="ConsensusException">If the network was unable to come to consensus before the duration of the transaction expired.</exception> /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception> public async Task <TransactionReceipt> SuspendNetworkAsync(SuspendNetworkParams suspendParameters, Action <IContext>?configure = null) { suspendParameters = RequireInputParameter.SuspendNetworkParams(suspendParameters); await using var context = CreateChildContext(configure); var gateway = RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var signatories = Transactions.GatherSignatories(context); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = new TransactionBody(context, transactionId); var startDate = DateTime.UtcNow.Add(suspendParameters.Starting); var endDate = startDate.Add(suspendParameters.Duration); transactionBody.Freeze = new FreezeTransactionBody { StartHour = startDate.Hour, StartMin = startDate.Minute, EndHour = endDate.Hour, EndMin = endDate.Minute, }; if (!suspendParameters.UpdateFile.IsNullOrNone()) { transactionBody.Freeze.UpdateFile = new FileID(suspendParameters.UpdateFile); transactionBody.Freeze.FileHash = ByteString.CopyFrom(suspendParameters.UpdateFileHash.Span); } var receipt = await transactionBody.SignAndExecuteWithRetryAsync(signatories, context); if (receipt.Status != ResponseCodeEnum.Success) { throw new TransactionException($"Failed to submit suspend/freeze command, status: {receipt.Status}", transactionId.ToTxId(), (ResponseCode)receipt.Status); } return(receipt.FillProperties(transactionId, new TransactionReceipt())); }
/// <summary> /// Retrieves the account records associated with an account that are presently /// held within the network because they exceeded the receive or send threshold /// values for autogeneration of records. /// </summary> /// <param name="address"> /// The Hedera Network Address to retrieve associated records. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// A detailed description of the account. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> public async Task <TransactionRecord[]> GetAccountRecordsAsync(Address address, Action <IContext>?configure = null) { address = RequireInputParameter.Address(address); await using var context = CreateChildContext(configure); var query = new Query { CryptoGetAccountRecords = new CryptoGetAccountRecordsQuery { Header = Transactions.CreateAskCostHeader(), AccountID = Protobuf.ToAccountID(address) } }; var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); long cost = (long)response.CryptoGetAccountRecords.Header.Cost; if (cost > 0) { var transactionId = Transactions.GetOrCreateTransactionID(context); query.CryptoGetAccountRecords.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId); response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); var precheckCode = getResponseHeader(response)?.NodeTransactionPrecheckCode ?? ResponseCodeEnum.Unknown; if (precheckCode != ResponseCodeEnum.Ok) { throw new TransactionException("Unable to retrieve transaction records.", Protobuf.FromTransactionId(transactionId), (ResponseCode)precheckCode); } } return(response.CryptoGetAccountRecords.Records.Select(record => { var result = new TransactionRecord(); Protobuf.FillRecordProperties(record, result); return result; }).ToArray());
/// <summary> /// Internal implementation for Multi Account Transfer Crypto. /// Returns either a receipt or record or throws an exception. /// </summary> private async Task <TResult> TransferImplementationAsync <TResult>(Dictionary <Account, long> sendAccounts, Dictionary <Address, long> receiveAddresses, Action <IContext>?configure) where TResult : new() { var transferList = RequireInputParameter.MultiTransfers(sendAccounts, receiveAddresses); var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payers = sendAccounts.Keys.ToArray <ISigner>().Append(RequireInContext.Payer(context)).ToArray(); var transfers = Transactions.CreateCryptoTransferList(transferList); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateCryptoTransferTransactionBody(context, transfers, transactionId, "Transfer Crypto"); var request = Transactions.SignTransaction(transactionBody, payers); var precheck = await Transactions.ExecuteRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode); ValidateResult.PreCheck(transactionId, precheck.NodeTransactionPrecheckCode); var receipt = await GetReceiptAsync(context, transactionId); if (receipt.Status != ResponseCodeEnum.Success) { throw new TransactionException($"Unable to execute crypto transfer, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is TransferRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, rec); rec.Transfers = Protobuf.FromTransferList(record.TransferList); } else if (result is TransactionReceipt rcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, rcpt); } return(result);
/// <summary> /// Retrieves detailed information regarding a Topic Instance. /// </summary> /// <param name="topic"> /// The Hedera Network Address of the Topic instance to retrieve. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// A detailed description of the contract instance. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> public async Task <TopicInfo> GetTopicInfoAsync(Address topic, Action <IContext>?configure = null) { topic = RequireInputParameter.Topic(topic); await using var context = CreateChildContext(configure); var query = new Query { ConsensusGetTopicInfo = new ConsensusGetTopicInfoQuery { Header = Transactions.CreateAskCostHeader(), TopicID = Protobuf.ToTopicID(topic) } }; var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); long cost = (long)response.ConsensusGetTopicInfo.Header.Cost; if (cost > 0) { var transactionId = Transactions.GetOrCreateTransactionID(context); query.ConsensusGetTopicInfo.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId); response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); ValidateResult.ResponseHeader(transactionId, getResponseHeader(response)); } return(Protobuf.FromTopicInfo(response.ConsensusGetTopicInfo.TopicInfo));
/// <summary> /// Retrieves the contents of a file from the network. /// </summary> /// <param name="file"> /// The address of the file contents to retrieve. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// The contents of the file as a blob of bytes. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> /// <exception cref="ConsensusException">If the network was unable to come to consensus before the duration of the transaction expired.</exception> /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception> public async Task <ReadOnlyMemory <byte> > GetFileContentAsync(Address file, Action <IContext>?configure = null) { file = RequireInputParameter.File(file); await using var context = CreateChildContext(configure); var query = new Query { FileGetContents = new FileGetContentsQuery { Header = Transactions.CreateAskCostHeader(), FileID = Protobuf.ToFileId(file) } }; var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); long cost = (long)response.FileGetContents.Header.Cost; if (cost > 0) { var transactionId = Transactions.GetOrCreateTransactionID(context); query.FileGetContents.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId); response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); ValidateResult.ResponseHeader(transactionId, getResponseHeader(response)); } return(new ReadOnlyMemory <byte>(response.FileGetContents.FileContents.Contents.ToByteArray()));
/// <summary> /// Retrieves the balance in tinybars from the network for a given contract. /// </summary> /// <param name="contract"> /// The hedera network contract address to retrieve the balance of. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// The balance of the associated contract. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> public async Task <ulong> GetContractBalanceAsync(Address contract, Action <IContext>?configure = null) { contract = RequireInputParameter.Contract(contract); await using var context = CreateChildContext(configure); var query = new Query { CryptogetAccountBalance = new CryptoGetAccountBalanceQuery { Header = Transactions.CreateAskCostHeader(), ContractID = Protobuf.ToContractID(contract) } }; var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); var cost = (long)response.CryptogetAccountBalance.Header.Cost; if (cost > 0) { var transactionId = Transactions.GetOrCreateTransactionID(context); query.CryptogetAccountBalance.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId); response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader); ValidateResult.ResponseHeader(transactionId, getResponseHeader(response)); } return(response.CryptogetAccountBalance.Balance);
/// <summary> /// Retrieves the transaction record for a given transaction ID. /// </summary> /// <param name="transaction"> /// Transaction identifier of the record /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// A transaction record with the specified id, or an exception if not found. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> /// <exception cref="TransactionException">If the network has no record of the transaction or request has invalid or had missing data.</exception> public async Task <TransactionRecord> GetTransactionRecordAsync(TxId transaction, Action <IContext>?configure = null) { transaction = RequireInputParameter.Transaction(transaction); await using var context = CreateChildContext(configure); var transactionId = Protobuf.ToTransactionID(transaction); // For the public version of this method, we do not know // if the transaction in question has come to consensus so // we need to get the receipt first (and wait if necessary). var query = new Query { TransactionGetReceipt = new TransactionGetReceiptQuery { TransactionID = transactionId } }; await Transactions.ExecuteNetworkRequestWithRetryAsync(context, query, getServerMethod, shouldRetry); // The Receipt status returned does notmatter in this case. // We may be retrieving a failed record (the status would not equal OK). var record = await GetTransactionRecordAsync(context, transactionId); var result = new TransactionRecord(); Protobuf.FillRecordProperties(record, result); return(result);
/// <summary> /// Deletes a contract instance from the network returning the remaining /// crypto balance to the specified address. Must be signed /// by the admin key. /// </summary> /// <param name="contractToDelete"> /// The Contract instance that will be deleted. /// </param> /// <param name="transferToAddress"> /// The address that will receive any remaining balance from the deleted Contract. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// A transaction receipt indicating a successful operation. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> /// <exception cref="ConsensusException">If the network was unable to come to consensus before the duration of the transaction expired.</exception> /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception> public async Task <TransactionReceipt> DeleteContractAsync(Address contractToDelete, Address transferToAddress, Action <IContext>?configure = null) { contractToDelete = RequireInputParameter.ContractToDelete(contractToDelete); transferToAddress = RequireInputParameter.TransferToAddress(transferToAddress); var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Delete Contract"); transactionBody.ContractDeleteInstance = new ContractDeleteTransactionBody { ContractID = Protobuf.ToContractID(contractToDelete), TransferAccountID = Protobuf.ToAccountID(transferToAddress) }; var request = Transactions.SignTransaction(transactionBody, payer); var precheck = await Transactions.ExecuteRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode); ValidateResult.PreCheck(transactionId, precheck.NodeTransactionPrecheckCode); var receipt = await GetReceiptAsync(context, transactionId); if (receipt.Status != ResponseCodeEnum.Success) { throw new TransactionException($"Unable to delete contract, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TransactionReceipt(); Protobuf.FillReceiptProperties(transactionId, receipt, result); return(result);
/// <summary> /// Retrieves the network address associated with the specified smart contract id. /// </summary> /// <param name="smartContractId"> /// The smart contract ID to look up. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// The network address associated with the smart contract ID. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> public async Task <Address> GetAddressFromSmartContractId(string smartContractId, Action <IContext>?configure = null) { smartContractId = RequireInputParameter.SmartContractId(smartContractId); var context = CreateChildContext(configure); var gateway = RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var transfers = Transactions.CreateCryptoTransferList((payer, -context.FeeLimit), (gateway, context.FeeLimit)); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateCryptoTransferTransactionBody(context, transfers, transactionId, "Get Contract By Solidity ID"); var query = new Query { GetBySolidityID = new GetBySolidityIDQuery { Header = Transactions.SignQueryHeader(transactionBody, payer), SolidityID = smartContractId } }; var response = await Transactions.ExecuteRequestWithRetryAsync(context, query, getRequestMethod, getResponseCode); ValidateResult.PreCheck(transactionId, getResponseCode(response)); var data = response.GetBySolidityID; if (data.ContractID != null) { return(Protobuf.FromContractID(data.ContractID)); } if (data.AccountID != null) { return(Protobuf.FromAccountID(data.AccountID)); } if (data.FileID != null) { return(Protobuf.FromFileID(data.FileID)); } throw new TransactionException($"Address from Smart Contract ID {smartContractId} was not found.", Protobuf.FromTransactionId(transactionId), ResponseCode.Unknown);
/// <summary> /// Public Constructor, an <code>Account</code> is immutable after creation. /// </summary> /// <param name="realmNum"> /// Network Realm Number /// </param> /// <param name="shardNum"> /// Network Shard Number /// </param> /// <param name="accountNum"> /// Network Account Number /// </param> /// <param name="privateKey"> /// Bytes representing an Ed25519 private key associated with this account for /// signing transactions. It is expected to be 48 bytes in length, prefixed /// with <code>0x302e020100300506032b6570</code>. /// </param> public Account(long realmNum, long shardNum, long accountNum, params ReadOnlyMemory <byte>[] privateKeys) { RealmNum = RequireInputParameter.RealmNumber(realmNum); ShardNum = RequireInputParameter.ShardNumber(shardNum); AccountNum = RequireInputParameter.AcountNumber(accountNum); _keys = RequireInputParameter.PrivateKeys(privateKeys); }
/// <summary> /// Internal helper function implementing the file delete functionality. /// </summary> public async Task <TResult> SystemRestoreFileImplementationAsync <TResult>(Address fileToRestore, Signatory?signatory, Action <IContext>?configure = null) where TResult : new() { fileToRestore = RequireInputParameter.FileToRestore(fileToRestore); await using var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var signatories = Transactions.GatherSignatories(context, signatory); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = new TransactionBody(context, transactionId); transactionBody.SystemUndelete = new SystemUndeleteTransactionBody { FileID = new FileID(fileToRestore) }; var receipt = await transactionBody.SignAndExecuteWithRetryAsync(signatories, context); if (receipt.Status != ResponseCodeEnum.Success) { throw new TransactionException($"Unable to delete file, status: {receipt.Status}", transactionId.ToTxId(), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is TransactionRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); record.FillProperties(rec); } else if (result is TransactionReceipt rcpt) { receipt.FillProperties(transactionId, rcpt); } return(result); }
/// <summary> /// Internal implementation for Transfer Crypto. /// Returns either a receipt or record or throws /// an exception. /// </summary> private async Task <TResult> TransferImplementationAsync <TResult>(Address fromAddress, Address toAddress, long amount, Signatory?signatory, Action <IContext>?configure) where TResult : new() { fromAddress = RequireInputParameter.FromAddress(fromAddress); toAddress = RequireInputParameter.ToAddress(toAddress); amount = RequireInputParameter.Amount(amount); var cryptoTransfers = RequireInputParameter.CryptoTransferList(new[] { KeyValuePair.Create(fromAddress, -amount), KeyValuePair.Create(toAddress, amount) }); return(await TransferImplementationAsync <TResult>(cryptoTransfers, null, signatory, configure)); }
/// <summary> /// Internal implementation of the update Topic functionality. /// </summary> private async Task <TResult> UpdateTopicImplementationAsync <TResult>(UpdateTopicParams updateParameters, Action <IContext>?configure) where TResult : new() { updateParameters = RequireInputParameter.UpdateParameters(updateParameters); await using var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var signatory = Transactions.GatherSignatories(context, updateParameters.Signatory); var updateTopicBody = new ConsensusUpdateTopicTransactionBody { TopicID = new TopicID(updateParameters.Topic) }; if (updateParameters.Memo != null) { updateTopicBody.Memo = updateParameters.Memo; } if (!(updateParameters.Administrator is null)) { updateTopicBody.AdminKey = new Key(updateParameters.Administrator); } if (!(updateParameters.Participant is null)) { updateTopicBody.SubmitKey = new Key(updateParameters.Participant); } if (updateParameters.RenewPeriod.HasValue) { updateTopicBody.AutoRenewPeriod = new Duration(updateParameters.RenewPeriod.Value); } if (!(updateParameters.RenewAccount is null)) { updateTopicBody.AutoRenewAccount = new AccountID(updateParameters.RenewAccount); } var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = new TransactionBody(context, transactionId); transactionBody.ConsensusUpdateTopic = updateTopicBody; var receipt = await transactionBody.SignAndExecuteWithRetryAsync(signatory, context); if (receipt.Status != ResponseCodeEnum.Success) { throw new TransactionException($"Unable to update Topic, status: {receipt.Status}", transactionId.ToTxId(), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is TransactionRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); record.FillProperties(rec); } else if (result is TransactionReceipt rcpt) { receipt.FillProperties(transactionId, rcpt); } return(result); }
/// <summary> /// Internal implementation of the update account functionality. /// </summary> private async Task <TResult> UpdateAccountImplementationAsync <TResult>(UpdateAccountParams updateParameters, Action <IContext>?configure) where TResult : new() { updateParameters = RequireInputParameter.UpdateParameters(updateParameters); await using var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var updateAccountBody = new CryptoUpdateTransactionBody { AccountIDToUpdate = new AccountID(updateParameters.Address) }; if (!(updateParameters.Endorsement is null)) { updateAccountBody.Key = new Key(updateParameters.Endorsement); } if (updateParameters.RequireReceiveSignature.HasValue) { updateAccountBody.ReceiverSigRequiredWrapper = updateParameters.RequireReceiveSignature.Value; } if (updateParameters.AutoRenewPeriod.HasValue) { updateAccountBody.AutoRenewPeriod = new Duration(updateParameters.AutoRenewPeriod.Value); } if (updateParameters.Expiration.HasValue) { updateAccountBody.ExpirationTime = new Timestamp(updateParameters.Expiration.Value); } if (!(updateParameters.Proxy is null)) { updateAccountBody.ProxyAccountID = new AccountID(updateParameters.Proxy); } var signatory = Transactions.GatherSignatories(context, updateParameters.Signatory); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = new TransactionBody(context, transactionId); transactionBody.CryptoUpdateAccount = updateAccountBody; var receipt = await transactionBody.SignAndExecuteWithRetryAsync(signatory, context); if (receipt.Status != ResponseCodeEnum.Success) { throw new TransactionException($"Unable to update account, status: {receipt.Status}", transactionId.ToTxId(), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is TransactionRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); record.FillProperties(rec); } else if (result is TransactionReceipt rcpt) { receipt.FillProperties(transactionId, rcpt); } return(result); }
/// <summary> /// Internal implementation for Transfer Crypto. /// Returns either a receipt or record or throws /// an exception. /// </summary> private async Task <TResult> TransferImplementationAsync <TResult>(Account fromAccount, Address toAddress, long amount, Action <IContext>?configure) where TResult : new() { fromAccount = RequireInputParameter.FromAccount(fromAccount); toAddress = RequireInputParameter.ToAddress(toAddress); amount = RequireInputParameter.Amount(amount); return(await TransferImplementationAsync <TResult>(new Dictionary <Account, long> { { fromAccount, amount } }, new Dictionary <Address, long> { { toAddress, amount } }, configure)); }
/// <summary> /// Retrieves the transaction record for a given transaction ID. /// </summary> /// <param name="transaction"> /// Transaction identifier of the record /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// A transaction record with the specified id, or an exception if not found. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception> public async Task <TransactionRecord> GetTransactionRecordAsync(TxId transaction, Action <IContext>?configure = null) { transaction = RequireInputParameter.Transaction(transaction); var context = CreateChildContext(configure); var transactionId = Protobuf.ToTransactionID(transaction); var record = await GetTransactionRecordAsync(context, transactionId); var result = new TransactionRecord(); Protobuf.FillRecordProperties(record, result); return(result); }
/// <summary> /// Retrieves the receipt from the network matching the transaction /// id. Will wait for the disposition of the receipt to be known. /// </summary> /// <param name="transaction"> /// Transaction identifier of the receipt. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// The receipt matching the transaction id, if found and marked /// sucessfull, otherwise a <see cref="TransactionException"/> is /// not found or returns an error status. /// </returns> /// <exception cref="TransactionException">If the network has no record of the transaction or request has invalid or had missing data.</exception> public async Task <TransactionReceipt> GetReceiptAsync(TxId transaction, Action <IContext>?configure = null) { transaction = RequireInputParameter.Transaction(transaction); await using var context = CreateChildContext(configure); var transactionId = new TransactionID(transaction); var receipt = await Transactions.GetReceiptAsync(context, transactionId); if (receipt.Status != ResponseCodeEnum.Success) { throw new TransactionException($"Unable to retreive receipt, status: {receipt.Status}", transaction, (ResponseCode)receipt.Status); } return(receipt.ToTransactionReceipt(transactionId)); }
/// <summary> /// Internal implementation of the submit message call. /// </summary> private async Task <TResult> SubmitUnsafeTransactionImplementationAsync <TResult>(ReadOnlyMemory <byte> transaction, Action <IContext>?configure) where TResult : new() { var innerTransactionId = RequireInputParameter.IdFromTransactionBytes(transaction); await using var context = CreateChildContext(configure); var gateway = RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var signatories = Transactions.GatherSignatories(context); var outerTransactionId = Transactions.GetOrCreateTransactionID(context); // Note: custom transaction body, does not carry a max fee since // the inner transaction is the transaction to process, it still // must be signed however. var transactionBody = new TransactionBody { TransactionID = outerTransactionId, NodeAccountID = new AccountID(RequireInContext.Gateway(context)), TransactionValidDuration = new Proto.Duration(context.TransactionDuration), UncheckedSubmit = new UncheckedSubmitBody { TransactionBytes = ByteString.CopyFrom(transaction.Span) } }; var precheck = await transactionBody.SignAndSubmitWithRetryAsync(signatories, context); ValidateResult.PreCheck(outerTransactionId, precheck); // NOTE: The outer transaction ID exists so that the administrative account has something to sign that // can be verified, however, the transaction never actually exists in the system so there will never be // a receipt for this submission, however, there will be an attempt to execute the submitted transaction // as this method bypasses PRECHECK validations. So, there will be a receipt for the inner traction, with // success or a failure code. Therefore we return the receipt or record for the custom transaction. var receipt = await Transactions.GetReceiptAsync(context, innerTransactionId); // Retain standard behavior of throwing an exception if the receipt has an error code. if (receipt.Status != ResponseCodeEnum.Success) { throw new TransactionException($"Submit Unsafe Transaction failed, status: {receipt.Status}", innerTransactionId.ToTxId(), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is TransactionRecord rec) { var record = await GetTransactionRecordAsync(context, innerTransactionId); record.FillProperties(rec); } else if (result is TransactionReceipt rcpt) { receipt.FillProperties(innerTransactionId, rcpt); } return(result); }
/// <summary> /// Retrieves detailed information regarding a Hedera Network Account. /// </summary> /// <param name="address"> /// The Hedera Network Address to retrieve detailed information of. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// A detailed description of the account. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> public async Task <AccountInfo> GetAccountInfoAsync(Address address, Action <IContext>?configure = null) { address = RequireInputParameter.Address(address); await using var context = CreateChildContext(configure); var query = new Query { CryptoGetInfo = new CryptoGetInfoQuery { AccountID = new AccountID(address) } }; var response = await query.SignAndExecuteWithRetryAsync(context); return(response.CryptoGetInfo.AccountInfo.ToAccountInfo()); }
/// <summary> /// Retrieves detailed information regarding a Smart Contract Instance. /// </summary> /// <param name="contract"> /// The Hedera Network Address of the Contract instance to retrieve. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// A detailed description of the contract instance. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> public async Task <ContractInfo> GetContractInfoAsync(Address contract, Action <IContext>?configure = null) { contract = RequireInputParameter.Contract(contract); await using var context = CreateChildContext(configure); var query = new Query { ContractGetInfo = new ContractGetInfoQuery { ContractID = new ContractID(contract) } }; var response = await query.SignAndExecuteWithRetryAsync(context); return(response.ContractGetInfo.ContractInfo.ToContractInfo()); }
/// <summary> /// Retrieves detailed information regarding a Topic Instance. /// </summary> /// <param name="topic"> /// The Hedera Network Address of the Topic instance to retrieve. /// </param> /// <param name="configure"> /// Optional callback method providing an opportunity to modify /// the execution configuration for just this method call. /// It is executed prior to submitting the request to the network. /// </param> /// <returns> /// A detailed description of the contract instance. /// </returns> /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception> /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception> /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception> public async Task <TopicInfo> GetTopicInfoAsync(Address topic, Action <IContext>?configure = null) { topic = RequireInputParameter.Topic(topic); await using var context = CreateChildContext(configure); var query = new Query { ConsensusGetTopicInfo = new ConsensusGetTopicInfoQuery { TopicID = new TopicID(topic) } }; var response = await query.SignAndExecuteWithRetryAsync(context); return(response.ConsensusGetTopicInfo.TopicInfo.ToTopicInfo()); }