Skip to content

Commit

Permalink
Update SmartPy examples for SmartPy 19.0 (#324)
Browse files Browse the repository at this point in the history
* @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
  • Loading branch information
timothymcmackin authored Feb 20, 2024
1 parent 0380bf0 commit 4af160b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
4 changes: 2 additions & 2 deletions docs/developing/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions docs/smart-contracts/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
29 changes: 9 additions & 20 deletions docs/smart-contracts/languages/smartpy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
31 changes: 24 additions & 7 deletions docs/tutorials/smart-contract/smartpy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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!")
Expand Down

0 comments on commit 4af160b

Please sign in to comment.