Nhảy tới nội dung
This page uses machine translation from English, which may contain errors or unclear language. For the most accurate information, please see the original English version. Some content may be in the original English due to frequent updates. Help us improve this page's translation by joining our effort on Crowdin. (Crowdin translation page, Contributing guide)

Triển khai hợp đồng thông minh

TxTypeSmartContractDeploy triển khai hợp đồng thông minh đến địa chỉ đã cho. Những thay đổi sau đây sẽ được thực hiện theo loại giao dịch này.

Loại giao dịch này có thể tạo tài khoản, chuyển mã thông báo, triển khai hợp đồng thông minh, thực hiện hợp đồng thông minh hoặc thực hiện kết hợp những hoạt động đã đề cập ở trên.

Nhập các gói @kaiachain/viem-ext để thêm các tính năng kaia trên web3

Thiết lập ứng dụng ví bằng createWalletClient, được cấu hình bằng chuỗi Kairos, phương thức truyền tải HTTPkhóa riêng của người gửi được chuyển đổi thành tài khoản.

Tạo yêu cầu giao dịch để triển khai hợp đồng thông minh bằng cách sử dụng prepareTransactionRequest. Chỉ định loại giao dịch (TxType.SmartContractDeploy)

Ký và gửi giao dịch cũ đến blockchain Kaia bằng phương thức sendTransaction của ứng dụng ví và ghi lại mã băm giao dịch.

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

Cải thiện trang này