Home > Software design >  Why I cannot send ftm with this simple code
Why I cannot send ftm with this simple code

Time:06-16

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim privatekey = System.IO.File.ReadAllText("privatekey.txt")
    'Dim publickey = "0x898568c58466957bedaE0e2A2457beb158a150de" ''
    Dim destination = "0x7fD0Ec4d9908A712852d32d110839Fc1A9Ce55d5"
    Dim rpcURL = "https://rpc.ftm.tools" ' where should I put this
    Dim chainID = 250 'Where should I put this?

    Dim account = New Nethereum.Web3.Accounts.Account(privatekey, chainID)
    Dim web3 = New Nethereum.Web3.Web3(account, rpcURL)
    Dim amount = New Nethereum.Hex.HexTypes.HexBigInteger(10)
    Await web3.TransactionManager.SendTransactionAsync(account.Address, destination, amount)
End Sub

I am learning to use nethereum.

I make this simple code. Simple. I want to send 10 wei of fantom to another address. Simple right.

I got this error

enter image description here

It's very weird.

Index was outside the bound of the array? What bound?

Anyway, this is such a very simple program. I wonder where do I went wrong with this?

This is the right way to do it right?

Other minor related question while we're at it (should I ask this separately). Why the hell chainID is the property of account but rpcURL is the property of web3. I thought fantom chain, that uses https://rpc.ftm.tools uses chainID 250. So if we know the chain id, we kind of know the rpc url and if we know the rpc url we sort of know that it'll only work for one chain ID anyway.

How do I fix my code? What's wrong? And if anyone can explain the chainID rpcURL dilemma it'll be great.

I look further where the error happens. It seems that the error happen when nethereum tries to compute gas fees.

at

Nethereum.RPC.Fee1559Suggestions.TimePreferenceFeeSuggestionStrategy.SuggestFees(FeeHistoryResult feeHistory, BigInteger tip)
   at Nethereum.RPC.Fee1559Suggestions.TimePreferenceFeeSuggestionStrategy.<SuggestFeesAsync>d__29.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Nethereum.RPC.Fee1559Suggestions.TimePreferenceFeeSuggestionStrategy.<SuggestFeeAsync>d__27.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Nethereum.RPC.TransactionManagers.TransactionManagerBase.<SetTransactionFeesOrPricingAsync>d__37.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Nethereum.Web3.Accounts.AccountSignerTransactionManager.<SignTransactionRetrievingNextNonceAsync>d__14.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Nethereum.Web3.Accounts.AccountSignerTransactionManager.<SignAndSendTransactionAsync>d__16.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Nethereum.RPC.TransactionReceipts.TransactionReceiptPollingService.<SendRequestAndWaitForReceiptAsync>d__5.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at testapp.Form1.VB$StateMachine_9_doSomething.MoveNext() in C:\Users\teguh\Dropbox\vb.net\testapp\testapp\Form1.vb:line 21
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at testapp.Form1.VB$StateMachine_8_Button1_Click.MoveNext() in C:\Users\teguh\Dropbox\vb.net\testapp\testapp\Form1.vb:line 3

I plan to do this manually anyway. But how exactly I should do that?

I changed the code to

  Dim privatekey = System.IO.File.ReadAllText("privatekey.txt")
    'Dim publickey = "0x898568c58466957bedaE0e2A2457beb158a150de" ''
    Dim destination = "0x7fD0Ec4d9908A712852d32d110839Fc1A9Ce55d5"
    Dim rpcURL = "https://rpc.ftm.tools" ' where should I put this
    Dim chainID = 250 'Where should I put this?

    Dim account = New Nethereum.Web3.Accounts.Account(privatekey, chainID)

    Dim web3Obj = New Nethereum.Web3.Web3(account, rpcURL)
    'Dim amount = Nethereum.Web3.Web3.Convert.ToWei(0.1)
    Dim balance = Await web3Obj.Eth.GetBalance.SendRequestAsync(account.Address)
    Dim EtherBalance = Nethereum.Web3.Web3.Convert.FromWei(balance.Value) 'I got the correct value here


    Dim result = Await web3Obj.Eth.GetEtherTransferService.TransferEtherAndWaitForReceiptAsync(destination, 1D, 1)

Basically, I am being stingy and say that the gas price should be 1 wei. I "deservedly" got this message

transaction underpriced

I think I should just code the gas price my self. How do I do so? What's the minimum gas price for fantom?

CodePudding user response:

Your issue is that Fantom is a Fork of Ethereum and has not implemented yet the EIP-1559 pricing model. For this you have to either provide the gas price (to force legacy) or enable it in the web3.Eth.TransactionManager (Legacy)

Nethereum uses the eth_gasPrice call automatically to set the Gas Price if not provided and if it is in Legacy mode. But you should check with the Fantom chain what is the best way to get your prices.

Why do you need to pass the chainId? This is required to sign the transaction, and to ensure you know what chain you are using. (Prevent inputting the wrong url and get a replay attack).

  • Related