본문으로 건너뛰기
This page uses machine translation from English, which may contain errors or unclear language. For the most accurate information, please see the original English version. Some content may be in the original English due to frequent updates. Help us improve this page's translation by joining our effort on Crowdin. (Crowdin translation page, Contributing guide)

Deploy

Web3 SDKs support to interact with Smart Contract via contract instance, not low-level transaction.

Import the web3 and @kaiachain/web3js-ext packages to add kaia features on web3

Define sender address and private key

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

Also, you can change the provider URL from kairos to quicknode

Convert the sender's private key to an account using web3.eth.accounts.privateKeyToAccount

Define compiled contract bytecode to deploy

Define contract Abi, you can get it from compiled solidity code

Create a contract instance using Web3.eth.Contract and defined contractAbi

Encode the data needed to deploy the contract

Create a transaction object with data is the encoded contract bytecode, from: senderAddress is sender's wallet address

Sign the transaction with the sender's account using signTransaction

Send the signed transaction to network and wait for the receipt

smartContractDeploy.js

const { Web3 } = require("@kaiachain/web3js-ext");
const senderAddr = "0x24e8efd18d65bcb6b3ba15a4698c0b0d69d13ff7";
const senderPriv = "0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49";
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io");
const web3 = new Web3(provider);
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv);
/* compiled in remix.ethereum.org (compiler: 0.8.18, optimizer: false)
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Counter {
uint256 public number;
event SetNumber(uint256 number);
constructor(uint256 initNumber) {
number = initNumber;
}
function setNumber(uint256 newNumber) public {
number = newNumber;
emit SetNumber(number);
}
function increment() public {
number++;
emit SetNumber(number);
}
}
*/
const bytecode = "0x608060405234801561001057600080fd5b5060405161031a38038061031a8339818101604052810190610032919061007a565b80600081905550506100a7565b600080fd5b6000819050919050565b61005781610044565b811461006257600080fd5b50565b6000815190506100748161004e565b92915050565b6000602082840312156100905761008f61003f565b5b600061009e84828501610065565b91505092915050565b610264806100b66000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80633fb5c1cb146100465780638381f58a14610062578063d09de08a14610080575b600080fd5b610060600480360381019061005b9190610160565b61008a565b005b61006a6100cd565b604051610077919061019c565b60405180910390f35b6100886100d3565b005b806000819055507f331bb01bcf77ec721a35a558a7984e8e6ca33b507d3ee1dd13b76f64381e54d46000546040516100c2919061019c565b60405180910390a150565b60005481565b6000808154809291906100e5906101e6565b91905055507f331bb01bcf77ec721a35a558a7984e8e6ca33b507d3ee1dd13b76f64381e54d460005460405161011b919061019c565b60405180910390a1565b600080fd5b6000819050919050565b61013d8161012a565b811461014857600080fd5b50565b60008135905061015a81610134565b92915050565b60006020828403121561017657610175610125565b5b60006101848482850161014b565b91505092915050565b6101968161012a565b82525050565b60006020820190506101b1600083018461018d565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006101f18261012a565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610223576102226101b7565b5b60018201905091905056fea264697066735822122012162749eb9714a6df7a34741c39edb78cf6e3d6d3e888872232594da5a1353164736f6c63430008120033";
const contractAbi = JSON.parse('[{"inputs":[{"internalType":"uint256","name":"initNumber","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"number","type":"uint256"}],"name":"SetNumber","type":"event"},{"inputs":[],"name":"increment","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"number","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"newNumber","type":"uint256"}],"name":"setNumber","outputs":[],"stateMutability":"nonpayable","type":"function"}]');
async function main() {
const contract = new web3.eth.Contract(contractAbi);
const data = contract.deploy({
data: bytecode,
arguments: [100],
}).encodeABI();
const tx = {
from: senderAddr,
data: data,
};
const signResult = await senderAccount.signTransaction(tx);
console.log("signedTx", signResult.transactionHash);
const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
console.log("receipt", receipt);
}
main();

output

❯ java smartContractDeploy.java
signedTx 0x476a6257353fc48dfd1bad5090026f065345c5ef0dbce85434c372b6d5817e75
receipt {
blockHash: '0x476295f949db32245fa433878bef56b5e2282aee46c8c2ec72a90bca64a9864a',
blockNumber: 148743465n,
contractAddress: '0xa0ca0d72ce4be89ccdaf4f3ed18313392523f903',
cumulativeGasUsed: 673526n,
effectiveGasPrice: 25000000000n,
from: '0x24e8efd18d65bcb6b3ba15a4698c0b0d69d13ff7',
gasUsed: 280644n,
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: 1n,
transactionHash: '0x476a6257353fc48dfd1bad5090026f065345c5ef0dbce85434c372b6d5817e75',
transactionIndex: 2n,
type: 2n
}

페이지를 개선해 주세요