Build on Shibarium
Welcome aboard! Simply switch to the Shibarium RPC and kickstart your journey. All the familiar tools used on Ethereum are fully supported on Shibarium. Whether you're using Truffle, Remix, or Web3js, Shibarium offers the same user experience as Ethereum.
Connect your wallet and deploy any decentralized application to either the mainnet Shibarium or testnet Puppynet. Puppynet is connected to Ethereum Sepolia Testnet, which serves as its ParentChain, a testnet layer 1 (L1).
To illustrate how you can create, deploy and verify smart contracts on Shibarium, we have added two tutorials:
- Deploy a smart contract with Remix
- Deploy and verify a smart contract with Hardhat
Overview
Shibarium functions as a layer 2 (L2) network to Ethereum, employing a proof-of-stake (PoS) consensus mechanism, consisting of the following two layers;
- Heimdall layer, a consensus layer that communicates with staking contracts deployed on the Ethereum mainnet and commits the Shibarium Network checkpoints to the Ethereum mainnet.
- Bor layer, a Geth-based execution layer composed of a set of block-producing Bor nodes shuffled by Heimdall nodes.
Wallets
To interact with the Shibarium Network, you need an Ethereum-based wallet because Shibarium operates on the Ethereum Virtual Machine (EVM). Set up a Metamask or Arkane Wallet to get started.
Smart contracts
Shibarium supports various services for testing, compiling, debugging, and deploying decentralized applications onto the Shibarium Network. These include deployment using thirdweb, Remix, Truffle, Hardhat, and Replit.
Deploy a smart contract on Shibarium using Remix
Now that you know what Shibarium is, let's deploy a contract.
You will need some BONE tokens to deploy and interact with the smart contract, which you can acquire from the Shibarium faucet. Copy your Metamask address,paste it into the address field of the faucet, and click on submit. The faucet will send you 10 test BONE.
Writing the contract
Make a new solidity file using REMIX Ethereum, for example. Your new Solidity script should contain the following code:
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.0 <0.7.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
- Line 1: Specifying SPDX license type, which is an addition after Solidity ^0.6.8. Whenever the source code of a smart contract is made available to the public, these licenses can help resolve/avoid copyright issues. If you do not wish to specify any license type, you can use a special license UNLICENSED or simply skip the whole comment (it won’t result in an error, just a warning).
- Line 2: On the second line, we are declaring which Solidity compiler we want to use. For instance, we are targeting any version between 0.4.0 and 0.7.0.
- Line 3: You declare your contract here and name it as
SimpleStorage
. - Line 4: Declaring a uint (Unsigned Integer) variable named
storedData
. This variable will be used to store data. - Line 5-7: Next, you will add a
set
function, used to change the value of thestoreData
variable. Theset
function accepts a parameterx
whose value you are placing intostoreData
. In addition, the function is marked as public which means that the function can be called outside the scope of this function and by other contracts. - Line 8-10: You will add a
get
function to retrieve the value ofstoreData
variable. This function is marked as view, which tells the Solidity compiler that this is a read-only function. Other than that, theget
function also has returns (uint), which means that the function will return a uint value.
Deploying the contract
Click on the Solidity logo from the left menu and click on Compile. After successful compilation, a green tick will appear on the Solidity logo.
Now, click on the option from the left menu to deploy the complied contract and select Injected Web3 as the environment. Below the environment field, the name and Chain ID of your network will appear. Make sure you have the correct contract name selected under the contract option. Once you have checked everything, click on Deploy and accept the transaction from the MetaMask pop-up window.
Once the contract deployment transaction is approved, the deployed contract will appear under the Deployed Contracts section.
Expand the deployed contract and click on get. It will return the value of storedData, which is currently zero since you have not input any number yet.
To input a value, enter a number in the field near the set button, click on set and approve the transaction from the MetaMask pop-up. Once the transaction is approved, the value of storedData will be the input number. To verify this, click on get, and the previously input value will be printed.
This is how contracts are deployed on Shibarium with Remix!
Deploy and verify a smart contract with Hardhat
Prerequisites
- Node.js: Make sure you have Node.js installed. You can download it from here.
- Hardhat: Install Hardhat globally using npm:
npm install -g hardhat
- Metamask Wallet: Set up a Metamask wallet if you don't have one. Add Shibarium's network to Metamask.
Set Up Hardhat Project
- Create a new directory for your project and navigate into it:
mkdir shibarium-smart-contract
cd shibarium-smart-contract
- Initialize a new Hardhat project:
npx hardhat
Follow the prompts to create a basic sample project.
- Install the necessary dependencies:
npm install --save-dev @nomiclabs/hardhat-ethers ethers
Write Your Smart Contract
- Create a new file in the contracts directory named SimpleStorage.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
}
Configure Hardhat for Shibarium
- Open hardhat.config.js and add the following network configuration for Shibarium:
require("@nomiclabs/hardhat-ethers");
module.exports = {
solidity: "0.8.0",
networks: {
shibarium: {
url: "https://www.shibrpc.com", // Replace with Shibarium's RPC URL
accounts: [process.env.PRIVATE_KEY] // Use an environment variable to securely store your private key
}
}
};
- Create a .env file in the root of your project and add your private key:
PRIVATE_KEY=your_private_key_here
Write the Deployment Script
- Create a new file in the scripts directory named deploy.js:
async function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);
const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
const simpleStorage = await SimpleStorage.deploy();
console.log("SimpleStorage deployed to:", simpleStorage.address);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
Deploy the Contract
- Run the deployment script:
npx hardhat run scripts/deploy.js --network shibarium
Verify the Contract on Shibarium
- Install the verification plugin:
npm install --save-dev @nomiclabs/hardhat-etherscan
- Update hardhat.config.js to include the Etherscan plugin and Shibarium's API key:
require("@nomiclabs/hardhat-ethers");
require("@nomiclabs/hardhat-etherscan");
module.exports = {
solidity: "0.8.0",
networks: {
shibarium: {
url: "https://www.shibrpc.com",
accounts: [process.env.PRIVATE_KEY]
}
},
etherscan: {
apiKey: {
shibarium: "shib",
},
customChains: [
{
network: "shibarium",
chainId: 109,
urls: {
apiURL: "https://www.shibariumscan.io/api/",
browserURL: "https://www.shibariumscan.io/"
}
},
]
},
};
- Verify your contract:
npx hardhat verify --network shibarium <DEPLOYED_CONTRACT_ADDRESS>
You have successfully deployed and verified a simple smart contract on Shibarium using Hardhat.
If you are creating and deploying a token, you might need to include its images and metadata in the Shibarium token repository. Learn how to add this data on the Add token data article.