wallet/pkg/tron/tron_balance.go
Anton Nesterov 5efcf35dd6
[init]
2024-08-31 16:59:34 +02:00

113 lines
3 KiB
Go

package tron
import (
"context"
"fmt"
"math/big"
"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"
"github.com/fbsobreira/gotron-sdk/pkg/proto/api"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type TronAccountBalance struct {
Address string `json:"address"`
USDT string `json:"usdt"`
TRX string `json:"trx"`
USDTRaw int64 `json:"usdtRaw"`
TRXRaw int64 `json:"trxRaw"`
TxFee string `json:"txFee"`
}
func TronBalance(address string) (*TronAccountBalance, error) {
var accountBalance TronAccountBalance = TronAccountBalance{
Address: address,
USDT: "0",
TRX: "0",
USDTRaw: 0,
TRXRaw: 0,
TxFee: "0",
}
conn := client.NewGrpcClient(tronNode.grpcNode)
err := conn.Start(grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}
balance, err := conn.TRC20ContractBalance(address, tronNode.contractAddress)
if err != nil {
return nil, err
}
decimals, err := conn.TRC20GetDecimals(tronNode.contractAddress)
if err != nil {
return nil, err
}
fmtUsdtBalance, _ := FormatSum(balance.Int64(), decimals.Int64())
accountBalance.USDT = fmtUsdtBalance
accountBalance.USDTRaw = balance.Int64()
account, err := conn.GetAccount(address)
if err != nil {
if err.Error() == "account not found" {
return &accountBalance, nil
}
return nil, err
}
params, _ := conn.Client.GetChainParameters(context.Background(), &api.EmptyMessage{})
txFee := 0
for _, el := range params.GetChainParameter() {
if el.Key == "getTotalEnergyTargetLimit" {
txFee += int(el.Value)
}
if el.Key == "getTransactionFee" {
txFee += int(el.Value)
}
}
accountBalance.TxFee, _ = FormatSum(int64(txFee), 6)
fmtTrxBalance, _ := FormatSum(account.Balance, 6)
accountBalance.TRX = fmtTrxBalance
accountBalance.TRXRaw = account.Balance
conn.Stop()
return &accountBalance, nil
}
func TronAllowance(owner, spender string) (int64, error) {
conn := client.NewGrpcClient(tronNode.grpcNode)
err := conn.Start(grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return 0, err
}
data := crypto.Keccak256Hash([]byte("allowance(address,address)")).Bytes()[:4]
ownerAddress, _ := address.Base58ToAddress(owner)
spenderAddress, _ := address.Base58ToAddress(spender)
req := ""
req += common.Bytes2Hex(data)
req += "0000000000000000000000000000000000000000000000000000000000000000"[len(ownerAddress.Hex())-4:] + ownerAddress.Hex()[4:]
req += "0000000000000000000000000000000000000000000000000000000000000000"[len(spenderAddress.Hex())-4:] + spenderAddress.Hex()[4:]
res, err := conn.TRC20Call("", tronNode.contractAddress, req, true, 0)
if err != nil {
fmt.Println("TRC20Call error: ", err)
return 0, err
}
allowance := new(big.Int)
//fmt.Println("res", res)
if len(res.GetConstantResult()) > 0 {
allowance.SetBytes(res.GetConstantResult()[0])
}
conn.Stop()
return allowance.Int64(), nil
}