背景:
因为需要频繁使用eth ropsten转账, python调用感觉不太方便, 所以整理了一个简单的转账客户端
代码:
package main
import (
"context"
"crypto/ecdsa"
"fmt"
"log"
"math/big"
"os"
"strconv"
//"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/crypto/sha3"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
args := os.Args
if len(args) != 4 {
fmt.Println(`
pls input correct parameters
usage:
$ ./transfer {to_address} {toke_type} {amount}
to_address: to token address
token_type: a***/usdt
amount: amount of token
ex:
./transfer xxxxxxxxxxxxxxx usdt 1`)
return
}
toArg := args[1]
amountArg1 := args[3]
token_type := args[2]
var tokenAddress common.Address
var gasLimit uint64
base := new(big.Float)
if token_type == "a***" {
/* a*** */
fmt.Println("transfer a***" )
base.SetFloat64(1000000000000000000)
tokenAddress = common.HexToAddress("xxxxxxxxxxxxxxxxxxxxxxxxxxxx")//合约地址
gasLimit = 210000
//fmt.Printf(contract )
} else {
/* USDT */
fmt.Println("transfer usdt" )
base.SetFloat64(1000000)
tokenAddress = common.HexToAddress("0x98b78f473dcfa4122dce98583e088d594185fba7")//USDT Contract: 0x98b78f473dcfa4122dce98583e088d594185fba7
gasLimit = 84309
}
amountArg, err := strconv.ParseFloat(amountArg1, 10)
fmt.Println(fmt.Sprintf("amount : %v", amountArg))
bigFloat := big.NewFloat(amountArg)
var bigFloat1 big.Float
bigFloat1.Mul(bigFloat, base)
client, err := ethclient.Dial("https://ropsten.infura.io/v3/xxxxxxxxxxxxxxxxxxxxxxxxxxxx")//infura地址
if err != nil {
log.Fatal(err)
}
privateKey, err := crypto.HexToECDSA("xxxxxxxxxxxxxxx")//私钥
if err != nil {
log.Fatal(err)
}
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.Fatal(err)
}
value := big.NewInt(0) // in wei (0 eth)
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
toAddress := common.HexToAddress(toArg)
transferFnSignature := []byte("transfer(address,uint256)")
hash := sha3.NewKeccak256()
hash.Write(transferFnSignature)
methodID := hash.Sum(nil)[:4]
//fmt.Println(hexutil.Encode(methodID)) // 0xa9059cbb
paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32)
amount := new(big.Int)
bigFloat1.Int(amount)
paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
var data []byte
data = append(data, methodID...)
data = append(data, paddedAddress...)
data = append(data, paddedAmount...)
tx := types.NewTransaction(nonce, tokenAddress, value, gasLimit, gasPrice, data)
chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatal(err)
}
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
log.Fatal(err)
}
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("tx sent: %s", signedTx.Hash().Hex()) // tx sent: xxxxxxxxxxxxxx
}
讲解:
合约地址
tokenAddress = common.HexToAddress("0x98b78f473dcfa4122dce98583e088d594185fba7")//USDT Contract: 0x98b78f473dcfa4122dce98583e088d594185fba7
来源:
https://ropsten.etherscan.io/token/0x98b78f473dcfa4122dce98583e088d594185fba7
infura地址:
client, err := ethclient.Dial("https://ropsten.infura.io/v3/xxxxxxxxxxxxxxxxxxxxxxxxxxxx")//infura地址
https://infura.io/dashboard/ethereum/02539f07320f43208937745e3058386f/settings
私钥:
privateKey, err := crypto.HexToECDSA("xxxxxxxxxxxxxxx")//私钥
编译收工
参考