无法从 nethereum 解码输入数据中获取可读整数
Cannot get readable integer from nethereum decoded input data
我在将 solidity 的 uint256 转换为可读的 c# 对象时遇到问题。
public Transaction DecodeInputData(Transaction tx)
{
EthApiContractService ethApi = new EthApiContractService(null);
var contract = ethApi.GetContract(Abi.Replace(@"\", string.Empty), Address);
var transfer = contract.GetFunction("transfer");
var decodedTx = transfer.DecodeInput(tx.input);
tx.to = (string)decodedTx[0].Result;
tx.value = "0x" + ((BigInteger)decodedTx[1].Result).ToString("x");
return tx;
}
发送示例:https://etherscan.io/tx/0x622760ad1a0ead8d16641d5888b8c36cb67be5369556f8887499f4ad3e3d1c3d
我们必须能够将 decodedTx[1].Result 变量(其:{53809663494440740791636285293469688360281260987263635605451211260198698423701})转换为 832189450200000=00200[01020]。
我们将此值转换为十六进制以实现兼容性。但是我得到的十六进制是; “0x76f730b400000000000000000000000000000000000000000000000482e51595”
我在 .net core 2.1 中使用 Nethereum 库
您正在尝试解码交易的智能合约功能参数。智能合约为ERC20智能合约,函数为Transfer方法
为此,您需要执行以下操作。
- 在此场景中创建您的函数消息,传递函数。
- 检索交易
- 使用 FunctionMessage 扩展方法 DecodeTransaction,解码原始交易输入的值。
using Nethereum.Web3;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts.CQS;
using Nethereum.Util;
using Nethereum.Web3.Accounts;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Contracts;
using Nethereum.Contracts.Extensions;
using System;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
public class GetStartedSmartContracts
{
[Function("transfer", "bool")]
public class TransferFunction : FunctionMessage
{
[Parameter("address", "_to", 1)]
public string To { get; set; }
[Parameter("uint256", "_value", 2)]
public BigInteger TokenAmount { get; set; }
}
public static async Task Main()
{
var url = "https://mainnet.infura.io";
var web3 = new Web3(url);
var txn = await web3.Eth.Transactions.GetTransactionByHash.SendRequestAsync("0x622760ad1a0ead8d16641d5888b8c36cb67be5369556f8887499f4ad3e3d1c3d");
var transfer = new TransferFunction().DecodeTransaction(txn);
Console.WriteLine(transfer.TokenAmount);
//BAT has 18 decimal places the same as Wei
Console.WriteLine(Web3.Convert.FromWei(transfer.TokenAmount));
}
}
您可以在 http://playground.nethereum.com
中进行测试
或者你也可以这样做,如果想查看函数是什么:
var functionCallDecoder = new FunctionCallDecoder();
if(functionCallDecoder.IsDataForFunction(ABITypedRegistry.GetFunctionABI<TransferFunction>().Sha3Signature, txn.Input)) {
var transfer = new TransferFunction().DecodeInput(txn.Input);
Console.WriteLine(Web3.Convert.FromWei(transfer.TokenAmount));
Console.WriteLine(transfer.To);
}
我在将 solidity 的 uint256 转换为可读的 c# 对象时遇到问题。
public Transaction DecodeInputData(Transaction tx)
{
EthApiContractService ethApi = new EthApiContractService(null);
var contract = ethApi.GetContract(Abi.Replace(@"\", string.Empty), Address);
var transfer = contract.GetFunction("transfer");
var decodedTx = transfer.DecodeInput(tx.input);
tx.to = (string)decodedTx[0].Result;
tx.value = "0x" + ((BigInteger)decodedTx[1].Result).ToString("x");
return tx;
}
发送示例:https://etherscan.io/tx/0x622760ad1a0ead8d16641d5888b8c36cb67be5369556f8887499f4ad3e3d1c3d
我们必须能够将 decodedTx[1].Result 变量(其:{53809663494440740791636285293469688360281260987263635605451211260198698423701})转换为 832189450200000=00200[01020]。
我们将此值转换为十六进制以实现兼容性。但是我得到的十六进制是; “0x76f730b400000000000000000000000000000000000000000000000482e51595”
我在 .net core 2.1 中使用 Nethereum 库
您正在尝试解码交易的智能合约功能参数。智能合约为ERC20智能合约,函数为Transfer方法
为此,您需要执行以下操作。
- 在此场景中创建您的函数消息,传递函数。
- 检索交易
- 使用 FunctionMessage 扩展方法 DecodeTransaction,解码原始交易输入的值。
using Nethereum.Web3;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts.CQS;
using Nethereum.Util;
using Nethereum.Web3.Accounts;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Contracts;
using Nethereum.Contracts.Extensions;
using System;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
public class GetStartedSmartContracts
{
[Function("transfer", "bool")]
public class TransferFunction : FunctionMessage
{
[Parameter("address", "_to", 1)]
public string To { get; set; }
[Parameter("uint256", "_value", 2)]
public BigInteger TokenAmount { get; set; }
}
public static async Task Main()
{
var url = "https://mainnet.infura.io";
var web3 = new Web3(url);
var txn = await web3.Eth.Transactions.GetTransactionByHash.SendRequestAsync("0x622760ad1a0ead8d16641d5888b8c36cb67be5369556f8887499f4ad3e3d1c3d");
var transfer = new TransferFunction().DecodeTransaction(txn);
Console.WriteLine(transfer.TokenAmount);
//BAT has 18 decimal places the same as Wei
Console.WriteLine(Web3.Convert.FromWei(transfer.TokenAmount));
}
}
您可以在 http://playground.nethereum.com
中进行测试或者你也可以这样做,如果想查看函数是什么:
var functionCallDecoder = new FunctionCallDecoder();
if(functionCallDecoder.IsDataForFunction(ABITypedRegistry.GetFunctionABI<TransferFunction>().Sha3Signature, txn.Input)) {
var transfer = new TransferFunction().DecodeInput(txn.Input);
Console.WriteLine(Web3.Convert.FromWei(transfer.TokenAmount));
Console.WriteLine(transfer.To);
}