Guest User

Untitled

a guest
Feb 19th, 2020
150
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import smartpy as sp
  2. class EventPlanner(sp.Contract):
  3. def __init__(self, initialOwner):
  4. self.init(owner = sp.set_type(initialOwner, sp.TAddress),
  5. nameToEvent = sp.map(tkey = sp.TString, tvalue=sp.TPair(t1=sp.TString, t2=sp.TInt)))
  6. @sp.entry_point
  7. def setDate(self, params):
  8. sp.verify(sp.sender == self.data.owner)
  9. self.checkEvent(params.name)
  10. self.data.nameToEvent[params.name] = sp.pair(params.newDate, sp.snd(self.data.nameToEvent[params.name]))
  11. @sp.entry_point
  12. def setNumGuests(self, params):
  13. #check whether the sender is the owner
  14. sp.verify(sp.sender == self.data.owner)
  15. self.checkEvent(params.name)
  16. #change number of guests
  17. self.data.nameToEvent[params.name] = sp.pair(sp.fst(self.data.nameToEvent[params.name]), params.newNumGuests)
  18. @sp.entry_point
  19. def changeOwner(self, params):
  20. #check whether the sender is the owner
  21. sp.verify(sp.sender == self.data.owner)
  22. #set owner to the new owner
  23. self.data.owner = params.newOwner
  24. #This function check whether the name already exists in the map, if it doesn't exist checkEvent will create it.
  25. def checkEvent(self, name):
  26. sp.if ~self.data.nameToEvent.contains(name):
  27. self.data.nameToEvent[name] = sp.pair("", 0)
  28. @sp.add_test(name = "SomeTest")
  29. def test():
  30. scenario = sp.test_scenario()
  31. scenario.h1("Event Planner")
  32. firstOwner = sp.address("tz1-firstOwner-address-1234")
  33. secondOwner = sp.address("tz1-secondOwner-address-5678")
  34. c1 = EventPlanner(firstOwner)
  35. scenario += c1
  36. # Invoke EventPlanner entry points and print results to HTML
  37. scenario.h2("Set date for Tezos Meetup to 11-28-2017")
  38. scenario += c1.setDate(name = "Tezos Meetup", newDate = "11-28-2017").run(sender = firstOwner)
  39. scenario.h2("Set number of guests for Tezos Meetup to 80")
  40. scenario += c1.setNumGuests(name = "Tezos Meetup", newNumGuests = 80).run(sender = firstOwner)
  41. scenario.h2("Change owner")
  42. scenario += c1.changeOwner(newOwner = secondOwner).run(sender = firstOwner)
  43. scenario.h2("New owner sets date for Tezos Meetup to 03-21-2019")
  44. scenario += c1.setDate(name = "Tezos Meetup", newDate = "03-21-2019").run(sender = secondOwner)
  45. scenario.h2("Old owner attempts to set date for Tezos Meetup")
  46. scenario += c1.setDate(name = "Tezos Meetup", newDate = "10-15-2018").run(sender = firstOwner, valid = False)
  47. # Verify expected results
  48. scenario.verify(sp.fst(c1.data.nameToEvent["Tezos Meetup"]) == '03-21-2019')
  49. scenario.verify(sp.snd(c1.data.nameToEvent["Tezos Meetup"]) == 80)
  50. scenario.verify((c1.data.owner) == sp.address('tz1-secondOwner-address-5678'))
RAW Paste Data