0

I am trying to wrap my head around the go ethereum library and sending transactions.

The issue is that I want to be able to query the balance BEFORE the transaction and AFTER. The code below gives me the same value even after making a second call to retrieve the balance and print it.

I have cut out bits of code that I think are superfluous to the question :

func main() {
    client, err := ethclient.Dial("https://rinkeby.infura.io")
    if err != nil {
        log.Fatal(err)
    }

    privateKey, err := crypto.HexToECDSA("49be7f9b459942977f07f99653b5b042144baefa092c5ac833d6ec9893a5b8dd")
    if err != nil {
        log.Fatal(err)
    }

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

    fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) // Under stand where this goes *publicKeyECDSA   

    balance, err := client.BalanceAt(context.Background(), fromAddress, nil)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Address: %d \nBalance:%s\n", fromAddress.Hex(), balance) // This is the first call to retrieve the balance

    nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
    if err != nil {
        log.Fatal(err)
    }

    value := big.NewInt(100000000000000) // Remember to read go docs for big.NewInt

    gasLimit := uint64(21000)

    gasPrice, err := client.SuggestGasPrice(context.Background()) // What's in SuggestGas Price??
    if err != nil {
        log.Fatal(err)
    }

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


    tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, nil) // All Parameters from Transactions in Whitepaper

    signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, privateKey)
    if err != nil {
        log.Fatal(err)
    }


    err = client.SendTransaction(context.Background(), signedTx)
    if err != nil {
        log.Fatal(err)
    }

    balanceAfterSend, err := client.BalanceAt(context.Background(), fromAddress, nil) // Second call to retrieve balance after transfer has been broadcasted
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("tx sent: %s\n", signedTx.Hash().Hex())
    fmt.Printf("Balance After Transfer : %d\n", balanceAfterSend) // This still gives the same value as the balance before.

}

When I run the program, I get the follow output in my terminal:

Balance:18749516000000000000
tx sent: 0x67a3b7f3d33253f2465ebc6d1cdf5b26cfac0b8d7a3c247ff184b06d46badfd2
Balance After Transfer : 18749516000000000000

I will appreciate any pointers on this.

1

You may call PendingBalanceAt to get the balance of the account while it's pending confirmations.

pendingBalance, err := client.PendingBalanceAt(context.Background(), fromAddress)
1

Sending a transaction doesn't mean it is mined immediately. This is why the balance doesn't change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.