在用web3j调用合约的时候会出现各种莫名其妙的bug,主要的原因有以下几点:
1.区块没同步完成:区块在同步完成之前即在服务器调用eth.syncing返回false之前操作账户一般都会有问题
2.gas price太低或者gas limit 异常都是因为调用的方法不对
3.连接超时是因为得设置httpclient的超时时间
4.调用之前需要将合约代码编译成.java格式的文件,具体编译方法参照web3j.io
废话不多说直接贴代码:
package com.springboot.controller;
import okhttp3.OkHttpClient;
import org.apache.commons.io.FileUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protoco