public void ExposesAccountValue() { var transactionId = Generator.TransactionID(); var txId = Protobuf.FromTransactionId(transactionId); Assert.Equal(txId.Address, Protobuf.FromAccountID(transactionId.AccountID)); }
public void ExposesValidStartNano() { var transactionId = Generator.TransactionID(); var txId = Protobuf.FromTransactionId(transactionId); Assert.Equal(txId.ValidStartNanos, transactionId.TransactionValidStart.Nanos); }
/// <summary> /// Creates a new Transaction Id /// </summary> /// <param name="configure"></param> /// <returns></returns> public TxId CreateNewTxId(Action <IContext>?configure = null) { var context = CreateChildContext(configure); var result = Protobuf.FromTransactionId(Transactions.GetOrCreateTransactionID(context)); _ = context.DisposeAsync(); return(result); }
public void DisimilarTxIdesAreNotConsideredEqual() { var txId1 = Protobuf.FromTransactionId(Generator.TransactionID()); var txId2 = Protobuf.FromTransactionId(Generator.TransactionID()); Assert.NotEqual(txId1, txId2); Assert.False(txId1 == txId2); Assert.True(txId1 != txId2); Assert.NotEqual(txId1.GetHashCode(), txId2.GetHashCode()); }
public void EquivalentTxIdAreConsideredEqual() { var transactionId = Generator.TransactionID(); var txId1 = Protobuf.FromTransactionId(transactionId); var txId2 = Protobuf.FromTransactionId(transactionId); Assert.Equal(txId1, txId2); Assert.True(txId1 == txId2); Assert.False(txId1 != txId2); Assert.Equal(txId1.GetHashCode(), txId2.GetHashCode()); }
public async Task NetworkForcesDelayOnTransactionForwardInTime() { await using var client = _network.NewClient(); var account = _network.Payer; var startInstant = Epoch.UniqueClockNanos(); var info = await client.GetAccountBalanceAsync(account, ctx => { ctx.Transaction = Protobuf.FromTransactionId(new Proto.TransactionID { AccountID = Protobuf.ToAccountID(_network.Payer), TransactionValidStart = Protobuf.ToTimestamp(DateTime.UtcNow.AddSeconds(6)) }); }); var duration = Epoch.UniqueClockNanos() - startInstant; Assert.InRange(duration, 4_000_000_000L, 240_000_000_000L); }
public async Task InvalidTransactionIdThrowsError() { await using var client = _network.NewClient(); var txId = Protobuf.FromTransactionId(new Proto.TransactionID { AccountID = Protobuf.ToAccountID(_network.Payer), TransactionValidStart = new Proto.Timestamp { Seconds = 500, Nanos = 100 } }); var tex = await Assert.ThrowsAsync <TransactionException>(async() => { await client.GetTransactionRecordAsync(txId); }); Assert.Equal(ResponseCode.RecordNotFound, tex.Status); Assert.StartsWith("Unable to retrieve transaction record.", tex.Message); }
/// <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> /// Internal implementation for Create Account /// Returns either a receipt or record or throws /// an exception. /// </summary> private async Task <TResult> CreateAccountImplementationAsync <TResult>(CreateAccountParams createParameters, Action <IContext>?configure) where TResult : new() { createParameters = RequireInputParameter.CreateParameters(createParameters); var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Create Account"); // Create Account requires just the 32 bits of the public key, without the prefix. var publicKeyWithoutPrefix = Keys.ImportPublicEd25519KeyFromBytes(createParameters.PublicKey).Export(KeyBlobFormat.PkixPublicKey).TakeLast(32).ToArray(); transactionBody.CryptoCreateAccount = new CryptoCreateTransactionBody { Key = new Proto.Key { Ed25519 = ByteString.CopyFrom(publicKeyWithoutPrefix) }, InitialBalance = createParameters.InitialBalance, SendRecordThreshold = createParameters.SendThresholdCreateRecord, ReceiveRecordThreshold = createParameters.ReceiveThresholdCreateRecord, ReceiverSigRequired = createParameters.RequireReceiveSignature, AutoRenewPeriod = Protobuf.ToDuration(createParameters.AutoRenewPeriod), }; 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 create account, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is AccountReceipt arcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, arcpt); arcpt.Address = Protobuf.FromAccountID(receipt.AccountID); } else if (result is AccountRecord arec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, arec); arec.Address = Protobuf.FromAccountID(receipt.AccountID); } return(result);
/// <summary> /// Internal helper function implementing file append services. /// </summary> public async Task <TResult> AppendFileImplementationAsync <TResult>(AppendFileParams appendParameters, Action <IContext>?configure = null) where TResult : new() { appendParameters = RequireInputParameter.AppendParameters(appendParameters); var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var appendFileBody = new FileAppendTransactionBody { FileID = Protobuf.ToFileId(appendParameters.File), Contents = ByteString.CopyFrom(appendParameters.Contents.ToArray()) }; var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Append File Content"); transactionBody.FileAppend = appendFileBody; 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 append to file, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is TransactionRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, rec); } else if (result is TransactionReceipt rcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, rcpt); } return(result);
/// <summary> /// Internal Helper function to retrieve the transaction record provided /// by the network following network consensus regarding a query or transaction. /// </summary> private async Task <Proto.TransactionRecord> GetTransactionRecordAsync(ContextStack context, TransactionID transactionRecordId) { 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 Transaction Record"); var query = new Query { TransactionGetRecord = new TransactionGetRecordQuery { Header = Transactions.SignQueryHeader(transactionBody, payer), TransactionID = transactionRecordId } }; var response = await Transactions.ExecuteRequestWithRetryAsync(context, query, getServerMethod, getResponseCode); var responseCode = getResponseCode(response); if (responseCode != ResponseCodeEnum.Ok) { throw new TransactionException("Unable to retrieve transaction record.", Protobuf.FromTransactionId(transactionRecordId), (ResponseCode)responseCode); } ValidateResult.PreCheck(transactionId, getResponseCode(response)); return(response.TransactionGetRecord.TransactionRecord);
/// <summary> /// Internal implementation of the contract call method. /// </summary> private async Task <TResult> CallContractImplementationAsync <TResult>(CallContractParams callParmeters, Action <IContext>?configure) where TResult : new() { callParmeters = RequireInputParameter.CallContractParameters(callParmeters); var context = CreateChildContext(configure); var gateway = RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Call Contract"); transactionBody.ContractCall = new ContractCallTransactionBody { ContractID = Protobuf.ToContractID(callParmeters.Contract), Gas = callParmeters.Gas, Amount = callParmeters.PayableAmount, FunctionParameters = Abi.EncodeFunctionWithArguments(callParmeters.FunctionName, callParmeters.FunctionArgs).ToByteString() }; 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($"Contract call failed, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is CallContractRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, rec); rec.Contract = Protobuf.FromContractID(record.Receipt.ContractID); rec.CallResult = Protobuf.FromContractCallResult(record.ContractCallResult); } else if (result is ContractReceipt rcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, rcpt); rcpt.Contract = Protobuf.FromContractID(receipt.ContractID); } return(result);
public async Task <TransactionReceipt> GetReceiptAsync(TxId transaction, Action <IContext>?configure = null) { transaction = RequireInputParameter.Transaction(transaction); await using var context = CreateChildContext(configure); var transactionId = Protobuf.ToTransactionID(transaction); var receipt = await GetReceiptAsync(context, transactionId); if (receipt.Status != ResponseCodeEnum.Success) { throw new TransactionException($"Unable to retreive receipt, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TransactionReceipt(); Protobuf.FillReceiptProperties(transactionId, receipt, result); return(result); }
/// <summary> /// Internal helper method implementing the file update service. /// </summary> public async Task <TResult> UpdateFileImplementationAsync <TResult>(UpdateFileParams 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 updateFileBody = new FileUpdateTransactionBody { FileID = Protobuf.ToFileId(updateParameters.File) }; if (!(updateParameters.Endorsements is null)) { updateFileBody.Keys = Protobuf.ToPublicKeyList(updateParameters.Endorsements); } if (updateParameters.Contents.HasValue) { updateFileBody.Contents = ByteString.CopyFrom(updateParameters.Contents.Value.ToArray()); } var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateTransactionBody(context, transactionId); transactionBody.FileUpdate = updateFileBody; 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 file, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is TransactionRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, rec); } else if (result is TransactionReceipt rcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, rcpt); } 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> /// Internal implementation of the Add Claim service. /// </summary> public async Task <TResult> AddClaimImplementationAsync <TResult>(Claim claim, Action <IContext>?configure) where TResult : new() { claim = RequireInputParameter.AddParameters(claim); var context = CreateChildContext(configure); var gateway = RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Add Claim"); transactionBody.CryptoAddClaim = new CryptoAddClaimTransactionBody { Claim = new Proto.Claim { AccountID = Protobuf.ToAccountID(claim.Address), Hash = ByteString.CopyFrom(claim.Hash.ToArray()), Keys = Protobuf.ToPublicKeyList(claim.Endorsements), ClaimDuration = Protobuf.ToDuration(claim.ClaimDuration) } }; 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 attach claim, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is TransactionRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, rec); } else if (result is TransactionReceipt rcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, rcpt); } 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> /// 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); var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var updateAccountBody = new CryptoUpdateTransactionBody { AccountIDToUpdate = Protobuf.ToAccountID(updateParameters.Account) }; if (!(updateParameters.Endorsement is null)) { updateAccountBody.Key = Protobuf.ToPublicKey(updateParameters.Endorsement); } if (updateParameters.SendThresholdCreateRecord.HasValue) { updateAccountBody.SendRecordThresholdWrapper = updateParameters.SendThresholdCreateRecord.Value; } if (updateParameters.ReceiveThresholdCreateRecord.HasValue) { updateAccountBody.ReceiveRecordThresholdWrapper = updateParameters.ReceiveThresholdCreateRecord.Value; } if (updateParameters.AutoRenewPeriod.HasValue) { updateAccountBody.AutoRenewPeriod = Protobuf.ToDuration(updateParameters.AutoRenewPeriod.Value); } if (updateParameters.Expiration.HasValue) { updateAccountBody.ExpirationTime = Protobuf.ToTimestamp(updateParameters.Expiration.Value); } var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Update Account"); transactionBody.CryptoUpdateAccount = updateAccountBody; var request = Transactions.SignTransaction(transactionBody, updateParameters.Account, 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 update account, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is AccountRecord arec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, arec); arec.Address = Protobuf.FromAccountID(receipt.AccountID); } else if (result is AccountReceipt arcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, arcpt); arcpt.Address = Protobuf.FromAccountID(receipt.AccountID); } return(result);
/// <summary> /// Internal implementation of the contract call method. /// </summary> private async Task <TResult> SubmitMessageImplementationAsync <TResult>(Address topic, ReadOnlyMemory <byte> message, Signatory?signatory, Action <IContext>?configure) where TResult : new() { topic = RequireInputParameter.Topic(topic); message = RequireInputParameter.Message(message); await using var context = CreateChildContext(configure); var gateway = 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.ConsensusSubmitMessage = new ConsensusSubmitMessageTransactionBody { TopicID = Protobuf.ToTopicID(topic), Message = ByteString.CopyFrom(message.Span) }; 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($"Submit Message failed, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is SubmitMessageRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, rec); rec.RunningHash = receipt.TopicRunningHash?.ToByteArray(); rec.SequenceNumber = receipt.TopicSequenceNumber; } else if (result is SubmitMessageReceipt rcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, rcpt); rcpt.RunningHash = receipt.TopicRunningHash?.ToByteArray(); rcpt.SequenceNumber = receipt.TopicSequenceNumber; } return(result);
/// <summary> /// Internal implementation of the Create File service. /// </summary> public async Task <TResult> CreateFileImplementationAsync <TResult>(CreateFileParams createParameters, Action <IContext>?configure) where TResult : new() { createParameters = RequireInputParameter.CreateParameters(createParameters); var context = CreateChildContext(configure); var gateway = RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Create File"); transactionBody.FileCreate = new FileCreateTransactionBody { ExpirationTime = Protobuf.ToTimestamp(createParameters.Expiration), Keys = Protobuf.ToPublicKeyList(createParameters.Endorsements), Contents = ByteString.CopyFrom(createParameters.Contents.ToArray()), }; 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 create file, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is FileRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, rec); rec.File = Protobuf.FromFileID(receipt.FileID); } else if (result is FileReceipt rcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, rcpt); rcpt.File = Protobuf.FromFileID(receipt.FileID); } return(result);
/// <summary> /// Internal implementation for Create Account /// Returns either a receipt or record or throws /// an exception. /// </summary> private async Task <TResult> CreateAccountImplementationAsync <TResult>(CreateAccountParams createParameters, Action <IContext>?configure) where TResult : new() { var publicKey = RequireInputParameter.KeysFromEndorsements(createParameters); await using var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var signatories = Transactions.GatherSignatories(context, createParameters.Signatory); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateTransactionBody(context, transactionId); transactionBody.CryptoCreateAccount = new CryptoCreateTransactionBody { Key = publicKey, InitialBalance = createParameters.InitialBalance, SendRecordThreshold = createParameters.SendThresholdCreateRecord, ReceiveRecordThreshold = createParameters.ReceiveThresholdCreateRecord, ReceiverSigRequired = createParameters.RequireReceiveSignature, AutoRenewPeriod = Protobuf.ToDuration(createParameters.AutoRenewPeriod), ProxyAccountID = createParameters.Proxy is null ? null : Protobuf.ToAccountID(createParameters.Proxy), }; 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 create account, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is CreateAccountRecord arec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, arec); arec.Address = Protobuf.FromAccountID(receipt.AccountID); } else if (result is CreateAccountReceipt arcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, arcpt); arcpt.Address = Protobuf.FromAccountID(receipt.AccountID); } return(result);
/// <summary> /// Internal Helper function to retrieve receipt record provided by /// the network following network consensus regarding a query or transaction. /// </summary> private async Task <Proto.TransactionReceipt> GetReceiptAsync(ContextStack context, TransactionID transactionId) { var query = new Query { TransactionGetReceipt = new TransactionGetReceiptQuery { TransactionID = transactionId } }; var response = await Transactions.ExecuteRequestWithRetryAsync(context, query, getServerMethod, shouldRetry); var responseCode = response.TransactionGetReceipt.Header.NodeTransactionPrecheckCode; switch (responseCode) { case ResponseCodeEnum.Ok: break; case ResponseCodeEnum.Busy: throw new ConsensusException("Network failed to respond to request for a transaction receipt, it is too busy. It is possible the network may still reach concensus for this transaction.", Protobuf.FromTransactionId(transactionId), (ResponseCode)responseCode); case ResponseCodeEnum.Unknown: case ResponseCodeEnum.ReceiptNotFound: throw new TransactionException($"Network failed return a transaction receipt, Status Code Returned: {responseCode}", Protobuf.FromTransactionId(transactionId), (ResponseCode)responseCode); } var status = response.TransactionGetReceipt.Receipt.Status; switch (status) { case ResponseCodeEnum.Unknown: throw new ConsensusException("Network failed to reach concensus within the configured retry time window, It is possible the network may still reach concensus for this transaction.", Protobuf.FromTransactionId(transactionId), (ResponseCode)status); case ResponseCodeEnum.TransactionExpired: throw new ConsensusException("Network failed to reach concensus before transaction request expired.", Protobuf.FromTransactionId(transactionId), (ResponseCode)status); case ResponseCodeEnum.RecordNotFound: throw new ConsensusException("Network failed to find a receipt for given transaction.", Protobuf.FromTransactionId(transactionId), (ResponseCode)status); default: return(response.TransactionGetReceipt.Receipt); }
/// <summary> /// Internal Create Contract Implementation /// </summary> public async Task <TResult> CreateContractImplementationAsync <TResult>(CreateContractParams createParameters, Action <IContext>?configure) where TResult : new() { createParameters = RequireInputParameter.CreateParameters(createParameters); await using var context = CreateChildContext(configure); var gateway = RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var signatory = Transactions.GatherSignatories(context, createParameters.Signatory); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateTransactionBody(context, transactionId); transactionBody.ContractCreateInstance = new ContractCreateTransactionBody { FileID = Protobuf.ToFileId(createParameters.File), AdminKey = createParameters.Administrator is null ? null : Protobuf.ToPublicKey(createParameters.Administrator), Gas = createParameters.Gas, InitialBalance = createParameters.InitialBalance, AutoRenewPeriod = Protobuf.ToDuration(createParameters.RenewPeriod), ConstructorParameters = ByteString.CopyFrom(Abi.EncodeArguments(createParameters.Arguments).ToArray()), Memo = context.Memo ?? "" }; 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 create contract, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is CreateContractRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, rec); rec.Contract = Protobuf.FromContractID(receipt.ContractID); } else if (result is CreateContractReceipt rcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, rcpt); rcpt.Contract = Protobuf.FromContractID(receipt.ContractID); } return(result);
/// <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> /// Internal helper function implementing the file delete functionality. /// </summary> public async Task <TResult> DeleteFileImplementationAsync <TResult>(Address fileToDelete, Signatory?signatory, Action <IContext>?configure = null) where TResult : new() { fileToDelete = RequireInputParameter.FileToDelete(fileToDelete); 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.FileDelete = new FileDeleteTransactionBody { FileID = Protobuf.ToFileId(fileToDelete) }; 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 file, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is TransactionRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, rec); } else if (result is TransactionReceipt rcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, rcpt); } return(result);
/// <summary> /// Internal implementation of the Create ConsensusTopic service. /// </summary> private async Task <TResult> CreateTopicImplementationAsync <TResult>(CreateTopicParams createParameters, Action <IContext>?configure) where TResult : new() { createParameters = RequireInputParameter.CreateParameters(createParameters); await using var context = CreateChildContext(configure); var gateway = RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var signatory = Transactions.GatherSignatories(context, createParameters.Signatory); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateTransactionBody(context, transactionId); transactionBody.ConsensusCreateTopic = new ConsensusCreateTopicTransactionBody { Memo = createParameters.Memo, AdminKey = createParameters.Administrator is null ? null : Protobuf.ToPublicKey(createParameters.Administrator), SubmitKey = createParameters.Participant is null ? null : Protobuf.ToPublicKey(createParameters.Participant), AutoRenewPeriod = Protobuf.ToDuration(createParameters.RenewPeriod), AutoRenewAccount = createParameters.RenewAccount is null ? null : Protobuf.ToAccountID(createParameters.RenewAccount) }; 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 create Consensus Topic, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is CreateTopicRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, rec); rec.Topic = Protobuf.FromTopicID(receipt.TopicID); } else if (result is CreateTopicReceipt rcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, rcpt); rcpt.Topic = Protobuf.FromTopicID(receipt.TopicID); } return(result);
/// <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> /// Internal implementation of the Delete Claim Methods /// </summary> public async Task <TResult> DeleteClaimImplementationAsync <TResult>(Address address, ReadOnlyMemory <byte> hash, Action <IContext>?configure = null) where TResult : new() { address = RequireInputParameter.Address(address); hash = RequireInputParameter.Hash(hash); var context = CreateChildContext(configure); RequireInContext.Gateway(context); var payer = RequireInContext.Payer(context); var transactionId = Transactions.GetOrCreateTransactionID(context); var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Delete Claim"); transactionBody.CryptoDeleteClaim = new CryptoDeleteClaimTransactionBody { AccountIDToDeleteFrom = Protobuf.ToAccountID(address), HashToDelete = ByteString.CopyFrom(hash.ToArray()) }; 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 remove Claim, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status); } var result = new TResult(); if (result is TransactionRecord rec) { var record = await GetTransactionRecordAsync(context, transactionId); Protobuf.FillRecordProperties(record, rec); } else if (result is TransactionReceipt rcpt) { Protobuf.FillReceiptProperties(transactionId, receipt, rcpt); } return(result);
/// <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());