wallet/pkg/tron/tron.go
Anton Nesterov f1bc53ce2a
[init]
2024-08-31 17:00:14 +02:00

177 lines
4 KiB
Go

package tron
import (
"fmt"
"os"
"sync"
)
const CONTRACT_ADDRESS string = "TRC20_USDT_CONTRACT_ADDRESS"
const TRON_GRPC_NODE string = "TRON_GRPC_NODE"
type TronNode struct {
contractAddress string
grpcNode string
feeLimit int64
}
var tronNode *TronNode
var once sync.Once
func GetTRC20ContractAddress() string {
return tronNode.contractAddress
}
func (t TronNode) Init() (*TronNode, error) {
if tronNode != nil {
return tronNode, nil
}
trc20Contract, isAddressPresent := os.LookupEnv(CONTRACT_ADDRESS)
if !isAddressPresent {
return nil, fmt.Errorf("missing environment variable: %s", CONTRACT_ADDRESS)
}
grpcNode, isGrpcNodePresent := os.LookupEnv(TRON_GRPC_NODE)
if !isGrpcNodePresent {
return nil, fmt.Errorf("missing environment variable: %s", TRON_GRPC_NODE)
}
if tronNode == nil {
once.Do(func() {
t.contractAddress = trc20Contract
t.grpcNode = grpcNode
t.feeLimit = 100000000
tronNode = &t
})
}
return tronNode, nil
}
const COMMON_ABI string = `
[
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [ { "name": "", "type": "string" } ],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{ "name": "_spender", "type": "address" },
{ "name": "_value", "type": "uint256" }
],
"name": "approve",
"outputs": [ { "name": "", "type": "bool" } ],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [ { "name": "", "type": "uint256" } ],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{ "name": "_from", "type": "address" },
{ "name": "_to", "type": "address" },
{ "name": "_value", "type": "uint256" }
],
"name": "transferFrom",
"outputs": [ { "name": "", "type": "bool" } ],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [ { "name": "", "type": "uint8" } ],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [ { "name": "_owner", "type": "address" } ],
"name": "balanceOf",
"outputs": [ { "name": "", "type": "uint256" } ],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [ { "name": "", "type": "string" } ],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{ "name": "_to", "type": "address" },
{ "name": "_value", "type": "uint256" }
],
"name": "transfer",
"outputs": [ { "name": "", "type": "bool" } ],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{ "name": "_token", "type": "address" },
{ "name": "_from", "type": "address" },
{ "name": "_to", "type": "address" },
{ "name": "_value", "type": "uint256" },
{ "name": "_camount", "type": "uint256" }
],
"name": "chargeTransfer",
"outputs": [ { "name": "", "type": "bool" } ],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{ "name": "_owner", "type": "address" },
{ "name": "_spender", "type": "address" }
],
"name": "allowance",
"outputs": [ { "name": "", "type": "uint256" } ],
"payable": false,
"type": "function"
},
{ "inputs": [], "payable": false, "type": "constructor" },
{
"anonymous": false,
"inputs": [
{ "indexed": true, "name": "_from", "type": "address" },
{ "indexed": true, "name": "_to", "type": "address" },
{ "indexed": false, "name": "_value", "type": "uint256" }
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{ "indexed": true, "name": "_owner", "type": "address" },
{ "indexed": true, "name": "_spender", "type": "address" },
{ "indexed": false, "name": "_value", "type": "uint256" }
],
"name": "Approval",
"type": "event"
}
]
`