KKSU
2024-05-09 7c93f4f06f148546d4c760cf40194212d78878e0
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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 io.reactivex.Flowable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.repository.query.ParameterOutOfBoundsException;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.DefaultBlockParameterNumber;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.StaticGasProvider;
 
import java.math.BigDecimal;
import java.math.BigInteger;
import java.rmi.activation.UnknownObjectException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author
 * @date 2022-03-23
 **/
@Slf4j
public class ChainService {
    private final static Map<String, ContractChainService> contractMap = new HashMap<>();
 
    static {
        for (ChainEnum chain : ChainEnum.values()) {
            if ("TRX".equals(chain.getChain())) {
                contractMap.put(chain.name(), new TrxService(chain.getAddress(), chain.getPrivateKey(), chain.getContractAddress(), chain.getApiKey()));
            } else {
                contractMap.put(chain.name(), new EthService(chain.getUrl(), chain.getAddress(), chain.getPrivateKey(), chain.getContractAddress()));
            }
        }
    }
 
    private ChainService() {
    }
 
    public final static ChainService INSTANCE = new ChainService();
 
    public static ContractChainService getInstance(String chainType) {
        ContractChainService contract = contractMap.get(chainType);
        if (contract == null) {
            throw new FebsException("参数错误");
        }
 
        return contract;
    }
 
    /**
     * 监听合约事件
     * @param startBlock 开始区块
     */
    public static void contractEventListener(BigInteger startBlock, ContractEventService event, String type) {
        contractEventListener(startBlock, null, event, type);
    }
 
    public static void contractEventListener(BigInteger startBlock, BigInteger endBlock, ContractEventService event, String type) {
        ChainEnum chain = ChainEnum.getValueByName(type);
        assert chain != null;
 
        EthUsdtContract contract = contract(chain.getPrivateKey(), chain.getContractAddress(), chain.getUrl());
        EthFilter filter = getFilter(startBlock, endBlock, chain.getContractAddress());
 
        Flowable<EthUsdtContract.TransferEventResponse> eventFlowable = contract.transferEventFlowable(filter);
        eventFlowable.subscribe(e -> {
            event.compile(e);
        }, error -> {
            log.error("合约监听启动报错", error);
        });
    }
 
    public static void coinRewardEventListener(BigInteger startBlock, BigInteger endBlock, ContractEventService event, String type) {
        ChainEnum chain = ChainEnum.getValueByName(type);
        assert chain != null;
 
        EthUsdtContract contract = contract(chain.getPrivateKey(), chain.getContractAddress(), chain.getUrl());
        EthFilter filter = getFilter(startBlock, endBlock, chain.getContractAddress());
 
        Flowable<EthUsdtContract.CoinRewardEventResponse> eventFlowable = contract.coinRewardEventFlowable(filter);
        eventFlowable.subscribe(e -> {
            event.coinReward(e);
        }, error -> {
            log.error("合约监听启动报错", error);
        });
    }
 
 
    private static EthUsdtContract contract(String privateKey, String contractAddress, String url) {
        Credentials credentials = Credentials.create(privateKey);
        return EthUsdtContract.load(contractAddress, Web3j.build(new HttpService(url)), credentials, new StaticGasProvider(BigInteger.valueOf(4500000L), BigInteger.valueOf(200000L)));
    }
 
    private static EthFilter getFilter(BigInteger startBlock, String contractAddress) {
        return getFilter(startBlock, null, contractAddress);
    }
 
    private static EthFilter getFilter(BigInteger startBlock, BigInteger endBlock, String contractAddress) {
        DefaultBlockParameter startParameterName = null;
        DefaultBlockParameter endParameterName = null;
        if (startBlock != null) {
            startParameterName = new DefaultBlockParameterNumber(startBlock);
        } else {
            startParameterName = DefaultBlockParameterName.EARLIEST;
        }
 
        if (endBlock != null) {
            endParameterName = new DefaultBlockParameterNumber(endBlock);
        } else {
            endParameterName = DefaultBlockParameterName.LATEST;
        }
 
        return new EthFilter(startParameterName, endParameterName, contractAddress);
    }
 
    /**
     * --todo 替换
     * @param args
     */
    public static void main(String[] args) {
        /**
         * 替换两个合约的地址
         */
        String contractAddress = ChainEnum.BSC_USDT.getContractAddress();
        String contractAddress1 = ChainEnum.BSC_GFA.getContractAddress();
 
        /**
         * 滑点接收钱包
         * GiveMeMoneyJob
         *      mineJob
         *          address参数
         */
        BigDecimal coinCnt = ChainService.getInstance(ChainEnum.BSC_GFA.name()).balanceOf("0xF6b06A30196aA5E318232a3b61319eab0FD4A3bF").setScale(8,BigDecimal.ROUND_DOWN);
        BigDecimal coinPrice = ChainService.getInstance(ChainEnum.BSC_GFA.name()).getPrice("0xF6b06A30196aA5E318232a3b61319eab0FD4A3bF").setScale(8,BigDecimal.ROUND_DOWN);
 
        /**
         * 批量转账的钱包地址
         *     注意钱包地址和私钥一起替换
         */
 
        String address = ChainEnum.BSC_USDT.getAddress();
        String address1 = ChainEnum.BSC_GFA.getAddress();
    }
 
}