跳至主要內容
本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 Crowdin 上的努力,幫助我們改進本頁面的翻譯。 (Crowdin translation page, Contributing guide)

智能合約部署

TxTypeSmartContractDeploy 向給定地址部署智能合約。 該交易類型將進行以下更改。

導入 web3@kaiachain/web3js-ext 軟件包,在 web3 上添加 kaia 功能

定義發送方付費方地址和私人密鑰

使用指定的 kairos 測試網 URL 設置提供程序。 web3 中的提供者是訪問區塊鏈數據的只讀抽象。

此外,您還可以將提供商 URL 從 kairos 更改為 quicknode

使用 "web3.eth.accounts.privateKeyToAccount "創建帶有私鑰的發件人錢包

使用 "web3.eth.accounts.privateKeyToAccount "用私鑰創建付費者錢包

定義交易對象

設置 `type:TxType.FeeDelegatedSmartContractDeploy",用於與付費方部署新合約的交易

設置 data:"0x60806040... 是編譯 solidity 源代碼的字節碼

設置其他所需參數,如 發件人地址人工可讀代碼格式

使用 signTransaction發送方賬戶上籤署交易

使用 "signTransactionAsFeePayer "以付費方賬戶簽署交易

將交易發送到區塊鏈。 它將返回交易收據

FeeDelegatedSmartContractDeployExample.js

const { Web3, TxType } = require("@kaiachain/web3js-ext");
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const feePayerAddr = "0xcb0eb737dfda52756495a5e08a9b37aab3b271da";
const feePayerPriv = "0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4";
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io");
const web3 = new Web3(provider);
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv);
const feePayerAccount = web3.eth.accounts.privateKeyToAccount(feePayerPriv);
async function main() {
const tx = {
type: TxType.FeeDelegatedSmartContractDeploy,
from: senderAddr,
data: "0x608060405234801561001057600080fd5b5060f78061001f6000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fb5c1cb1460415780638381f58a146053578063d09de08a14606d575b600080fd5b6051604c3660046083565b600055565b005b605b60005481565b60405190815260200160405180910390f35b6051600080549080607c83609b565b9190505550565b600060208284031215609457600080fd5b5035919050565b60006001820160ba57634e487b7160e01b600052601160045260246000fd5b506001019056fea2646970667358221220e0f4e7861cb6d7acf0f61d34896310975b57b5bc109681dbbfb2e548ef7546b364736f6c63430008120033",
humanReadable: false, // must be false
codeFormat: 0, // must be 0
};
// Sign transaction by sender
const signResult1 = await senderAccount.signTransaction(tx);
console.log("senderTxHashRLP", signResult1.rawTransaction);
// Sign and send transaction by fee payer
const signResult2 = await feePayerAccount.signTransactionAsFeePayer(signResult1.rawTransaction);
console.log("signedTx", signResult2.transactionHash);
const receipt = await web3.eth.sendSignedTransaction(signResult2.rawTransaction);
console.log("receipt", receipt);
}
main();

output

❯ node FeeDelegatedSmartContractDeployExample.js
senderTxHashRLP 0x29f901888203b9850ba43b74008304f8a1808094a2a8854b1802d8cd5de631e690817c253d6a9153b90116608060405234801561001057600080fd5b5060f78061001f6000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fb5c1cb1460415780638381f58a146053578063d09de08a14606d575b600080fd5b6051604c3660046083565b600055565b005b605b60005481565b60405190815260200160405180910390f35b6051600080549080607c83609b565b9190505550565b600060208284031215609457600080fd5b5035919050565b60006001820160ba57634e487b7160e01b600052601160045260246000fd5b506001019056fea2646970667358221220e0f4e7861cb6d7acf0f61d34896310975b57b5bc109681dbbfb2e548ef7546b364736f6c634300081200338080f847f8458207f6a0a480e856acb96964dee71443f7c1cdae7681df5073c6a0a81b2651172bd227b3a0249072c7e4f0dfc54af5f1bfaae187dda4f9b8d5dde0a93172cd10a1f6617ca4
signedTx 0x57cf3bfddf3e72effc39e80b9cb6995273822e51d50f55da93a14574b5dae9d1
receipt {
blockHash: '0x84f6aefd1ee6425e03cd8571fbfb653cf5971c729e7b3864456bb36bfd212622',
blockNumber: 148744885n,
contractAddress: '0x661c557c1b7c2b1fbd6c4793d210cd1e421064e9',
cumulativeGasUsed: 333958n,
effectiveGasPrice: 25000000000n,
from: '0xa2a8854b1802d8cd5de631e690817c253d6a9153',
gasUsed: 140317n,
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: 1n,
transactionHash: '0x57cf3bfddf3e72effc39e80b9cb6995273822e51d50f55da93a14574b5dae9d1',
transactionIndex: 1n,
type: 0n
}

讓這個頁面變得更好