본문으로 건너뛰기

Smart Contract Deploy

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

Import the ethers and @kaiachain/ethers-ext modules to add kaia features on ethers.js.

Define sender address and sender private key

Set up the provider with the specified kaia Baobab testnet URL. A provider in ethers is a read-only abstraction to access the blockchain data.

Also, you can change the provider URL from baobab to allthatnode

Create a sender's wallet using the sender's private key and the provider

Declare a transaction object

Specify the transaction type as SmartContractDeploy

Set the compiled bytecode from the solidity code you want to deploy to the blockchain network

Send the tx to the blockchain. Function sendTransaction internally signs with the private key of the account and then transmits it to the blockchain network.

The wait function returns the tx receipt if it is completed in the blockchain.

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
}