Skip to content

Commit

Permalink
Merge pull request #39 from emmanuelJet/master
Browse files Browse the repository at this point in the history
TUTORIAL UPDATE by @emmanuelJet
  • Loading branch information
Pegahbit committed Jul 17, 2019
2 parents 031e981 + 4ddb93b commit ae9a035
Showing 1 changed file with 86 additions and 25 deletions.
111 changes: 86 additions & 25 deletions deploy-with-init-params.md
@@ -1,17 +1,57 @@
# TUTORIAL: Deploying Sophia smart contracts with init parameters

## Tutorial Overview
This tutorial will walk you through the deployment of smart contracts with init parameters throug the use of forgae.

This tutorial will walk you through the deployment of smart contracts with init parameters through the use of forgae.

## Prerequisites
- You have completed [this tutorial](smart-contract-deployment-in-forgae.md) that teaches you how to deploy a contract without init parameters.

## Step 0. Run your forgae node
We would need a local node to compile and deploy on. The easiest option is to spawn one from forgae
You have completed [this tutorial](smart-contract-deployment-in-forgae.md) that teaches you how to deploy a contract without init parameters.

## Step 0: Create a project and Run your forgae node

We would need a local node to compile and deploy on. The easiest option is to spawn one from forgae. Follow the below steps to create a project, initilize the project and test the project using forgae on your CLI

```
Step 1: $ mkdir forgae-init
Step 2: $ cd forgae-init
Step 3: $ forgae init
Step 4: $ forgae node
```

### Check if you are on track

- Step 3 Output

```
===== Initializing ForgAE =====
===== Installing aepp-sdk =====
===== Installing ForgAE locally =====
===== Installing yarn locally =====
===== Creating project file & dir structure =====
===== Creating contracts directory =====
===== Creating tests directory =====
===== Creating integrations directory =====
===== Creating deploy directory =====
===== Creating docker directory =====
===== ForgAE was successfully initialized! =====
```

- Step 4 Output

```
forgae node
===== Starting node =====
........Creating forgae-init_node3_1 ...
Creating forgae-init_node1_1 ...
Creating forgae-init_proxy_1 ...
Creating forgae-init_node2_1 ...
..............
===== Node was successfully started =====
===== Funding default wallets =====
....
===== Default wallets was successfully funded! =====
```

Do not forget to stop it once you are done developing
Expand All @@ -20,51 +60,72 @@ Do not forget to stop it once you are done developing
forgae node --stop
```

## Step 1. Update your example project
Lets add some state and init parameters to our example contract.
## Step 1: Update your ExampleContract.aes

Lets add some state and init parameters to our example contract which can be found at **contracts/ExampleContract.aes**

```
contract ExampleContract =
record state =
{ savedNumber : int }
public stateful function init(num : int) =
{ savedNumber = num }
record state = { saved_string : string }
function init() = { saved_string = "aeternity" }
public function get_string() : string =
state.saved_string
public function savedNumber() : int = state.savedNumber
public stateful function register_string(word : string) =
put(state { saved_string = word})
```

As you can see the contract now has a state variable `savedNumber` of type int. The initial value will be passed by the init function. We've also added a read function for this value.
As you can see the contract now has a state variable `saved_string` of type `string`. The initial value will be passed by the init function. We've also added a read function `get_string` for this value and a write function `register_string` to write and change the value in `saved_string`.

Run forgae compile to verify that your contract compiles successfully

```
forgae compile
```

## Step 2. Change our default deploy script
Lets change our default deploy script to add parameters. The parameters of the init functions are always passed as tuple. Here is how our new deploy script deploy function looks like
## Step 2. Update your deploy.js

Lets add some parameters to our example deploy script which can be found at **deployment/deploy.js**. The parameters of the init functions are always passed as tuple. Here is how our new deploy script looks like

```
const deploy = async (network, privateKey) => {
let deployer = new Deployer(network, privateKey)
const Deployer = require('forgae-lib').Deployer;
const deploy = async (network, privateKey, compiler) => {
let deployer = new Deployer(network, privateKey, compiler)
let contract = await deployer.deploy("./contracts/ExampleContract.aes")
let contract = await deployer.deploy("./contracts/ExampleContract.aes", gasLimit, `(42)`)
// Getting savedString value in our ExampleContract
let get_string = await contract.call('get_string')
console.log(get_string.value)
let encodedSavedNumber = await contract.callStatic('savedNumber')
let decodedSavedNumber = await encodedSavedNumber.decode("int")
console.log(decodedSavedNumber.value) // 42
// Writing new value ('hello world') to our saved_string
await contractCall('register_string', 'hello world')
// Getting new saved_string value in our ExampleContract
let get_string2 = await contract.call('get_string')
console.log(get_string2.value)
};
module.exports = {
deploy
};
```

As you can see, we are now passing the initial value value of 42 as tuple string. *Note*: If you are passing string, do not forget to add quotes (`"`) around the string too (`("Some string")`). More than one parameter can be passed separated by coma (`("Some string", 42)`))
As you can see, we got our first initial value of `aeternity` then we are add a new value `hello world` as a tuple string and finally got the new value.

## Step 3. Run our deploy script

Running our deployment script with forgae is trivial. Just run :
```
forgae deploy
```
You will see in your terminal the value of the saved number - 42.

You will see in your terminal the value of the saved string - aeternity.

## Conclusion
Smart contracts are frequently in need of init params. Keep in mind the specifics of tuples and you will be able to successfully initialize your awesome Aeternity smart contracts.

The æternity team will keep this tutorial updated. If you encounter any problems please contact us through the [æternity Forum](https://forum.aeternity.com/c/development).
Smart contracts are frequently in need of init params. Keep in mind the specifics of tuples and you will be able to successfully initialize your awesome Aeternity smart contracts. The æternity team will keep this tutorial updated. If you encounter any problems please contact us through the [æternity Forum](https://forum.aeternity.com/c/development).

0 comments on commit ae9a035

Please sign in to comment.