From 6d331bb3e8abacbf3962c0aada0dead479bf4372 Mon Sep 17 00:00:00 2001
From: KKSU <15274802129@163.com>
Date: Sun, 28 Apr 2024 21:36:35 +0800
Subject: [PATCH] 测试环境新版

---
 src/main/java/cc/mrbird/febs/dapp/chain/EthService.java |   86 ++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 76 insertions(+), 10 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/dapp/chain/EthService.java b/src/main/java/cc/mrbird/febs/dapp/chain/EthService.java
index 1b043f1..460504e 100644
--- a/src/main/java/cc/mrbird/febs/dapp/chain/EthService.java
+++ b/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;
     }
 
@@ -332,4 +370,32 @@
         }
     }
 
+    @Override
+    public BigInteger totalSupply() {
+        try {
+            String methodName = "totalSupply";
+            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;
+            BigInteger totalSupply = BigInteger.ZERO;
+            try {
+                ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();
+                List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
+                totalSupply = (BigInteger) results.get(0).getValue();
+                return totalSupply.divide(BigInteger.TEN.pow(decimals()));
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return BigInteger.ZERO;
+    }
 }

--
Gitblit v1.9.1