1

I am tinkering with a piece of code that creates keys and addresses. When I run the code I get the following error.

insufficient funds for gas * price + value

My question is, how can I get funds into this address?

Any suggestions?

package main

import (
    "context"
    "crypto/ecdsa"
    "fmt"
    "log"
    "math/big"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/common/hexutil"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/ethclient"
    "github.com/jimlawless/whereami"
    "golang.org/x/crypto/sha3"
)

func main() {
    client, err := ethclient.Dial("https://rinkeby.infura.io/v3/xyz")

    if err != nil {
        log.Fatal(err.Error() + "@" + err.Error() + " @ " + whereami.WhereAmI())
    }

    privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
    if err != nil {
        log.Fatal(err.Error() + "@" + err.Error() + " @ " + whereami.WhereAmI())
    }

    publicKey := privateKey.Public()
    publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    if !ok {
        log.Fatal("error casting public key to ECDSA")
    }

    fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
    fmt.Print("fromAddress: ")
    fmt.Println(fromAddress)
    nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
    if err != nil {
        log.Fatal(err.Error() + "@" + err.Error() + " @ " + whereami.WhereAmI())
    }

    value := big.NewInt(0)      // in wei (0 eth)
    gasLimit := uint64(2000000) // in units
    gasPrice, err := client.SuggestGasPrice(context.Background())
    if err != nil {
        log.Fatal(err.Error() + "@" + err.Error() + " @ " + whereami.WhereAmI())
    }

    toAddress := common.HexToAddress("0x4592d8f8d7b001e72cb26a73e4fa1806a51ac79d")
    tokenAddress := common.HexToAddress("0x28b149020d2152179873ec60bed6bf7cd705775d")

    transferFnSignature := []byte("transfer(address,uint256)")
    hash := sha3.NewLegacyKeccak256()
    hash.Write(transferFnSignature)
    methodID := hash.Sum(nil)[:4]
    fmt.Println(hexutil.Encode(methodID)) // 0xa9059cbb

    paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32)
    fmt.Println(hexutil.Encode(paddedAddress)) // 0x0000000000000000000000004592d8f8d7b001e72cb26a73e4fa1806a51ac79d

    amount := new(big.Int)
    amount.SetString("1000000000000000000000", 10) // 1000 tokens
    paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
    fmt.Println(hexutil.Encode(paddedAmount)) // 0x00000000000000000000000000000000000000000000003635c9adc5dea00000

    var data []byte
    data = append(data, methodID...)
    data = append(data, paddedAddress...)
    data = append(data, paddedAmount...)

    tx := types.NewTransaction(nonce, tokenAddress, value, gasLimit, gasPrice, data)
    signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, privateKey)
    if err != nil {
        log.Fatal(err.Error() + "@" + err.Error() + " @ " + whereami.WhereAmI())
    }

    err = client.SendTransaction(context.Background(), signedTx)
    if err != nil {
        log.Fatal(err.Error() + "@" + err.Error() + " @ " + whereami.WhereAmI())
    }

    fmt.Printf("tx sent: %s", signedTx.Hash().Hex()) // tx sent: 0xa56316b637a94c4cc0331c73ef26389d6c097506d581073f927275e7a6ece0bc
}

pigfox
  • 1,301
  • 3
  • 28
  • 52

1 Answers1

1

To get funds on the account, you transfer them from another account that already has funds. This depends on the network or the execution environment where you deploy your accounts.

In the case of Rinkerby testnet, you transfer them from the developer faucet.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • When I tweeted https://twitter.com/pigfoxeria/status/1488672478129909762 then went to https://faucet.rinkeby.io and added the url and clicked give me ether I got the following error "insufficient funds for gas * price + value" from https://faucet.rinkeby.io – pigfox Feb 02 '22 at 00:39
  • If you have another question please open another question. I am not Rikerby user support in SO comments. – Mikko Ohtamaa Feb 02 '22 at 05:09