Deploying a smart contract to the test Casper network

This tutorial will give an introduction on how to deploy a smart contract to the Casper block chain for testing. The contract used is the one created in the previous video:

Creating a smart contract in Rust

Throughout this tutorial series it will be shown how to develop a simple web based game that will interact with the Casper blockchain. The game will be written in JavaScript and the Casper blockchain is used to store the players high score for the game. A contract is created that can store and retrieve the high-score from the blockchain. Now the contract can be deployed to the Casper test network for testing.

The final game can be seen here:

Full source code can be found here:

https://github.com/playcasper/snake-casper-game

Pre-requisites:

  • Have a Linux development environment setup which includes Cmake, Cargo and Rust
  • Have some programming knowledge and be familiar with Rust.
  • Have a contract compiled down to WASM

Setup the Linux Environment for deployment

The casper-client needs to be installed in order to interact with the Casper blockchain from a terminal.

Use the following command in a terminal.

cargo install casper-client

Creating a test Casper account

A test account needs to be created on the Casper blockchain. There are a couple of ways to do this. One method is from the command line but it is easier to create an account on the official Casper network website. Visit the website here:

https://cspr.live

From here it is possible to monitor a whole load of information about the Casper block chain, for example how many deploys have been made, the cost of a single Casper token and a lot of other information. On the right-hand side there is an option to choose between the live site and a test version of the site.

Choosing the test network will provide access to the block chain but in a completely protected test environment.

Next thing to do is to create an account. This is achieved by using Casper Signer which is an extension for the Chrome browser. Download and install the Casper Signer extension by clicking here. Make sure you are running Google Chrome and not a different browser.

https://chrome.google.com/webstore/detail/casper-signer/djhndpllfiibmcdbnmaaahkhchcoijce

You will be prompted to create an account. Give it a name and follow all the instructions. You will be allocated a secret key which you should store somewhere secure and is used for the deployment. You will also be given a public key and an AccountHash which is used to identify each user on the blockchain. The AccountHash is derived from the public key.

Once logged in to the website it is required to acquire some pretend Casper tokens for the account. To do this, go the Tools options and then Faucet. Fill this out and Request tokens. This can only be done once. Visit your account page to see your current balance.

Deploying the contract

Now the test contract can be deployed to the block chain. All that is needed is an address of where to send it. This can be picked by going to Tools and Connected Peers. These are the addresses of the current peer nodes that can accept the Casper deployments and queries. For this tutorial the following address was chosen:

http://195.201.174.222:7777

The port 7777 is always used when querying the blockchain

Every time a new block is created, on the blockchain, the overall state of the blockchain changes. So before every query it is necessary to retrieve the latest global state. This is done by running the following command:

casper-client get-state-root-hash --node-address http://195.201.174.222:7777

This command uses the casper-client API to get the state root hash from the requested peer node.

The output that is returned is in JSON format and shows the state root hash here.

{
  "id": -141111138692010598,
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.4.13",
    "state_root_hash": "b6c1691baeb0c9140dbfa3e0e391506d400ef473b63519436c0ccd87f2583da6"
  }
}

Copy the state root hash which can be used in the rest of the commands.

For example, it can be used to get the status of the user’s account on the blockchain using the query-state command:

casper-client query-state \
--node-address http://195.201.174.222:7777 \
--state-root-hash  \
--key 

To deploy the smart contract to the blockchain, the put-deploy command is used.

casper-client put-deploy \
--node-address http://195.201.174.222:7777 \
--chain-name casper-test \
--secret-key .keys/private.key \
--payment-amount 20000000000 \
--session-path projects/highscore/target/wasm32-unknown-unknown/release/highscore.wasm

This command requires a peer node to connect to, the name for the test network and a link to the previously saved private key. It is necessary to specify a payment amount. This is dependent on the size of the WASM file that is being deploy, and is called the Gas amount. And finally, a reference to the WASM file of our smart contract.

Once the contract is deployed you are given a deploy hash. This hash to used to query the status of the deployment.

casper-client get-deploy --node-address http://195.201.174.222:7777

At the bottom of the JSON output there is the execution result of the deployment. If it is blank this means the contract is still being deployed and verified. This can sometimes take a minute or two. The command can be ran again until you get a valid execution output. If not enough tokens are allocated in the deployment then an “out of gas error” will occur.

It is possible to verify the status of a deployment by using the test website and looking at the recent deployments section. Here is an entry for the contract that we just deployed and how much it actually cost.

Now that the contract is successfully deployed, it is possible to test it with some values.

Test the deployed contract

One option is to see what the default high score is set to. Use the following command to query a value inside the contract. First get the state hash as usual.

casper-client get-state-root-hash --node-address http://195.201.174.222:7777

Then use the query-state command but this time with the q parameter.

casper-client query-state \
--node-address http://195.201.174.222:7777 \
--state-root-hash    \
--key  \
-q "highscore/highest_score"

The initial highscore, for a new deployment should be 0.

The put-deploy command can be used to insert a new highscore like this.

casper-client put-deploy \
--node-address http://195.201.174.222:7777 \
--chain-name casper-test \
--secret-key .keys/private.key \
--payment-amount 100000000 \
--session-name "highscore" \
--session-entry-point "highscore_set" \
--session-arg=name:"string='Adrian'" \
--session-arg=value:"i32='27'"

The contract entry point of highscore_set is used with the arguments, name and score.

Check that this worked using the deploy hash.

casper-client get-deploy --node-address http://195.201.174.222:7777

Use the previously used command to retreive the highest score. The highest score should now be 27 instead of 0.

casper-client get-state-root-hash --node-address http://195.201.174.222:7777

casper-client query-state \
--node-address http://195.201.174.222:7777 \
--state-root-hash  \
--key  \
-q "highscore/highest_score"

This time the current high score is 27.

It is also possible to retrieve the high score per user. First, get the state hash and then query the global state using our account hash.

casper-client query-global-state \
--node-address http://195.201.174.222:7777 \
--state-root-hash  \
--key 

This gives a list of all the named keys in the account. This shows the high score named keys, so using this instead of the account hash we can tunnel deeper into the high score contract.

casper-client query-global-state \
--node-address http://195.201.174.222:7777 \
--state-root-hash  \
--key 

Here we can see the keys for the individual users, the highest score overall and the user with the highest score. For exmaple, use this hash to query what is Adrian’s current high score. So, you can see the highest score for Adrian is 27.

So, that is how to deploy your smart contract to the test Casper network and do some basic testing before going live.