KKSU
2024-06-06 424ad35dcd1fafb51a0ba77523b11e6871f2858c
src/main/java/cc/mrbird/febs/dapp/chain/ChainService.java
@@ -6,6 +6,7 @@
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
@@ -13,6 +14,11 @@
import okhttp3.Response;
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;
@@ -21,13 +27,17 @@
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.protocol.websocket.WebSocketClient;
import org.web3j.protocol.websocket.WebSocketService;
import org.web3j.tx.gas.StaticGasProvider;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
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;
@@ -67,6 +77,7 @@
    /**
     * 监听合约事件
     *
     * @param startBlock 开始区块
     */
    public static void contractEventListener(BigInteger startBlock, ContractEventService event, String type) {
@@ -88,6 +99,33 @@
        });
    }
    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();
        }
    }
    private static EthUsdtContract contract(String privateKey, String contractAddress, String url) {
        Credentials credentials = Credentials.create(privateKey);
@@ -100,6 +138,16 @@
                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)));
    }
    private static EthFilter getFilter(BigInteger startBlock, String contractAddress) {
        return getFilter(startBlock, null, contractAddress);
    }