5

I'm working on integrating Nethereum into my .NET 5 C# API and can do read queries against my chosen blockchain (BSC), but cannot get a SendTransactionAsync or SendRequestAsync to successfully execute. I'm consistently getting the following exception:
Nethereum.JsonRpc.Client.RpcResponseException: 'transaction type not supported: eth_sendRawTransaction'.

Here are code snippets of what I have tried:

// Setup
var account = new Account(privateKey, chainId);
var rpcUrl = "https://data-seed-prebsc-2-s2.binance.org:8545/";
var client = new RpcClient(new Uri(rpcUrl));
var web3 = new Web3(account, client);

var mediaTokenAddress = "0x1E4d1BFDa5d55C2176E9E3e8367BAe720525a8e0";
var mtSvc = new MediaTokenService(web3, mediaTokenAddress);
var mintMsg = new MintNftFunction
{
    FromAddress = account.Address,
    Recipient = "REDACTED",
    MetadataHash = "TestMetaDataHash",
    MediaHash = "TestMediaHash",
    SeasonId = 1
};
// Attempt #1:  Using C# classes generated by the Nethereum CodeGen library
var txReceipt = await mtSvc.MintNftRequestAndWaitForReceiptAsync(mintMsg);
// Attempt #2
var txHandler = web3.Eth.GetContractTransactionHandler<MintNftFunction>();
var signedTx = await txHandler.SignTransactionAsync(mediaTokenAddress, mintMsg);
var txReceipt = await web3.Eth.Transactions.SendTransaction.SendRequestAsync(signedTx);
// Attempt #3
var txInput = mintMsg.CreateTransactionInput(mediaTokenAddress);
var txReceipt = await web3.Eth.TransactionManager.SendTransactionAsync(txInput);

Is there a configuration step I'm missing? Any help is appreciated!

EDIT: I want to call a contract method that will change values within the contract, rather than sending currency. So I need help figuring out how to do that.

2 Answers2

10

For those that come across this issue, I resolved it by setting the following flag on my web3 instance:

web3.TransactionManager.UseLegacyAsDefault = true;

If there is a way to do what I need without setting this flag, please feel free to leave a comment.

  • so by default nethereum is using the not legacy, is there a way to work around the error message with non legacy? – BigChief Jul 14 '22 at 13:24
0

Here is an example of how I do this with Nethereum

var web3 = new Nethereum.Web3.Web3("YOUR_NODE_ADDRESS");

var privateKey = "someprivatekey";
var senderAddress = "0x..."; // put actual sender address

var receiveAddress = "0x..."; //put actual receiver address

var txCount = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(senderAddress);

double sendAmount = 5.09540000; //this is ETH
var amountInWei = Web3.Convert.ToWei(sendAmount);

//600 GWEI = 0.000000600
//60 GWEI = 0.000000060
var gwei = 147; // this is 0.000000147 ETH. You will want to calculate this based on network fees
var gasPrice = Web3.Convert.ToWei(0.000000001 * gwei); 
var gasLimit = Web3.Convert.ToWei(0.000000000000021);


var encoded = Web3.OfflineTransactionSigner.SignTransaction(privateKey, receiveAddress, amountInWei, txCount.Value, gasPrice, gasLimit);

//This is what prompts the transactions
Web3.OfflineTransactionSigner.GetSenderAddress(encoded).Dump();
//TX Returns from this action
var txId = await web3.Eth.Transactions.SendRawTransaction.SendRequestAsync("0x" + encoded);
//Dump out the TX if successful
txId.Dump();

I've used this many times and it has worked for me just fine.

mathis1337
  • 1,426
  • 8
  • 13
  • This is of course if you are wanting to send ETH> if you wan to send tokens or interact with contracts then that would require something else, but your post does not specify. Just that you are wanting to send a transaction – mathis1337 Aug 19 '21 at 22:44
  • Thank you for pointing this out @mathis1337. I will update my post. I need help with sending transactions that call functions on contracts which change contract state. – Crimson Salamander Aug 20 '21 at 02:12
  • Although, I could try this approach anyway and see what happens. – Crimson Salamander Aug 20 '21 at 02:17
  • Were you able to get this all worked out? I'm having issues getting my .net api c# code to send the final mint transaction. I have the ECDSA hash sign working but I can't get the sender/minter address and the owner address right it seems. I keep getting "Invalid account used signing" – 0xElGato Jan 06 '22 at 16:58