231 lines
6.1 KiB
Go
231 lines
6.1 KiB
Go
package eth
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math/big"
|
|
|
|
"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"
|
|
)
|
|
|
|
func (e *EthAccount) WithdrawUSDTByComissionContract(contractAddr string, from string, to string, amount string, camount string) (string, error) {
|
|
client, err := ethclient.Dial(ethNode.rpcNode)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
metodId := crypto.Keccak256Hash([]byte("chargeTransfer(address,address,address,uint256,uint256)")).Bytes()[:4]
|
|
|
|
paddedTokenAddress := common.LeftPadBytes(common.HexToAddress(ethNode.usdtContractAddress).Bytes(), 32)
|
|
paddedFromAddress := common.LeftPadBytes(common.HexToAddress(from).Bytes(), 32)
|
|
paddedToAddress := common.LeftPadBytes(common.HexToAddress(to).Bytes(), 32)
|
|
|
|
amountToSend := floatStringToDec(amount, ethNode.usdtContractDecimals)
|
|
paddedAmount := common.LeftPadBytes(amountToSend.Bytes(), 32)
|
|
commissionToSend := floatStringToDec(camount, ethNode.usdtContractDecimals)
|
|
paddedCommission := common.LeftPadBytes(commissionToSend.Bytes(), 32)
|
|
|
|
var data []byte
|
|
data = append(data, metodId...)
|
|
data = append(data, paddedTokenAddress...)
|
|
data = append(data, paddedFromAddress...)
|
|
data = append(data, paddedToAddress...)
|
|
data = append(data, paddedAmount...)
|
|
data = append(data, paddedCommission...)
|
|
|
|
nonce, err := client.PendingNonceAt(context.Background(), e.address)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
gasLimit := uint64(210000)
|
|
gasTipCap, err := client.SuggestGasTipCap(context.Background())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
fmt.Println("Nonce", nonce)
|
|
chainId, _ := client.ChainID(context.Background())
|
|
toAddress := common.HexToAddress(contractAddr)
|
|
tx := types.NewTx(&types.DynamicFeeTx{
|
|
ChainID: chainId,
|
|
To: &toAddress,
|
|
Nonce: nonce,
|
|
Value: big.NewInt(0),
|
|
Gas: gasLimit,
|
|
Data: data,
|
|
GasTipCap: big.NewInt(gasTipCap.Int64() * 2),
|
|
GasFeeCap: big.NewInt(40000000000),
|
|
})
|
|
|
|
signedTx, err := e.SignTx(tx)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = client.SendTransaction(context.Background(), signedTx)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return signedTx.Hash().Hex(), nil
|
|
}
|
|
|
|
func (e *EthAccount) WithdrawUSDTBySpender(from string, to string, amount string) (string, error) {
|
|
client, err := ethclient.Dial(ethNode.rpcNode)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
metodId := crypto.Keccak256Hash([]byte("transferFrom(address,address,uint256)")).Bytes()[:4]
|
|
|
|
paddedFromAddress := common.LeftPadBytes(common.HexToAddress(from).Bytes(), 32)
|
|
paddedToAddress := common.LeftPadBytes(common.HexToAddress(to).Bytes(), 32)
|
|
|
|
amountToSend := floatStringToDec(amount, ethNode.usdtContractDecimals)
|
|
paddedAmount := common.LeftPadBytes(amountToSend.Bytes(), 32)
|
|
|
|
var data []byte
|
|
data = append(data, metodId...)
|
|
data = append(data, paddedFromAddress...)
|
|
data = append(data, paddedToAddress...)
|
|
data = append(data, paddedAmount...)
|
|
|
|
nonce, err := client.PendingNonceAt(context.Background(), e.address)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
gasLimit := uint64(60000)
|
|
gasTipCap, err := client.SuggestGasTipCap(context.Background())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
chainId, _ := client.ChainID(context.Background())
|
|
toAddress := common.HexToAddress(ethNode.usdtContractAddress)
|
|
tx := types.NewTx(&types.DynamicFeeTx{
|
|
ChainID: chainId,
|
|
To: &toAddress,
|
|
Nonce: nonce,
|
|
Value: big.NewInt(0),
|
|
Gas: gasLimit,
|
|
Data: data,
|
|
GasTipCap: gasTipCap,
|
|
GasFeeCap: big.NewInt(20000000000),
|
|
})
|
|
|
|
signedTx, err := e.SignTx(tx)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = client.SendTransaction(context.Background(), signedTx)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return signedTx.Hash().Hex(), nil
|
|
}
|
|
|
|
func (e *EthAccount) WithdrawUSDT(recipient string, amount string) (string, error) {
|
|
client, err := ethclient.Dial(ethNode.rpcNode)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
metodId := crypto.Keccak256Hash([]byte("transfer(address,uint256)")).Bytes()[:4]
|
|
paddedAddress := common.LeftPadBytes(common.HexToAddress(recipient).Bytes(), 32)
|
|
|
|
amountToSend := floatStringToDec(amount, ethNode.usdtContractDecimals)
|
|
paddedAmount := common.LeftPadBytes(amountToSend.Bytes(), 32)
|
|
|
|
var data []byte
|
|
data = append(data, metodId...)
|
|
data = append(data, paddedAddress...)
|
|
data = append(data, paddedAmount...)
|
|
|
|
nonce, err := client.PendingNonceAt(context.Background(), e.address)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
gasLimit := uint64(60000)
|
|
gasTipCap, err := client.SuggestGasTipCap(context.Background())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
chainId, _ := client.ChainID(context.Background())
|
|
toAddress := common.HexToAddress(ethNode.usdtContractAddress)
|
|
tx := types.NewTx(&types.DynamicFeeTx{
|
|
ChainID: chainId,
|
|
To: &toAddress,
|
|
Nonce: nonce,
|
|
Value: big.NewInt(0),
|
|
Gas: gasLimit,
|
|
Data: data,
|
|
GasTipCap: gasTipCap,
|
|
GasFeeCap: big.NewInt(20000000000),
|
|
})
|
|
|
|
signedTx, err := e.SignTx(tx)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = client.SendTransaction(context.Background(), signedTx)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return signedTx.Hash().Hex(), nil
|
|
}
|
|
|
|
func (e *EthAccount) WithdrawETH(recipient string, amount string) (string, error) {
|
|
client, err := ethclient.Dial(ethNode.rpcNode)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
nonce, err := client.PendingNonceAt(context.Background(), e.Address())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
gasTipCap, err := client.SuggestGasTipCap(context.Background())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
value := floatStringToWei(amount)
|
|
gasLimit := uint64(21000)
|
|
toAddress := common.HexToAddress(recipient)
|
|
chainId, _ := client.ChainID(context.Background())
|
|
|
|
tx := types.NewTx(&types.DynamicFeeTx{
|
|
ChainID: chainId,
|
|
To: &toAddress,
|
|
Nonce: nonce,
|
|
Value: value,
|
|
Gas: gasLimit,
|
|
Data: nil,
|
|
GasTipCap: gasTipCap,
|
|
GasFeeCap: big.NewInt(20000000000),
|
|
})
|
|
|
|
signedTx, err := e.SignTx(tx)
|
|
if err != nil {
|
|
fmt.Println("sign error", err)
|
|
return "", err
|
|
}
|
|
|
|
err = client.SendTransaction(context.Background(), signedTx)
|
|
if err != nil {
|
|
fmt.Println("send tx", err)
|
|
return "", err
|
|
}
|
|
|
|
return signedTx.Hash().Hex(), nil
|
|
}
|