Skip to main content

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's and fee payer's addresses and private keys

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 wallets for the sender and fee payer using their private keys

Define the transaction object for a fee-delegated smart contract deployment

Specify the transaction type as FeeDelegatedSmartContractDeploy

Specify the contract bytecode, you can retreive it from block explorer

Populate the transaction with additional details (e.g., gas limit)

Sign the transaction with the sender's private key

Send the transaction to blockchain using fee payer's wallet. Function sendTransactionAsFeePayer adds a signature with FeePayer’s private key to the sender’s signature and transmits it to the blockchain network.

The wait function returns the tx receipt if the tx was sent to the blockchain successfully.

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
}