xiaoyong931011
2023-06-07 3c1a3bb702693a70487154c8804c648be8c29630
src/main/java/cc/mrbird/febs/dapp/chain/ChainService.java
@@ -1,26 +1,49 @@
package cc.mrbird.febs.dapp.chain;
import cc.mrbird.febs.common.exception.FebsException;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.reactivestreams.Subscription;
import org.springframework.data.repository.query.ParameterOutOfBoundsException;
import org.springframework.util.Base64Utils;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
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.Log;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.websocket.WebSocketClient;
import org.web3j.protocol.websocket.WebSocketService;
import org.web3j.tx.Contract;
import org.web3j.tx.gas.StaticGasProvider;
import org.web3j.utils.Async;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.rmi.activation.UnknownObjectException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -60,6 +83,7 @@
    /**
     * 监听合约事件
     *
     * @param startBlock 开始区块
     */
    public static void contractEventListener(BigInteger startBlock, ContractEventService event, String type) {
@@ -81,15 +105,69 @@
        });
    }
    public static void wssContractEventListener(BigInteger startBlock, ContractEventService event, String type) {
        WebSocketService ws = null;
        WebSocketClient webSocketClient = null;
        Web3j web3j = null;
        try {
            webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/78074065950e4915aef4f12b6f357d16"));
            ws = new WebSocketService(webSocketClient, true);
            ws.connect();
            web3j = Web3j.build(ws);
            ChainEnum chain = ChainEnum.getValueByName(type);
            assert chain != null;
            EthUsdtContract ethUsdtContract = wssContract(chain.getPrivateKey(), chain.getContractAddress(), web3j);
            EthFilter filter = getFilter(startBlock, null, chain.getContractAddress());
            Flowable<EthUsdtContract.TransferEventResponse> eventFlowable = ethUsdtContract.transferEventFlowable(filter);
            Disposable subscribe = eventFlowable.subscribe(event::compile, error -> {
                log.error("币安监听异常", error);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void wssBaseCoinEventListener(BigInteger startBlock, BaseCoinService event) {
        WebSocketService ws = null;
        WebSocketClient webSocketClient = null;
        Web3j web3j = null;
        try {
            webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/78074065950e4915aef4f12b6f357d16"));
            ws = new WebSocketService(webSocketClient, true);
            ws.connect();
            web3j = Web3j.build(ws);
            Disposable subscribe = web3j.replayPastAndFutureTransactionsFlowable(new DefaultBlockParameterNumber(startBlock)).subscribe(event::compile, error ->{
                log.error("监听链上异常", error);
            });
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static EthUsdtContract contract(String privateKey, String contractAddress, String url) {
        Credentials credentials = Credentials.create(privateKey);
        HttpService httpService = new HttpService(url);
//        httpService.addHeader("Authorization", "Bearer " + Base64.encode("tfc:tfc123".getBytes()));
//        httpService.addHeader("Authorization", "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJwdWJsaWMiLCJleHAiOjE2NTk5MzcxOTAsImp0aSI6IjRiMjNkYTVjLWRlZWEtNDYzNi04YjMwLWNmMmZmMjVkM2NlYyIsImlhdCI6MTY1OTkzMzU5MCwiaXNzIjoiQW5rciIsIm5iZiI6MTY1OTkzMzU5MCwic3ViIjoiZmNiNjY0YjItOGEwNC00N2E5LTg3ZjMtNTJhMjE2ODVlMzEzIn0.YfEwvDByU2MGHywsblZpEmKMIbjv4cWYkn5CaFglXY0TSANzd2pCSbIe40yU_R9_nV6xZeE8Uk74jJOdd_QvMpFyUgo-MMNWZP6uiEaYvK_K3tlpk5yzeZq9D4ruWaq8rFKggr-iaRGzu6coRSAOFv2prWll3a7NdEbmkM-y5Y85xYD6g1N-TPIpE_Y-_-WPf3JUavk744kG8YyHhGvAmk2IL0N2xePfC6CHesdJhwvmJJXzr_53dbPwit1y5KljS0iTZz3mGTML2bq4hGaEHbQxeY2fBpZOSm8sPMz-zB9IVJQKzH5-DXlPKz01mJ9XiBJlubfHsN72RdqFD-O2Tw");
        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())),
                Web3j.build(httpService),
                credentials,
                new StaticGasProvider(BigInteger.valueOf(4500000L), BigInteger.valueOf(200000L)));
    }
    private static EthUsdtContract wssContract(String privateKey, String contractAddress, Web3j web3j) {
        Credentials credentials = Credentials.create(privateKey);
//        httpService.addHeader("Authorization", "Bearer " + Base64.encode("tfc:tfc123".getBytes()));
//        httpService.addHeader("Authorization", "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJwdWJsaWMiLCJleHAiOjE2NTk5MzcxOTAsImp0aSI6IjRiMjNkYTVjLWRlZWEtNDYzNi04YjMwLWNmMmZmMjVkM2NlYyIsImlhdCI6MTY1OTkzMzU5MCwiaXNzIjoiQW5rciIsIm5iZiI6MTY1OTkzMzU5MCwic3ViIjoiZmNiNjY0YjItOGEwNC00N2E5LTg3ZjMtNTJhMjE2ODVlMzEzIn0.YfEwvDByU2MGHywsblZpEmKMIbjv4cWYkn5CaFglXY0TSANzd2pCSbIe40yU_R9_nV6xZeE8Uk74jJOdd_QvMpFyUgo-MMNWZP6uiEaYvK_K3tlpk5yzeZq9D4ruWaq8rFKggr-iaRGzu6coRSAOFv2prWll3a7NdEbmkM-y5Y85xYD6g1N-TPIpE_Y-_-WPf3JUavk744kG8YyHhGvAmk2IL0N2xePfC6CHesdJhwvmJJXzr_53dbPwit1y5KljS0iTZz3mGTML2bq4hGaEHbQxeY2fBpZOSm8sPMz-zB9IVJQKzH5-DXlPKz01mJ9XiBJlubfHsN72RdqFD-O2Tw");
        return EthUsdtContract.load(contractAddress,
                web3j,
                credentials,
                new StaticGasProvider(BigInteger.valueOf(4500000L), BigInteger.valueOf(200000L)));
    }
@@ -129,7 +207,7 @@
//            log.error("--->", error);
//        });
        System.out.println(ChainService.getInstance(ChainEnum.BSC_TFC.name()).totalSupply());
//        System.out.println(ChainService.getInstance(ChainEnum.BSC_TFC.name()).totalSupply());
    }
}