java web3j 智能合约 读方法和写方法

pom.xml

		<dependency>
			<groupId>org.web3j</groupId>
			<artifactId>core</artifactId>
			<version>5.0.0</version>
		</dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.3.1</version>
        </dependency>

代码

package com.example.demo.controller;

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.crypto.Credentials;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.*;
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;
import java.util.Optional;

/**
 * @author 
 */
public class ContractUtil {

    private Web3j bscWeb3j = Web3j.build(new HttpService("https://data-seed-prebsc-1-s1.binance.org:8545"));
    
    private Credentials credentials = Credentials.create("matemask的私钥");

    /**
     * call
     * @return
     */
    public Object call() {
        String methodName = "claimUpgratedMaterial";
        List<Type> inputParameters = new ArrayList<>();
        inputParameters.add(new Uint256(1));
        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("0x500616C4a5CCeE062b6c9aD8855F48940FE754c0", "", data);

        EthCall ethCall;
        Object balanceValue = null;
        try {
            ethCall = bscWeb3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();
            List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
            if (results != null) {
                balanceValue = results.get(0).getValue();
            }
        } catch (IOException e) {

        }
        return balanceValue;
    }

    /**
     * send
     * @return
     */
    public String send() throws Exception {
        BigInteger nonce = getNonce("0x500616C4a5CCeE062b6c9aD8855F48940FE754c0");
        String methodName = "startProcess";
        List<Type> inputParameters = new ArrayList<>();
        inputParameters.add(new Uint256(1));
        inputParameters.add(new Uint256(1000));
        List<TypeReference<?>> outputParameters = new ArrayList<>();
        TypeReference<Uint256> typeReference = new TypeReference<Uint256>() {};
        outputParameters.add(typeReference);
        
        Function function = new Function(methodName, inputParameters, outputParameters);

        String functionEncode = FunctionEncoder.encode(function);

        BigInteger gasPrice = bscWeb3j.ethGasPrice().send().getGasPrice().multiply(BigInteger.TEN);
        BigInteger gasLimit = new BigInteger("500000");
        
        RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, "0x72A49A8Af3eF49B3058F4e3f320c411eE9979165",functionEncode);
        EthSendTransaction response = bscWeb3j.ethSendRawTransaction(Numeric.toHexString(TransactionEncoder.signMessage(rawTransaction, credentials)))
                .sendAsync()
                .get();
        if (response.hasError()) {
            System.out.println("合约deposit方法执行异常:" + response.getError().getMessage());
        } else {
        	System.out.println("deposit执行完成,nonce=[" + nonce + "],hash=[" + response.getTransactionHash() + "]");
            String hash = response.getTransactionHash();
            
            while (true) {
            	boolean isOk = transactionCheck(hash);
            	
                System.out.println("transactionCheck:" + isOk);
                
            	if (isOk) {
					break;
				}
				
            	try {
					Thread.sleep(800);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
        }
        
        return null;
    }


    /**
     * 使用交易hash查询交易状态
     * @param hash
     * @return
     * @throws IOException
     */
    public boolean transactionCheck(String hash) throws IOException {
        Optional<TransactionReceipt> receipt = bscWeb3j.ethGetTransactionReceipt(hash).send().getTransactionReceipt();
        if (receipt.isPresent()) {
            TransactionReceipt transactionReceipt = receipt.get();
            return transactionReceipt.isStatusOK();
        } else {
            return false;
        }
    }

    /**
     * 获取账户的Nonce
     * @param address
     * @return
     */
    public BigInteger getNonce(String address) {
        try {
            EthGetTransactionCount getNonce = bscWeb3j.ethGetTransactionCount(address,DefaultBlockParameterName.PENDING).send();
            if (getNonce == null){
                throw new RuntimeException("net error");
            }
            return getNonce.getTransactionCount();
        } catch (IOException e) {
            throw new RuntimeException("net error");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值