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

Smart Contract Deploy

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

导入 ethers@kaiachain/ethers-ext 模块,在 ethers.js 上添加 kaia 功能。

定义发送方付费方的地址和私钥

使用指定的 kairos 测试网 URL 设置提供程序。 以太坊中的提供者是访问区块链数据的只读抽象。

此外,您还可以将提供商 URL 从 kairos 更改为 quicknode

使用寄件人和付款人的私人密钥为他们创建钱包

为费用委托智能合约部署定义交易对象

指定交易类型为 收费委托智能合约部署

指定合同字节码,可从程序块资源管理器中获取

在交易中填入其他详细信息(如气体限值)

发送方的私人密钥签署交易

使用付费者的钱包将交易发送到区块链上。 函数 "sendTransactionAsFeePayer "会在发送者的签名上添加一个带有 FeePayer 私钥的签名,并将其传输到区块链网络。

如果 tx 已成功发送到区块链,wait 函数将返回 tx 收据。

TxTypeFeeDelegatedSmartContractDeploy.js

const ethers = require("ethers");
const { Wallet, TxType } = require("@kaiachain/ethers-ext");
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const feePayerAddr = "0xcb0eb737dfda52756495a5e08a9b37aab3b271da";
const feePayerPriv = "0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4";
const provider = new ethers.providers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const senderWallet = new Wallet(senderPriv, provider);
const feePayerWallet = new Wallet(feePayerPriv, provider);
async function main() {
const tx = {
type: TxType.FeeDelegatedSmartContractDeploy,
from: senderAddr,
value: 0,
gasLimit: 1_000_000,
input: "0x608060405234801561001057600080fd5b5060f78061001f6000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fb5c1cb1460415780638381f58a146053578063d09de08a14606d575b600080fd5b6051604c3660046083565b600055565b005b605b60005481565b60405190815260200160405180910390f35b6051600080549080607c83609b565b9190505550565b600060208284031215609457600080fd5b5035919050565b60006001820160ba57634e487b7160e01b600052601160045260246000fd5b506001019056fea2646970667358221220e0f4e7861cb6d7acf0f61d34896310975b57b5bc109681dbbfb2e548ef7546b364736f6c63430008120033",
humanReadable: false, // must be false
codeFormat: 0, // must be 0
};
// Sign transaction by sender
const populatedTx = await senderWallet.populateTransaction(tx);
const senderTxHashRLP = await senderWallet.signTransaction(populatedTx);
console.log("senderTxHashRLP", senderTxHashRLP);
// Sign and send transaction by fee payer
const sentTx = await feePayerWallet.sendTransactionAsFeePayer(senderTxHashRLP);
console.log("sentTx", sentTx.hash);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
}
main();

output

❯ node TxTypeFeeDelegatedSmartContractDeploy.js
senderTxHashRLP 0x29f901888203ae850ba43b7400830f4240808094a2a8854b1802d8cd5de631e690817c253d6a9153b90116608060405234801561001057600080fd5b5060f78061001f6000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fb5c1cb1460415780638381f58a146053578063d09de08a14606d575b600080fd5b6051604c3660046083565b600055565b005b605b60005481565b60405190815260200160405180910390f35b6051600080549080607c83609b565b9190505550565b600060208284031215609457600080fd5b5035919050565b60006001820160ba57634e487b7160e01b600052601160045260246000fd5b506001019056fea2646970667358221220e0f4e7861cb6d7acf0f61d34896310975b57b5bc109681dbbfb2e548ef7546b364736f6c634300081200338080f847f8458207f6a0ba7429346994b62a646f74f05f5cf1bc8ed28040edf5b6bcd753efab516e5c22a07730efae99c9d0c0f3d7d845452f55a22ed368ade14eb6af7dfdbfdc748ae1ff
sentTx 0x58431572e2dc795b9f33f42278c9611da7f95db4548b6b252e6782b272f7a1d0
receipt {
to: null,
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
contractAddress: '0x7915deAD26C71540fC48384f35bd0744349BABf7',
transactionIndex: 0,
gasUsed: BigNumber { _hex: '0x02241d', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0x43dcff9990b1236a508d514806cdf7c0257a18329a7ff5b81f4fabb28dd174af',
transactionHash: '0x58431572e2dc795b9f33f42278c9611da7f95db4548b6b252e6782b272f7a1d0',
logs: [],
blockNumber: 148732426,
confirmations: 1,
cumulativeGasUsed: BigNumber { _hex: '0x02241d', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}

让这个页面变得更好