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

智慧契約部署

TxTypeSmartContractDeploy 將智慧型契約部署到指定的位址。 此交易類型將作出以下變更。

此類型的交易可以建立帳戶、轉移代幣、部署智慧型契約、執行智慧型契約,或執行前述的混合交易。

匯入 @kaiachain/viem-ext 套件以在 web3 上新增 kaia 功能

使用 createWalletClient 設定一個錢包用戶端,並配置 Kairos 鏈HTTP 傳輸,以及轉換為帳戶的寄件者私密金鑰

使用 prepareTransactionRequest 建立部署智慧型契約的交易請求。 指定交易類型 (TxType.SmartContractDeploy)

使用錢包用戶端 sendTransaction 方法簽署並傳送傳統交易到 Kaia 區塊鏈,並記錄交易哈希值。

SmartContractDeploy.js

import {
http,
createWalletClient, kairos,
TxType,
privateKeyToAccount
} from "@kaiachain/viem-ext";
const senderWallet = createWalletClient({
chain: kairos,
transport: http(),
account: privateKeyToAccount(
"0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8"
),
});
// Example usage
(async () => {
const tx = await senderWallet.prepareTransactionRequest({
type: TxType.SmartContractDeploy,
account: senderWallet.account,
value: 0,
humanReadable: false,
codeFormat: 0,
data: '0x608060405234801561001057600080fd5b5060f78061001f6000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fb5c1cb1460415780638381f58a146053578063d09de08a14606d575b600080fd5b6051604c3660046083565b600055565b005b605b60005481565b60405190815260200160405180910390f35b6051600080549080607c83609b565b9190505550565b600060208284031215609457600080fd5b5035919050565b60006001820160ba57634e487b7160e01b600052601160045260246000fd5b506001019056fea2646970667358221220e0f4e7861cb6d7acf0f61d34896310975b57b5bc109681dbbfb2e548ef7546b364736f6c63430008120033',
});
console.log('tx', tx);
const sentTx = await senderWallet.sendTransaction(tx);
console.log("contract deploy tx", sentTx);
})();

output

❯ node SmartContractDeploy.js
tx {
type: 40,
account: {
address: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
nonceManager: undefined,
sign: [AsyncFunction: sign],
signAuthorization: [AsyncFunction: signAuthorization],
signMessage: [AsyncFunction: signMessage],
signTransaction: [AsyncFunction: signTransaction],
signTypedData: [AsyncFunction: signTypedData],
source: 'privateKey',
type: 'local',
publicKey: '0x04dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cdaf06ca34ae8714cf3dae06bacdb78c7c2d4054bd38961d40853cd5f15955da79'
},
value: 0,
humanReadable: false,
codeFormat: 0,
data: '0x608060405234801561001057600080fd5b5060f78061001f6000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fb5c1cb1460415780638381f58a146053578063d09de08a14606d575b600080fd5b6051604c3660046083565b600055565b005b605b60005481565b60405190815260200160405180910390f35b6051600080549080607c83609b565b9190505550565b600060208284031215609457600080fd5b5035919050565b60006001820160ba57634e487b7160e01b600052601160045260246000fd5b506001019056fea2646970667358221220e0f4e7861cb6d7acf0f61d34896310975b57b5bc109681dbbfb2e548ef7546b364736f6c63430008120033',
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
nonce: 2382,
chainId: 1001,
gas: 106737n,
gasPrice: '0x66720b300',
gasLimit: 266842
}
contract deploy tx 0xbae1cacead055c19fa326af76598f99f779ed30a4c33513a6688a490a0bde12c

讓這個頁面變得更好