Files
eth_transfer_go/utils/ethtransferhelper/transfer_helper.go

176 lines
5.6 KiB
Go
Raw Normal View History

2025-05-13 15:44:44 +08:00
package ethtransferhelper
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"golang.org/x/crypto/sha3"
)
// transferErc20Token 发送 ERC-20 代币交易。
// tokenAmount: 要发送的代币数量 (例如1.0 代表 1 个代币)。
// fromPrivateKey: 发送者的私钥。
// tokenAddress: ERC-20 代币的合约地址。
// toAddress: 接收者的地址。
// tokenDecimals: 代币的小数位数 (例如USDC 是 6很多其他代币是 18)。
func transferErc20Token(
client *ethclient.Client,
fromPrivateKey string,
tokenAddress string,
toAddress string,
tokenAmount float64,
tokenDecimals uint8,
) (*types.Transaction, error) {
// 1. 解析私钥
privateKey, fromAddressCommon, err := GetAddressFromPrivateKey(fromPrivateKey)
if err != nil {
return nil, err
}
// 3. 获取发送者的 nonce交易序号
nonce, err := client.PendingNonceAt(context.Background(), fromAddressCommon)
if err != nil {
return nil, fmt.Errorf("获取 nonce 失败: %w", err)
}
// 4. 设置交易的 value (对于代币转账value 是 0)
value := big.NewInt(0) // 不发送 ETH
// 5. 获取 Gas 价格
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return nil, fmt.Errorf("获取 Gas 价格失败: %w", err)
}
// 6. 将地址字符串转换为 common.Address 类型
toAddressCommon := common.HexToAddress(toAddress)
tokenAddressCommon := common.HexToAddress(tokenAddress)
// 7. 构造 ERC-20 transfer 函数的调用数据
// 7.1 函数签名transfer(address,uint256)
transferFnSignature := []byte("transfer(address,uint256)")
hash := sha3.New256()
hash.Write(transferFnSignature)
methodID := hash.Sum(nil)[:4] // 取前 4 个字节作为方法 ID
// 7.2 填充接收者地址和转账金额
paddedAddress := common.LeftPadBytes(toAddressCommon.Bytes(), 32)
// 7.3 将代币数量转换为最小单位
amountBigInt, err := convertTokenAmountToBigInt(tokenAmount, tokenDecimals)
if err != nil {
return nil, fmt.Errorf("转换代币数量失败: %w", err)
}
paddedAmount := common.LeftPadBytes(amountBigInt.Bytes(), 32)
// 7.4 拼接调用数据
var data []byte
data = append(data, methodID...)
data = append(data, paddedAddress...)
data = append(data, paddedAmount...)
// 8. 估算 Gas 消耗
gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
From: fromAddressCommon,
To: &tokenAddressCommon,
Data: data,
})
if err != nil {
return nil, fmt.Errorf("估算 Gas 消耗失败: %w", err)
}
// 9. 增加 Gas 限制的安全边际
gasLimit = gasLimit * 11 / 10 // 增加 10%
if gasLimit < 23000 {
gasLimit = 23000 // 最小 Gas 限制
}
// 10. 创建交易
tx := types.NewTransaction(nonce, tokenAddressCommon, value, gasLimit, gasPrice, data)
// 11. 获取链 ID
chainID, err := client.NetworkID(context.Background())
if err != nil {
return nil, fmt.Errorf("获取链 ID 失败: %w", err)
}
// 12. 签名交易
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
return nil, fmt.Errorf("签名交易失败: %w", err)
}
// 13. 发送交易
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
return nil, fmt.Errorf("发送交易失败: %w", err)
}
return signedTx, nil
}
// getAddressFromPrivateKey 从私钥获取公钥和地址。
func GetAddressFromPrivateKey(fromPrivateKey string) (*ecdsa.PrivateKey, common.Address, error) {
// 1. 移除 "0x" 前缀(如果存在)
if strings.HasPrefix(fromPrivateKey, "0x") {
fromPrivateKey = fromPrivateKey[2:]
}
privateKey, err := crypto.HexToECDSA(fromPrivateKey)
if err != nil {
return nil, common.Address{}, fmt.Errorf("解析私钥失败: %w", err)
}
// 2. 从私钥获取公钥和发送者地址
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return nil, common.Address{}, errors.New("无法将公钥转换为 ECDSA 类型")
}
fromAddressCommon := crypto.PubkeyToAddress(*publicKeyECDSA)
return privateKey, fromAddressCommon, nil
}
// convertTokenAmountToBigInt 将代币数量 (例如1.0) 转换为最小单位的整数表示 (例如USDC 的 1000000)。
func convertTokenAmountToBigInt(tokenAmount float64, tokenDecimals uint8) (*big.Int, error) {
// 1. 使用最大精度格式化浮点数
amountStr := fmt.Sprintf("%.18f", tokenAmount) // 使用最大精度
// 2. 找到小数点的位置
decimalPointIndex := strings.Index(amountStr, ".")
// 3. 如果没有小数点,则添加足够的 0
if decimalPointIndex == -1 {
amountStr += "." + strings.Repeat("0", int(tokenDecimals))
} else {
// 4. 计算需要填充的 0 的数量
paddingNeeded := int(tokenDecimals) - (len(amountStr) - decimalPointIndex - 1)
// 5. 填充 0 或截断多余的小数位
if paddingNeeded > 0 {
amountStr += strings.Repeat("0", paddingNeeded)
} else if paddingNeeded < 0 {
amountStr = amountStr[:decimalPointIndex+int(tokenDecimals)+1]
}
// 6. 移除小数点
amountStr = strings.ReplaceAll(amountStr, ".", "")
}
// 7. 将字符串转换为 big.Int
amountBigInt := new(big.Int)
amountBigInt, ok := amountBigInt.SetString(amountStr, 10)
if !ok {
return nil, fmt.Errorf("将金额字符串转换为 big.Int 失败: %s", amountStr)
}
return amountBigInt, nil
}