我的 C# Web API 中的 Nethereuem SendTransactionAsync 失败,且事务类型不受支持:eth_sendRawTransaction

Cri*_*der 5 c# blockchain .net-5 nethereum

我正在努力将 Nethereum 集成到我的 .NET 5 C# API 中,并且可以针对我选择的区块链 (BSC) 进行读取查询,但无法成功执行 SendTransactionAsync 或 SendRequestAsync。我一直遇到以下异常:
Nethereum.JsonRpc.Client.RpcResponseException: 'transaction type not supported: eth_sendRawTransaction'

以下是我尝试过的代码片段:

// 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
};
Run Code Online (Sandbox Code Playgroud)
// Attempt #1:  Using C# classes generated by the Nethereum CodeGen library
var txReceipt = await mtSvc.MintNftRequestAndWaitForReceiptAsync(mintMsg);
Run Code Online (Sandbox Code Playgroud)
// Attempt #2
var txHandler = web3.Eth.GetContractTransactionHandler<MintNftFunction>();
var signedTx = await txHandler.SignTransactionAsync(mediaTokenAddress, mintMsg);
var txReceipt = await web3.Eth.Transactions.SendTransaction.SendRequestAsync(signedTx);
Run Code Online (Sandbox Code Playgroud)
// Attempt #3
var txInput = mintMsg.CreateTransactionInput(mediaTokenAddress);
var txReceipt = await web3.Eth.TransactionManager.SendTransactionAsync(txInput);
Run Code Online (Sandbox Code Playgroud)

我缺少配置步骤吗?任何帮助表示赞赏!

编辑:我想调用一个合约方法来更改合约内的值,而不是发送货币。所以我需要帮助弄清楚如何做到这一点。

Cri*_*der 10

对于遇到此问题的人,我通过在 web3 实例上设置以下标志来解决它:

web3.TransactionManager.UseLegacyAsDefault = true;
Run Code Online (Sandbox Code Playgroud)

如果有一种方法可以在不设置此标志的情况下完成我需要的操作,请随时发表评论。


小智 0

这是我如何使用 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();
Run Code Online (Sandbox Code Playgroud)

我已经使用过很多次了,它对我来说效果很好。