package tron import ( "encoding/hex" "fmt" "strconv" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/fbsobreira/gotron-sdk/pkg/address" "github.com/fbsobreira/gotron-sdk/pkg/client" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" ) const trc20TransferFromSig = "0x23b872dd" func (ta *TronAccount) WithdrawUSDTByCommissionContract(contractAddr string, from string, to string, amount string, camount string) (string, error) { amountFloat, _ := strconv.ParseFloat(amount, 64) comissionFloat, _ := strconv.ParseFloat(camount, 64) conn := client.NewGrpcClient(tronNode.grpcNode) err := conn.Start(grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return "", err } dec, err := conn.TRC20GetDecimals(tronNode.contractAddress) if err != nil { return "", err } metodId := crypto.Keccak256Hash([]byte("chargeTransfer(address,address,address,uint256,uint256)")).Bytes()[:4] token, _ := address.Base58ToAddress(tronNode.contractAddress) fromAddress, _ := address.Base58ToAddress(from) toAddress, _ := address.Base58ToAddress(to) amountToSend := Float64ToDecimals(amountFloat, dec.Int64()) paddedAmount := common.LeftPadBytes(amountToSend.Bytes(), 32) comissionToSend := Float64ToDecimals(comissionFloat, dec.Int64()) paddedComission := common.LeftPadBytes(comissionToSend.Bytes(), 32) req := "" req += common.Bytes2Hex(metodId) fmt.Println("req: ", req) req += "0000000000000000000000000000000000000000000000000000000000000000"[len(token.Hex())-4:] + token.Hex()[4:] req += "0000000000000000000000000000000000000000000000000000000000000000"[len(fromAddress.Hex())-4:] + fromAddress.Hex()[4:] req += "0000000000000000000000000000000000000000000000000000000000000000"[len(toAddress.Hex())-4:] + toAddress.Hex()[4:] req += common.Bytes2Hex(paddedAmount) req += common.Bytes2Hex(paddedComission) txe, err := conn.TRC20Call(ta.Address(), contractAddr, req, false, tronNode.feeLimit) if err != nil { fmt.Println("TRC20Call error: ", err) return "", err } tx, err := ta.SignTx(txe.Transaction) if err != nil { return "", err } _, err = conn.Broadcast(tx) if err != nil { fmt.Println("Broadcast error: ", err) return "", err } conn.Stop() return hex.EncodeToString(txe.GetTxid()), nil } func (ta *TronAccount) WithdrawUSDTBySpender(from string, to string, amount string) (string, error) { amountFloat, _ := strconv.ParseFloat(amount, 64) conn := client.NewGrpcClient(tronNode.grpcNode) err := conn.Start(grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return "", err } dec, err := conn.TRC20GetDecimals(tronNode.contractAddress) if err != nil { return "", err } addrA, _ := address.Base58ToAddress(from) addrB, _ := address.Base58ToAddress(to) amountToSend := Float64ToDecimals(amountFloat, dec.Int64()) paddedAmount := common.LeftPadBytes(amountToSend.Bytes(), 32) req := trc20TransferFromSig req += "0000000000000000000000000000000000000000000000000000000000000000"[len(addrA.Hex())-4:] + addrA.Hex()[4:] req += "0000000000000000000000000000000000000000000000000000000000000000"[len(addrB.Hex())-4:] + addrB.Hex()[4:] req += common.Bytes2Hex(paddedAmount) txe, err := conn.TRC20Call(ta.Address(), tronNode.contractAddress, req, false, tronNode.feeLimit) if err != nil { return "", err } tx, err := ta.SignTx(txe.Transaction) if err != nil { return "", err } _, err = conn.Broadcast(tx) if err != nil { fmt.Println("Broadcast error: ", err) return "", err } conn.Stop() return hex.EncodeToString(txe.GetTxid()), nil } func (ta *TronAccount) WithdrawUSDT(recipient string, amount string) (string, error) { amountFloat, _ := strconv.ParseFloat(amount, 64) sender := ta.Address() conn := client.NewGrpcClient(tronNode.grpcNode) err := conn.Start(grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return "", err } dec, err := conn.TRC20GetDecimals(tronNode.contractAddress) if err != nil { return "", err } amountToSend := Float64ToDecimals(amountFloat, dec.Int64()) txe, err := conn.TRC20Send(sender, recipient, tronNode.contractAddress, amountToSend, tronNode.feeLimit) if err != nil { return "", err } tx, err := ta.SignTx(txe.Transaction) if err != nil { return "", err } _, err = conn.Broadcast(tx) if err != nil { fmt.Println("Broadcast error: ", err) return "", err } conn.Stop() return hex.EncodeToString(txe.GetTxid()), nil } func (ta *TronAccount) WithdrawTRX(recipient string, amount string) (string, error) { amountFloat, _ := strconv.ParseFloat(amount, 64) sender := ta.Address() conn := client.NewGrpcClient(tronNode.grpcNode) err := conn.Start(grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return "", err } amountToSend := Float64ToDecimals(amountFloat, 6) txe, err := conn.Transfer(sender, recipient, amountToSend.Int64()) if err != nil { return "", err } tx, err := ta.SignTx(txe.Transaction) if err != nil { return "", err } _, err = conn.Broadcast(tx) if err != nil { fmt.Println("Broadcast error: ", err) return "", err } conn.Stop() return hex.EncodeToString(txe.GetTxid()), nil }