Search…
Deploying your contract to Aleph Zero Testnet
In this tutorial, we will go over the multiple ways to interact with smart contract environments on the Aleph Zero blockchain.
We will now deploy our brand new smart contract to Aleph Zero Testnet. There are multiple ways to interact with smart contracts environment on the Aleph Zero blockchain. Here we will present two of them: using the web wallet or from the command line, via cargo contract tool.

Web wallet

Deploying contracts

All the required tools for deploying and interacting with smart contracts are conveniently built in the wallet/explorer web frontend. Make sure you created an account there and got some free TZERO from the faucet, otherwise some tabs and buttons in the wallet might not be visible to you.
Go to the Developer tab and click Contracts in the pop-up menu to access the smart contracts manager:
Now click "Upload & deploy code" to invoke the deployment popup:
Choose the account you would like to use to deploy the contract (in case you have many). This account is going to hold the whole initial supply of your new token created by mytoken contract. Now click the other field below and choose the location of mytoken.contract file you have previously generated with cargo contract (it should be located in mytoken/target/ink/ folder). Metadata contained in the .contract file should be immediately parsed by the wizard and it should present the summary of your contract's ABI (a list of constructors and methods):
You can modify the name of the code bundle if you wish. Then click Next to go to the constructor call menu:
Here you won't be able to choose the constructor (since our contract has only one), but you can adjust the parameter defining the initial supply of your new token. Leave the gas limit with its default value and click "Deploy". In the next menu enter your account's password and click "Sign and submit".
If everything goes well your contract will be deployed and listed in the contracts manager:
In the contracts manager you can see two lists: contracts and code hashes. The reason behind that is the unique way of handling smart contracts deployment in Substrate-based chains. The code of the contract is stored on the blockchain storage as a separate entry from instances of the contract. Thanks to that you can create multiple instances of the same contract without having to upload the whole code of the contract with every instantiation and pay storage fees for duplicated code. You can use the newly created entry in "code hashes" to call the contract constructor again (also from a different account) and create as many instances as you wish.

Interacting with contracts

