| | |
| | | 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 wzy |
| | | * @author |
| | | * @date 2022-03-23 |
| | | **/ |
| | | @Slf4j |
| | | public class ChainService { |
| | | private final static Map<String, ContractChainService> contractMap = new HashMap<>(); |
| | | |
| | | private final String ETH_PREFIX = "0x"; |
| | | private final EthService ETH = new EthService(); |
| | | private final TrxService TRX = TrxService.INSTANCE; |
| | | 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() {} |
| | | private ChainService() { |
| | | } |
| | | |
| | | public final static ChainService INSTANCE = new ChainService(); |
| | | |
| | | /** |
| | | * 获取制定账号的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.balanceOfDecimal(address); |
| | | public static ContractChainService getInstance(String chainType) { |
| | | ContractChainService contract = contractMap.get(chainType); |
| | | if (contract == null) { |
| | | throw new FebsException("参数错误"); |
| | | } |
| | | return balance; |
| | | |
| | | return contract; |
| | | } |
| | | |
| | | /** |
| | | * 判断地址是否授权给制定账户 |
| | | * |
| | | * @param address |
| | | * @return |
| | | * 监听合约事件 |
| | | * @param startBlock 开始区块 |
| | | */ |
| | | public boolean isAllowance(String address) { |
| | | BigInteger result; |
| | | if (address.startsWith(ETH_PREFIX)) { |
| | | result = ETH.ethAllowance(address); |
| | | 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); |
| | | }); |
| | | } |
| | | |
| | | |
| | | 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 { |
| | | result = TRX.allowance(address); |
| | | startParameterName = DefaultBlockParameterName.EARLIEST; |
| | | } |
| | | |
| | | 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")); |
| | | if (endBlock != null) { |
| | | endParameterName = new DefaultBlockParameterNumber(endBlock); |
| | | } else { |
| | | hash = TRX.transfer(address, amount); |
| | | endParameterName = DefaultBlockParameterName.LATEST; |
| | | } |
| | | return hash; |
| | | |
| | | return new EthFilter(startParameterName, endParameterName, contractAddress); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | // System.out.println(ChainService.INSTANCE.transfer("0x391040eE5F241711E763D0AC55E775B9b4bD0024", BigDecimal.valueOf(5))); |
| | | ChainEnum chain = ChainEnum.getValueByName(ChainEnum.BSC_TFC.name()); |
| | | assert chain != null; |
| | | |
| | | // System.out.println(new EthService().ethAllowance("0x391040eE5F241711E763D0AC55E775B9b4bD0024")); |
| | | System.out.println(ChainService.INSTANCE.balanceOf("0x391040eE5F241711E763D0AC55E775B9b4bD0024")); |
| | | 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); |
| | | }); |
| | | } |
| | | |
| | | } |