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