KKSU
2024-04-16 1d5f689f34d827ac4be67fd84212a5cb7fc80d57
src/main/java/cc/mrbird/febs/dapp/chain/EthService.java
@@ -153,6 +153,40 @@
    }
    @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();
        }
        int decimal = decimals();
        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<>();