Skip to content

Smartpy Smart Contract

Bhaskar Singh edited this page May 3, 2020 · 3 revisions
import smartpy as sp

class Plant(sp.Contract):
    def __init__(self, owner):
        self.init(owner = owner, dead = False, bullet_seed = 5, kills = 0)
    
    
    # shoot ball to kill zombies
    # with verify
    @sp.entry_point
    def shoot_to_kill_1(self, params):
        # only allow owner to shoot
        sp.verify(self.data.owner == sp.sender)
        # make sure that plant is not dead
        sp.verify(self.data.dead == False)
        # decrease bullet seed by one
        self.data.bullet_seed -= 1
        # increase kill by one
        self.data.kills += 1

    
    # shoot ball to kill zombies
    # with if/else
    @sp.entry_point
    def shoot_to_kill_2(self, params):
        # only allow owner to shoot
        sp.if self.data.owner == sp.sender:
            # decrease bullet seed by one
            self.data.bullet_seed -= 1
            # increase kill by one
            self.data.kills += 1
        sp.else:
            pass
    
    # buy bullet seeds
    @sp.entry_point
    def buy_bullet_seeds(self):
        # only allow owner to buy bullet seeds
        sp.verify(self.data.owner == sp.sender)
        # shop owner address
        shop_owner = sp.address("tz1YtuZ4vhzzn7ssCt93Put8U9UJDdasdci4")
        # check that amount, equals bullet_seeds price
        sp.verify(sp.amount == sp.mutez(500))
        # send amount to shop owner
        sp.send(shop_owner, sp.amount)
        # increase bullet seeds by 5
        self.data.bullet_seed += 5
        
    
        

@sp.add_test(name = "Plant")
def test():
    owner = sp.address("tz1YtuZ4vhzzn7ssCt93Put8U9UJDdvCXci4")
    c1 = Plant(owner)
    scenario = sp.test_scenario()
    scenario += c1
    scenario += c1.shoot_to_kill_1().run(sender = owner)
    scenario += c1.shoot_to_kill_2().run(sender = owner)
    scenario += c1.buy_bullet_seeds().run(sender = owner, amount = sp.mutez(500))
Clone this wiki locally