KKSU
2025-03-18 664184af3e070dee665ee736caffa0297804975f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cc.mrbird.febs.mall.chain.ercCoin;
 
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.core.methods.response.EthCall;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Numeric;
 
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
 
public class EthService implements ContractChainService {
 
    private Web3j web3j;
    private String url;
    private String ownerAddress;
    private String privateKey;
    private String contractAddress;
 
    public EthService(String url, String address, String privateKey, String contractAddress) {
        this.url = url;
        this.ownerAddress = address;
        this.privateKey = privateKey;
        this.contractAddress = contractAddress;
 
        HttpService service = new HttpService(url);
        web3j = Web3j.build(service);
    }
 
    @Override
    public int decimals() {
        try {
            String methodName = "decimals";
            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 decimals = BigInteger.ZERO;
            try {
                ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();
                List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
                decimals = (BigInteger) results.get(0).getValue();
                return decimals.intValue();
            } catch (IOException e) {
                e.printStackTrace();
            }
 
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
 
    @Override
    public BigInteger blockNumber() {
        Request<?, EthBlockNumber> request = web3j.ethBlockNumber();
        EthBlockNumber send = null;
        try {
            send = request.send();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        if (send != null) {
            return Numeric.decodeQuantity(send.getResult());
        } else {
            throw new NullPointerException();
        }
    }
 
}