Now it's finally the time to play around with our new token! In the "contracts" list pick your instance of mytoken contract and click on "Messages(3)" to see the list of available contract calls:
As you can see, all the messages defined in our contract are listed here. Read-only methods (the ones that don't modify the storage) are marked as "read" whereas the ones requiring submitting a transaction are indicated with "exec". If a read method has no parameters (like our totalSupply), it gets automatically executed by the wallet and the resulting current value is shown.
Let's check if everything went fine with creating the initial supply of our new token. Click on the "read" button next to balanceOf message and fill in the details in the popup menu:
The first account ("call from account") can be any account, since we won't be submitting any transaction to the chain. The second account should be the one you used to call the contract constructor. Click "Read" and the result of the call should immediately appear below the fields you just filled in:
Let's finally transfer some of our new tokens to someone else. Head back to the list of available calls and click "exec" next to transfer message of your contract:
Choose the amount and the recipient of the transfer. When you click on the "recipient" field, the wallet will show you a pop-up list of all accounts present in your Accounts and Address book. You can also simply paste in that field some other account address from outside these lists. Now click "Execute", enter your password again, and click "Sign and submit". The transfer should happen almost instantly. You can use balanceOf method to verify that your account has transferred some of their initial supply to the chosen recipient.

Command line

Deploying contracts

If you wish to interact with smart contracts on the Aleph Zero blockchain in a more automated and programmable way, the cargo contract command line tool, which we have compiled our contract with, can also be used to perform all the actions described above. A brief summary of all extrinsics-related functionalities of cargo contract can be found here.
Every cargo contract subcommand which interacts with a live chain needs to be invoked with flags defining the chain endpoint address and the user's private key (seed phrase). To make the commands present in this section more concise, let's first define some environmental variables with values that will be used with these flags:
export SEED="[put your 12 words seed phrase here]"
export URL="wss://ws.test.azero.dev"
Deploying our new contract can be done with instantiate subcommand. Make sure you are in mytoken folder, where our contract lives, and execute the following command:
cargo contract instantiate --suri "$SEED" --url "$URL" \
--constructor new_token \
--args 1000
The output of this command will contain a list of various events generated by the chain (paying fees, creating contact's account etc.) as a result of our deployment transaction. The last event should be System ➜ ExtrinsicSuccess indicating that the deployment was successful, followed by the information about the address of the contract we just created. Let's store this address in another environmental variable for more convenient interaction with the contract:
export CONTRACT="5GNruCfnGXjSPkW7LkRnB45MiHJJtvc6NBKZnDSnFh4So3ws
The contract address can also be used to import an existing contract into web wallet we used in the previous section. In the smart contracts manager click "Add an existing contract" and paste the address there. You also need to upload the metadata.json file with ABI, which was produced during compilation and should be present in mytoken/target/ink/ folder. After that you can interact with the contract in the same way as described previously.

Interacting with contracts

As mentioned previously in the web wallet section, there are two types of calls that one can perform on a smart contract: state queries, which ask about contract state without modifying it and executable calls, which modify the state and require submitting a signed transaction and paying a fee. These two types of actions are distinguished in cargo contract by the --dry-run flag.
Let's start with performing the simplest argumentless state query to find out the total supply of our token:
cargo contract call --suri "$SEED" --url "$URL" \
--contract "$CONTRACT" \
--message total_supply \
--dry-run
The output will contain, among others, the data returned by our total_supply function:
Result Success!
Reverted false
Data 1000
Gas Consumed 248300975
Gas Required 6815744000
Storage Deposit StorageDeposit::Charge(0)
To perform a state query with arguments, like balance_of, we need to add --args flag, similarly to when we were deploying the contact and and calling its constructor:
cargo contract call --suri "$SEED" --url "$URL" \
--contract "$CONTRACT" \
--message balance_of \
--args 5FWmHxBXH4WfrryA6xdbaQRJALJ549aL11HMyybqDy5iNRtE \
--dry-run
Output:
Result Success!
Reverted false
Data 1000
Gas Consumed 322051074
Gas Required 6815744000
Storage Deposit StorageDeposit::Charge(0)
Here the argument can be any valid account address. Of course, at this point, all acounts other than the contract creator (the address associated with the seed phrase we have put in $SEED) have 0 tokens and the creator holds the total supply of 1000 tokens. So let's change it by transferring some tokens to another account:
cargo contract call --suri "$SEED" --url "$URL" \
--contract "$CONTRACT" \
--message transfer \
--args 5D853t8wQuHJpfWvtcB3VUyKo8Ki44HQwgTmynGT4i5UVhbr 100
This time we want to send a transaction that modifies the chain state, so the --dry-run flag should be omitted. The output will contain a list of events generated by our transaction ending with familiar System ➜ ExtrinsicSuccess indicating that our call was successful. We can now verify that the transfer of 100 tokens indeed happened:
cargo contract call --suri "$SEED" --url "$URL" \
--contract "$CONTRACT" \
--message balance_of \
--args 5FWmHxBXH4WfrryA6xdbaQRJALJ549aL11HMyybqDy5iNRtE \
--dry-run
Output:
Result Success!
Reverted false
Data 900
Gas Consumed 322051074
Gas Required 6815744000
Storage Deposit StorageDeposit::Charge(0)
cargo contract call --suri "$SEED" --url "$URL" \
--contract "$CONTRACT" \
--message balance_of \
--args 5D853t8wQuHJpfWvtcB3VUyKo8Ki44HQwgTmynGT4i5UVhbr \
--dry-run
Output:
Result Success!
Reverted false
Data 100
Gas Consumed 322051074
Gas Required 6815744000
Storage Deposit StorageDeposit::Charge(0)

Next steps

Congratulations! You are now a smart contract developer. If you would like to learn more about ink! smart contracts, we encourage you to take a dive into the excellent ink! documentation. You can also check a collection of example ink! contracts located here. If you have any problems or questions we are always happy to help, just reach us using one of the channels listed on alephzero.org.
Copy link
Outline
Web wallet
Command line
Next steps