Skip to content

Deploying Contracts to CESS

hong edited this page Jul 25, 2023 · 7 revisions

Please refer to the most updated version!

This document is intended to help developers deploy both Ink! and EVM smart contracts to the CESS network.

Deploying Ink! Contracts to CESS Network

Requirements

  1. WebAssembly optimizer binaryen: To install the binaryen package run the following command:-

    sudo apt install binaryen

  2. cargo-contract package: This provides a command-line interface for working with smart contracts using the ink! language.

    • Install cargo-dylint to check ink! contracts and warn you about issues that might lead to security vulnerabilities.

      cargo install cargo-dylint dylint-link

    • Install cargo-contract by running the following command:

      cargo install --version 1.5.1 cargo-contract --force

    • Verify the installation and explore the commands available by running the following command:

      cargo contract --help

  3. Access to CESS node using https://polkadot.js.org/apps

Create a new smart contract project

We will create new smart contract project using the cargo-contract package downloaded in the previous step. Run the following command:-

cargo contract new flipper
cd flipper
code .

Open the Cargo.toml file and have version 3.3 for all ink crates.

ink_primitives = { version = "3.3", default-features = false }
ink_metadata = { version = "3.3", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.3", default-features = false }
ink_storage = { version = "3.3", default-features = false }
ink_lang = { version = "3.3", default-features = false }

To run the test for the flipper contract run the following command:-

cargo test

You should see the following output:-

running 2 tests

test flipper::tests::it_works ... ok

test flipper::tests::default_works ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

To builds and generate the WebAssembly binary for the contracts project run the following command:-

cargo +nightly contract build

After the build is complete you can find the flipper.contract file in the target/ink/ folder, this file will be used to deploy the contract on the CESS network.

Deploying the contract

Open https://polkadot.js.org/apps and navigate to Developers => Contracts page as shown in Fig-1.

Fig-1 Ink Contracts

Fig-1: Contracts Page

Click on Upload & deploy code to open Upload & deploy code dialog box. On the dialog box select the account which will be used as the deployment account and upload the flipper.contract file. Give a discriptive name to the contract and click the next button to advance to the next dialog box. See fig-2.

Fig-2 Ink Contracts

Fig-2: Upload & deploy code 1/2

Depending on the Deployment Constructor review and update the values if needed or accept the default values and click deploy. See fig-3.

Fig-3 Ink Contracts

Fig-3:

On the next dialog box authorize and submit signed transaction and wait for the transaction to be included into the block, see Fig-4.

Fig-4 Ink Contracts

Fig-4: Authorize and submit signed transaction

Once the contract is diployed, you can view the contract in the contracts page as shown in fig-5.

Fig-5 Ink Contracts

Fig-5: Contracts page

To access the contract you can open the contracts page and in contracts list you can find the list of all the contracts diployed. See fig-6

Fig-6 Ink Contracts

Fig-6: Accessing the contract

As shown in the above you can access smart contract functions using the polkadot.js.org/apps contracts page.

Further reading

Deploying EVM Contracts to CESS Network

Requirements

  1. MetaMask: Required to get Ethereum address and to connect to the CESS Chain.
  2. Remix IDE: IDE to develop, compile, and deploy smart contracts to the chain.
  3. CESS node: Have access to the CESS node. (Make sure the node allows access to MetaMask)

For the current guide, we will be deploying the contract to the CESS test chain. The following steps will guide you to deploying EVM-based contracts on CESS Chain:-

Adding CESS Network to MataMask

Open the MetaMask setting tab, click on the “Networks” tab, click on “Add a network” and then “Add a network manually” as shown in Fig-1

Fig-1

On “Add a network manually” page enter the following details:-

Note:- In case MetaMask is not able to connect your RPC URL, make sure the cors is allowed for MetaMask in the CESS node.

image

Fig-2: Add a network manually.

Convert the Account address to the CESS-supported address

Copy the account address from MetaMask. Refer to Fig-3 for finding where to find the account address.

Fig-3

Fig-3: Account Address in MetaMask

Open the following link Substrate Address Converter and refer the Fig-4.

Fig-4

Fig-4: Substrate address converter.

Fund the address using polkadot.js.org/apps

Using polkadot.js.org/apps transfer amount to the contract address as shown in fig-5

Fig-5

Fig-5: Transfer TCESS to address obtained by converting Ethereum account address.

Validate the fund in MetaMask

To validate the funds are in the Ethereum account, open MetaMask and check the account has the funds transferred. Check the fig-6

Fig-6

Fig-6: Confirm amount in Ethereum account in MetaMask.

Deploy contract to address using Remix IDE

Open Remix Ethereum and go to “File explorer”. In File explorer open the smart contract you wish to compile and deploy. See fig-7 for reference.

Fig-7

Fig-7: selecting the contract to compile.

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, refer to Fig-8.

Fig-8

Fig-8: Compile the Contract

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 as shown in fig-9.

Fig-9

Fig-9: Deploy the contract.

Note: When you click deploy you may need to click confirm in MetaMask to allow remix to access the account and submit the transaction.

Click Confirm to submit the transaction to deploy the smart contract as shown in fig-10

Fig-10

Fig-10: MetaMask confirm to deploy the contract

When the transaction is deployed and mined in the chain you’ll see the following message. See Fig-11.

Fig-11

Fig-11: Smart Contract deployed successfully.

In the “Deployed Contracts” section in the remix you can call the function of the smart contract. See fig-12.

Fig-12

Fig-12: Deployed Contracts in Remix

Transfer token from Ethereum account to CESS address

Convert the Substrate address to Ethereum account address using the link Substrate Address Converter. See Fig-13.

Fig-13

Fig-13: Convert CESS account to Ethereum account address.

Copy the Ethereum equivalent address of the substrate address and use MetaMask to transfer fund. See Fig-14 and Fig-15

Fig-14

Fig-14: Ethereum address > CESS account address

Fig-15

Fig-15: Confirmed Transaction

Confirm the balance in the Polkadot.js.org Developer RPC calls. Use the Ethereum address used in previous step. See fig-16.

Fig-16

Fig-16: Developer=> RPC calls => eth => getBalance

Withdraw the balance from Ethereum account to CESS account

To withdraw the balance from Ethereum account to CESS account. Go to Developer => Extrinsics => evm => withdraw. See Fig-17 for reference.

Fig-17

Fig-17: Withdrawing the balance from the Ethereum account address.

Validate the balances in Accounts tab of polkadot.js.org Explorer, see fig-18.

Fig-18

Fig-18: Validating the balance on Explorer.