# Deploy a Solidity Smart Contract

## Overall

In this tutorial, we will learn how to deploy a [**Solidity** Smart Contract](https://docs.soliditylang.org/en/latest/introduction-to-smart-contracts.html) on the CESS blockchain. Solidity smart contracts are widely deployed on EVM-compatible chains, notably Ethereum. CESS blockchain is also EVM-compatible and allows Solidity developers to deploy their contracts on CESS with no or minimal changes.

## Preparation

You will need the following to deploy a Solidity smart contract to CESS.

* **MetaMask**: Required to get an Ethereum address and to connect to the CESS chain
* **CESS Acount**: Refer to [this article](https://github.com/CESSProject/doc-v2/blob/main/community/cess-account.md) on how to create a CESS account and [this article](https://github.com/CESSProject/doc-v2/blob/main/developer/guides/testnet-faucet.md) on getting testnet tokens from our faucet.
* **Remix IDE**: Access to [Remix IDE](https://remix.ethereum.org/) to develop, compile, and deploy smart contracts to the chain
* **Access to CESS Node**: Make sure the node allows access from MetaMask.

The following steps will guide you to deploy a Solidity contract on the CESS testnet.

{% hint style="info" %}
This tutorial involves working with the EVM in CESS test-chain. You will understand better if you understand the reasoning and mechanism behind the [Subtrate and EVM address conversion](https://github.com/CESSProject/doc-v2/blob/main/developer/guides/substrate-evm.md).
{% endhint %}

## Add CESS Network to MetaMask

Open the MetaMask setting tab, click on the **Networks** tab, click on **Add a network** and then **Add a network manually**.

![Metamask: Adding a Network](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-3e63de858a04353cbc284eda7c0f4daaaa12f6a0%2F01.png?alt=media)

On **Add a network manually** page, enter the following details:

* Network Name: **CESS Testnet**
* New RPC URL, one of the following:
  * <https://testnet-rpc.cess.network/ws/>
* Chain ID: **11330**
* Currency Symbol: **TCESS**

![Metamask: Adding CESS Testnet](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-f206ea561ee4a1b2d67d83f9c3939fa02b0b32a3%2F02.png?alt=media)

## Convert the Substrate Address to an EVM Address

Copy the account address from MetaMask.

![Metamask: My Account](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-a6c2933aac895ee6ef588ad8bccb5ef2d90e208a%2F03.png?alt=media)

Open the page to [Substrate Address Converter](https://hoonsubin.github.io/evm-substrate-address-converter).

![Substrate Address Converter](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-76e25543e5f99198eee5accbf358899cf0aee5d3%2F04.png?alt=media)

Input the following:

* Current Address Scheme: **H160**
* Change Address Prefix: \**11330*
* Intput address: *your metamask account*
* Copy the account address starting with "cX.."

Get the EVM address output.

## Fund the Account

Using [CESS Explorer](https://testnet.cess.network/) **Accounts -> Transfer** to transfer some balance to the Substrate address output above. For testnet goto [testnet faucet](https://cess.network/faucet.html) to get fund drip into this Substrate address.

![CESS Explorer: Transfer Amount](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-4f6d3a5f295a32eedbf392f79428fdcd8c9cdfe2%2F05.png?alt=media)

## Validate the Fund

To validate the funds are in the Ethereum account, open MetaMask and check that account has the funds transferred

![Metamask: Check My Account](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-088c3d21a7326f7ee01c062763b465c8f319b881%2F06.png?alt=media)

## Deploy a Contract Using Remix IDE

{% hint style="info" %}
You can use one of the [sample Solidity smart contract codes here](https://github.com/CESSProject/cess-examples/tree/main/hardhat).
{% endhint %}

Open [Remix IDE](https://remix.ethereum.org/) and go to **File explorer**.

In File explorer, open the smart contract you wish to compile and then deploy.

![Remix: Deploy a Contract](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-14de87c4b50459f67f361839c2130125e560b916%2F07.png?alt=media)

Once the file is selected, go to tab **Solidity Compiler**, you should see the selected file, press the **Compile** button to compile the contract. Once compiled, you’ll see the "green tick" mark and compiled (\*.sol) file.

![Remix: Contract Compiled](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-0f0ebed34053a3f7dca9e9b71c953034588cbb9d%2F08.png?alt=media)

Go to **Deploy and run Transactions**, once the compilation is successful, you should see the compiled \*.sol file selected, ready to be deployed. In the **Environments** drop-down, select **Injected Provider - MetaMask** and click **deploy**.

![Remix: Deploy Contract to CESS](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-2c38344fdfbc273db1a6477329e1e34951e02913%2F09.png?alt=media)

{% hint style="info" %}
When you click **Deploy**, you will need to confirm in MetaMask to allow Remix to access the account and submit the transaction.
{% endhint %}

Click **Confirm** to submit the transaction to deploy the smart contract.

![Metamask: Confirm Deploy Transaction](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-51c662d2c0d24d19e14c184d0e2d78390ff80506%2F10.png?alt=media)

After the transaction is deployed and processed on-chain, you’ll see the following message.

![Remix: Deployment Succeeded](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-58b991dc4dca64e9d57fb9d2138ea93886db0e28%2F11.png?alt=media)

In the **Deployed Contracts** section in the Remix, you can call the function of the smart contract.

![Remix: Interact with the Contract](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-650f4ddf821ecfc35042be39bdd4c9cb1f807841%2F12.png?alt=media)

## Transfer Tokens to the CESS Account

Convert the Substrate address to Ethereum account address using the link [Substrate Address Converter](https://hoonsubin.github.io/evm-substrate-address-converter).

![Substrate-EVM Address Converter](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-056ad6930ead495eefb91ef5c34d81835ed01e18%2F13.png?alt=media)

Copy the Ethereum equivalent address and use MetaMask to transfer fund.

| <p><img src="https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-b4b4a2382d8d7c4cac859389db0de16eed3d8db3%2F14.png?alt=media" alt="deploy-sc-solidity-14"><br>Transfer Fund in Metamask 1</p> | <p><img src="https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-cb917f59593517772d9b044e55b8250af9d4d190%2F15.png?alt=media" alt="deploy-sc-solidity-15"><br>Transfer Fund in Metamask 2</p> |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

Confirm the balance in the [CESS Explorer: Developer RPC calls](https://testnet.cess.network/#/rpc). Use the Ethereum address in previous step.

![CESS Explorer: Get Balance](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-6a4705baafd97673d28a055d9e6c5c2c3a5f4a45%2F16.png?alt=media)

## Withdraw Balances to the CESS Account

To withdraw the balance from the Ethereum account to CESS account, follow the route **Developer => Extrinsics => evm => withdraw**.

![CESS Explorer: Sending evm:withdraw Transaction](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-53463e13196c9028d636c99874b46242fc95a93f%2F17.png?alt=media)

Validate the balances in **Accounts** tab of CESS Explorer.

![CESS Explorer: Account Updated](https://2951078474-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFWgzFJTgbmbm8iaxYNRS%2Fuploads%2Fgit-blob-1df7d655befc9a6cc350bf7c6eb95234789ee75a%2F18.png?alt=media)
