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

Smart Contract Deploy

TxTypeSmartContractDeploy deploys a smart contract to the given address. The following changes will be made by this transaction type.

This type of transaction can create an account, transfer tokens, deploy a smart contract, execute a smart contract, or perform a mix of aforementioned.

Import @kaiachain/viem-ext packages to add kaia features on web3

Set up a wallet client using createWalletClient, configured with the Kairos chain, an HTTP transport, and the sender’s private key converted to an account.

Create a transaction request for deploying a smart contract using prepareTransactionRequest. Specify the transaction type (TxType.SmartContractDeploy)

Sign and send the legacy transaction to the Kaia blockchain using the wallet client’s sendTransaction method, and log the transaction hash.

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

讓這個頁面變得更好