Smart contracts
How to deploy smart contracts
Kiichain supports natively supports both CosmWasm and EVM smart contracts. Developers can choose between any of the two to create amazing dApps.
Working with Cosmwasm
Prerequisites
Install Rust and CosmWasm toolchain:
rustup default stable
rustup target add wasm32-unknown-unknown
cargo install cargo-generate --features vendored-openssl
cargo install cargo-run-script
Create a new CosmWasm contract:
cargo generate --git https://github.com/CosmWasm/cosmwasm-template.git --name my-contract
cd my-contract
Compile the contract:
cargo wasm
cargo schema
Upload and Instantiate the Contract
Store the Contract
kiichaind wasmd tx wasm store artifacts/my_contract.wasm --from my-key --gas auto --gas-adjustment 1.2 --node <KIICHAIN_NODE> --chain-id <KIICHAIN_CHAIN_ID>
Instantiate the Contract
kiichaind wasmd tx wasm instantiate <CONTRACT_CODE_ID> '{}' --from my-key --label "My Contract" --gas auto --gas-adjustment 1.2 --node <KIICHAIN_NODE> --chain-id <KIICHAIN_CHAIN_ID>
Execute the Contract
kiichaind wasmd tx wasm execute <CONTRACT_ADDRESS> '{"your_method": {}}' --from my-key --gas auto --gas-adjustment 1.2 --node <KIICHAIN_NODE> --chain-id <KIICHAIN_CHAIN_ID>
Working with EVM
Prerequisites
Install Node.js and Hardhat:
npm install -g hardhat
mkdir my-evm-contract && cd my-evm-contract
npx hardhat
Install dependencies:
npm install --save-dev @nomicfoundation/hardhat-toolbox
Write and Compile the Smart Contract
Create a Solidity file (MyContract.sol
) in contracts/
:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract MyContract {
string public message;
constructor(string memory _message) {
message = _message;
}
function setMessage(string memory _newMessage) public {
message = _newMessage;
}
}
Deploy the Smart Contract
Create a deployment script (scripts/deploy.js
):
const hre = require("hardhat");
async function main() {
const MyContract = await hre.ethers.getContractFactory("MyContract");
const contract = await MyContract.deploy("Hello, Kiichain!");
await contract.deployed();
console.log("Contract deployed at:", contract.address);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Deploy to Kiichain:
npx hardhat run scripts/deploy.js --network kiichain
Interact with the Deployed Contract
Using Hardhat Console:
npx hardhat console --network kiichain
const contract = await ethers.getContractAt("MyContract", "<DEPLOYED_CONTRACT_ADDRESS>");
await contract.setMessage("New Message");
References
Last updated