| | |
| | | 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 lombok.extern.slf4j.Slf4j; |
| | | import okhttp3.Interceptor; |
| | | import okhttp3.OkHttpClient; |
| | | import okhttp3.Request; |
| | | 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; |
| | | 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.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; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * @author |
| | | * @date 2022-03-23 |
| | | **/ |
| | | @Slf4j |
| | | public class ChainService { |
| | | private final static Map<String, ContractChainService> contractMap = new HashMap<>(); |
| | | |
| | | private final static String TRX_ADDRESS = "TUFzqZRpLwLWJU4jcdf77RKS3Ts2uEhmWL"; |
| | | private final static String TRX_PRIVATE = "e08dce7a4626f97b790e791bcdec31cffab46233744bb1aa133f69f98623d3fb"; |
| | | private final static String TRX_CONTRACT_ADDRESS = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"; |
| | | private final static String API_KEY = "9d461be6-9796-47b9-85d8-b150cbabbb54"; |
| | | |
| | | private final static String ETH_URL = "https://mainnet.infura.io/v3/f54a5887a3894ebb9425920701a97fe0"; |
| | | private final static String ETH_ADDRESS = "0x6c5640c572504a75121e57760909a9dd0E672f2D"; |
| | | private final static String ETH_PRIVATE = "77f650768ff50a4243c008fbae1be9ffe74c52908ee9081e2e15f3d3411690bb"; |
| | | private final static String ETH_CONTRACT_ADDRESS = "0xdac17f958d2ee523a2206206994597c13d831ec7"; |
| | | |
| | | private final static String BSC_URL = "https://bsc-dataseed1.ninicoin.io"; |
| | | private final static String BSC_ADDRESS = "0x971c09aA9735EB98459B17EC8b48932D24CbB931"; |
| | | private final static String BSC_PRIVATE = "0x5f38d0e63157f535fc21f89ea13ec3cd245691c20795c1d2cb60233b3ba7bb47"; |
| | | private final static String BSC_CONTRACT_ADDRESS = "0x55d398326f99059fF775485246999027B3197955"; |
| | | |
| | | private final static ContractChainService ETH = new EthService(ETH_URL, ETH_ADDRESS, ETH_PRIVATE, ETH_CONTRACT_ADDRESS); |
| | | private final static ContractChainService BSC = new EthService(BSC_URL, BSC_ADDRESS, BSC_PRIVATE, BSC_CONTRACT_ADDRESS); |
| | | private final static ContractChainService TRX = new TrxService(TRX_ADDRESS, TRX_PRIVATE, TRX_CONTRACT_ADDRESS, API_KEY); |
| | | |
| | | private final String ETH_PREFIX = "0x"; |
| | | 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) { |
| | | switch (chainType) { |
| | | case "ETH" : |
| | | return ETH; |
| | | case "BSC" : |
| | | return BSC; |
| | | case "TRX" : |
| | | return TRX; |
| | | default: |
| | | break; |
| | | ContractChainService contract = contractMap.get(chainType); |
| | | if (contract == null) { |
| | | throw new FebsException("参数错误"); |
| | | } |
| | | |
| | | 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 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); |
| | | 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(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))); |
| | | } |
| | | |
| | | 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); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | // 0x391040eE5F241711E763D0AC55E775B9b4bD0024 0x977A9dDFb965a9A3416Fa72cA7F91c4949c18f25 |
| | | System.out.println(getInstance("BSC").isAllowance("0x977a9ddfb965a9a3416fa72ca7f91c4949c18f25")); |
| | | // 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); |
| | | // }); |
| | | |
| | | // System.out.println(getInstance("BSC").decimals());; |
| | | |
| | | // System.out.println(getInstance("ETH").allowance("0x391040eE5F241711E763D0AC55E775B9b4bD0024")); |
| | | System.out.println(ChainService.getInstance(ChainEnum.BSC_TFC.name()).totalSupply()); |
| | | } |
| | | // |
| | | // /** |
| | | // * 获取制定账号的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.balanceOf(address); |
| | | // } |
| | | // return balance; |
| | | // } |
| | | // |
| | | // /** |
| | | // * 判断地址是否授权给制定账户 |
| | | // * |
| | | // * @param address |
| | | // * @return |
| | | // */ |
| | | // public boolean isAllowance(String address) { |
| | | // BigInteger result; |
| | | // if (address.startsWith(ETH_PREFIX)) { |
| | | // 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.startsWith(ETH_PREFIX)) { |
| | | // String resp = HttpUtil.get("https://etherscan.io/autoUpdateGasTracker.ashx?sid=75f30b765180f29e2b7584b8501c9124"); |
| | | // JSONObject data = JSONObject.parseObject(resp); |
| | | // hash = ETH.approveTransfer(address, amount, data.getString("avgPrice")); |
| | | // } else { |
| | | // hash = TRX.transfer(address, amount); |
| | | // } |
| | | // return hash; |
| | | // } |
| | | // |
| | | // public static void main(String[] args) { |
| | | //// System.out.println(ChainService.INSTANCE.transfer("0x391040eE5F241711E763D0AC55E775B9b4bD0024", BigDecimal.valueOf(5))); |
| | | // |
| | | //// System.out.println(new EthService().ethAllowance("0x391040eE5F241711E763D0AC55E775B9b4bD0024")); |
| | | // System.out.println(ChainService.INSTANCE.balanceOf("0x391040eE5F241711E763D0AC55E775B9b4bD0024")); |
| | | // } |
| | | |
| | | } |