导语
接上篇文章 《使用hardhat部署solidity智能合约到测试网》
部署完合约之后,那么如何区块链进行交互呢?这节的主要内容就是使用web3.js和web3j来与区块链以及链上的合约进行交互。其中web3.js是属于javascript的版本的,
web3j是java版本的。
合约交互的类型
以太坊可以看做一个公共数据库,这个数据库提供了读、写功能,但是不能删除。
发起交易其实就是将交易写库,写库不能白写,其他节点帮你记录了数据,你要支付一些费用给帮你记录的节点,这个费用就是我们经常看到的gas。因此以太坊上所有对链上的数据进行更改的操作都是需要花费gas的,而读取是免费的。
我们可以简单将与合约交易的类型分成一下三种:
1. 只读调用
2. 支付gas的调用
3. 支付gas和value的调用
1,2两种我们上面也都谈到了,那么第三种是什么呢。第三种情况就是调用合约时将的ETH转到合约中,这个value就是这个转账的数值,合约是可以接受ETH的。
如果想要合约接收ETH需要,调用的方法加上 payable 关键字声明,除此之外合约还要有声明一个 receive()方法,表明此合约可以接收ETH.
在solidity 0.6.10中是receive,不同的版本方法的声明可能会有差异。
receive() payable external {}
言归正传,下面就开始进入正题
使用web3.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);
}
执行结果
使用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();
}
}
总结
- 只读合约的调用的是call方法,这种方法无需私钥签名,只需要知道合约的地址和方法(方法名,入参、出参)即可。
- 支付gas以及带value的调用,都是需要账户私钥的签名,分为三个步骤:encode(方法编码)、sign (对方法签名)、send(发送签名交易)。
- 不管是js和java,调用流程都是一致的,本质上都是和以太坊节点通过json-rpc调用
参考文档
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());
}
}