Created
August 9, 2021 14:36
-
-
Save shwetalsoni/ff5ac96e03e4d361b6a380b8d125cc0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run and deploy it at https://smartpy.io/ | |
import smartpy as sp | |
class User(sp.Contract): | |
# Constructor | |
def __init__(self): | |
self.init(userMap = sp.map()) # Initializes a map data structure to store user data with email as key | |
# Entry point to add new user data | |
@sp.entry_point | |
def addUser(self, params): # Takes in email, name, gender in params | |
self.data.userMap[params.email] = sp.record( # Here email is the key and the data is stored in a record (similar to a struct in C/C++) | |
name = params.name, | |
gender = params.gender, | |
score = 0, | |
) # For each user we store the email (as key), name, gender and score | |
# Entry point to update the score of a user | |
@sp.entry_point | |
def updateScore(self, params): # Takes in email and score in params | |
self.data.userMap[params.email].score += params.score # Adds the incomming score to the existing score of a user | |
# Tests for the smart contract | |
if "templates" not in __name__: | |
@sp.add_test(name = "User") | |
def test(): | |
c1 = User() # Created an object of User class | |
scenario = sp.test_scenario() | |
scenario.h1("User Data") # Displays h1 heading (as in HTML) | |
scenario += c1 # Displays current state of c1 | |
scenario += c1.addUser( # Displays the state of c1 after calling the addUser entry point | |
email = 'hello@example.com', | |
name = 'Bob', | |
gender = 'male' | |
) | |
scenario += c1.updateScore( # Displays the state of c1 after updating the score to 10 | |
email = 'hello@example.com', | |
score = 10 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment