Deploying a smart contract with Remix
On this page, you will learn how to create, deploy and verify a smart contract with the Remix IDE to the Lisk Sepolia testnet.
Remix Online IDE is a powerful toolset for developing, deploying, debugging, and testing Ethereum and EVM-compatible smart contracts. It requires no setup and can be accessed directly through the browser under https://remix.ethereum.org/.
Prerequisites
Wallet funds
Deploying contracts to the blockchain requires a gas fee. Therefore, you will need to fund your wallet with ETH to cover those gas fees.
For this guide, you will be deploying a contract to the Lisk Sepolia Testnet.
You can deposit the required tokens by using the Lisk Bridge.
In case your wallet doesn't hold enough SepoliaETH
, use one of the available faucets for the Ethereum Sepolia Testnet like https://sepoliafaucet.com to receive free Testnet ETH.
Then, use the aforementioned Lisk Bridge to send tokens from the Ethereum Sepolia Testnet to the Lisk Sepolia Testnet.
You can deploy a contract on Lisk Mainnet by adopting the same process. Before deploying to Mainnet, ensure that your wallet has enough ETH.
1. Open Remix
Navigate to Remix in your browser.
2. Create a new file
Inside the contracts
folder, click the 📄 ("Create new file") button to create a new empty Solidity file.
You can name this file whatever you'd like, e.g., MyNFT.sol
.
3. Copy the example contract
Copy the following example contract into your new file to deploy a simple NFT contract, or replace it with your own contract you wish to deploy:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract MyNFT is ERC721 {
uint256 public currentTokenId;
constructor() ERC721("My NFT", "MNFT") {}
function mint(address recipient) public returns (uint256) {
uint256 newItemId = ++currentTokenId;
_safeMint(recipient, newItemId);
return newItemId;
}
}
4. Compile the contract
Please double-check that the compiler version of the Remix IDE matches the compiler version mentioned in the smart contract: pragma solidity ^0.8.28;
.
Press the green play button at the top to compile the contract.
5. Test the contract
To test the contract, we are using the Solidity Unit Testing plugin of the Remix IDE. If you haven't used this plugin before and are not seeing the double-check icon in the sidebar, you first have to activate it from the Remix plugin manager.
To start testing, create a new folder called tests
at the root level of your Remix workspace.
This is the default directory for tests.
If you want to change the test directory, you can do so by clicking on the double-check icon and specifying the path in the Test directory
field.
Next, open the Solidity file which you want to test, then switch to the "Solidity Unit Testing" plugin and click on the Generate
button.
This will create a test file (e.g., myNFT_test.sol) in the tests directory.
This file contains information about developing tests for a contract.
Below is an example of a generic test file myNFT_test.sol
:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0;
// This import is automatically injected by Remix
import "remix_tests.sol";
// This import is required to use custom transaction context
// Although it may fail compilation in 'Solidity Compiler' plugin
// But it will work fine in 'Solidity Unit Testing' plugin
import "remix_accounts.sol";
import "../contracts/MyNFT.sol";
// File name has to end with '_test.sol', this file can contain more than one testSuite contracts
contract testSuite {
/// 'beforeAll' runs before all other tests
/// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll'
function beforeAll() public {
// <instantiate contract>
Assert.equal(uint(1), uint(1), "1 should be equal to 1");
}
function checkSuccess() public {
// Use 'Assert' methods: https://remix-ide.readthedocs.io/en/latest/assert_library.html
Assert.ok(2 == 2, 'should be true');
Assert.greaterThan(uint(2), uint(1), "2 should be greater than to 1");
Assert.lesserThan(uint(2), uint(3), "2 should be lesser than to 3");
}
function checkSuccess2() public pure returns (bool) {
// Use the return value (true or false) to test the contract
return true;
}
function checkFailure() public {
Assert.notEqual(uint(1), uint(1), "1 should not be equal to 1");
}
/// Custom Transaction Context: https://remix-ide.readthedocs.io/en/latest/unittesting.html#customization
/// #sender: account-1
/// #value: 100
function checkSenderAndValue() public payable {
// account index varies 0-9, value is in wei
Assert.equal(msg.sender, TestsAccounts.getAccount(1), "Invalid sender");
Assert.equal(msg.value, 100, "Invalid value");
}
}
Take the auto-generated template as a basis to create sufficient unit tests that verify your contract behavior in various scenarios. Remix injects a built-in assert library for testing. You can visit the assert library in the Remix docs. Additionally, Remix allows the usage of special functions in the test file to make testing more structural.
They are:
beforeEach()
- Runs before each testbeforeAll()
- Runs before all testsafterEach()
- Runs after each testafterAll()
- Runs after all tests
For our new NFT contract, we add the following test:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0;
// This import is automatically injected by Remix
import "remix_tests.sol";
// This import is required to use custom transaction context
// Although it may fail compilation in 'Solidity Compiler' plugin
// But it will work fine in 'Solidity Unit Testing' plugin
import "remix_accounts.sol";
import "../contracts/MyNFT.sol";
// File name has to end with '_test.sol', this file can contain more than one testSuite contracts
contract testSuite {
MyNFT nft;
function beforeAll() public {
nft = new MyNFT();
}
// Test minting functionality
function testMint() public {
// Store initial token ID
uint256 initialTokenId = nft.currentTokenId();
// Mint a new NFT to this contract's address
uint256 newTokenId = nft.mint(TestsAccounts.getAccount(2));
// Check that token ID incremented
Assert.equal(newTokenId, initialTokenId + 1, "Token ID should increment by 1");
// Check currentTokenId updated
Assert.equal(nft.currentTokenId(), newTokenId, "currentTokenId should match new token ID");
// Check ownership
Assert.equal(nft.ownerOf(newTokenId), TestsAccounts.getAccount(2), "Contract should own the minted NFT");
}
// Test minting to specific address
function testMintToAddress() public {
// Mint NFT to test address
uint256 newTokenId = nft.mint(TestsAccounts.getAccount(1));
// Check ownership
Assert.equal(nft.ownerOf(newTokenId), TestsAccounts.getAccount(1), "Test address should own the minted NFT");
// Check token balance
Assert.equal(nft.balanceOf(TestsAccounts.getAccount(1)), 1, "Test address should have 1 NFT");
}
}
There are several ways to customize your solidity tests. For example, you can customize the compiler and the transaction context.
Once you are done with writing tests, select the test file(s) and click on Run
to execute the tests.
The execution will run in a separate environment.
After completing the execution of one file, a test summary will be shown:
Result for tests/myNFT_test.sol
Passed: 2
Failed: 0
Time Taken: 0.06s
6. Deploy the contract
Open the Deploy & run transactions
tab (this looks like an Ethereum logo with an arrow pointing right).
Make sure that your environment is set to "Injected Provider", your wallet is connected to Lisk or Lisk Sepolia network, and Remix has access to your wallet.
Then, select the MyNFT
contract from the deployment dropdown and click the orange Deploy
button to deploy the contract and confirm the contract deployment in your connected wallet.
Check the Remix log messages; they should include the contract address. Paste this address in Blockscout, to see the contract in the Lisk blockchain explorer: https://sepolia-blockscout.lisk.com/address/0x73e7a94dD5760d862F6FD9f8ea5D4245Bb143446.
In case you chose to deploy on the Lisk Mainnet, you need to paste the address on https://blockscout.lisk.com instead.
7. Verify the contract
If you want to interact with your contract on the block explorer, you first need to verify it. The above contract has already been verified, so you should be able to view your version on a block explorer already. For the remainder of this guide, we'll walk through how to verify your contract with Remix on the Lisk Sepolia Testnet.
You can apply the same steps for verifying a contract on Lisk Mainnet, in case you deployed it there in the previous step, just use https://blockscout.lisk.com instead of https://sepolia-blockscout.lisk.com in step 2.
- In Remix, right-click on the contract you wish to verify and select
Flatten
. This will create a new fileMyNFT_flattened.sol
. - Now, switch to your newly deployed contract on https://sepolia-blockscout.lisk.com/
- Go to the contract tab and click on the blue
Verify and Publish
button.- (Optional) Set a license for your contract.
- Choose
Solidity (Single file)
as the verification method. - Choose the compiler version that fits your contract.
- Disable code optimization.
- Copy the flattened source code from Remix and paste it into the
Enter the Solidity Contract Code
field.
- Check that all info is correct and click the
Verify and Publish
button, to verify your contract.
Once verified, the code tab will include the ✅ icon, and the source code will be viewable.
Interacting with the Smart Contract
After the contract is verified, you can use the Read Contract
and Write Contract
tabs to interact with the deployed contract via Blockscout: https://sepolia-blockscout.lisk.com/address/0x73e7a94dD5760d862F6FD9f8ea5D4245Bb143446?tab=contract.
Don't forget to update the contract address in the Blockscout URL.
You'll also need to connect your wallet first, by clicking the Connect Wallet
button.