Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes and cleanup #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 29 additions & 41 deletions src/contracts/book_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ class Variables:
price = Bytes("PRICE")
sold = Bytes("SOLD")
likes = Bytes("LIKES") # Uint64
dislikes = Bytes("DISLIKES") # Uint64
address = Bytes("ADDRESS")
owner = Bytes("OWNER")
liked = Bytes("liked")

class AppMethods:
like = Bytes("like")
Expand All @@ -20,27 +18,42 @@ class AppMethods:

def application_creation(self):
return Seq([
# checks for any empty/invalid values in the input data
Assert(Txn.application_args.length() == Int(4)),
Assert(Txn.note() == Bytes("books:uv1")),
Assert(Btoi(Txn.application_args[3]) > Int(0)),
Assert(
And(
Len(Txn.application_args[0]) > Int(0),
Len(Txn.application_args[1]) > Int(0),
Len(Txn.application_args[2]) > Int(0),
Btoi(Txn.application_args[3]) > Int(0),
)
),
App.globalPut(self.Variables.name, Txn.application_args[0]),
App.globalPut(self.Variables.image, Txn.application_args[1]),
App.globalPut(self.Variables.description, Txn.application_args[2]),
App.globalPut(self.Variables.price, Btoi(Txn.application_args[3])),
App.globalPut(self.Variables.sold, Int(0)),
App.globalPut(self.Variables.likes, Int(0)),
App.globalPut(self.Variables.dislikes, Int(0)),
App.globalPut(self.Variables.address, Global.creator_address()),
App.globalPut(self.Variables.owner, Txn.sender()),
Approve()
])

def setOptIn(self):
return Seq([
App.localPut(Txn.sender(), self.Variables.liked, Int(0)),
Approve()
])

# allow users to buy a book
# checks are carried to ensure that the sender is a valid buyer and
# the amount sent in the payment transactions matches the book price
def buy(self):
count = Txn.application_args[1]
valid_number_of_transactions = Global.group_size() == Int(2)

valid_payment_to_seller = And(
Gtxn[1].type_enum() == TxnType.Payment,
Gtxn[0].sender() != Global.creator_address(),
Gtxn[1].receiver() == Global.creator_address(),
Gtxn[1].amount() == App.globalGet(self.Variables.price) * Btoi(count),
Gtxn[1].sender() == Gtxn[0].sender(),
Expand All @@ -56,49 +69,23 @@ def buy(self):

return If(can_buy).Then(update_state).Else(Reject())

# like
# likes a book
def like(self):

Assert(
And(
# The number of transactions within the group transaction must be exactly 2.
# first one being the adopt function and the second being the payment transactions
Global.group_size() == Int(1),

# Txn.applications[0] is a special index denoting the current app being interacted with
Txn.applications.length() == Int(1),

# The number of arguments attached to the transaction should be exactly 2.
Txn.application_args.length() == Int(1),

),
)

return Seq([
Assert(App.localGet(Txn.sender(), self.Variables.liked) == Int(0)),
App.localPut(Txn.sender(), self.Variables.liked, Int(1)),
App.globalPut(self.Variables.likes, App.globalGet(self.Variables.likes) + Int(1)),
Approve()
])


# dislike
# dislikes a book
def dislike(self):

Assert(
And(
# The number of transactions within the group transaction must be exactly 2.
# first one being the adopt function and the second being the payment transactions
Global.group_size() == Int(1),

# Txn.applications[0] is a special index denoting the current app being interacted with
Txn.applications.length() == Int(1),

# The number of arguments attached to the transaction should be exactly 2.
Txn.application_args.length() == Int(1),
),
)

return Seq([
App.globalPut(self.Variables.dislikes, App.globalGet(self.Variables.dislikes) + Int(1)),
Assert(App.localGet(Txn.sender(), self.Variables.liked) == Int(1)),
App.localPut(Txn.sender(), self.Variables.liked, Int(0)),
App.globalPut(self.Variables.likes, App.globalGet(self.Variables.likes) - Int(1)),
Approve()
])

Expand All @@ -109,6 +96,7 @@ def application_start(self):
return Cond(
[Txn.application_id() == Int(0), self.application_creation()],
[Txn.on_completion() == OnComplete.DeleteApplication, self.application_deletion()],
[Txn.on_completion() == OnComplete.OptIn, self.setOptIn()],
[Txn.application_args[0] == self.AppMethods.buy, self.buy()],
[Txn.application_args[0] == self.AppMethods.like, self.like()],
[Txn.application_args[0] == self.AppMethods.dislike, self.dislike()]
Expand All @@ -118,4 +106,4 @@ def approval_program(self):
return self.application_start()

def clear_program(self):
return Return(Int(1))
return Return(Int(1))