-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmy_token.py
33 lines (24 loc) · 956 Bytes
/
my_token.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# my_token
# Smart Contract State
S = Hash(default_value=0)
# This runs when our contract is created on the blockchain, and never again.
@construct
def seed():
# Give yourself 50 tokens!
S['me'] = 50
# This method will be exported so our users can call it
@export
def transfer(amount: int, receiver: str):
# ctx.caller is the verified identity of the person who signed this transaction
# we will keep this reference as the "sender" of the transaction
sender = ctx.caller
# get the sender's balance from State
balance = S[sender]
# Assert the sender has the appropriate balance to send
# If this assert fails the method will fail here
# All values revert and no more code is executed
assert balance >= amount, "Transfer amount exceeds available token balance"
# subtract the tokens from the sender's balance
S[sender] -= amount
# add tokens to the receiver's balance
S[receiver] += amount