How to deploy a Smart Contract on Gnosis Chain

How to deploy a Smart Contract on Gnosis Chain

ยท

4 min read

Deploying a smart contract on a blockchain can be a daunting task, especially if you are new to the world of blockchain. Fortunately, thirdweb has recently introduced a new feature that makes it easier to deploy smart contracts to various blockchains. In this blog, I will guide you through the steps to deploy a smart contract on Gnosis Chain using thirdweb's new feature.

What is Gnosis Chain?

Gnosis Chain is a new EVM-compatible blockchain that is designed specifically for decentralized applications (dApps). It is built on top of the Ethereum blockchain and is optimized for high-speed and low-cost transactions. Gnosis Chain also supports smart contracts, which are self-executing contracts with the terms of the agreement between buyer and seller being directly written into lines of code.

Step 1: Write the smart contract

Use the below command to create a new project with your preferences set up

npx thirdweb create contract

Now start writing the smart contract in the src directory.
Below I have written a simple smart contract that allows anyone to deposit any ERC20 token and gives some percentage to the admin.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
// import "hardhat/console.sol";

contract PurplePay {
    address public admin;
    uint256 public adminFeePercentage;
    uint256 public totalFeeCollected; 

    mapping (address => mapping(address => uint256)) public userData;

    event Deposit(address indexed _from, address indexed _tokenAddress, uint256 _amount);
    event Withdraw(address indexed _to, address indexed _tokenAddress, uint256 _amount);

    constructor() {
        admin = msg.sender;
        adminFeePercentage = 1;
    }

    modifier onlyAdmin() {
        require(msg.sender == admin, "Only Admin is allowed");
        _;
    }

    function deposit(address _tokenAddress, uint256 _amount) external {
        IERC20 token = IERC20(_tokenAddress);

        // console.log("Starting transferFrom: %s", address(token));
        require(token.transferFrom(msg.sender, address(this), _amount), "Token transfer failed");

        uint256 adminFee = _amount * adminFeePercentage / 100;
        totalFeeCollected += adminFee;
        userData[msg.sender][_tokenAddress] += (_amount - adminFee);

        require(token.transfer(admin, adminFee), "Admin fee transfer failed");

        emit Deposit(msg.sender, _tokenAddress, _amount);
    }

    function withdraw(address _tokenAddress, uint256 _amount) external {
        address sender = msg.sender;
        require(userData[sender][_tokenAddress] >= _amount, "Insufficient balance");
        userData[sender][_tokenAddress] -= _amount;
        IERC20 token = IERC20(_tokenAddress);
        require(token.transfer(sender, _amount), "Token transfer failed");

        emit Withdraw(sender, _tokenAddress, _amount);
    }

    function setAdmin(address _newAdmin) external onlyAdmin {
        require(_newAdmin != address(0), "Invalid admin address");
        admin = _newAdmin;
    }

    function setAdminFeePercentage(uint256 _adminFeePercentage) external onlyAdmin {
        require(_adminFeePercentage <= 100, "Invalid Admin Fee Percentage");
        adminFeePercentage = _adminFeePercentage;
    }
}

Step 2: Deploy using thirdweb

Use the below command to deploy your smart contract using a single command

npx thirdweb deploy

What this will do is give you a link to thirdweb dashboard to complete the transaction and deploy your smart. Something like this ๐Ÿ‘‡

Step 2: Configure Your Contract

Once your contract is imported, you will need to select the network you want to deploy your smart contract to. thirdweb doesn't have Gnosis chain by default so we will import it by clicking on Configure Network

Now click on Add Network and hit Deploy Now

Remember to grab some xDai from this faucet

This will deploy it to the Gnosis chain and will then ask for a signing message to add this contract to your dashboard to easily interact with it!

Here is the link to the contract I deployed to the Gnosis chain using thirdweb.

Bonus

You can also import any existing contract which is not deployed using thirdweb to the dashboard to easily!! Here is how ->

Import your contract to thirdweb's dashboard. To do this, simply click on the Import Contract button and simply paste the contract address and the chain.

Make sure that your contract is written in Solidity, which is the programming language used to write smart contracts.

Verify Your Contract

thirdweb has also launched a new feature to verify your smart contracts with just one click!

Feedback on thirdweb's features

Overall, thirdweb's new feature has greatly improved our development workflow. It has made it much easier to deploy smart contracts to various blockchains, including Gnosis Chain.

However, we did face a small problem - we were not able to import contracts to thirdweb dashboard which were deployed using Remix. We would like to see more integration with third-party tools, such as Truffle and Remix, which are commonly used by developers to write and test smart contracts.

Conclusion

Deploying a smart contract on Gnosis Chain using thirdweb's new feature is a simple and straightforward process. By following the steps outlined in this article, you can easily deploy your own smart contract and start building decentralized applications on Gnosis Chain. We hope that this guide has been helpful and that you will continue to explore the exciting world of blockchain development. For more information on thirdweb's new feature, check out their blog post on 'Any EVM, Any Contract' - https://blog.thirdweb.com/any-contract-any-evm-chain/

My deployed contract - https://thirdweb.com/gnosis/0x2e29b044DCeF53796A9f3437C4FA05036761F669

ย