0

Can someone help me successfully send ERC20 tokens using the Nethereum package in C# .NET? I am able to successfully get account balances, but when I try to send, it just sits there....

I am using the Infura.io project api also with the below security:

  • eth_accounts
  • eth_call
  • eth_getBalance
  • eth_getTransactionReceipt
  • eth_sendRawTransaction

var client = new EthClient(new RpcUrl("https://mainnet.infura.io/v3/-MyProjectID-"));

Here is the code I am using:

--The call to the transfer method

/* transfer 100 tokens */
        var transactionHashTask = client.transferTokens(coinOwnerAddress, coinOwnerPrivateKey, toAddress, contractAddress, 0);
        var transactionHash = transactionHashTask.Result.ToString();
        lblTransHash.Text = "Transaction hash: " + transactionHash;

--Code that contains the actual method

        public async Task<string> transferTokens(string senderAddress, string privateKey, string receiverAddress, string contractAddress, UInt64 tokens)
    {
        var transactionMessage = new TransferFunction()
        {
            FromAddress = senderAddress,
            To = receiverAddress,
            AmountToSend = tokens
            
        };
        var transferHandler = web3.Eth.GetContractTransactionHandler<TransferFunction>();
        Task<string> transactionHashTask = transferHandler.SendRequestAsync(contractAddress,transactionMessage);
        return await transactionHashTask;
    }
  • Update: I was able to get an error message: RpcResponseException: execution reverted: eth_estimateGas Any idea's? – user2634840 Jan 17 '22 at 04:28

3 Answers3

1

You might have to add something like this to your transactionMessage. Figure out what the Gas price is for the Blockchain network you are transacting with and use that. Here is an example.

transferHandler.Gas = Web3.Web3.Convert.ToWei(0.000112, Nethereum.Util.UnitConversion.EthUnit.Gwei);

Also, take a look at this Doc. Value or _value is actually TokenAmount.

https://docs.nethereum.com/en/latest/nethereum-smartcontrats-gettingstarted/

TransferFunction should look like this.

      To = To,
      //Shows Value in MetaMask
      Value = Web3.Web3.Convert.ToWei(Value, ERC20Contract.DecimalPlaces),
      //Amount to actually transfer
      AmountToSend = Web3.Web3.Convert.ToWei(Value, ERC20Contract.DecimalPlaces),
Dumber_Texan2
  • 840
  • 2
  • 12
  • 34
0

You are transferring something right? So maybe you have to send extra to account for the gas fees. But i'm no expert. Let me know if you solve this please.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31839674) – Hoppeduppeanut May 27 '22 at 04:50
0

The transfer function doesn't have AmountToSend parameter. It has TokenAmount. So change like below

var transactionMessage = new TransferFunction()
    {
        To = receiverAddress,
        TokenAmount= tokens
        
    };
Jini Jose
  • 1
  • 1
  • Here is the AmountToSend param. https://github.com/Nethereum/Nethereum/blob/master/src/Nethereum.Contracts/CQS/ContractMessageBase.cs – Dumber_Texan2 Mar 01 '23 at 20:55