wallet/pkg/eth/eth.go

184 lines
4.3 KiB
Go
Raw Permalink Normal View History

2024-08-31 14:46:20 +00:00
package eth
import (
"fmt"
"os"
"strconv"
"sync"
)
const CONTRACT_ADDRESS string = "ERC20_USDT_CONTRACT_ADDRESS"
const CONTRACT_DECIMALS string = "ERC20_USDT_CONTRACT_DECIMALS"
const ETH_RPC_NODE string = "ETH_RPC_NODE"
type EthNode struct {
usdtContractAddress string
usdtContractDecimals int
rpcNode string
}
var ethNode *EthNode
var once sync.Once
func GetERC20ContractAddress() string {
return ethNode.usdtContractAddress
}
func (e EthNode) Init() (*EthNode, error) {
if ethNode != nil {
return ethNode, nil
}
usdtContract, isAddressPresent := os.LookupEnv(CONTRACT_ADDRESS)
if !isAddressPresent {
return nil, fmt.Errorf("missing environment variable: %s", CONTRACT_ADDRESS)
}
usdtContractDecimals, isDecimalsPresent := os.LookupEnv(CONTRACT_DECIMALS)
if !isDecimalsPresent {
return nil, fmt.Errorf("missing environment variable: %s", CONTRACT_DECIMALS)
}
rpcNode, isRpcNodePresent := os.LookupEnv(ETH_RPC_NODE)
if !isRpcNodePresent {
return nil, fmt.Errorf("missing environment variable: %s", ETH_RPC_NODE)
}
if ethNode == nil {
once.Do(func() {
e.usdtContractAddress = usdtContract
e.usdtContractDecimals, _ = strconv.Atoi(usdtContractDecimals)
e.rpcNode = rpcNode
ethNode = &e
})
}
return ethNode, 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"
}
]
`