---
# System prepended metadata

title: ERC20 Token sample contract deployment to Casper network

---

# ERC20 Token sample contract deployment to Casper network
>In that guide we will create sample ERC20 token contract using [casper-erc20](https://crates.io/crates/casper-erc20) rust crate
>We will deploy it to the [Casper test-net](https://testnet.cspr.live/#/blocks)
>And we will interact with it's entry points (functions)

## Creating ERC20 token
Using rust crate [casper-erc20](https://crates.io/crates/casper-erc20)

```bash=
cargo install cargo-casper
cargo casper --erc20 <PATH TO NEW PROJECT>
```

## Compiling and Testing contracts

## Creating account keys and Funding account balance

We will use **casper-client** command to create keys

```bash=
casper-client keygen <TARGET DIRECTORY>
```

We will fund our account balance using [Faucet](https://testnet.cspr.live/tools/faucet) on Casper Test-net.

## Deploying

### 1. Create a deploy and send it to the network for execution

```bash=
casper-client put-deploy --chain-name casper-test \
    -n http://135.181.214.240:7777 \
    -k <key_folder>/secret_key.pem \
    -p 100000000000 \
    -s <project_folder>/contracts/target/wasm32-unknown-unknown/release/<contract_name>.wasm \
    -a "name:string='<Token_Name>'" \
    -a "symbol:string='<TOKEN_SYMBOL'" \
    -a "decimals:u8='<token_decimals>'" \
    -a "total_supply:U256='<token_initial_supply>'"
```
>**<contract_name>** you can find in **Cargo.toml** file under code line:
>```
>[[bin]]
>name = "<contract_name>"
>```

You will recieve such a result:
```
{
  "id": -5570510397520769907,
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.3.4",
    "deploy_hash": "<your_deploy_hash_value>"
  }
}
```
### 2. Gets deploy and it's status

```bash=
casper-client get-deploy <your_deploy_hash_value> \
    -n http://135.181.214.240:7777
```

And we will recieve our deploy
```
{
  "id": 8083872860496500035,
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.3.4",
    "deploy": {
      "approvals": [
        {
          "signature": "[130 hex chars]",
          "signer": "<your_account_public_key_hex>"
        }
      ],
      "hash": "<your_deploy_hash_value>",
      "header": {
        "account": "<your_account_public_key_hex>",
        "body_hash": "06d4d62d8f48dd43d2eb10032f7bd399cab5560f577166f97e75a8707149e080",
        "chain_name": "casper-test",
        "dependencies": [],
        "gas_price": 1,
        "timestamp": "2021-10-06T19:51:25.388Z",
        "ttl": "30m"
      },
      "payment": {
        "ModuleBytes": {
          "args": [
            [
              "amount",
              {
                "bytes": "0500e8764817",
                "cl_type": "U512",
                "parsed": "100000000000"
              }
            ]
          ],
          "module_bytes": ""
        }
      },
      "session": {
        "ModuleBytes": {
          "args": [
            [
              "name",
              {
                "bytes": "0e0000005772617070656420436173706572",
                "cl_type": "String",
                "parsed": "Wrapped Casper"
              }
            ],
            [
              "symbol",
              {
                "bytes": "050000005743535052",
                "cl_type": "String",
                "parsed": "WCSPR"
              }
            ],
            [
              "decimals",
              {
                "bytes": "09",
                "cl_type": "U8",
                "parsed": 9
              }
            ],
            [
              "total_supply",
              {
                "bytes": "00",
                "cl_type": "U256",
                "parsed": "0"
              }
            ]
          ],
          "module_bytes": "[503170 hex chars]"
        }
      }
    },
    "execution_results": []
  }
}
```

### 3. Get the Latest Global State Hash
We need to obtain the global state hash after our contract has been deployed to the network.
```bash=
casper-client get-state-root-hash -n http://135.181.214.240:7777 | jq -r
```
We will recieve such a result
```
{
  "id": 3786153198655629300,
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.3.4",
    "state_root_hash": "10edf15684b9ae900b7eeb4b1cbd3c7d88c5bbd7d0a011d420852a08af7755b0"
  }
}
```

### 4. Query State
We will take the **<global_state_hash>** from previous step and we will include it here, along with the account public key hex that created the contract.
```bash=
casper-client query-state -n http://135.181.214.240:7777 \
-k <account_public_key_hex or path to the file> \
-s <state_root_hash>
```
Our result will look like that
```
{
  "id": 4290851353126884171,
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.3.4",
    "merkle_proof": "[19588 hex chars]",
    "stored_value": {
      "Account": {
        "account_hash": "account-hash-d1c042b49c4b1c1b97c2c79c9468308538d0fa6c1c0d27f1f4ba846dde368489",
        "action_thresholds": {
          "deployment": 1,
          "key_management": 1
        },
        "associated_keys": [
          {
            "account_hash": "account-hash-d1c042b49c4b1c1b97c2c79c9468308538d0fa6c1c0d27f1f4ba846dde368489",
            "weight": 1
          }
        ],
        "main_purse": "uref-c9e0c951abc2fa458db164abf48623c00a3c1f8f00586baf39e3ef8bc794d6da-007",
        "named_keys": [
          {
            "key": "hash-ca52b10cd3170652275d2ba16bbd6e3a48ac7098a3835feb314da1d0a372bf02",
            "name": "erc20_token_contract"
          },
          {
            "key": "uref-0172086f98d41089e751bf229fc0fc8374fa99ce152a28019a20085262ab9b28-007",
            "name": "my-key-name"
          }
        ]
      }
    }
  }
}
```

Here under **"named_keys"** we can see **hash** and **name** of ERC20 contract that we deployed.

## Query the contract State
Now that we have the hash of the contract, we can query the contract’s internal state.
To do this, we pass in the contract’s hash and the global state hash.
If we look at the ERC20 contract, we see a token name specified as _name. We can query for the value stored here using next comand:
```bash=
casper-client query-state -n http://135.181.214.240:7777 \
-k <contract_hash> \
-s <state_root_hash> \
-q <key_of_value for example "name"> | jq -r
```
Our response will look like that:
```
{
  "id": 4215164428913749000,
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.3.4",
    "merkle_proof": "[43440 hex chars]",
    "stored_value": {
      "CLValue": {
        "bytes": "0e0000005772617070656420436173706572",
        "cl_type": "String",
        "parsed": "Wrapped Casper"
      }
    }
  }
}
```
That is how we read **"Named key"** values from the contract.

## Calling a Contract by Name & Entry Point
To call a contract by its name, run the put-deploy command using the session-name option:
```bash=
casper-client put-deploy --session-name <NAME> \
    --session-entry-point <FUNCTION_NAME>
```
>However such execute only possible if you are calling a function (entry point) of a contract that you deployed to the network and signed it with your secret key

```bash=
casper-client put-deploy --chain-name casper-test \
    -n http://135.181.214.240:7777 \
    -k ~/code/casper/keys/hackathon_secret_key.pem \
    -p 100000000000 \
    --session-name erc20_token_contract \
    --session-entry-point name
```
## Calling a Contract by Hash and Entry Point

```bash=
casper-client put-deploy  --session-hash <HEX STRING> --session-entry-point <FUNCTION_NAME>
```
```bash=
casper-client put-deploy --chain-name casper-test \
    -n http://135.181.214.240:7777 \
    -k ~/code/casper/keys/hackathon_secret_key.pem \
    -p 100000000000 \
    --session-hash hash-ca52b10cd3170652275d2ba16bbd6e3a48ac7098a3835feb314da1d0a372bf02 \
    --session-entry-point name
```

## Deploy a pre_deposit contract

```bash=
casper-client put-deploy --chain-name casper-test \
    -n http://135.181.214.240:7777 \
    -k ~/code/casper/keys/hackathon_2/hackathon_2_secret_key.pem \
    -p 100000000000 \
    -s  ~/code/casper/WCSPR/contracts/target/wasm32-unknown-unknown/release/pre_deposit.wasm \
    -a "cspr_amount:U512='100'" \
    -a "wcspr_contract_hash:string='hash-ca52b10cd3170652275d2ba16bbd6e3a48ac7098a3835feb314da1d0a372bf02'"
```    
```bash=
casper-client get-deploy a0f0f77cb89f5519cbf25c89e39c7d612cf38d28db2841ff41fa6caca667355b \
    -n http://135.181.214.240:7777
```