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

Smart Contract Deploy

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

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

定义发件人地址和发件人私人密钥

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

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

使用发件人的私人密钥和提供商创建发件人钱包

声明一个事务对象

指定交易类型为 SmartContractDeploy

从要部署到区块链网络的 solidity 代码中设置编译字节码

向区块链发送 tx。 函数 "sendTransaction "使用账户的私钥进行内部签名,然后将其传输到区块链网络。

TxTypeSmartContractDeploy.js

const ethers = require("ethers");
const { Wallet, TxType } = require("@kaiachain/ethers-ext");
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const provider = new ethers.providers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const wallet = new Wallet(senderPriv, provider);
async function main() {
const tx = {
type: TxType.SmartContractDeploy,
from: senderAddr,
value: 0,
gasLimit: 1_000_000,
input: "0x608060405234801561001057600080fd5b5060f78061001f6000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fb5c1cb1460415780638381f58a146053578063d09de08a14606d575b600080fd5b6051604c3660046083565b600055565b005b605b60005481565b60405190815260200160405180910390f35b6051600080549080607c83609b565b9190505550565b600060208284031215609457600080fd5b5035919050565b60006001820160ba57634e487b7160e01b600052601160045260246000fd5b506001019056fea2646970667358221220e0f4e7861cb6d7acf0f61d34896310975b57b5bc109681dbbfb2e548ef7546b364736f6c63430008120033",
humanReadable: false, // must be false
codeFormat: 0, // must be 0
};
const sentTx = await wallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
}
main();

output

❯ node TxTypeSmartContractDeploy.js
sentTx 0xeff15464362194155acfb4e0eb0cedc470320d3d12fc504dbab9f918cf57452d
receipt {
to: null,
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
contractAddress: '0x028016AE0996097bB329a91f3D0C44C0Ee700f34',
transactionIndex: 1,
gasUsed: BigNumber { _hex: '0x01fd0d', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0x07a0f27ebddc4c4633c5ea70125e6e09ecc460b19f0e83ff8b271ad34aa868ec',
transactionHash: '0xeff15464362194155acfb4e0eb0cedc470320d3d12fc504dbab9f918cf57452d',
logs: [],
blockNumber: 148720946,
confirmations: 2,
cumulativeGasUsed: BigNumber { _hex: '0x04387b', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}

让这个页面变得更好