From 0e7137ebaabe8e7099b6b7360c6e0c2ff0d6c4e6 Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph <32677156+jet774@users.noreply.github.com> Date: Tue, 16 Jul 2019 17:19:20 +0100 Subject: [PATCH 1/4] TUTORIAL UPDATE by @emmanuelJet: Deploying Sophia Smart Contract with Init parameters --- deploy-with-init-params.md | 126 +++++++++++++++++++++++++++++-------- 1 file changed, 101 insertions(+), 25 deletions(-) diff --git a/deploy-with-init-params.md b/deploy-with-init-params.md index 1c1a301..36e0a63 100644 --- a/deploy-with-init-params.md +++ b/deploy-with-init-params.md @@ -1,17 +1,72 @@ # 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 throug 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 ``` -forgae node +===== 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 + +``` +.Creating forgae-init_node2_1 ... +Creating forgae-init_node3_1 ... +Creating forgae-init_proxy_1 ... +Creating forgae-init_node1_1 ... + +..v2.1.0: Pulling from aeternity/aesophia_http + +.898c46f3b1a1: Pulling fs layer +..... + +63366dfa0a50: Verifying Checksum +63366dfa0a50: Download complete +..... + +...898c46f3b1a1: Pull complete +63366dfa0a50: Pull complete +..... + +...898c46f3b1a1: Pull complete +63366dfa0a50: Pull complete +..... + +Digest: sha256:b10c43af93cf702e50dc490b43771ef6fb89a764d990916505a7bfea72a0614d +Status: Downloaded newer image for aeternity/aesophia_http:v2.1.0 +..... + + ``` Do not forget to stop it once you are done developing @@ -20,20 +75,25 @@ 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 = { savedString : string } + + function init() = { savedString = "aeternity" } - public function savedNumber() : int = state.savedNumber + public function getString() : string = + state.savedString + + public stateful function registerString(word : string) = + put(state { savedString = 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 `savedString` of type `string`. The initial value will be passed by the init function. We've also added a read function `getString` for this value and a write function `registerString` to write and change the value in `savedString`. Run forgae compile to verify that your contract compiles successfully @@ -41,30 +101,46 @@ 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; - let contract = await deployer.deploy("./contracts/ExampleContract.aes", gasLimit, `(42)`) +const deploy = async (network, privateKey, compiler) => { + let deployer = new Deployer(network, privateKey, compiler) - let encodedSavedNumber = await contract.callStatic('savedNumber') - let decodedSavedNumber = await encodedSavedNumber.decode("int") - console.log(decodedSavedNumber.value) // 42 + let contract = await deployer.deploy("./contracts/ExampleContract.aes") + + // Getting savedString value in our ExampleContract + let getString = await contract.callStatic('getString') + console.log(getString.value) + + // Writing new value ('hello world') to our savedString + await contractCall('registerString', 'hello world') + + // Getting new savedString value in our ExampleContract + let getString2 = await contract.callStatic('getString') + console.log(getString2.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). \ No newline at end of file +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). From fe78ccbfa0ac8b93eafcd37778bfcbb461dbf718 Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph <32677156+jet774@users.noreply.github.com> Date: Tue, 16 Jul 2019 20:53:44 +0100 Subject: [PATCH 2/4] TUTORIAL UPDATE by @emmanuelJet: Deploying Sophia Smart Contract with Init parameters --- deploy-with-init-params.md | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/deploy-with-init-params.md b/deploy-with-init-params.md index 36e0a63..3a98f45 100644 --- a/deploy-with-init-params.md +++ b/deploy-with-init-params.md @@ -40,33 +40,18 @@ Step 4: $ forgae node - Step 4 Output ``` -.Creating forgae-init_node2_1 ... -Creating forgae-init_node3_1 ... -Creating forgae-init_proxy_1 ... +===== Starting node ===== +........Creating forgae-init_node3_1 ... Creating forgae-init_node1_1 ... +Creating forgae-init_proxy_1 ... +Creating forgae-init_node2_1 ... +.............. -..v2.1.0: Pulling from aeternity/aesophia_http - -.898c46f3b1a1: Pulling fs layer -..... - -63366dfa0a50: Verifying Checksum -63366dfa0a50: Download complete -..... - -...898c46f3b1a1: Pull complete -63366dfa0a50: Pull complete -..... - -...898c46f3b1a1: Pull complete -63366dfa0a50: Pull complete -..... - -Digest: sha256:b10c43af93cf702e50dc490b43771ef6fb89a764d990916505a7bfea72a0614d -Status: Downloaded newer image for aeternity/aesophia_http:v2.1.0 -..... - +===== Node was successfully started ===== +===== Funding default wallets ===== +.... +===== Default wallets was successfully funded! ===== ``` Do not forget to stop it once you are done developing From c6d872cb63db337447e67a2fe76e56b2584e1f71 Mon Sep 17 00:00:00 2001 From: "Emmanuel Joseph (Jet)" Date: Wed, 17 Jul 2019 09:46:11 +0100 Subject: [PATCH 3/4] TUTORIAL UPDATE by @emmanuelJet --- deploy-with-init-params.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/deploy-with-init-params.md b/deploy-with-init-params.md index 3a98f45..1297857 100644 --- a/deploy-with-init-params.md +++ b/deploy-with-init-params.md @@ -2,7 +2,7 @@ ## 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 @@ -99,14 +99,14 @@ const deploy = async (network, privateKey, compiler) => { let contract = await deployer.deploy("./contracts/ExampleContract.aes") // Getting savedString value in our ExampleContract - let getString = await contract.callStatic('getString') + let getString = await contract.call('getString') console.log(getString.value) // Writing new value ('hello world') to our savedString await contractCall('registerString', 'hello world') // Getting new savedString value in our ExampleContract - let getString2 = await contract.callStatic('getString') + let getString2 = await contract.call('getString') console.log(getString2.value) }; From 4ddb93b470f1a832ea4fd882ec85782c1b2191cd Mon Sep 17 00:00:00 2001 From: "Emmanuel Joseph (Jet)" Date: Wed, 17 Jul 2019 09:54:20 +0100 Subject: [PATCH 4/4] TUTORIAL UPDATE by @emmanuelJet Renamed variables and function names from pascalCase to snake case --- deploy-with-init-params.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/deploy-with-init-params.md b/deploy-with-init-params.md index 1297857..31c1e1e 100644 --- a/deploy-with-init-params.md +++ b/deploy-with-init-params.md @@ -67,18 +67,18 @@ Lets add some state and init parameters to our example contract which can be fou ``` contract ExampleContract = - record state = { savedString : string } + record state = { saved_string : string } - function init() = { savedString = "aeternity" } + function init() = { saved_string = "aeternity" } - public function getString() : string = - state.savedString + public function get_string() : string = + state.saved_string - public stateful function registerString(word : string) = - put(state { savedString = word}) + public stateful function register_string(word : string) = + put(state { saved_string = word}) ``` -As you can see the contract now has a state variable `savedString` of type `string`. The initial value will be passed by the init function. We've also added a read function `getString` for this value and a write function `registerString` to write and change the value in `savedString`. +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 @@ -99,15 +99,15 @@ const deploy = async (network, privateKey, compiler) => { let contract = await deployer.deploy("./contracts/ExampleContract.aes") // Getting savedString value in our ExampleContract - let getString = await contract.call('getString') - console.log(getString.value) + let get_string = await contract.call('get_string') + console.log(get_string.value) - // Writing new value ('hello world') to our savedString - await contractCall('registerString', 'hello world') + // Writing new value ('hello world') to our saved_string + await contractCall('register_string', 'hello world') - // Getting new savedString value in our ExampleContract - let getString2 = await contract.call('getString') - console.log(getString2.value) + // Getting new saved_string value in our ExampleContract + let get_string2 = await contract.call('get_string') + console.log(get_string2.value) }; module.exports = {