Deploying Smart Contracts on CrossFi Chain’s EVM
Introduction
CrossFi Chain offers full EVM compatibility, making it an attractive platform for developers familiar with Ethereum. In this article, we walk through deploying smart contracts on CrossFi using the familiar Ethereum development environment. Whether you’re migrating from Ethereum or building a brand-new decentralized application, this guide covers everything from setting up your environment to testing and deploying your contract on CrossFi.
Setting Up Your Development Environment
Before writing your first contract, ensure you have the necessary tools installed:
Node.js & npm: For managing dependencies.
CrossFi RPC URL: Obtain the testnet or mainnet RPC endpoint from the CrossFi docs.
Install Hardhat by running:
npm install --save-dev hardhat
Then, initialize your project:
npx hardhat
Writing a Simple Smart Contract
Create a simple Solidity contract (e.g., contracts/HelloCrossFi.sol
) that demonstrates basic functionality:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloCrossFi {
string public greeting;
constructor(string memory _greeting) {
greeting = _greeting;
}
function setGreeting(string memory _newGreeting) public {
greeting = _newGreeting;
}
}
This contract stores a greeting message and allows anyone to update it.
Testing Your Contract
Before deployment, write tests to ensure your contract functions correctly. Using Hardhat’s testing environment, create a test file (test/HelloCrossFi.js
):
const { expect } = require("chai");
describe("HelloCrossFi", function () {
it("Should return the initial greeting", async function () {
const HelloCrossFi = await ethers.getContractFactory("HelloCrossFi");
const hello = await HelloCrossFi.deploy("Hello, CrossFi!");
await hello.deployed();
expect(await hello.greeting()).to.equal("Hello, CrossFi!");
});
it("Should update the greeting", async function () {
const HelloCrossFi = await ethers.getContractFactory("HelloCrossFi");
const hello = await HelloCrossFi.deploy("Hello, CrossFi!");
await hello.deployed();
await hello.setGreeting("Hi, CrossFi!");
expect(await hello.greeting()).to.equal("Hi, CrossFi!");
});
});
Run the tests with:
npx hardhat test
Deploying to CrossFi
Once testing is successful, deploy your contract to CrossFi. Update your Hardhat configuration to include the CrossFi network settings using its RPC URL and chain ID. For example, in hardhat.config.js
:
require("@nomiclabs/hardhat-waffle");
module.exports = {
solidity: "0.8.4",
networks: {
crossfi: {
url: "https://rpc.testnet.crossfi.org", // Replace with actual URL
accounts: [`0x${process.env.PRIVATE_KEY}`],
chainId: 1234, // Replace with CrossFi chain ID
},
},
};
Deploy your contract with:
npx hardhat run scripts/deploy.js --network crossfi
Verifying and Interacting with Your Contract
After deployment, verify the contract on CrossFi explorers or use available API endpoints to monitor transactions. You can interact with the contract through a web interface using libraries such as ethers.js or web3.js.
Conclusion
Deploying smart contracts on CrossFi’s EVM is straightforward for developers already familiar with Ethereum. With tools like Hardhat and robust testing practices, you can leverage CrossFi’s low fees and high throughput to build efficient decentralized applications. This guide serves as a practical introduction, paving the way for more complex projects on CrossFi.