Skip to content

Instantly share code, notes, and snippets.

@ckruger097
Created August 20, 2021 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckruger097/a156d04a5f3542eac20e6eea5129946d to your computer and use it in GitHub Desktop.
Save ckruger097/a156d04a5f3542eac20e6eea5129946d to your computer and use it in GitHub Desktop.
Updated Go file for transferring tokens in goethereumbook
package main
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/metachris/eth-go-bindings/erc20"
"io/ioutil"
"log"
"math/big"
"strings"
)
func main() {
client, err := ethclient.Dial("https://rinkeby.infura.io/v3/{{API_KEY}}") // API KEY needed from infura
if err != nil {
log.Fatal(err)
}
address := common.HexToAddress("0x0000000000000000000000000000000000000000") // contract of token, e.g. https://rinkeby.etherscan.io/address/0x014dF965e86d241b7CB1303C235CBDB197cf3e2e
token, err := erc20.NewErc20(address, client) // see metachris/eth-go-bindings for go bindings
if err != nil {
log.Fatal(err)
}
name, err := token.Name(nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Name of token:", name) // optional
key, err := ioutil.ReadFile("./keystores/{{KEYSTORE_FILE}}") // keystore of wallet that holds ERC-20 tokens
chainId, err := client.NetworkID(context.Background())
if err != nil {
log.Fatal(err)
}
auth, err := bind.NewTransactorWithChainID(strings.NewReader(string(key)), "PASSWORD", chainId)
if err != nil {
log.Fatal(err)
}
value := big.NewInt(10000000000000000000) // 10 of given token
tx, err := token.Transfer(auth, common.HexToAddress("0x0000000000000000000000000000000000000000"), value) // recipient address
if err != nil {
log.Fatal(err)
}
fmt.Printf("transfer pending: 0x%x\n", tx.Hash())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment