Skip to content

Latest commit

 

History

History
70 lines (43 loc) · 2.59 KB

Chapter 2 Day 2.md

File metadata and controls

70 lines (43 loc) · 2.59 KB

Quest Chapter 2 Day 2 ✅

1. Explain why we wouldn't call changeGreeting in a script.

You can't call changeGreeting in a script because the script tab is only for reading the contract, not for modifying the contract. Calling functions (making the function run) that modify the code in the contract are done in the transactions tab.

2. What does the AuthAccount mean in the prepare phase of the transaction?

In the prepare phase of the transaction, AuthAccount means that it's allowing the smart contract to access data/information in a user's account/address. This occurs when a user signs a transaction in their FLOW wallet.

3. What is the difference between the prepare phase and the execute phase in the transaction?

The prepare phase (1st phase) of the transaction allows the smart contract ot access information and data in a user's account, and the execute phase (2nd phase) of the transaction calls functions to change data in a user's account/address on the blockchain.

4. This is the hardest quest so far, so if it takes you some time, do not worry! I can help you in the Discord if you have questions.

  • Add two new things inside your contract:

    • A variable named myNumber that has type Int (set it to 0 when the contract is deployed)
    • A function named updateMyNumber that takes in a new number named newNumber as a parameter that has type Int and updates myNumber to be newNumber

image

pub contract HelloWorld {

    pub var myNumber: Int
    
    pub fun updateMyNumber(newNumber: Int) {
      self.myNumber = newNumber
    }

    init() {
    self.myNumber = 0
    }
}
  • Add a script that reads myNumber from the contract

script 1

import HelloWorld from 0x01

pub fun main (): Int {
    return HelloWorld.myNumber
}
  • Add a transaction that takes in a parameter named myNewNumber and passes it into the updateMyNumber function. Verify that your number changed by running the script again.

transaction ch2d2

import HelloWorld from 0x01

transaction(myNewNumber: Int) {

  prepare(signer: AuthAccount) {}

  execute {
    HelloWorld.updateMyNumber(newNumber: myNewNumber)
  }
}

script 2