1

I have the following code

var transactionMessage = new TransferFunction
        {
            FromAddress = accountFrom,
            To = accountTo,
            AmountToSend = tokens,
            Gas = new HexBigInteger(gasPrice)
        };

        var tokenService = new Nethereum.StandardTokenEIP20.StandardTokenService(web, accountTo);
        var transferReceipt = await tokenService.TransferAndWaitForReceiptAsync(transactionMessage);

        return transferReceipt.TransactionHash;

It works great for sending ETH. However I need to modify it to send custom ERC20 tokens. Any ideas how to do it?

2

I didn't use Nethereum by myself, but why won't you use such code (taken from here):

 var transactionMessage = new TransferFunction
 {
      FromAddress = senderAddress,
      To = newAddress,
      TokenAmount = 1000
 };

var transferHandler = web3.Eth.GetContractTransactionHandler<TransferFunction>();
var transferReceipt = await transferHandler.SendRequestAndWaitForReceiptAsync(contractAddress, transactionMessage);

var transaction = await web3.Eth.Transactions.GetTransactionByHash.SendRequestAsync(transferReceipt.TransactionHash);
var transferDecoded = transaction.DecodeTransactionToFunctionMessage<TransferFunction>();

TransferFunction is defined here:

[Function("transfer", "bool")]
public class TransferFunction : FunctionMessage
{
    [Parameter("address", "_to", 1)]
    public string To { get; set; }

    [Parameter("uint256", "_value", 2)]
    public int TokenAmount { get; set; }
}

There are also examples of getting token balance.

  • I could have swore I tried something similar before. Thanks a lot! – Peter Chikov Aug 29 '18 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.