KKSU
2024-05-16 c268540881fe493bc40e76ba82793a63c0897c91
src/main/java/cc/mrbird/febs/dapp/chain/EthService.java
@@ -153,6 +153,43 @@
    }
    @Override
    public BigDecimal getPrice(String address) {
        BigInteger decimals = BigInteger.ZERO;
        try {
            String methodName = "getPrice";
            List<Type> inputParameters = new ArrayList<>();
            List<TypeReference<?>> outputParameters = new ArrayList<>();
            TypeReference<Uint256> typeReference = new TypeReference<Uint256>() {
            };
            outputParameters.add(typeReference);
            Function function = new Function(methodName, inputParameters, outputParameters);
            String data = FunctionEncoder.encode(function);
            Transaction transaction = Transaction.createEthCallTransaction(null, contractAddress, data);
            EthCall ethCall;
            try {
                ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();
                List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
                decimals = (BigInteger) results.get(0).getValue();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /**
         * todo 修改了
         */
        int decimal = decimals();
//        int decimal = 8;
        double res = new BigDecimal(decimals).divide(BigDecimal.valueOf(Math.pow(10, decimal)), 8, RoundingMode.HALF_DOWN).doubleValue();
        if (res > 0) {
            return new BigDecimal(res);
        }
        return BigDecimal.ZERO;
    }
    @Override
    public BigInteger allowance(String address) {
        String methodName = "allowance";
        List<TypeReference<?>> outputParameters = new ArrayList<>();
@@ -184,16 +221,17 @@
    }
    public String getGas() {
        String gas;
        if (url.contains("infura.io")) {
            String resp = HttpUtil.get("https://etherscan.io/autoUpdateGasTracker.ashx?sid=75f30b765180f29e2b7584b8501c9124");
            JSONObject data = JSONObject.parseObject(resp);
            gas = data.getString("avgPrice");
        } else {
            String resp = HttpUtil.get("https://gbsc.blockscan.com/gasapi.ashx?apikey=key&method=gasoracle");
            JSONObject data = JSONObject.parseObject(resp);
            gas = data.getString("FastGasPrice");
        }
        String gas = "1";
//        String gas;
//        if (url.contains("infura.io")) {
//            String resp = HttpUtil.get("https://etherscan.io/autoUpdateGasTracker.ashx?sid=75f30b765180f29e2b7584b8501c9124");
//            JSONObject data = JSONObject.parseObject(resp);
//            gas = data.getString("avgPrice");
//        } else {
//            String resp = HttpUtil.get("https://gbsc.blockscan.com/gasapi.ashx?apikey=key&method=gasoracle");
//            JSONObject data = JSONObject.parseObject(resp);
//            gas = data.getString("FastGasPrice");
//        }
        return StrUtil.isBlank(gas) ? "35" : gas;
    }