跳至主要内容
本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)

智能合约部署

TxTypeSmartContractDeploy 向给定地址部署智能合约。 该交易类型将进行以下更改。

收费代表团

导入**@kaiachain/viem-ext**软件包,在 web3 上添加 kaia 功能

使用 createWalletClient,配置Kairos链HTTP传输和转换为账户的发送方私钥,建立发送方和付费方钱包。

使用prepareTransactionRequest创建价值转移交易请求,指定发送方账户、接收方地址、要转移的价值(本例中为 0 KLAY)、交易类型**(TxType.FeeDelegatedSmartContractDeploy)**、humanReadable: false指定合约代码为字节码(非人工可读源代码)、codeFormat:0 表示合约代码的格式(通常是 EVM 字节码),data 包含要部署的智能合约的编译字节码。

使用钱包客户端的 signTransaction 方法签署交易,并记录交易哈希值。

费用支付者使用函数 "signTransactionAsFeePayer "签署发件人已经签署的交易,同意支付交易费用。

使用 "kaia_sendRawTransaction "方法向 Kairos 区块链网络发送完全签名的授权交易(由发送方和支付方签名),并返回交易哈希值或网络响应。

TxTypeFeeDelegatedSmartContractDeploy.js

import { http, createWalletClient, kairos, TxType, privateKeyToAccount } from "@kaiachain/viem-ext";
const senderWallet = createWalletClient({
chain: kairos,
transport: http(),
account: privateKeyToAccount(
"0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8"
),
})
const feePayerWallet = createWalletClient({
chain: kairos,
transport: http(),
account: privateKeyToAccount(
"0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4"
),
});
// Example usage
(async () => {
const tx = await senderWallet.prepareTransactionRequest({
type: TxType.FeeDelegatedSmartContractDeploy,
account: senderWallet.account,
value: 0,
humanReadable: false,
codeFormat: 0,
data: '0x608060405234801561001057600080fd5b5060f78061001f6000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fb5c1cb1460415780638381f58a146053578063d09de08a14606d575b600080fd5b6051604c3660046083565b600055565b005b605b60005481565b60405190815260200160405180910390f35b6051600080549080607c83609b565b9190505550565b600060208284031215609457600080fd5b5035919050565b60006001820160ba57634e487b7160e01b600052601160045260246000fd5b506001019056fea2646970667358221220e0f4e7861cb6d7acf0f61d34896310975b57b5bc109681dbbfb2e548ef7546b364736f6c63430008120033',
});
const signedTx = await senderWallet.signTransaction(tx);
console.log('signedTx', signedTx)
const feePayerSignedTx = await feePayerWallet.signTransactionAsFeePayer(
signedTx
);
const sentFeePayerTx = await feePayerWallet.request({
method: "klay_sendRawTransaction",
params: [feePayerSignedTx],
});
console.log("fee payer contract deploy tx", sentFeePayerTx);
})();

output

❯ js TxTypeFeeDelegatedSmartContractDeploy.js
signedTx 0x29f9018882095985066720b3008304125a808094a2a8854b1802d8cd5de631e690817c253d6a9153b90116608060405234801561001057600080fd5b5060f78061001f6000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fb5c1cb1460415780638381f58a146053578063d09de08a14606d575b600080fd5b6051604c3660046083565b600055565b005b605b60005481565b60405190815260200160405180910390f35b6051600080549080607c83609b565b9190505550565b600060208284031215609457600080fd5b5035919050565b60006001820160ba57634e487b7160e01b600052601160045260246000fd5b506001019056fea2646970667358221220e0f4e7861cb6d7acf0f61d34896310975b57b5bc109681dbbfb2e548ef7546b364736f6c634300081200338080f847f8458207f6a0ef78ab459d0c14e7a7c9ddf4594d89a335479788af2d844d0d5ea0216be562fba042b0794cd2a5bedf23361ede5aa494fe4f5f79eb66c768678b27f992a6daadab
fee payer contract deploy tx 0x8c07cbd99de8a0ff2b84bfb9c1a7e6729e93d03b86be7db091d7981e352f779f

让这个页面变得更好