def test(): #Accounts alice = sp.test_account('Alice') bob = sp.test_account('Bob') john = sp.test_account('John') anshu = sp.test_account('Anshu') ronak = sp.test_account('Ronak') scenario = sp.test_scenario() c = SimpleLottery() scenario += c scenario.h1('Lottery Test') scenario += c.buyTicket(1).run(sender = alice, amount = sp.tez(1)) scenario += c.buyTicket(1).run(sender = bob, amount = sp.tez(1)) scenario += c.buyTicket(1).run(sender = john, amount = sp.tez(1)) scenario += c.buyTicket(1).run(sender = ronak, amount = sp.tez(1)) scenario.h3('Buy Multiple (Change)') scenario += c.buyTicket(3).run(sender = anshu, amount = sp.tez(3)) scenario.h3('Final Contract Balance') scenario.show(c.balance)
def transfer_eth(self, params): sp.verify(~self.data.lock) sp.verify(sp.amount > sp.tez(self.data.fees)) self.data.funds += sp.amount self.data.transfers.push( sp.record(address=params.address, amount=(sp.amount - sp.tez(self.data.fees))))
def __init__(self): self.init( parties = sp.map(tkey=sp.TAddress, tvalue=sp.TMutez), minAmount = sp.tez(0), total = sp.tez(0), maxTime = sp.timestamp(0) # + 345600 )
def test(): scenario = sp.test_scenario() scenario.h1("ALA Contract") value = 1 admin = sp.address("tz123") alice = sp.address("tz1456") bob = sp.address("tz1678") c1 = ALACoin(admin) c2 = Options(c1.address,admin) scenario += c1 scenario += c2 scenario += c1.mint(address = alice, amount = 12).run(sender = alice,amount = sp.tez(12)) scenario += c1.mint(address = bob, amount = 10).run(sender = alice,amount = sp.tez(10)) scenario += c1.AddContract(c2.address).run(sender=admin) scenario += c2.Payment().run(sender=alice) # scenario += c1.LockToken(address = bob, amount = 10).run(sender = admin) # scenario += c1.UnlockToken(address = bob, amount = 10).run(sender = admin) # scenario += c1.withdraw().run(sender = bob)
def test(): scenario = sp.test_scenario() admin = sp.address("tz1LAWQmxJcnK43vR9G8jsNTzR6b2unZ58NX") alice = sp.test_account("Alice") bob = sp.test_account("Robert") alex = sp.test_account("Alex") c1 = ALAToken(admin) c2 = PutOptions(admin,100,120,c1.address) scenario += c1 scenario += c2 scenario += c1.AddContract(c2.address).run(sender=admin) scenario += c1.mint(address = alice.address, value = 2).run(sender = alice,amount = sp.tez(2)) scenario += c2.putSeller(amount=50000).run(now=45,sender=alice,amount=sp.tez(100000)) scenario += c1.mint(address = bob.address, value = 1).run(sender = bob,amount = sp.tez(1)) scenario += c2.putBuyer(StrikePrice=400,Options=1,expire=14).run(now=50,sender=bob) scenario += c2.WithdrawPremium().run(sender=alice) # scenario += c2.ModifyPrice(price=300).run(sender=admin) # scenario += c2.ReleaseContract().run(sender=bob)
def mutualClose(self, params): sp.verify(self.data.status == OPEN) # Check customer signature sp.verify(sp.check_signature(self.data.custPk, params.custSig, sp.pack(sp.record( chanID = self.data.chanID, custAddr = self.data.custAddr, merchAddr = self.data.merchAddr, custBal = params.custBal, merchBal = params.merchBal) ) )) # Check merchant signature sp.verify(sp.check_signature(self.data.merchPk, params.merchSig, sp.pack(sp.record( chanID = self.data.chanID, custAddr = self.data.custAddr, merchAddr = self.data.merchAddr, custBal = params.custBal, merchBal = params.merchBal) ) )) self.data.custBal = params.custBal self.data.merchBal = params.merchBal sp.send(self.data.custAddr, self.data.custBal) sp.send(self.data.merchAddr, self.data.merchBal) self.data.custBal = sp.tez(0) self.data.merchBal = sp.tez(0) self.data.status = CLOSED
def merchClaim(self, params): sp.verify(self.data.merchAddr == sp.sender) sp.verify(self.data.status == MERCH_CLOSE) sp.verify(self.data.delayExpiry < sp.now) sp.send(self.data.merchAddr, self.data.custBal + self.data.merchBal) self.data.custBal = sp.tez(0) self.data.merchBal = sp.tez(0) self.data.status = CLOSED
def crop_request_to_farmers(self, params): farmers_contract = sp.contract(sp.TIntOrNat, address = params.address, entry_point="supplier_request").open_some() farmers_contract_two=sp.contract(sp.TIntOrNat, address = params.address, entry_point="supplier_request_rate").open_some() customer_contract_three=sp.contract(sp.TIntOrNat, address = self.data.customer, entry_point="get_suppliers_mrp").open_some() sp.transfer(arg=params._rate,amount=sp.tez(0),destination=farmers_contract_two) sp.if self.data.farmers_rate<=params._rate: self.data.mrp=params._rate+2 sp.transfer(arg=params.quantity, amount = sp.tez(0), destination=farmers_contract) sp.transfer(arg=self.data.mrp,amount=sp.tez(0),destination=customer_contract_three)
def buyTicket(self, qty): sp.verify(sp.tez(qty) == sp.amount) change = sp.local('change', sp.tez(0)) canBuy = sp.local('canBuy', qty) remaining = sp.as_nat(self.data.limit - self.data.id) sp.if qty > remaining: canBuy.value = remaining change.value = sp.tez(sp.as_nat(qty - remaining))
def __init__(self, owner, fromOwner, counterparty, fromCounterparty, epoch, hashedSecret): self.init(fromOwner=fromOwner, fromCounterparty=fromCounterparty, balanceOwner=sp.tez(0), balanceCounterparty=sp.tez(0), hashedSecret=hashedSecret, epoch=epoch, owner=owner, counterparty=counterparty)
def Contract_Initialisation(self, price, start_date, dep_date, nuki, deposit, fair, owner, visitor, contract): self.data.price = price sp.verify(start_date != dep_date) self.data.start_date = start_date self.data.dep_date = dep_date self.data.nuki = nuki self.data.deposit = deposit self.data.fair = fair self.data.balances[owner] = sp.record(balance = sp.tez(100)) self.data.balances[contract] = sp.record(balance = sp.tez(0)) self.data.balances[visitor] = sp.record(balance = sp.tez(100))
def deposit(self, params): sp.verify(sp.amount > sp.mutez(0), message = "Deposit too low") sp.verify(sp.sender != self.data.issuer, message = "Invalid address") period = self.getPeriod() tokenBalance = sp.local('tokenBalance', 0) requiredCollateral = sp.local('requiredCollateral', sp.tez(0)) expectedReturn = sp.ediv(sp.amount, self.data.schedule[period]) coins = sp.ediv(sp.amount, sp.tez(1)) sp.if (expectedReturn.is_some()) & (coins.is_some()): tokenBalance.value = sp.fst(expectedReturn.open_some()) wholeCoins = sp.fst(coins.open_some()) sp.verify(tokenBalance.value > wholeCoins, message = "Deposit too low") requiredCollateral.value = sp.tez(sp.as_nat(tokenBalance.value - wholeCoins))
def __init__(self): self.init( guest = sp.test_account("VOID").address, host = sp.test_account("VOID").address, nuki = sp.test_account("VOID").address, rent = sp.tez(0), g_deposit = sp.tez(0), h_deposit = sp.tez(0), active = False, # G engaged/sent deposit in_house = False, # G in house grace_ended = False, # No more complaints complaint = False, # G complained refund = False, # G accepted refund # contract_id = 0, cp = 0)
def GetAllowance(self, params): owner = params.owner spender = params.spender contr = params.contr src = self.data.ledger[owner] dest_allowance = src.allowances[spender] sp.transfer(dest_allowance, sp.tez(0), contr)
def testEscrow(): scenario = sp.test_scenario() seller = "tz2AAA" buyer = "tz2BBB" myContract = Escrow() #set the seller and price scenario += myContract scenario += myContract.setSeller(price=1).run(sp.address(seller), amount=sp.tez(2)) #set the buyer scenario += myContract.setBuyer().run(sp.address(buyer), amount=sp.tez(2)) # buyer confirms they received item scenario += myContract.confirmReceived().run(sp.address("tz2BBB"))
def vestedAmount(self, params): vested_amount = self._vested(sp.record( beneficiery= params.beneficiery, schedule_name= params.schedule_name)) sp.transfer( vested_amount, sp.tez(0), sp.contract(sp.TNat, params.target).open_some())
def mint(self, params): sp.verify(params.value>0) tezValue=sp.tez(sp.as_nat(params.value)) sp.verify(sp.amount == tezValue) self.addAddressIfNecessary(params.address) self.data.ledger[params.address].balance += abs(self.data.xtzPrice*params.value*100) self.data.totalSupply += abs(self.data.xtzPrice*params.value*100)
def request_tokens(self, targets): sp.set_type(targets, sp.TSet(sp.TAddress)) token = sp.contract(self.token_contract.batch_transfer.get_type(), self.data.token, entry_point = "transfer").open_some(message = "Incompatible token interface") targets = targets.elements().map(lambda target: sp.record(to_ = target, token_id = 0, amount = self.data.max_amount)) sp.transfer([sp.record(from_ = sp.to_address(sp.self), txs = targets)], sp.tez(0), token)
def test(): admin = sp.address("tz1hPnEdcKWrdeZEAQiGfmV6knHA5En1fCwQ") # Put Buyers token = sp.address("KT1KtyJeL78tdHnzwCPE8M14WDb1zqsnLkjQ") bob = sp.address("tz1678") # Put Sellers alice = sp.address("tz1456") alex = sp.address("tz1910") scenario = sp.test_scenario() c1 = PutOptions(admin,10,20,token) scenario += c1 scenario += c1.RestartCycle().run(sender=admin,now=30) scenario += c1.putBuyer(StrikePrice=400,Options=1,expire=14).run(now=50,sender=bob,valid=False) scenario += c1.putSeller(amount=50000).run(now=45,sender=alice,amount=sp.tez(100000)) scenario += c1.putSeller(amount=10000).run(now=45,sender=alice) scenario += c1.putSeller(amount=10000).run(now=45,sender=alex) scenario += c1.putBuyer(StrikePrice=400,Options=1,expire=14).run(now=50,sender=bob) #scenario += c1.ResetContract().run(sender=alice,now=1209655) # scenario += c1.ModifyPrice(price=300).run(sender=admin) # scenario += c1.ReleaseContract().run(sender=bob)
def test(): scenario = sp.test_scenario() admin = sp.test_account("Alice") alice = sp.test_account("Alice") bob = sp.test_account("Robert") alex = sp.test_account("Alex") token = sp.address("KT1KtyJeL78tdHnzwCPE8M14WDb1zqsnLkjQ") oracle = XTZOracle(admin.address) scenario += oracle c1 = PutOptions(admin.address,10,20,token,oracle.address) scenario += c1 scenario += oracle.feedData(price=400).run(sender=admin) scenario += oracle.feedData(price=600).run(sender=admin) scenario += oracle.feedStrike(one=320,two=360,three=400,four=440,five=480).run(sender=admin) scenario += oracle.GetputSeller(Options=1,Ratio=80,address=admin.address,expire=14).run(sender=admin) scenario += c1.RestartCycle().run(sender=admin,now=30) scenario += c1.putSeller(amount=50000).run(now=45,sender=alice,amount=sp.tez(100000)) scenario += c1.putSeller(amount=10000).run(now=45,sender=alice) scenario += c1.putSeller(amount=10000).run(now=45,sender=alex) scenario += c1.putBuyer(Options=1,StrikePrice=100,expire=7).run(sender=bob) #scenario += oracle.getDataMint(address=alice.address,amount=10).run(sender=alice)
def merchDispute(self, params): sp.verify(self.data.merchAddr == sp.sender) sp.verify(self.data.status == CUST_CLOSE) sp.verify(self.data.revLock == sp.sha256(params.secret)) sp.send(self.data.merchAddr, self.data.custBal) self.data.custBal = sp.tez(0) self.data.status = CLOSED
def test(): scenario = sp.test_scenario() scenario.h1("FA1.2 template - StableCoin") scenario.table_of_contents() # sp.test_account generates ED25519 key-pairs deterministically: admin = sp.test_account("Administrator") alice = sp.test_account("Alice") bob = sp.test_account("Robert") # Let's display the accounts: scenario.h1("Accounts") scenario.show([admin, alice, bob]) c1 = VaultOpener( sp.address("KT1HsVdNMvy4uTeorCFJD2kPVGzHXhpzJZjV"), sp.address("KT1LiKCNGz2NHvtUH6hhB9usqw8TfuAktusy"), sp.address("tz1XfbFQgxj1sSfe2YyJPba5ZQuKMcV4FXAX") ) scenario += c1 scenario += c1.OpenVault().run(sender=alice,amount=sp.tez(1)) # vault Opener - KT1Q5SPEdRTdki5fVXS7UHikFmWA2ckd3KdA
def pay_cust(self, params): sp.verify(self.data.payNum < params.payNum) sp.verify(sp.tez(0) <= params.amount) sp.verify(params.amount <= self.data.merchBal) self.data.payNum = params.payNum self.data.merchBal -= params.amount self.data.custBal += params.amount
def custClaim(self, params): sp.verify(self.data.custAddr == sp.sender) sp.verify(self.data.status == CUST_CLOSE) sp.verify(self.data.delayExpiry < sp.now) sp.send(self.data.custAddr, self.data.custBal) self.data.custBal = sp.tez(0) self.data.status = CLOSED
def InitializeExchange(self, params): token_amount = sp.as_nat(params.token_amount) candidate = params.candidate sp.verify(self.data.invariant == sp.mutez(0), message="Wrong invariant") sp.verify(self.data.totalShares == 0, message="Wrong totalShares") sp.verify(((sp.amount > sp.mutez(1)) & ( sp.amount < sp.tez(500000000))), message="Wrong amount") sp.verify(token_amount > sp.nat(10), message="Wrong tokenAmount") self.data.tokenPool = token_amount self.data.tezPool = sp.amount self.data.invariant = sp.split_tokens(self.data.tezPool, self.data.tokenPool, sp.nat(1)) self.data.shares[sp.sender] = sp.nat(1000) self.data.totalShares = sp.nat(1000) token_contract = sp.contract( sp.TRecord(account_from=sp.TAddress, destination=sp.TAddress, value=sp.TNat), address=self.data.tokenAddress, entry_point="Transfer" ).open_some() sp.transfer(sp.record(account_from=sp.sender, destination=self.data.address, value=token_amount), sp.mutez(0), token_contract) self.data.candidates[sp.sender] = candidate self.data.votes[candidate] = sp.as_nat(1000) self.data.delegated = candidate sp.set_delegate(sp.some(candidate))
def mint(self, params): tezValue=sp.tez(sp.as_nat(params.amount)) sp.verify(sp.amount == tezValue) self.addAddressIfNecessary(params.address) self.data.balances[params.address].balance += params.amount*10000 self.data.totalSupply += params.amount*10000
def __init__(self): self.init( origin = sp.address('tz1'), destiny = sp.address('tz2'), amount = sp.tez(0), immutability = sp.bool(False) )
def selectWinner(self): #random id randomId = (sp.timestamp_from_utc_now() - sp.timestamp(0)) % 5 #pay out sp.send(self.data.ticketToAddress[randomId], sp.tez(5)) self.data.previousWinners.push(self.data.ticketToAddress[randomId])
def transferHosp(self, params): c = sp.contract(sp.TIntOrNat, address=params.address, entry_point="processRequest").open_some() sp.transfer(arg=params.amtVaccine, amount=sp.tez(0), destination=c) self.data.vaccineCount -= params.amtVaccine sp.if params.address==self.data.hosp: self.data.hosp=sp.address("KT1RcyuZeu6RcoeWDAUSVgUkrReWE2JrNMZh") self.data.hospReqQ = 0
def transferVaccine(self, params): self.data.address = params.address c = sp.contract(sp.TInt, address=self.data.address, entry_point="incCount").open_some() sp.transfer(arg=params.reqVaccine, amount=sp.tez(0), destination=c) self.data.vaccineAvailable -= params.reqVaccine sp.if params.address==self.data.address: self.data.address=sp.address("KT1RQBkagyLEHHx292djmg2MZ4CDpS5NYQeW") self.data.req = 0