From ff123cfa33f9d89cac37b372c2fccec7e07663b2 Mon Sep 17 00:00:00 2001 From: Tim McMackin Date: Fri, 16 Feb 2024 12:49:54 -0500 Subject: [PATCH 1/4] Topic about sources of information available to developers and dApps (#313) * Topic about sources of information available to developers and dApps * Can get current balance --- docs/developing/information.md | 48 ++++++++++++++++++++++++++++++++++ sidebars.js | 8 +++--- 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 docs/developing/information.md diff --git a/docs/developing/information.md b/docs/developing/information.md new file mode 100644 index 000000000..03ad803f1 --- /dev/null +++ b/docs/developing/information.md @@ -0,0 +1,48 @@ +--- +title: Getting information about the blockchain +authors: Tim McMackin +last_update: + date: 8 February 2024 +--- + +Developers and dApps can get information about the Tezos blockchain, such as account balances, from these sources: + +## The Octez client + +The [The Octez client](./octez-client) provides information about accounts, addresses, and many other things. +For example, you can get the balance of an account with this command: + +```bash +octez-client get balance for tz1QCVQinE8iVj1H2fckqx6oiM85CNJSK9Sx +``` + +## The RPC protocol + +The [RPC](../architecture/rpc) protocol provides information about the blockchain that nodes use to communicate with each other. +This data is not always in the format that developers and dApps need. +For example, the RPC interface does not provide a way to get information about a specific operation by its hash. + +You can get some information about accounts, contracts, and other things from RPC requests. +For example, this RPC request gets the current balance of an account: + +```bash +curl -X GET https://rpc.ghostnet.teztnets.com/chains/main/blocks/head/context/contracts/tz1QCVQinE8iVj1H2fckqx6oiM85CNJSK9Sx/balance +``` + +## Indexers + +Indexers are off-chain applications that retrieve blockchain data, process it, and store it in a way that makes it easier to search and use. +For example, you can use the [TZKT API](https://api.tzkt.io/) to get the recent operations an account made with this request: + +```bash +curl -X GET https://api.ghostnet.tzkt.io/v1/accounts/tz1QCVQinE8iVj1H2fckqx6oiM85CNJSK9Sx/operations +``` + +For more information, see [Indexers](./information/indexers). + +## Block explorers + +Block explorers use data from indexers to show information in a human-friendly interface. +For example, this link shows information about a contract, including its current storage, entrypoints, and transaction history: https://better-call.dev/ghostnet/KT1R4i4qEaxF7v3zg1M8nTeyrqk8JFmdGLuu/operations + +For more information about block explorers, see [Block explorers](./information/block-explorers). diff --git a/sidebars.js b/sidebars.js index ea244c79c..790e5f373 100644 --- a/sidebars.js +++ b/sidebars.js @@ -88,10 +88,10 @@ const sidebars = { { type: 'category', label: 'Getting information about the blockchain', - // link: { // TODO - // id: 'developing/information', - // type: 'doc', - // }, + link: { + id: 'developing/information', + type: 'doc', + }, items: [ { type: 'category', From b38e8c98643a64ddde7d6e733243b0159c090b3c Mon Sep 17 00:00:00 2001 From: Tim McMackin Date: Mon, 19 Feb 2024 14:37:55 -0500 Subject: [PATCH 2/4] DAL tutorial updates (#321) * tip about installing nano * how to get into the Docker container * how to get the name * this is part 3 * clarify shebang * clarify adding dependencies * hide network message * last updated dates --- .../get-dal-params.md | 15 +++++++++---- .../get-slot-info.md | 12 ++++++++-- .../set-up-environment.md | 22 ++++++++++++++++++- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/docs/tutorials/build-files-archive-with-dal/get-dal-params.md b/docs/tutorials/build-files-archive-with-dal/get-dal-params.md index 1ba332ea7..4fd156bcb 100644 --- a/docs/tutorials/build-files-archive-with-dal/get-dal-params.md +++ b/docs/tutorials/build-files-archive-with-dal/get-dal-params.md @@ -2,7 +2,7 @@ title: "Part 2: Getting the DAL parameters" authors: 'Tezos Core Developers' last_update: - date: 9 February 2024 + date: 14 February 2024 --- The Data Availability Layer stores information about the available data in layer 1 blocks. @@ -55,6 +55,8 @@ To get the DAL parameters, you can use built-in functions in the Tezos [Rust SDK As a reminder, the kernel of a Smart Rollup is a WASM program. The `proto-alpha` feature is necessary to get access to the functions specific to the DAL because they are not yet released in the main version of the Smart Rollup toolkit. + If you need a text editor inside the Docker container, you can run `sudo apk add nano` to install the [Nano text editor](https://www.nano-editor.org/). + 1. Create a file named `src/lib.rs` to be the kernel. 1. In the `src/lib.rs` file, add this code: @@ -117,7 +119,11 @@ Follow these steps to deploy the Smart Rollup to Weeklynet and start a node: For simplicity, this command runs the Smart Rollup in observer mode, which does not require a stake of 10,000 tez to publish commitments. -1. Open a new terminal window and run this command to watch the node's log: +1. Open a new terminal window in the same environment. +If you are using a Docker container, you can enter the container with the `docker exec` command, as in `docker exec -it my-image /bin/sh`. +To get the name of the Docker container, you run the `docker ps` command. + +1. Run this command to watch the node's log: ```bash tail -F _rollup_node/kernel.log @@ -178,7 +184,8 @@ Then you can run it any tme you update the `lib.rs` or `Cargo.toml` files to dep ``` If you run this script and see an error that says that the file was not found, update the first line of the script (the shebang) to the path to your shell interpreter. -For example, if you are using the Tezos Docker image, the path is `/bin/sh`. +For example, if you are using the Tezos Docker image, the path is `/bin/sh`, so the first line becomes `#!/bin/sh`. +Then try the command `./deploy_smart_rollup.sh $MY_ACCOUNT` again. In the next section, you will get information about the state of slots in the DAL. -See [Part 2: Getting slot information](./get-slot-info). +See [Part 3: Getting slot information](./get-slot-info). diff --git a/docs/tutorials/build-files-archive-with-dal/get-slot-info.md b/docs/tutorials/build-files-archive-with-dal/get-slot-info.md index 3b516ccbd..798b71228 100644 --- a/docs/tutorials/build-files-archive-with-dal/get-slot-info.md +++ b/docs/tutorials/build-files-archive-with-dal/get-slot-info.md @@ -1,8 +1,8 @@ --- -title: "Part 2: Getting slot information" +title: "Part 3: Getting slot information" authors: 'Tezos Core Developers' last_update: - date: 7 February 2024 + date: 14 February 2024 --- When clients send data to the DAL, they must choose which slot to put it in. @@ -114,6 +114,14 @@ Follow these steps to update the Smart Rollup to access information about slot 0 tezos-smart-rollup-host = { version = "0.2.2", features = [ "proto-alpha" ] } ``` + The end of the file looks like this: + + ```toml + [dependencies] + tezos-smart-rollup = { version = "0.2.2", features = [ "proto-alpha" ] } + tezos-smart-rollup-host = { version = "0.2.2", features = [ "proto-alpha" ] } + ``` + 1. Stop the Smart Rollup process. 1. Run the commands to build and deploy the Smart Rollup and start the node. diff --git a/docs/tutorials/build-files-archive-with-dal/set-up-environment.md b/docs/tutorials/build-files-archive-with-dal/set-up-environment.md index 9f469160b..4c764f336 100644 --- a/docs/tutorials/build-files-archive-with-dal/set-up-environment.md +++ b/docs/tutorials/build-files-archive-with-dal/set-up-environment.md @@ -2,7 +2,7 @@ title: "Part 1: Setting up an environment" authors: 'Tim McMackin' last_update: - date: 9 February 2024 + date: 14 February 2024 --- Because Weeklynet requires a specific version of the Octez suite, you can't use most wallet applications and installations of the Octez suite with it. @@ -29,6 +29,18 @@ To set up an environment and account in a Docker container, follow these steps: The image tag in this command changes each time the network is reset. + :::tip + If you're not used to working inside Docker containers, you can map a folder on your computer to a folder in the container to create a [Docker volume](https://docs.docker.com/storage/volumes/). + This way, you can edit files on your computer and the changes will appear on the files inside the container. + For example, to start a container and map the current folder to the `/home/tezos` folder in the container, run this command: + + ```bash + docker run -it --entrypoint=/bin/sh -v .:/home/tezos tezos/tezos:master_7f3bfc90_20240116181914 + ``` + + You can map a folder like this only when you create a container; you cannot add it later. + ::: + 1. Copy the URL of the public RPC endpoint for Weeklynet, such as `https://rpc.weeklynet-2024-01-17.teztnets.com`. This endpoint also changes each time the network is reset. @@ -47,6 +59,14 @@ The parts of the Octez suite don't use this environment variable directly, but y octez-client -E $ENDPOINT config init ``` +1. Optional: Hide the network warning message by running this command: + + ```bash + export TEZOS_CLIENT_UNSAFE_DISABLE_DISCLAIMER=y + ``` + + This command suppresses the message that your instance of the Octez client is not using Mainnet. + 1. Create an account with the command `octez-client gen keys $MY_ACCOUNT`, where `$MY_ACCOUNT` is an alias for your account. 1. Get the public key hash of the new account by running the command `octez-client show address $MY_ACCOUNT`. From 0380bf0b445dd2914ec02ad150a68798453230bb Mon Sep 17 00:00:00 2001 From: Tim McMackin Date: Mon, 19 Feb 2024 14:38:27 -0500 Subject: [PATCH 3/4] DAL baker tutorial updates (#323) * hide network message * make this into steps * new terminal * add stake command --- docs/tutorials/join-dal-baker/get-octez.md | 10 +- .../join-dal-baker/prepare-account.md | 104 +++++++++++------- docs/tutorials/join-dal-baker/run-dal-node.md | 4 +- 3 files changed, 78 insertions(+), 40 deletions(-) diff --git a/docs/tutorials/join-dal-baker/get-octez.md b/docs/tutorials/join-dal-baker/get-octez.md index 3b8a1bcc6..b7a9471a4 100644 --- a/docs/tutorials/join-dal-baker/get-octez.md +++ b/docs/tutorials/join-dal-baker/get-octez.md @@ -2,7 +2,7 @@ title: "Step 1: Get a Weeklynet-compatible Octez version" authors: Tezos core developers last_update: - date: 23 January 2024 + date: 14 February 2024 --- The Weeklynet test network restarts every Wednesday at 0h UTC, and for most of its lifetime (from level 512) it runs a development version of the Tezos protocol, called Alpha, which is not part of any released version of Octez. @@ -40,5 +40,13 @@ This endpoint also changes each time the network is reset. octez-client -E https://rpc.weeklynet-2024-01-17.teztnets.com config init ``` +1. Optional: Hide the network warning message by running this command: + + ```bash + export TEZOS_CLIENT_UNSAFE_DISABLE_DISCLAIMER=y + ``` + + This command suppresses the message that your instance of the Octez client is not using Mainnet. + Now you have the Octez client and node configured to work with Weeklynet. The next step is to start an Octez node; continue to [Step 2: Run an Octez node on Weeklynet](./run-node). diff --git a/docs/tutorials/join-dal-baker/prepare-account.md b/docs/tutorials/join-dal-baker/prepare-account.md index 0dbe465f2..4645d7260 100644 --- a/docs/tutorials/join-dal-baker/prepare-account.md +++ b/docs/tutorials/join-dal-baker/prepare-account.md @@ -2,64 +2,92 @@ title: "Step 3: Set up a baker account on Weeklynet" authors: Tezos core developers last_update: - date: 23 January 2024 + date: 14 February 2024 --- Our baker needs a user account consisting of a pair of keys and an address. -The simplest way to get an account that works with Weeklynet is to use the Octez client to randomly generate them and associate them to the `my_baker` alias: -```bash -octez-client gen keys my_baker -``` +1. Open a new terminal window in the same environment. +If you are using a Docker container, you can enter the container with the `docker exec` command, as in `docker exec -it my-image /bin/sh`. +To get the name of the Docker container, you run the `docker ps` command. -The address of the generated account can be obtained with the following command: +1. Create or import an account in the Octez client. +The simplest way to get an account that works with Weeklynet is to use the Octez client to randomly generate an account. +This command creates an account and associates it with the `my_baker` alias: -```bash -octez-client show address my_baker -``` + ```bash + octez-client gen keys my_baker + ``` -Let's record this address in a shell variable, this will be useful for the some commands which cannot get addresses by their octez-client aliases. + The address of the generated account can be obtained with the following command: -```bash -MY_BAKER="$(octez-client show address my_baker | head -n 1 | cut -d ' ' -f 2)" -``` + ```bash + octez-client show address my_baker + ``` -At this point, the balance of the `my_baker` account is still empty as can be seen with the following command: +1. Record this address in a shell variable so you can use it for commands that cannot get addresses by their Octez client aliases: -```bash -octez-client get balance for my_baker -``` + ```bash + MY_BAKER="$(octez-client show address my_baker | head -n 1 | cut -d ' ' -f 2)" + ``` -In order to get some consensus and DAL rights, we need to put some tez in the account. Fortunately, getting free testnet tez is easy thanks to the testnet faucet. To use it, we need to enter the generated address in the Weeklynet faucet linked from https://teztnets.com/weeklynet-about. We need at least 6k tez for running a baker but the more tez we have the more rights we will get and the shorter we will have to wait to produce blocks and attestations. That being said, baking with too much stake prevents us from leaving the network without disturbing or even halting it so to avoid breaking the network for all other testers let's not be too greedy. 50k tez is enough to get enough rights to easily check if our baker behaves as expected while not disturbing the network too much when our baker stops operating. + At this point, the balance of the `my_baker` account is still zero, as you can see by running this command: -We can verify that the faucet sent the tez to the account with the same `get balance` command: + ```bash + octez-client get balance for my_baker + ``` -```bash -octez-client get balance for my_baker -``` +1. Get some tez from the Weeklynet faucet. -At this point, the `my_baker` account owns enough stake to bake but has still no consensus or DAL rights because we haven't declared our intention to become a baker to the Tezos protocol. This can be achieved with the following command: + In order to get some consensus and DAL rights, we need to put some tez in the account. Fortunately, getting free testnet tez is easy thanks to the testnet faucet. To use it, we need to enter the generated address in the Weeklynet faucet linked from https://teztnets.com/weeklynet-about. We need at least 6k tez for running a baker but the more tez we have the more rights we will get and the shorter we will have to wait to produce blocks and attestations. That being said, baking with too much stake prevents us from leaving the network without disturbing or even halting it so to avoid breaking the network for all other testers let's not be too greedy. 50k tez is enough to get enough rights to easily check if our baker behaves as expected while not disturbing the network too much when our baker stops operating. -```bash -octez-client register key my_baker as delegate -``` +1. Verify that the faucet sent the tez to the account with the same `get balance` command: -Seven cycles later (about 1h40 on Weeklynet), our baker will start receiving rights. To see for instance its consensus attestation rights in the current cycle, we can use the following RPC call: + ```bash + octez-client get balance for my_baker + ``` -```bash -octez-client rpc get /chains/main/blocks/head/helpers/attestation_rights\?delegate="$MY_BAKER" -``` + At this point, the `my_baker` account owns enough stake to bake but has still no consensus or DAL rights because we haven't declared our intention to become a baker to the Tezos protocol. -To see the DAL attestation rights of all bakers, we can use the following RPC call: +1. Register your account as a delegate by running the following command: -```bash -octez-client rpc get /chains/main/blocks/head/context/dal/shards -``` + ```bash + octez-client register key my_baker as delegate + ``` -This command returns an array of DAL attestation rights. The 2048 shards which are expected to be attested at this level are shared between active bakers proportionally to their stake. Each baker is assigned a slice of shard indices represented in the output of this command by a pair consisting of the first index and the length of the slice. So to check if some rights were assigned to us we can filter the array to our baker by running this command: +1. Stake the tez, saving a small amount for transaction fees. +For example, if your account has 50k tez, stake 49990 tez by running this command: -```bash -octez-client rpc get /chains/main/blocks/head/context/dal/shards | grep "$MY_BAKER" -``` + ```bash + octez-client stake 49990 for my_baker + ``` + + Seven cycles later (about 1h40 on Weeklynet), our baker will start receiving rights. To see for instance its consensus attestation rights in the current cycle, we can use the following RPC call: + + ```bash + octez-client rpc get /chains/main/blocks/head/helpers/attestation_rights\?delegate="$MY_BAKER" + ``` + + When your baker has attestation rights, the previous command returns information about them, as in this example: + + ```json + [ { "level": 9484, + "delegates": + [ { "delegate": "tz1Zs6zjxtLxmff51tK2AVgvm4PNmdNhLcHE", + "first_slot": 280, "attestation_power": 58, + "consensus_key": "tz1Zs6zjxtLxmff51tK2AVgvm4PNmdNhLcHE" } ] } ] + ``` + + To see the DAL attestation rights of all bakers, we can use the following RPC call: + + ```bash + octez-client rpc get /chains/main/blocks/head/context/dal/shards + ``` + + This command returns an array of DAL attestation rights. The 2048 shards which are expected to be attested at this level are shared between active bakers proportionally to their stake. Each baker is assigned a slice of shard indices represented in the output of this command by a pair consisting of the first index and the length of the slice. So to check if some rights were assigned to us we can filter the array to our baker by running this command: + + ```bash + octez-client rpc get /chains/main/blocks/head/context/dal/shards | grep "$MY_BAKER" + ``` When attestation rights are assigned to your baker, continue to [Step 4: Run an Octez DAL node on Weeklynet](./run-dal-node.md). diff --git a/docs/tutorials/join-dal-baker/run-dal-node.md b/docs/tutorials/join-dal-baker/run-dal-node.md index 077f6d1b8..e82c25cb7 100644 --- a/docs/tutorials/join-dal-baker/run-dal-node.md +++ b/docs/tutorials/join-dal-baker/run-dal-node.md @@ -5,7 +5,9 @@ last_update: date: 23 January 2024 --- -Start the DAL node by running this command: +The DAL node is responsible for temporarily storing data and providing it to bakers and Smart Rollups. + +To start the DAL node, open a new terminal window in the same environment and run this command: ```bash octez-dal-node run >> "$HOME/octez-dal-node.log" 2>&1 From 4af160bbd099417a76cef523cef243535c3d25f3 Mon Sep 17 00:00:00 2001 From: Tim McMackin Date: Tue, 20 Feb 2024 12:13:18 -0500 Subject: [PATCH 4/4] Update SmartPy examples for SmartPy 19.0 (#324) * @sp.add_test() no longer takes a param * Have to cast the param now for some reason * typo * The simulation includes compilation * No longer needed --- docs/developing/testing.md | 4 +-- docs/smart-contracts/events.md | 4 +-- docs/smart-contracts/languages/smartpy.mdx | 29 +++++++------------- docs/tutorials/smart-contract/smartpy.mdx | 31 +++++++++++++++++----- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/docs/developing/testing.md b/docs/developing/testing.md index 8345cb4c7..3de4e5690 100644 --- a/docs/developing/testing.md +++ b/docs/developing/testing.md @@ -38,10 +38,10 @@ The following SmartPy test code snippet is for a Tezos smart contract that acts ```bash if "templates" not in __name__: - @sp.add_test(name="Calculator") + @sp.add_test() def test(): c1 = main.Calculator() - scenario = sp.test_scenario(main) + scenario = sp.test_scenario("Calculator") scenario.h1("Calculator") scenario += c1 c1.multiply(x=2, y=5) diff --git a/docs/smart-contracts/events.md b/docs/smart-contracts/events.md index a1f01f2f4..dded545be 100644 --- a/docs/smart-contracts/events.md +++ b/docs/smart-contracts/events.md @@ -72,10 +72,10 @@ def main(): if "templates" not in __name__: - @sp.add_test(name="Events") + @sp.add_test() def test(): c1 = main.Events(12) - scenario = sp.test_scenario(main) + scenario = sp.test_scenario("Events", main) scenario.h1("Add") scenario += c1 c1.add(2).run( diff --git a/docs/smart-contracts/languages/smartpy.mdx b/docs/smart-contracts/languages/smartpy.mdx index e55e8cf2d..36388e62f 100644 --- a/docs/smart-contracts/languages/smartpy.mdx +++ b/docs/smart-contracts/languages/smartpy.mdx @@ -55,17 +55,6 @@ sp.verify(self.data.x > 2) Here, `sp.verify()` checks that the field `x` is larger than `2` and raises an error if it is not. This is performed at run time, i.e., in the blockchain, once translated into Michelson. -Since Python does not allow its control statements to be overloaded, certain language constructs are desugared by a pre-processor: `sp.if`, `sp.else`, `sp.for`, `sp.while` are SmartPy commands. - -For example, we will use: - -```python -sp.if self.data.x > 2: - self.data.x += 1 -``` - -If we would have used the `if` native to Python it would not be interpreted and compiled in Michelson. - ## About the raffle contract A raffle is a game of chance that distributes a winning prize. @@ -108,10 +97,10 @@ class Raffle(sp.Contract): def open_raffle(self): pass - @sp.add_test(name = "Raffle") + @sp.add_test() def test(): r = Raffle() - scenario = sp.test_scenario() + scenario = sp.test_scenario("Raffle") scenario.h1("Raffle") scenario += r ``` @@ -199,7 +188,7 @@ class Raffle(sp.Contract): alice = sp.test_account("Alice") admin = sp.test_account("Administrator") r = Raffle(admin.address) - scenario = sp.test_scenario() + scenario = sp.test_scenario("Raffle") scenario.h1("Raffle") scenario += r @@ -346,12 +335,12 @@ On _SmartPy_, a test is a method of the contract class, preceded by `@sp.add_tes Inside this method, you need to instantiate your contract class and your scenarios, to which you will add the contract instance and all the related calls that you want to test. For instance: ```python -@sp.add_test(name="Raffle") +@sp.add_test() def test(): alice = sp.test_account("Alice") admin = sp.test_account("Administrator") r = Raffle(admin.address) - scenario = sp.test_scenario() + scenario = sp.test_scenario("Raffle") scenario.h1("Raffle") scenario += r ``` @@ -560,14 +549,14 @@ class Raffle(sp.Contract): self.data.sold_tickets[ticket_id] = current_player - @sp.add_test(name="Raffle") + @sp.add_test() def test(): alice = sp.test_account("Alice") jack = sp.test_account("Jack") admin = sp.test_account("Administrator") r = Raffle(admin.address) - scenario = sp.test_scenario() + scenario = sp.test_scenario("Raffle") scenario.h1("Raffle") scenario += r @@ -774,13 +763,13 @@ class Raffle(sp.Contract): self.data.sold_tickets = sp.map() self.data.raffle_is_open = False - @sp.add_test(name="Raffle") + @sp.add_test() def test(): alice = sp.test_account("Alice") jack = sp.test_account("Jack") admin = sp.test_account("Administrator") r = Raffle(admin.address) - scenario = sp.test_scenario() + scenario = sp.test_scenario("Raffle") scenario.h1("Raffle") scenario += r diff --git a/docs/tutorials/smart-contract/smartpy.mdx b/docs/tutorials/smart-contract/smartpy.mdx index d3b3a8320..4454d4e94 100644 --- a/docs/tutorials/smart-contract/smartpy.mdx +++ b/docs/tutorials/smart-contract/smartpy.mdx @@ -2,7 +2,7 @@ title: Deploy a smart contract with SmartPy authors: 'John Joubert, Sasha Aldrick, Tim McMackin' last_update: - date: 9 November 2023 + date: 15 February 2024 --- This tutorial covers writing and deploying a simple smart contract with the SmartPy programming language. @@ -91,7 +91,7 @@ Now you have an account and funds that you can use to work with Tezos. The contract that you will create has these basic parts: -- A function that initializes the contract and sets the starting value for it storage. +- A function that initializes the contract and sets the starting value for its storage. - Internal functions called entrypoints that run code when clients call the contract. @@ -115,6 +115,9 @@ You can work with SmartPy code in any IDE, but this online IDE keeps you from ha def main(): class StoreGreeting(sp.Contract): def __init__(self, greeting): # Note the indentation + # Initialize the storage with a string passed at deployment time + # Cast the greeting parameter to a string + sp.cast(greeting, sp.string) self.data.greeting = greeting @sp.entrypoint # Note the indentation @@ -139,22 +142,28 @@ You can work with SmartPy code in any IDE, but this online IDE keeps you from ha 1. Add this code, which creates automated tests: ```python - @sp.add_test(name = "StoreGreeting") + # Automated tests that run on simulation + @sp.add_test() def test(): - scenario = sp.test_scenario(main) + # Initialize the test scenario + scenario = sp.test_scenario("StoreGreeting", main) scenario.h1("StoreGreeting") + # Initialize the contract and pass the starting value contract = main.StoreGreeting("Hello") scenario += contract + # Verify that the value in storage was set correctly scenario.verify(contract.data.greeting == "Hello") + # Test the entrypoints and check the new storage value contract.replace(text = "Hi") contract.append(text = ", there!") scenario.verify(contract.data.greeting == "Hi, there!") ``` - These tests run automatically on compilation to verify that the replace and append entrypoints work. + When you run the SmartPy file, SmartPy runs a simulation in which it tests and compiles the contract. + In this case, the tests verify that the replace and append endpoints work. For more information about SmartPy and tests, see the [SmartPy documentation](https://smartpy.io/). The SmartPy online IDE looks like this: @@ -170,6 +179,9 @@ import smartpy as sp def main(): class StoreGreeting(sp.Contract): def __init__(self, greeting): # Note the indentation + # Initialize the storage with a string passed at deployment time + # Cast the greeting parameter to a string + sp.cast(greeting, sp.string) self.data.greeting = greeting @sp.entrypoint # Note the indentation @@ -180,16 +192,21 @@ def main(): def append(self, params): self.data.greeting += params.text -@sp.add_test(name = "StoreGreeting") +# Automated tests that run on simulation +@sp.add_test() def test(): - scenario = sp.test_scenario(main) + # Initialize the test scenario + scenario = sp.test_scenario("Test scenario", main) scenario.h1("StoreGreeting") + # Initialize the contract and pass the starting value contract = main.StoreGreeting("Hello") scenario += contract + # Verify that the value in storage was set correctly scenario.verify(contract.data.greeting == "Hello") + # Test the entrypoints and check the new storage value contract.replace(text = "Hi") contract.append(text = ", there!") scenario.verify(contract.data.greeting == "Hi, there!")