Deploying Smart Contracts
This guide covers two primary methods for deploying smart contracts to the XOS network: using the browser-based Remix IDE or the command-line Hardhat framework. Both approaches provide reliable deployment options depending on your development preferences.
Method 1: Deployment via Remix IDE
Remix offers a convenient browser-based environment ideal for quick development and testing.
Preparation Steps:
1. Configure Your Wallet
- Install MetaMask Extension: Download and install MetaMask in your browser if you haven't already.
- Add the XOS Network: Configure MetaMask with these network parameters:
- Network Name: XOS Network
- RPC URL: https://testnet-rpc.x.ink
- Chain ID: 1267
- Currency Symbol: XOS
- Block Explorer: https://xoscan.io
2. Access Remix IDE
- Navigate to Remix IDE in your browser.
3. Create or Import Your Contract
- Write a new contract or import an existing one. Here's a simple example:solidity
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract SimpleStorage { string private storedData; constructor(string memory initialValue) { storedData = initialValue; } function set(string memory newValue) public { storedData = newValue; } function get() public view returns (string memory) { return storedData; } }
4. Compile Your Contract
- Select the Solidity Compiler tab
- Choose the appropriate compiler version
- Click Compile to build your contract
5. Deploy Your Contract
- Navigate to the Deploy & Run Transactions tab
- Select Injected Provider - MetaMask from the environment dropdown
- Ensure MetaMask is connected to the XOS network
- Enter constructor arguments if required
- Click Deploy and confirm the transaction in MetaMask
6. Confirm Deployment
- Once deployed, you'll receive a contract address
- You can view your contract on XOS Explorer by searching for this address
Method 2: Deployment via Hardhat
Hardhat provides a professional development environment with advanced features for larger projects.
Setup Process:
1. Prepare Your Development Environment
- Install Node.js: Download and install Node.js if not already installed.
- Create a Hardhat Project:bashSelect "Create a JavaScript project" when prompted.
mkdir my-xos-project cd my-xos-project npm init -y npm install --save-dev hardhat npx hardhat init
2. Configure Network Settings
For secure credential management, use environment variables:
2.1 Install Required Packages
npm install dotenv @nomicfoundation/hardhat-toolbox
2.2 Create Environment File
Create a .env
file in your project root:
XOS_RPC_URL=https://testnet-rpc.x.ink
PRIVATE_KEY=your_wallet_private_key
2.3 Update Hardhat Configuration
Modify hardhat.config.js
:
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
module.exports = {
solidity: "0.8.18",
networks: {
xos: {
url: process.env.XOS_RPC_URL,
accounts: [process.env.PRIVATE_KEY]
}
}
};
Security Warning: Never commit your
.env
file to version control. Add it to.gitignore
.
3. Create Your Contract
Add your contract to the contracts
directory, for example contracts/SimpleStorage.sol
.
4. Compile Your Contract
npx hardhat compile
5. Create Deployment Script
Create a file scripts/deploy.js
:
const hre = require("hardhat");
async function main() {
const initialValue = "Initial Value";
const SimpleStorage = await hre.ethers.getContractFactory("SimpleStorage");
console.log("Deploying SimpleStorage...");
const simpleStorage = await SimpleStorage.deploy(initialValue);
await simpleStorage.waitForDeployment();
console.log(`SimpleStorage deployed to: ${await simpleStorage.getAddress()}`);
// Verify the contract was initialized correctly
const storedValue = await simpleStorage.get();
console.log(`Initial stored value: ${storedValue}`);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
6. Deploy Your Contract
npx hardhat run scripts/deploy.js --network xos
7. Verify Deployment
- Note the contract address from the console output
- Check your contract on the XOS Explorer
Conclusion
Both deployment methods offer effective ways to deploy contracts to the XOS network. Remix provides simplicity and a visual interface, while Hardhat offers more control and automation capabilities for professional development workflows. Choose the approach that best fits your project requirements and development style.