# Contract Quick Start
## Overview
In this tutorial you'll get up and running with CENNZnet smart contracts fast.
We'll deploy a pre-compiled smart contract using the CENNZnet client tool (`cennz-cli`).
You'll gain familiarity working with a live CENNZnet chain and the tools involved.
For a more in-depth look into writing a smart contract on CENNZnet try [this tutorial](https://cennznetdocs.com/CENNZNet/tutorials/spin2win.md)
# Pre-requisites
You should have some familiarity with javascript and installed the nodejs runtime to get the most out of using `cennz-cli`.
# Go-!
1. Follow the guide [here](https://hackmd.io/s1WOcG9PRtOqwyKewXuuDQ) to setup a live test network using our blockchain as a service (BaaS) portal.
Take note of the public websocket URL, which we'll use to interact with the testnet.
2. Download the exemplary smart contract files:
- [contract wasm binary](https://github.com/cennznet/spin2win/releases/download/1.0.0/spin2win.wasm)
- [contract ABI JSON](https://github.com/cennznet/spin2win/releases/download/1.0.0/Spin2Win.json)
3. Install and configure the CENNZnet sdk (requires nodejs)
Open a terminal and run the following
```bash
npm i -g @cennznet/cli
```
Setup a local wallet. Choose no password if prompted.
```
cennz-cli wallet:create
```
Add your OnFinality admin account to the wallet
```
cennz-cli wallet:add --seedHex=<admin key hex>
```
It will output the added account address e.g.
> account 5HmAyfAC4zmmNgKXPvGjUG9ZrRy3ZxnMJT3ZTmL8V7UcpEhW is added
Lastly, install handy helper scripts
```
cennz-cli script:update
```
### Store the contract code on chain
The first step for using smart contracts on CENNZnet is to store the code on chain AKA `putCode`
- Replace `ws://example.com:9944` with the websocket address of your OnFinality network
- Replace `5HmAyfAC4zmmNgKXPvGjUG9ZrRy3ZxnMJT3ZTmL8V7UcpEhW` with the admin account/address from your testnet
Run:
```bash
cennz-cli script:run \
-c ws://example.com:9944 \
contract-deploy \
</path/to/contract/wasm> 5HmAyfAC4zmmNgKXPvGjUG9ZrRy3ZxnMJT3ZTmL8V7UcpEhW
```
Example Output
```bash
contract code hash: 0xeaa938b74aed89cc346530a314047bbc05469c31ec13809d36d6bc298806fa5e
ExtrinsicSuccess
```
Keep the `code hash` for use in the next step.
### Initialize the contract
The second step is to call the contract constructor. This will assign an address to the contract and makes it "alive" on chain.
- Replace `ws://example.com:9944` with the ws address of your node
- Replace `5HmAyfAC4zmmNgKXPvGjUG9ZrRy3ZxnMJT3ZTmL8V7UcpEhW` with the root/admin account ID from your testnet
- Replace `0xeaa938b74aed89cc346530a314047bbc05469c31ec13809d36d6bc298806fa5e` with the code hash given in previous step
Run:
```bash
cennz-cli script:run \
-c ws://example.com:9944 \
contract-instantiate \
5HmAyfAC4zmmNgKXPvGjUG9ZrRy3ZxnMJT3ZTmL8V7UcpEhW \ # user account
0xeaa938b74aed89cc346530a314047bbc05469c31ec13809d36d6bc298806fa5e \ # code hash
</path/to/contract/abi.json> \
10000 \ # endowment
20000 # gas fee
```
Example Output:
```
contract addr: 5DWJ3xBVZLvSxX8baKhkdnS1QLohWWCYWdcH8so73aWr6WUM
```
Note your contract address will be slightly different. This is because the address is a function of the account ID used, among other things, which will be different on your testnet.
If you see the extrinsic failed, try increasing the gas amount by increments of `10000`.
Often times a contract extrinsic will fail if not enough gas is supplied.
### Call the smart contract
At this stage the contract is alive on chain and ready for use. We'll use the cennznet client to call the contract.
We need an additional package to handle type mappings between the contract wasm and JS (ABI).
Install the dependencies like so:
```
npm install @polkadot/api-contract bs58
```
Now create a js file locally (`call.js`) with the following content,
(Make sure to replace `ADMIN` address,`CONTRACT_TO_CALL` address, and `ABI_JSON_PATH` with your own
```js
// call.js
const fs = require('fs');
const contractApi = require('@polkadot/api-contract');
const CONTRACT_TO_CALL = '5D7e2QgKu2efvAhr6xNeBwhj8TGWykFaUcfeiTCMQrmfP59i';
const ADMIN = '5HmAyfAC4zmmNgKXPvGjUG9ZrRy3ZxnMJT3ZTmL8V7UcpEhW';
const ABI_JSON_PATH = "</path/to/abi.json>";
const abi = new contractApi.Abi(
JSON.parse(fs.readFileSync(ABI_JSON_PATH))
);
// Use our wallet to sign extrinsics by default.
// It sould have the admin keypair stored from earlier.
const wallet = await loadWallet();
api.setSigner(wallet);
let resp = await api.tx.contract.call(
CONTRACT_TO_CALL, // Contract Address
0, // Input fee
50000, // Gas fee
abi.messages.spin(ADMIN) // Contract method data
).signAndSend(ADMIN);
console.log("Sent tx with hash: " + resp.toHex());
```
Now run the script with `cennz-cli`
```bash
cennz-cli repl -c ws://example.com:9944 -p call.js
```
Sweet success!
> Sent tx with hash: 0xcbe4c75c07cdef2b2041670aec6eeacab5b29ac9c3e515e486d222adbd70cd83
Try sending a few more and head to [https://cennznet.js.org/cennznet-ui/#/explorer](https://cennznet.js.org/cennznet-ui/#/accounts) and you will see `contract.Call` events logged on chain.
You successfully deployed a smart contract on a live blockchain network and called it with the CENNZnet SDK.
Good stuff. What's next?