Smart Contract #3 - Hello World Part 3

This smart contract illustrates how the owner of a smart contract is authorized to update the smart contract.

This smart contract demonstrates that only the smart contract owner, represented by the public key, is able to update the smart contract.

Steps

In a terminal session, trigger the following smart contract:

tymlez-sc publish --templateName=SC_HelloWorld_Part3 "Hello" "My Bot"

You should see the following output:

[INFO] commands.publish - publishing contract SC_HelloWorld_Part3....
[INFO] commands.publish - Signed tx...
[INFO] commands.publish - Post signed tx ....
[INFO] commands.publish - Smart Contract published successfully
[INFO] commands.publish - Contract id: c544928eae2ccc607e871ca284059312051732d12624f154061f5813beada6ea

Now, obtain the value stored in the contract memory of the smart contract:

tymlez-sc call --functionName=getContractMemory

You will see a contract ID and the value stored in the smart contract:

[INFO] commands.call - contract ID: c544928eae2ccc607e871ca284059312051732d12624f154061f5813beada6ea
[INFO] commands.call - { greeting: 'Hello',
robot_name: 'My Bot',
robot_owner: 'GBEYYYHN2aS2KBp5M4GvRhxEqMiXNRfZ4ShuWDwvgJXq' }

Now change the value stored in the memory:

tymlez-sc call --functionName=changeGreeting "Hey"

You should see the contract ID:

[INFO] commands.call - contract ID: c544928eae2ccc607e871ca284059312051732d12624f154061f5813beada6ea
[INFO] commands.call - Signed tx...
[INFO] commands.call - Post signed tx ....
[INFO] commands.call - Smart Contract updated !!!

Check if the value has been changed:

tymlez-sc call --functionName=getContractMemory

You should see the following output:

{ greeting: 'Hey',
robot_name: 'My Bot',
robot_owner: 'GBEYYYHN2aS2KBp5M4GvRhxEqMiXNRfZ4ShuWDwvgJXq' }

In the next step, we'll change the owner. This is done by adding a new keypair (hence adding a new owner) to your .tymlez/config.json.

Clone your existing default environment:

tymlez-dev config cloneContext --source=cloud --target=cloud1

Now, set the default context to the newly cloned environment:

tymlez-dev config setContext --defaultContext=cloud1

Generate a new keypair. This keypair represents the new owner:

tymlez-dev config generateKeyPair

Now, change the value again:

tymlez-sc call --functionName=changeGreeting "Hola"

You should see the following output:

[ERROR] commands.call - Only contract owner is authorized to update the state.

Here, the changeGreeting command is rejected, since the owner of the contract has been changed.

To be able to change the greeting, you'll have to change the owner to the original owner of the contract.

To do this, return to the original default environment:

tymlez-dev config setContext --defaultContext=cloud

Now, execute the changeGreeting function again:

tymlez-sc call --functionName=changeGreeting "Hola"

You should now see the following output:

[INFO] commands.call - contract ID: 86104fdc364c3790ab2cf0c14f16f7f9dcd512d93f3eb99303b01e43523a5c3a
[INFO] commands.call - Signed tx...
[INFO] commands.call - Post signed tx ....
[INFO] commands.call - Smart Contract updated !!!

Make a final call to see the updated value stored in the contract memory:

tymlez-sc call --functionName=getContractMemory

Return to the smart contracts main page