0

我无法将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})转换为 83218945020000000000。

我们将此值转换为十六进制以兼容。但我得到的十六进制是;“0x76f730b400000000000000000000000000000000000000000000000482e51595”

我正在使用带有 .net core 2.1 的 Nethereum 库

4

1 回答 1

1

您正在尝试解码交易的智能合约功能参数。智能合约为ERC20智能合约,功能为Transfer方法。

为此,您需要执行以下操作。

  • 在此场景中创建您的函数消息,即传递函数。
  • 检索交易
  • 使用 FunctionMessage 扩展方法 DecodeTransaction,解码原始 Transaction 输入的值。
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);
        }

于 2019-12-24T10:54:12.940 回答