-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer_tokens.go
105 lines (87 loc) · 3.1 KB
/
transfer_tokens.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"context"
"crypto/ecdsa"
"fmt"
"log"
"math/big"
"os"
"github.com/ethereum/go-ethereum"
"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"
"golang.org/x/crypto/sha3"
)
func main() {
client, err := ethclient.Dial(os.Getenv("RINKEBY_INFURA_URL"))
if err != nil {
log.Fatalf("ethclient connection error: %s", err.Error())
}
privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
if err != nil {
log.Fatalf("try get private key error: %s", err.Error())
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatalf("try get peding nonce error: %s", err.Error())
}
// add amount to 0 because we'll send tokens
value := big.NewInt(0)
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatalf("try get SuggestGasPrice error: %s", err.Error())
}
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)))
paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32)
fmt.Println("paddedAddress: ", hexutil.Encode(paddedAddress))
amount := new(big.Int)
// this set value to 1,000 tokens. This is the value the smart-contract understands
// as 1,000 tokens from a user representation.
amount.SetString("1000000000000000000000", 10)
paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
fmt.Println(hexutil.Encode(paddedAmount))
var data []byte
data = append(data, methodID...)
data = append(data, paddedAddress...)
data = append(data, paddedAmount...)
// Set gasLimit
gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
To: &tokenAddress,
Data: data,
})
if err != nil {
log.Fatalf("try estimateGas error: %s", err.Error())
}
fmt.Println("gas limit: ", gasLimit)
// Create a new transaction
tx := types.NewTransaction(nonce, tokenAddress, value, gasLimit, gasPrice, data)
chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatalf("try get chainID error: %s", err.Error())
}
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
log.Fatalf("try signed transaction error: %s", err.Error())
}
// broadcast the transaction
// Insufficient funds for gas * price + value
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatalf("try send transaction error: %s", err.Error())
}
fmt.Println("tx sent: ", signedTx.Hash().Hex())
}