Reputation: 1401
Any ideas about this? I am attempting to make a arbitrage swap using the following contract The error is:
Low-level call error: err: insufficient funds for gas * price + value: address 0x have 26389996078916648 want 3681518208500000000 (supplied gas 500000000)
3681518208500000000 is 3.1354971585ETH or $8099.91 @ 2583.29 $ Per Ether. There obviously something wrong here with the the following code:
func gasCalculated(from, to, token string, amount int) *big.Int {
// Fetch the contract ABI
contractABI, err := abi.JSON(strings.NewReader(flashLoanReceiverABI())) // Correct ABI fetching
if err != nil {
fmt.Println("Failed to fetch ABI:", err)
return big.NewInt(0)
}
// Create context
ctx := context.Background()
// Fetch the base fee and gas tip (miner's tip) for the next block
header, err := connection.Get().HeaderByNumber(ctx, nil)
if err != nil {
fmt.Println("Failed to get header:", err)
return big.NewInt(0)
}
tip, err := connection.Get().SuggestGasTipCap(ctx)
if err != nil {
fmt.Println("Failed to suggest gas tip cap:", err)
return big.NewInt(0)
}
// Increase the tip cap if it's too low (e.g., minimum 1 Gwei)
minTipCap := big.NewInt(1000000000) // 1 Gwei
if tip.Cmp(minTipCap) < 0 {
tip.Set(minTipCap)
}
// Adding a margin of 2 Gwei to the base fee
margin := big.NewInt(2000000000) // 2 Gwei
// Calculate GasFeeCap by adding a margin to the base fee to ensure transaction inclusion
gasFeeCap := new(big.Int).Add(header.BaseFee, margin)
// Add the suggested gas tip cap to ensure priority in the transaction pool
gasFeeCap.Add(gasFeeCap, tip)
// Specify the sender and recipient addresses
fromAddress := common.HexToAddress(from)
toAddress := common.HexToAddress(to)
tokenAddress := common.HexToAddress(token)
addressType, err := abi.NewType("address", "", nil)
if err != nil {
log.Fatalf("Failed to create address type: %v", err)
}
addresses := abi.Arguments{
{Type: addressType},
{Type: addressType},
{Type: addressType},
}
packedData, err := addresses.Pack(fromAddress, toAddress, tokenAddress)
if err != nil {
log.Fatalf("Failed to pack addresses: %v", err)
}
data, err := contractABI.Pack("onFlashLoan", fromAddress, tokenAddress, big.NewInt(int64(amount)), big.NewInt(0), packedData)
if err != nil {
fmt.Println("ABI packing failed:", err)
return big.NewInt(0)
}
callMsg := ethereum.CallMsg{
From: fromAddress,
To: &toAddress,
GasFeeCap: gasFeeCap,
GasTipCap: tip,
Value: big.NewInt(0), // Adjust if ETH is sent along with the call
Data: data,
}
// Perform the low-level eth_call to check the transaction
var result hexutil.Bytes
result, err = connection.Get().CallContract(ctx, callMsg, header.Number)
if err != nil {
fmt.Printf("Low-level call %v\n", err) //<--Low-level call error: err: insufficient funds for gas * price + value: address 0x have 26389996078916648 want 3681518208500000000 (supplied gas 500000000)
return big.NewInt(0)
}
// Decode or inspect `result` to understand the failure
fmt.Printf("Low-level call result: %x\n", result)
// Estimate the gas required for the transaction
gasLimit, err := connection.Get().EstimateGas(ctx, callMsg)
if err != nil {
fmt.Println("Failed to estimate gas:", err)
return big.NewInt(0)
}
return big.NewInt(int64(gasLimit))
}
Upvotes: 0
Views: 107