def test(): scenario = sp.test_scenario() scenario.h1("ExchangeRateOracle Tests") admin = sp.test_account("Admin") scenario.show(admin) operatorA = sp.test_account("operatorA") scenario.show(operatorA) operatorB = sp.test_account("operatorB") scenario.show(operatorB) caller = sp.test_account("caller") scenario.show(caller) oracle = ExchangeRateOracle(admin.address) scenario.register(oracle) consumer = ExchangeRateConsumer(oracle.address) scenario.register(consumer) scenario += oracle.updateOperatorList(operators = sp.set([operatorA.address, operatorB.address])).run(sender = admin) scenario += oracle.registerFeed(currencyPair = "USDEUR", cost = sp.mutez(1000)).run(sender = admin) scenario += oracle.updateFeed(currencyPair = "USDEUR", value = sp.nat(92), precision = sp.nat(2), timestamp = sp.timestamp(0), source = "Trusted Source").run(sender = operatorA) scenario += consumer.sendQuery(currencyPair = "USDEUR").run(sender = caller, amount = sp.mutez(1000)) scenario.verify(consumer.data.value == 92) scenario.verify(consumer.data.currencyPair == "USDEUR")
def withdraw(self, amount): sp.verify((sp.sender == self.data.owner) | (sp.sender == self.data.contractAddress)) c = sp.contract(sp.TUnit, self.data.contractAddress, entry_point="depositFunds").open_some() sp.transfer(sp.unit, sp.mutez(amount), c)
def assertTransfer(self, params): sp.set_type( params, sp.TRecord( from_=sp.TAddress, to_=sp.TAddress, # we can assert the role of this operator operator=sp.TAddress ) ) sp.if ~self.data.allowlist.contains(params.from_) | self.data.blocklist.contains(params.from_): # controller should be able to move tokens out of a blocked address c = sp.contract( t = sp.TRecord( role=sp.TNat, account=sp.TAddress ), address = sp.sender, entry_point = "assertRole" ).open_some() sp.transfer( sp.record( role=TOKEN_CONTROLLER_ROLE, account=params.operator ), sp.mutez(0), c )
def interest(self, params): sp.verify(sp.amount >= self.data.tz_amount) sp.verify(sp.amount == sp.mutez(params)) sp.verify(self.data.immutable == sp.bool(False)) self.data.immutable = sp.bool(True) self.data.interested_party = sp.sender
def InvestLiquidity(self, params): minShares = params.minShares candidate = params.candidate sp.verify(sp.amount > sp.mutez(0), message="Wrong amount") sp.verify(minShares > sp.nat(0), message="Wrong tokenAmount") tezPerShare = sp.split_tokens(self.data.tezPool, sp.nat(1), self.data.totalShares) sp.verify(sp.amount >= tezPerShare, message="Wrong tezPerShare") sharesPurchased = sp.fst(sp.ediv(sp.amount, tezPerShare).open_some()) sp.verify(sharesPurchased >= minShares, message="Wrong sharesPurchased") tokensPerShare = self.data.tokenPool / self.data.totalShares tokensRequired = sharesPurchased * tokensPerShare share = sp.local("share", self.data.shares.get(sp.sender, 0)).value self.data.shares[sp.sender] = share + sharesPurchased self.data.tezPool += sp.amount self.data.tokenPool += tokensRequired self.data.invariant = sp.split_tokens(self.data.tezPool, self.data.tokenPool, sp.nat(1)) self.data.totalShares += sharesPurchased sp.if self.data.candidates.contains(sp.sender): prevVotes = self.data.votes.get(self.data.candidates[sp.sender], 0) self.data.votes[self.data.candidates[sp.sender]] = abs( prevVotes - share)
def hasWon(self,cycle,range): betsByCycle = self.data.bettors[sp.sender] betAmount = sp.local("betAmount",sp.mutez(0)) betAmount = betsByCycle[cycle].amount totalRewards = sp.local("totalRewards",sp.mutez(0)) totalRewards = sp.split_tokens(self.data.cycleData[cycle].totalAmount , sp.as_nat(sp.fst(self.data.cycleData[cycle].roi)) , sp.as_nat(sp.snd(self.data.cycleData[cycle].roi))) totalRewards = sp.split_tokens(totalRewards , sp.nat(98) , sp.nat(100)) reward = sp.split_tokens(totalRewards , sp.fst(sp.ediv(betAmount , sp.mutez(1)).open_some()) , sp.fst(sp.ediv(self.data.cycleData[cycle].amountByRange[range],sp.mutez(1)).open_some()) ) betsByCycle[cycle].withdrawn=True betsByCycle[cycle].withdrawnAmount = betsByCycle[cycle].amount + reward sp.send(sp.sender , betsByCycle[cycle].amount + reward)
def test(): scenario = sp.test_scenario() scenario.h1("Initialisation") deployer = sp.address("tz1PCVSQfsHmrKRgCdLhrN8Yanb5mwEL8rxu") player1 = sp.address("tz1U5E7U225tYk5uuNDpQpcQ2e4hCmekBadh") player2 = sp.address("tz1hHLY4AzGXrqzapnA3JYurGrPb5d3RnG7M") c1 = BettingGame(deployer) scenario += c1 scenario.h2("Bet Creation") scenario += c1.createBet(number=43,seed=3452).run(sender=player1,valid=False) scenario += c1.createBet(number=101,seed=2314).run(sender=player1,amount=sp.mutez(500000),valid=False) scenario += c1.createBet(number=43,seed=3452).run(sender=player1,amount=sp.mutez(500000)) scenario.h2("Bet Realisation") scenario += c1.realiseBet(bet=1,number=23,seed=5421).run(sender=player2,valid=False) scenario += c1.realiseBet(bet=1,number=123,seed=5421).run(sender=player2,amount=sp.mutez(500000),valid=False) scenario += c1.realiseBet(bet=1,number=23,seed=5421).run(sender=player2,amount=sp.mutez(500000))
def _transfer(self, params): sp.set_type(params, sp.TRecord( token_id = sp.TOption(sp.TNat), token_address = sp.TAddress, from_ = sp.TAddress, to_ = sp.TAddress, amount = sp.TNat ) ) sp.if params.token_id.is_some(): c = sp.contract( t = sp.TRecord( token_id = sp.TNat, from_ = sp.TAddress, to_ = sp.TAddress, amount = sp.TNat ), address = params.token_address, entry_point = "transfer" ).open_some() sp.transfer( sp.record( token_id = params.token_id.open_some(), from_ = params.from_, to_ = params.to_, amount = params.amount ), sp.mutez(0), c )
def test(): scenario = sp.test_scenario() scenario.h1("FA2 Contract Name: " + config.name) 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.h2("Accounts") scenario.show([admin, alice, bob]) c1 = FA2(config, admin.address) scenario += c1 if config.non_fungible: # TODO return scenario.h2("Initial Minting") scenario.p("The administrator mints 100 token-0's to Alice.") scenario += c1.mint(address = alice.address, amount = 100, symbol = 'TK0', token_id = 0).run(sender = admin) scenario.h2("Create certificate") scenario += c1.create_certificate(months = 5).run(sender = bob, amount=sp.mutez(100000000)) scenario.h2("redeem certificate") scenario += c1.redeem_certificate(token_id = 1).run(sender = bob)
def accept_bid_for_bot(self, params): # Make sure that sp.sender has admin access otherwise don't proceed forward # Make sure that contract isn't paused sp.set_type(params.token_id, sp.TNat) sp.set_type(params.nft_amount, sp.TNat) # Make sure that the NFT token id is already present in the ledger sp.verify(self.token_id_set.contains(self.data.all_tokens, params.token_id), "TOKEN ID NOT FOUND") # NFT transfer amount should be 1 sp.verify(params.nft_amount == 1, "NFT AMOUNT OF 1 REQUIRED") # Make sure that the caller is the owner of NFT token id else throw error user = self.ledger_key.make(sp.sender, params.token_id) sp.verify(self.data.ledger.contains(user) == True, "NOT OWNER OF NFT TOKEN ID") # Make sure their is aleast one bid for NFT token id sp.verify(sp.len(self.data.bid[params.token_id]) > 0, "NO BIDDER YET") highest_bidder = sp.local("highest_bidder", sp.record( has_bid = False, bidder = sp.address("tz1iLVzBpCNTGz6tCBK2KHaQ8o44mmhLTBio"), bid_value = sp.mutez(0) )) # transfer the highest bidder value to the seller account sp.for bidder in self.data.bid[params.token_id]: # store highest bidder sp.if bidder.bid_value > highest_bidder.value.bid_value: highest_bidder.value = bidder
def _mint(self, params): sp.set_type(params, sp.TRecord( to_ = sp.TAddress, amount = sp.TNat, token_address = sp.TAddress, token_id = sp.TOption(sp.TNat), metadata=sp.TOption(sp.TMap(sp.TString, sp.TBytes)) ) ) sp.if params.token_id.is_some(): c = sp.contract( t = sp.TRecord( address = sp.TAddress, amount = sp.TNat, token_id=sp.TNat, metadata=sp.TMap(sp.TString, sp.TBytes) ), address = params.token_address, entry_point = "mint" ).open_some() sp.transfer( sp.record( address = params.to_, amount = params.amount, token_id = params.token_id.open_some(), metadata = params.metadata.open_some() ), sp.mutez(0), c )
def test(): scenario = sp.test_scenario() admin = sp.address("tz1XP2AUZAaCbi1PCNQ7pEdMS1EEjL4p4YPY") mark = sp.test_account("Mark") elon = sp.test_account("Mars") alice = sp.test_account("alice") # (1) Initialize Cryptobot as `cryptobot` with non_fungible set to True. # (2) Add it to the scenario. cryptobot = Cryptobot(FA2.FA2_config(non_fungible = True), admin) scenario += cryptobot # Mint 1 token to mark with symbol - "CTB", amount - 1, token_id - 0 and extras - {"value": "First bot"} scenario += cryptobot.mint(address = mark.address, amount = 1, symbol = 'CTB', token_id = 5, extras = { "value": "4th bot"}).run(sender = admin) # Mint 1 token to mark with symbol - "CTB", amount - 1, token_id - 1 and extras - {"value": "Second bot"} scenario += cryptobot.mint(address = elon.address, amount = 1, symbol = 'CTB', token_id = 6, extras = { "value": "Second bot" }).run(sender = admin) scenario += cryptobot.offer_bot_for_sale(token_id = 6, min_sale_price = sp.mutez(1000)).run(sender = elon) # scenario += cryptobot.bot_no_longer_for_sale(token_id = 6).run(sender = elon) scenario += cryptobot.enter_bid_for_bot(token_id = 6).run(sender = mark, amount = sp.mutez(10000000)) scenario += cryptobot.enter_bid_for_bot(token_id = 6).run(sender = alice, amount = sp.mutez(12000000)) scenario += cryptobot.widthdraw_bid_for_bot(token_id = 6).run(sender = alice) scenario += cryptobot.accept_bid_for_bot(token_id = 6, nft_amount = 1).run(sender = elon)
def sendAddressInfo(self, params): # query registry sp.verify(self.data.addressRegistry.contains(params.addr), message = "Address is not in registry") _name = self.data.addressRegistry[params.addr] response = sp.record(name = _name, info = self.data.nameRegistry[_name]) # send sp.transfer(response, sp.mutez(0), params.k)
def checkRedeem(self, address): sp.set_type(address, sp.TAddress) contract = sp.contract(sp.TPair(sp.TAddress, sp.TContract( sp.TBool)), self.data.contract, entry_point="hasRedeemed").open_some() payload = sp.pair(address, sp.contract( sp.TBool, sp.self_address, entry_point="callback").open_some()) sp.transfer(payload, sp.mutez(0), contract)
def withdrawToken(self,params): sp.verify(params.amount > 0) sp.verify(self.data.ledger.contains(sp.sender)) sp.verify(self.data.ledger[sp.sender].balance >= params.amount) self.data.ledger[sp.sender].balance = sp.as_nat(self.data.ledger[sp.sender].balance - params.amount) self.data.totalSupply = sp.as_nat(self.data.totalSupply - params.amount) sp.send(sp.sender,sp.mutez(params.amount*100))
def requestDataFromOrO(self, params): contract = sp.contract(sp.TRecord(currency=sp.TString), params.oracleAddress, entry_point="getDataFromOrO").open_some() requestRecord = sp.record(currency=params.currency) sp.transfer(requestRecord, sp.mutez(5000), contract)
def withdrawBuyOrder(self, params): sp.verify(self.data.orders[params.id].creator == sp.sender) sp.verify(self.data.buyLongOrders.contains(params.id) | self.data.buyShortOrders.contains(params.id)) order = self.data.orders[params.id] sp.if self.data.buyLongOrders.contains(params.id): del self.data.buyLongOrders[params.id] sp.send(order.creator, sp.mutez((order.quantity * order.price) // 10))
def updateLambda(unitParam): sp.set_type(unitParam, sp.TUnit) storeContractHandle = sp.contract(sp.TNat, box.address, 'update').open_some() sp.result([ sp.transfer_operation(newValue, sp.mutez(0), storeContractHandle) ])
def completeBet(self,params): uuid = self.data.uuid self.data.uuid = self.data.uuid+1 self.data.tempCompleteBetData[uuid] = sp.record(betId = params.betId,betType=params.betType) entryAddress = sp.to_address(sp.self_entry_point(entry_point='completeBetFromOro')) contract = sp.contract(sp.TRecord(uuid=sp.TNat,entryAddress=sp.TAddress),self.data.oracleAddress,entry_point="getDataFromOro").open_some() sp.transfer(sp.record(uuid=uuid,entryAddress=entryAddress),sp.mutez(5000),contract)
def __init__(self, owner, asset_name, reserve_price, min_increase): self.init(owner = owner, asset_id = asset_id, current_bid = sp.mutez(reserve_price), min_increase = min_increase, highest_bidder = owner, is_active = sp.bool(True))
def getDataFromOro(self,params): sp.set_type(params.entryAddress,sp.TAddress) errcd = sp.record(uuid=params.uuid,cycleNumber=0) contract = sp.contract(sp.TRecord(uuid = sp.TNat, cycleNumber = sp.TNat),params.entryAddress).open_some() sp.if sp.amount == sp.mutez(5000): sp.transfer(sp.record(uuid=params.uuid,cycleNumber=self.data.cycleNumber),sp.mutez(0),contract)
def cancel_helper(self, oracle, waiting_request_id): sp.verify(waiting_request_id.is_some()) oracle_contract = sp.contract( sp.TNat, oracle, entry_point="cancel_request").open_some() sp.transfer(waiting_request_id.open_some(), sp.mutez(0), oracle_contract) waiting_request_id.set(sp.none)
def request_helper(self, amount, job_id, parameters, oracle, waiting_request_id, target, timeout_minutes=5): sp.verify(~waiting_request_id.is_some()) target = sp.set_type_expr( target, sp.TContract( sp.TRecord(client_request_id=sp.TNat, result=value_type))) waiting_request_id.set(sp.some(self.data.next_request_id)) token = sp.contract(sp.TRecord(oracle=sp.TAddress, params=request_type), self.data.token, entry_point="proxy").open_some() params = sp.record(amount=amount, target=sp.to_address(target), job_id=job_id, parameters=parameters, timeout=sp.now.add_minutes(timeout_minutes), client_request_id=self.data.next_request_id) sp.transfer(sp.record(oracle=oracle, params=params), sp.mutez(0), token) self.data.next_request_id += 1
def breakGlass(self, newContracts): sp.set_type( newContracts, sp.TRecord( governorContract=sp.TAddress, tokenContract=sp.TAddress, ovenProxyContract=sp.TAddress, stabilityFundContract=sp.TAddress, developerFundContract=sp.TAddress, ).layout(("governorContract", ("tokenContract", ("ovenProxyContract", ("stabilityFundContract", "developerFundContract")))))) # Can only be called by the multisig. sp.verify(sp.sender == self.data.multisigAddress, "NOT_MSIG") targetParam = (newContracts.governorContract, (newContracts.tokenContract, (newContracts.ovenProxyContract, (newContracts.stabilityFundContract, newContracts.developerFundContract)))) targetContractHandle = sp.contract( sp.TPair( sp.TAddress, sp.TPair( sp.TAddress, sp.TPair(sp.TAddress, sp.TPair(sp.TAddress, sp.TAddress)))), self.data.targetAddress, 'updateContracts').open_some() sp.transfer(targetParam, sp.mutez(0), targetContractHandle)
def test(): scenario = sp.test_scenario() # Create HTML output for debugging scenario.h1("English Auction") # Initialize test addresses initial_owner = sp.address("tz1-firstOwner-address-1234") bidder1 = sp.address("tz1-bidder-address-1111") bidder2 = sp.address("tz1-bidder-address-2222") bidder3 = sp.address("tz1-bidder-address-3333") bidder4 = sp.address("tz1-bidder-address-4444") final_owner = sp.address("tz1-secondOwner-address-5678") # Instantiate EnglishAuction contract c1 = EnglishAuction(owner = initial_owner, asset_name = "IdkWhatToSell", reserve_price = 10, min_increase = 3) # Print contract instance to HTML scenario += c1 # scenario += c1.cancelAuction().run(sender = initial_owner, valid = False) # Put bids by bidders and print results to HTML scenario.h2("Bid by bidder1") scenario += c1.bid().run(sender = bidder1, amount = sp.mutez(13)) scenario.h2("Bid by bidder2") scenario += c1.bid().run(sender = bidder2, amount = sp.mutez(20)) scenario.h2("Bid by bidder2") scenario += c1.bid().run(sender = bidder3, amount = sp.mutez(18), valid = False) scenario.h2("Attempting resolve bid") scenario += c1.resolveBid().run(sender = bidder3, valid = False) scenario += c1.resolveBid().run(sender = initial_owner) scenario.h2("Restart auction") scenario += c1.restartAuction(reserve_price = 50, min_increase = 5).run(sender = bidder2) scenario.h2("Bid by bidder3") scenario += c1.bid().run(sender = bidder3, amount = sp.mutez(60)) scenario.h2("Cancel auction") scenario += c1.cancelAuction().run(sender = bidder2) scenario.h2("Transfer ownership") scenario += c1.transferOwnership(new_owner = bidder4).run(sender = bidder2)
def endAuction(self, asset_id, owner): self.data.ended = sp.bool(True) # destroyInstance c = sp.contract(sp.TRecord(asset_id=sp.TNat, owner=sp.TAddress), self.data.master_auction_contract, entry_point="destroyInstance").open_some() sp.transfer(sp.record(asset_id=asset_id, owner=owner), sp.mutez(0), c)
class CheckSig(sp.Contract): @sp.entry_point def run(self, params): sp.set_type(params.x, sp.TNat) sp.if params.x > 9: sp.transfer(True, sp.mutez(0), params.k) sp.else: sp.transfer(False, sp.mutez(0), params.k)
def dispatchRoyalties(self, params): sp.verify(sp.sender == self.data.administrator) rounded = sp.as_nat(10**10) muCVRtez = sp.as_nat(self.data.circulatingSupply)*rounded // params.amount + 1 sp.for address in params.addresses: sp.if (self.data.balances.contains(address) & (sp.as_nat(self.data.balances[address].balance)*rounded > muCVRtez)): sendMuTez = sp.as_nat(self.data.balances[address].balance)*rounded // muCVRtez sp.send(address, sp.mutez(sendMuTez))
def restartAuction(self, params): sp.verify(sp.sender == self.data.owner) sp.verify(~self.data.is_active) self.data.current_bid = sp.mutez(params.reserve_price) self.data.min_increase = params.min_increase self.data.highest_bidder = sp.sender self.data.is_active = True
def setSeller(self, params): #ensure seller hasn't already been set sp.verify(~self.data.seller.is_some()) #the seller sets the price and must send the price in mutez as insurance self.data.price = params.price sp.verify(sp.amount == sp.mutez(self.data.price)) self.data.seller = sp.some(sp.sender)