本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 Crowdin 上的努力,幫助我們改進本頁面的翻譯。 (Crowdin translation page, Contributing guide)
撰寫(費用委託)
當您要執行智能合約時,您可以向 feepayer 發送**"交易 ",其中包括支付請求**。
從 Web3j 和 kaia 庫(web3j-ext
)中導入必要的類。
使用 Web3j 連接到 kaia 區塊鏈並指定 URL
使用私鑰創建發送方和付費方證書。
指定已部署合約的**地址
確定交易的gas價格和gas限額
從 credentials.getAddress()
獲取發件人地址
獲取發件人地址的 nonce
從 Kaia 網絡獲取鏈 ID
為交易參數初始化變量
創建gas供應商,並設定靜態gas價格和gas限額
使用聯繫地址、Web3j 實例、憑據和gas提供商加載合約。 您可以通過該實例讀寫合約
在寫入之前,獲取合約中存儲的當前數字
為調用方法 setNumber
設置合約參數
將交易類型定義為 FEE_DELEGATED_SMART_CONTRACT_EXECUTION
創建原始交易
使用 "KlayTransactionEncoder.signMessage "以**發送方的身份簽署交易。
使用 "KlayTransactionEncoder.signMessageAsFeePayer "將交易簽署為付費方。
向 kaia 網絡發送已簽署的交易
短暫延遲以確保交易完成
寫入後,獲取存儲在合約中的更新號
關閉 Web3j 實例
WriteContractWithFeeDelegationExample.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 WriteContractWithFeeDelegationExample { /** * @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); KlayCredentials credentials_feePayer = KlayCredentials.create(keySample.LEGACY_KEY_FEEPAYER_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.FEE_DELEGATED_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); // Sign same message as Fee payer signedMessage = KlayTransactionEncoder.signMessageAsFeePayer(raw, chainId, credentials_feePayer); 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 WriteContractWithFeeDelegationExample.javanumber before : 298number after : 50