跳至主要内容
本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)

使用 TxType 写入

从 Web3j 和 kaia 库(web3j-ext)中导入必要的类。

使用 Web3j 连接到 kaia 区块链并指定 URL

使用私钥创建 KlayCredentials

定义与智能合约交互的**地址

为合约交易定义gas价格gas限额

定义from并获取chainIdnonce

创建一个gas 供应商,具有静态gas价格和gas限额

使用联系地址Web3j 实例凭据gas 提供商加载合约。 您可以通过该实例读写合约

在写入之前,获取当前存储在合约中的数字

为调用方法 setNumber 设置合约参数

将事务类型设为 SMART_CONTRACT_EXECUTION

创建原始事务对象

使用 KlayTransactionEncoder.signMessage 签署交易

签署的交易发送至 kaia 网络

等待交易完成

写入后,获取存储在合约中的更新编号

关闭 web3j 实例

WriteContractWithKlaytnTxTypeExample.java

package org.web3j.example.contracts;
import java.io.IOException;
import java.math.BigInteger;
import org.web3j.crypto.KlayCredentials;
import org.web3j.crypto.KlayRawTransaction;
import org.web3j.crypto.KlayTransactionEncoder;
import org.web3j.crypto.transaction.type.TxType;
import org.web3j.crypto.transaction.type.TxTypeFeeDelegatedSmartContractExecution;
import org.web3j.crypto.transaction.type.TxType.Type;
import org.web3j.tx.response.PollingTransactionReceiptProcessor;
import org.web3j.tx.response.TransactionReceiptProcessor;
import org.web3j.example.keySample;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthChainId;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.kaia.Web3j;
import org.web3j.utils.Numeric;
import org.web3j.protocol.kaia.core.method.response.KlayCallResponse;
import org.web3j.protocol.kaia.core.method.response.TransactionReceipt;
import org.web3j.tx.gas.StaticGasProvider;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Uint;
import java.util.Arrays;
import java.util.Collections;
public class WriteContractWithKlaytnTxTypeExample {
/**
* @throws Exception
*
*/
public static void run() throws Exception {
Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL));
KlayCredentials credentials = KlayCredentials.create(keySample.LEGACY_KEY_privkey);
String contractAddr = "0x95Be48607498109030592C08aDC9577c7C2dD505";
BigInteger GAS_PRICE = BigInteger.valueOf(50000000000L);
BigInteger GAS_LIMIT = BigInteger.valueOf(6721950);
String from = credentials.getAddress();
BigInteger nonce = web3j.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).send()
.getTransactionCount();
EthChainId EthchainId = web3j.ethChainId().send();
long chainId = EthchainId.getChainId().longValue();
BigInteger value = BigInteger.ZERO;
StaticGasProvider gasProvider = new StaticGasProvider(GAS_PRICE, GAS_LIMIT);
Counter counter = Counter.load(contractAddr, web3j, credentials.convertToCredentials(), gasProvider);
// Get number before Contract Write
System.out.println("number before : " + counter.number().send());
// Contract Write (Set number with setNumber function)
Function function = new Function("setNumber", // Function name
Arrays.asList(new Uint(BigInteger.valueOf(50))), // Function input parameters
Collections.emptyList()); // Function returned parameters
String txData = FunctionEncoder.encode(function);
byte[] payload = Numeric.hexStringToByteArray(txData);
TxType.Type type = Type.SMART_CONTRACT_EXECUTION;
KlayRawTransaction raw = KlayRawTransaction.createTransaction(
type,
nonce,
GAS_PRICE,
GAS_LIMIT,
contractAddr,
value,
from,
payload);
// Sign as sender
byte[] signedMessage = KlayTransactionEncoder.signMessage(raw, chainId, credentials);
String hexValue = Numeric.toHexString(signedMessage);
web3j.ethSendRawTransaction(hexValue).send();
try {
Thread.sleep(2000);
} catch (Exception e) {
System.out.println(e);
}
// Get number after Contract Write
System.out.println("number after : " + counter.number().send());
web3j.shutdown();
}
}

output

❯ java WriteContractWithKlaytnTxTypeExample.java
number before : 298
number after : 50

让这个页面变得更好