fix
Helius
2022-08-08 69e44659412c7868592331d3b1570b87147b4025
src/main/java/cc/mrbird/febs/dapp/chain/ChainService.java
@@ -4,7 +4,9 @@
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 okhttp3.OkHttpClient;
import org.springframework.data.repository.query.ParameterOutOfBoundsException;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
@@ -22,6 +24,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
 * @author
@@ -60,25 +63,37 @@
     * @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, chain.getContractAddress());
        EthFilter filter = getFilter(startBlock, endBlock, chain.getContractAddress());
        contract.transferEventFlowable(filter).subscribe(e -> {
        Flowable<EthUsdtContract.TransferEventResponse> eventFlowable = contract.transferEventFlowable(filter);
        eventFlowable.subscribe(e -> {
            event.compile(e);
        }, error -> {
            log.error("--->", 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)));
        return EthUsdtContract.load(contractAddress,
                Web3j.build(new HttpService(url, new OkHttpClient().newBuilder()
                                                                    .connectTimeout(100, TimeUnit.SECONDS)
                                                                    .writeTimeout(100, TimeUnit.SECONDS)
                                                                    .readTimeout(100, TimeUnit.SECONDS)
                                                                    .build())),
                credentials,
                new StaticGasProvider(BigInteger.valueOf(4500000L), BigInteger.valueOf(200000L)));
    }
    // 18097238  18098663
    private static EthFilter getFilter(BigInteger startBlock, String contractAddress) {
        return getFilter(startBlock, null, contractAddress);
    }
@@ -102,17 +117,19 @@
    }
    public static void main(String[] args) {
        ChainEnum chain = ChainEnum.getValueByName(ChainEnum.BSC_TFC.name());
        assert chain != null;
//        ChainEnum chain = ChainEnum.getValueByName(ChainEnum.BSC_TFC.name());
//        assert chain != null;
//
//        EthUsdtContract contract = contract(chain.getPrivateKey(), chain.getContractAddress(), chain.getUrl());
//        EthFilter filter = getFilter(new BigInteger("18097238"), chain.getContractAddress());
//
//        contract.transferEventFlowable(filter).subscribe(e -> {
//            System.out.println(1);
//        }, error -> {
//            log.error("--->", error);
//        });
        EthUsdtContract contract = contract(chain.getPrivateKey(), chain.getContractAddress(), chain.getUrl());
        EthFilter filter = getFilter(new BigInteger("18097238"), new BigInteger("18098663"), chain.getContractAddress());
        contract.transferEventFlowable(filter).subscribe(e -> {
            System.out.println(1);
        }, error -> {
            log.error("--->", error);
        });
        System.out.println(ChainService.getInstance(ChainEnum.BSC_TFC.name()).totalSupply());
    }
}