-
Notifications
You must be signed in to change notification settings - Fork 100
Deploying Contracts to CESS
This document is intended to help developers deploy both Ink! and EVM smart contracts to the CESS network.
-
WebAssembly optimizer binaryen: To install the binaryen package run the following command:-
sudo apt install binaryen
-
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
-
-
Access to CESS node using https://polkadot.js.org/apps
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.
Open https://polkadot.js.org/apps and navigate to Developers => Contracts page as shown in Fig-1.
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: 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:
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: 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: 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: Accessing the contract
As shown in the above you can access smart contract functions using the polkadot.js.org/apps contracts page.
- MetaMask: Required to get Ethereum address and to connect to the CESS Chain.
- Remix IDE: IDE to develop, compile, and deploy smart contracts to the chain.
- 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:-
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
On “Add a network manually” page enter the following details:-
- Network Name: CESS_test.
- New RPC URL: http://127.0.0.1:9933 (This can be any open http/https RPC URL accessible to you by CESS).
- Chain ID: 11330
- Currency Symbol: MTCESS
Note:- In case MetaMask is not able to connect your RPC URL, make sure the cors is allowed for MetaMask in the CESS node.
Fig-2: Add a network manually.
Copy the account address from MetaMask. Refer to Fig-3 for finding where to find the account address.
Fig-3: Account Address in MetaMask
Open the following link Substrate Address Converter and refer the Fig-4.
Fig-4: Substrate address converter.
Using polkadot.js.org/apps transfer amount to the contract address as shown in fig-5
Fig-5: Transfer TCESS to address obtained by converting Ethereum account address.
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: Confirm amount in Ethereum account in MetaMask.
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: 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: 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: 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: 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: 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: Deployed Contracts in Remix
Convert the Substrate address to Ethereum account address using the link Substrate Address Converter. See 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: Ethereum address > CESS account address
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: Developer=> RPC calls => eth => getBalance
To withdraw the balance from Ethereum account to CESS account. Go to Developer => Extrinsics => evm => withdraw. See Fig-17 for reference.
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: Validating the balance on Explorer.