确保合约已经部署、以下操作基于合约已经部署的前提下
使用web3j与合约交互
这里使用的是maven项目,第一步要引入web3j的包
引入pom
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
代码
Java 版本的直接上代码,也就是上述三种方式的调用
import org.junit.Test;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.*;
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.EthGetTransactionCount;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.DefaultGasProvider;
import org.web3j.utils.Numeric;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class We3jTest2 {
public static String node = "https://rinkeby.infura.io/v3/7d00bf84530c4264969a4f0f231de8b6";
Web3j web3j;
Credentials credentials;
{
web3j = Web3j.build(new HttpService(node));
credentials = Credentials.create("私钥");
}
public static final String contractAddress = "0xb19c13d0A37cDDE5c1F969a0d9BD6a50B3A11B4E";
/**
* 调用合约的只读方法,无需gas
* @throws Exception
*/
@Test
public void getName() throws Exception {
Function function = new Function(
"getName",
Collections.emptyList(),
Arrays.asList(new TypeReference<Utf8String>(){}));
String encodedFunction = FunctionEncoder.encode(function);
org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
Transaction.createEthCallTransaction(null, contractAddress, encodedFunction),
DefaultBlockParameterName.LATEST)
.sendAsync().get();
List<Type> results = FunctionReturnDecoder.decode(response.getValue(), function.getOutputParameters());
Utf8String preValue = (Utf8String)results.get(0);
System.out.println(preValue.getValue());
}
/**
* 需要支付gas的方法
* @throws Exception
*/
@Test
public void setName() throws Exception {
Function function = new Function(
"setName",
Arrays.asList(new Utf8String("Tom")),
Collections.emptyList());
BigInteger nonce = getNonce(credentials.getAddress());
String encodedFunction = FunctionEncoder.encode(function);
BigInteger gasLimit = new BigInteger("300000");
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, DefaultGasProvider.GAS_PRICE,gasLimit, contractAddress, encodedFunction);
org.web3j.protocol.core.methods.response.EthSendTransaction response =
web3j.ethSendRawTransaction(Numeric.toHexString(TransactionEncoder.signMessage(rawTransaction, credentials)))
.sendAsync()
.get();
String transactionHash = response.getTransactionHash();
System.out.println(transactionHash);
}
/**
* 需要支付gas和value的合约方法调用
* @throws Exception
*/
@Test
public void payETH() throws Exception {
BigInteger nonce = getNonce(credentials.getAddress());
Function function = new Function("payETH",
Collections.EMPTY_LIST,
Collections.EMPTY_LIST);
String functionEncode = FunctionEncoder.encode(function);
BigInteger value = new BigInteger("200");
// 与不需要支付的value的方法调用,差别就在于多传一个eth数量的value参数
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, DefaultGasProvider.GAS_PRICE,DefaultGasProvider.GAS_LIMIT, contractAddress, value,functionEncode);
org.web3j.protocol.core.methods.response.EthSendTransaction response =
web3j.ethSendRawTransaction(Numeric.toHexString(TransactionEncoder.signMessage(rawTransaction, credentials)))
.sendAsync()
.get();
String transactionHash = response.getTransactionHash();
System.out.println(transactionHash);
}
private BigInteger getNonce(String address) throws Exception {
EthGetTransactionCount ethGetTransactionCount =
web3j.ethGetTransactionCount(address, DefaultBlockParameterName.LATEST)
.sendAsync()
.get();
return ethGetTransactionCount.getTransactionCount();
}
}
/**
* 根据交易地址hash查询交易数据input
* @throws Exception
*/
public static void callTransaction(Web3j web3j ) throws Exception {
EthTransaction transaction = web3j.ethGetTransactionByHash("0xfa0716964f83e68554ec0614228a58362939e225ef37bc2d96012b5dc0471a83").sendAsync().get();
Optional<org.web3j.protocol.core.methods.response.Transaction> optionalTransaction = transaction.getTransaction();
if(optionalTransaction.isPresent()){
org.web3j.protocol.core.methods.response.Transaction transactionInfo = optionalTransaction.get();
System.out.println("交易input信息---------------"+transactionInfo.getInput());
}
}
总结
- 只读合约的调用的是call方法,这种方法无需私钥签名,只需要知道合约的地址和方法(方法名,入参、出参)即可。
- 支付gas以及带value的调用,都是需要账户私钥的签名,分为三个步骤:encode(方法编码)、sign (对方法签名)、send(发送签名交易)。
- 不管是js和java,调用流程都是一致的,本质上都是和以太坊节点通过json-rpc调用
代码
根据交易hash查询交易内容及区块信息
package com.example.demo.contract;
import com.example.demo.util.Constants;
import org.bouncycastle.util.encoders.Hex;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.methods.response.EthBlock;
import org.web3j.protocol.core.methods.response.EthTransaction;
import org.web3j.protocol.http.HttpService;
import java.math.BigInteger;
import java.util.concurrent.ExecutionException;
public class HelloWorldDeploy {
public static void main(String[] args) throws Exception {
// 启动客户端
Web3j web3j = Web3j.build(new HttpService(Constants.URL));
Credentials credentials = WalletUtils.loadCredentials(Constants.PASSWORD, Constants.SOURCE);
System.out.println("getCredentialsAddress : " + credentials.getAddress());
pendingTransactionFlowable(web3j);
}
/**
* 根据交易hash查询交易信息
*
* @param web3j
* @throws ExecutionException
* @throws InterruptedException
*/
public static void pendingTransactionFlowable(Web3j web3j) throws ExecutionException, InterruptedException {
EthTransaction ethTx = web3j.ethGetTransactionByHash("0xbf56c46fcadf382d5d8fc5c617828220ba0441912cf6020759c51f7d3961ff60").sendAsync().get();
org.web3j.protocol.core.methods.response.Transaction tx = ethTx.getTransaction().get();
System.out.println("交易input:" + tx.getInput());
String input = tx.getInput();
String decodedInput = new String(Hex.decode(input.substring(2)));
// String decodedInput = new String(Hex.decode("546f6d"));
System.out.println("解码后的input值:" + decodedInput.substring(decodedInput.indexOf("\u0003") + 1).trim());
String blockHash = tx.getBlockHash();
BigInteger blockNumber = tx.getBlockNumber();
String from = tx.getFrom();
String to = tx.getTo();
BigInteger amount = tx.getValue();
// 交易信息
System.out.println("交易地址 hash :" + tx.getHash());
System.out.println("交易区块 :" + blockNumber);
System.out.println("转出 account:" + from);
System.out.println("转入(合约) account:" + to);
System.out.println("交易value:" + amount);
System.out.println("交易input:" + tx.getInput());
// 根据区块hash 查询区块信息
EthBlock ethBlock = web3j.ethGetBlockByHash(blockHash, true).sendAsync().get();
EthBlock.Block blockByHash = ethBlock.getBlock();
System.out.println("区块hash :" + blockHash);
// 根据区块number 查询区块信息
DefaultBlockParameter blockParameter = DefaultBlockParameter.valueOf(blockNumber);
ethBlock = web3j.ethGetBlockByNumber(blockParameter, true).sendAsync().get();
EthBlock.Block bk = ethBlock.getBlock();
System.out.println("区块 number: " + blockNumber);
System.out.println("区块 hash:" + bk.getHash());
System.out.println("区块 number:" + blockByHash.getNumber());
System.out.println("区块 parent hashes:" + blockByHash.getParentHash());
// 根据区块 查询交易信息
for (EthBlock.TransactionResult<?> txResult : blockByHash.getTransactions()) {
EthBlock.TransactionObject txObject = (EthBlock.TransactionObject) txResult;
// 交易信息
System.out.println("转出 account:" + txObject.getFrom());
System.out.println("转入(合约):" + txObject.getTo());
System.out.println("交易value:" + txObject.getValue());
System.out.println("交易input:" + txObject.getInput());
}
}
}
使用web3.js与合约交互
以下操作在node.js环境下运行
在项目中引入web3.js
npm install web3
项目骨架
var Web3 = require('web3');
var web3 = new Web3("https://rinkeby.infura.io/v3/7d00bf84530c4264969a4f0f231de8b6");
var privateKey = "私钥";
var contractAbi = [{"inputs":[],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getName","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"payETH","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"_name","type":"string"}],"name":"setName","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}];
var contractAddress = "0xb19c13d0A37cDDE5c1F969a0d9BD6a50B3A11B4E"
var hello = new web3.eth.Contract(contractAbi, contractAddress);
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
async function main(){
await getName();
}
async function getName(){
var name = await hello.methods.getName().call();
console.log(name);
}
只读调用
async function getName(){
var name = await hello.methods.getName().call();
console.log(name);
}
async function getBalance(){
var balance = await hello.methods.getBalance().call();
console.log("balance = "+balance);
}
支付gas的调用
async function setName(){
var name = "Jack";
var functionEncode = await hello.methods.setName(name).encodeABI();
var sign = await web3.eth.accounts.signTransaction({
gas: 300000,
to: contractAddress,
data: functionEncode,
}, privateKey);
var result = await web3.eth.sendSignedTransaction(sign.rawTransaction);
console.log("setName txHash = "+result.transactionHash);
}
支付gas和value的调用
async function reviceETH(){
var ethValue = 100;
var functionEncode = await hello.methods.reviceETH().encodeABI();
var sign = await web3.eth.accounts.signTransaction({
gas: 300000,
to: contractAddress,
data: functionEncode,
value: ethValue
}, privateKey);
var result = await web3.eth.sendSignedTransaction(sign.rawTransaction);
console.log("reviceETH resultTxHash = "+result.transactionHash);
}
参考文档
web3.js 中文文档
web3j 英文文档
web3h 中文文档
上述内容如有错误欢迎留言讨论。
补充:
web3j 调用返回多个地址参数的类型(未验证)
合约返回的值是一个地址数组的情况怎么写。
如下,是我的一个例子
@Test
public void callContractTransaction() throws Exception {
Function function = new Function(
"getComponents",
Collections.EMPTY_LIST,
Arrays.asList(new TypeReference<DynamicArray<Address>>(){}));
String encodedFunction = FunctionEncoder.encode(function);
org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
Transaction.createEthCallTransaction(null, contractAddress, encodedFunction),
DefaultBlockParameterName.LATEST)
.sendAsync().get();
Assert.isNull(response.getError(),"callContractTransaction error");
List<Type> results = FunctionReturnDecoder.decode(response.getValue(), function.getOutputParameters());
for (Type result : results) {
System.out.println(result.getValue());
}
}