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 token_metadata(self, tok): """ Return the token-metadata URI for the given token. For a reference implementation, dynamic-views seem to be the most flexible choice. """ sp.set_type(tok, sp.TNat) sp.result(self.data.token_metadata[tok])
def get_balance(self, req): """This is the `get_balance` view defined in TZIP-12.""" sp.set_type( req, sp.TRecord( owner = sp.TAddress, token_id = sp.TNat ).layout(("owner", "token_id"))) user = self.ledger_key.make(req.owner, req.token_id) sp.verify(self.data.token_metadata.contains(req.token_id), message = self.error_message.token_undefined()) sp.result(self.data.ledger[user].balance)
def getEffectiveExpiry(self, params): b = sp.bind_block() with b: address = sp.fst(params) sp.if self.data.permit_data.permit_expiries.contains(params) & self.data.permit_data.permit_expiries[params].is_some(): permit_expiry = self.data.permit_data.permit_expiries[params].open_some() sp.result(permit_expiry) sp.else: sp.if self.data.permit_data.user_expiries.contains(address) & self.data.permit_data.user_expiries[address].is_some(): user_expiry = self.data.permit_data.user_expiries[address].open_some() sp.result(user_expiry)
def getPrice(self, assetCode): sp.set_type(assetCode, sp.TString) # Verify this normalizer has data for the requested asset. sp.verify( self.data.oracleData.contains(assetCode), message="bad request" ) # Callback with the requested data. sp.result(self.data.oracleData[assetCode])
def f_process_request(req): user = self.ledger_key.make(req.owner, req.token_id) sp.verify(self.data.token_metadata.contains(req.token_id), message = self.error_message.token_undefined()) sp.if self.data.ledger.contains(user): balance = self.data.ledger[user].balance sp.result( sp.record( request = sp.record( owner = sp.set_type_expr(req.owner, sp.TAddress), token_id = sp.set_type_expr(req.token_id, sp.TNat)), balance = balance))
def is_operator(self, query): sp.set_type(query, sp.TRecord(token_id = sp.TNat, owner = sp.TAddress, operator = sp.TAddress).layout( ("owner", ("operator", "token_id")))) sp.result( self.operator_set.is_member(self.data.operators, query.owner, query.operator, query.token_id) )
def token_metadata(self, token_id): """ This method will become an offchain view if the contract uses TZIP-016 metadata and the config `use_token_metadata_offchain_view` is set to TRUE. Return the token-metadata URI for the given token. (token_id must be 0) For a reference implementation, dynamic-views seem to be the most flexible choice. """ sp.set_type(token_id, sp.TNat) sp.result(self.data.token_metadata[token_id])
def getAllowance(self, params): sp.set_type( params, sp.TRecord( owner = sp.TAddress, spender = sp.TAddress ).layout(("owner", "spender")) ) sp.verify(self.data.operable) sp.result(self.data.ledger[params.owner].approvals[params.spender])
def transfer_presigned(self, params): sp.set_type(params, sp.TRecord( from_=sp.TAddress, to_=sp.TAddress, value=sp.TNat)) params_hash = sp.blake2b(sp.pack(params)) permit_key = sp.pair(params.from_, params_hash) sp.if self.data.permit_data.permits.contains(permit_key): permit_submission_timestamp = self.data.permit_data.permits[permit_key] effective_expiry = self.getEffectiveExpiry(permit_key) # Deleting permit regardless of whether or not its expired sp.if sp.as_nat(sp.now - permit_submission_timestamp) >= effective_expiry: # Expired self.delete_permit(permit_key) sp.result(sp.bool(False))
def getPrice(self, assetCode): sp.set_type(assetCode, sp.TString) # Verify this normalizer has data for the requested asset. sp.verify( self.data.assetMap.contains(assetCode), message="bad request" ) # Callback with the requested data. assetData = self.data.assetMap[assetCode] normalizedPrice = assetData.computedPrice lastUpdateTime = assetData.lastUpdateTime sp.result((lastUpdateTime, normalizedPrice))
def transfer_presigned(self, params): sp.set_type( params, sp.TRecord(from_=sp.TAddress, to_=sp.TAddress, value=sp.TNat)) params_hash = sp.blake2b(sp.pack(params)) #unsigned = sp.blake2b(mi.operator("SELF; ADDRESS; CHAIN_ID; PAIR; PAIR; PACK", [sp.TPair(sp.TNat, sp.TBytes)], [sp.TBytes])(sp.pair(self.data.counter, params_hash))) permit_key = sp.pair(params.from_, params_hash) effective_expiry = sp.local("effective_expiry", 0) with sp.if_(self.data.permits.contains(permit_key)): permit_submission_timestamp = self.data.permits[permit_key] with sp.if_( self.data.permit_expiries.contains(permit_key) & self.data.permit_expiries[permit_key].is_some()): effective_expiry.value = self.data.permit_expiries[ permit_key].open_some() with sp.else_(): with sp.if_( self.data.user_expiries.contains(params.from_) & self.data.user_expiries[params.from_].is_some()): effective_expiry.value = self.data.user_expiries[ params.from_].open_some() with sp.else_(): effective_expiry.value = self.data.default_expiry # Deleting permit regardless of whether or not its expired with sp.if_( sp.as_nat(sp.now - permit_submission_timestamp) >= effective_expiry.value): # Expired self.delete_permit(permit_key) sp.result(sp.bool(False)) with sp.else_(): self.delete_permit(permit_key) sp.result(sp.bool(True)) with sp.else_(): sp.result(sp.bool(False))
def balance_of(self, params): # paused may mean that balances are meaningless: sp.verify( ~self.is_paused(), message = self.error_message.paused()) sp.set_type(params, Balance_of.entry_point_type()) def f_process_request(req): user = self.ledger_key.make(req.owner, req.token_id) sp.verify(self.data.token_metadata.contains(req.token_id), message = self.error_message.token_undefined()) sp.if self.data.ledger.contains(user): balance = self.data.ledger[user].balance sp.result( sp.record( request = sp.record( owner = sp.set_type_expr(req.owner, sp.TAddress), token_id = sp.set_type_expr(req.token_id, sp.TNat)), balance = balance)) sp.else: sp.result( sp.record( request = sp.record( owner = sp.set_type_expr(req.owner, sp.TAddress), token_id = sp.set_type_expr(req.token_id, sp.TNat)), balance = 0))
def getEffectiveExpiry(self, params): sp.set_type(params, sp.TPair(sp.TAddress, sp.TBytes)) address = sp.fst(params) with sp.if_( self.data.permit_expiries.contains(params) & self.data.permit_expiries[params].is_some()): permit_expiry = self.data.permit_expiries[params].open_some() sp.result(permit_expiry) with sp.else_(): with sp.if_( self.data.user_expiries.contains(address) & self.data.user_expiries[address].is_some()): user_expiry = self.data.user_expiries[address].open_some() sp.result(user_expiry) with sp.else_(): sp.result(self.data.default_expiry)
class Permit(sp.Contract): def __init__(self): self.error_message=Error_message() with open('metadata/metadata.json', 'r') as f: #loads then dumps to confirm correctly formatted json md = json.dumps(json.load(f)) self.init(permit_data = init_permit_data(), metadata = init_metadata(md)) def getEffectiveExpiry(self, params): b = sp.bind_block() with b: address = sp.fst(params) sp.if self.data.permit_data.permit_expiries.contains(params) & self.data.permit_data.permit_expiries[params].is_some(): permit_expiry = self.data.permit_data.permit_expiries[params].open_some() sp.result(permit_expiry) sp.else: sp.if self.data.permit_data.user_expiries.contains(address) & self.data.permit_data.user_expiries[address].is_some(): user_expiry = self.data.permit_data.user_expiries[address].open_some() sp.result(user_expiry) sp.else: default_expiry = self.data.permit_data.default_expiry sp.result(default_expiry)
class FishcakeBox(sp.Contract): def __init__(self, amount, tokenAddr): self.init(users=sp.big_map(tkey=sp.TAddress, tvalue=sp.TBool), redeemAmt=amount, tokensDistributed=0, fischake=tokenAddr) self.transfer = FA2.Batch_transfer(FA2.getFishcakeConfig()) @sp.entry_point def redeem(self): sp.verify(~self.data.users.contains(sp.sender)) self.data.users[sp.sender] = True self.data.tokensDistributed += self.data.redeemAmt contract = sp.contract(self.transfer.get_type( ), self.data.fischake, entry_point="transfer").open_some() payload = [self.transfer.item(from_=sp.self_address, txs=[sp.record(to_=sp.sender, amount=self.data.redeemAmt, token_id=0)])] sp.transfer(payload, sp.mutez(0), contract) @sp.view(sp.TBool) def hasRedeemed(self, address): sp.if self.data.users.contains(address): sp.result(True) sp.else: sp.result(False)
def transfer_presigned(self, params): sp.set_type(params, sp.TRecord( from_=sp.TAddress, to_=sp.TAddress, value=sp.TNat)) params_hash = sp.blake2b(sp.pack(params)) permit_key = sp.pair(params.from_, params_hash) sp.if self.data.permit_data.permits.contains(permit_key): permit_submission_timestamp = self.data.permit_data.permits[permit_key] effective_expiry = self.getEffectiveExpiry(permit_key) # Deleting permit regardless of whether or not its expired sp.if sp.as_nat(sp.now - permit_submission_timestamp) >= effective_expiry: # Expired self.delete_permit(permit_key) sp.result(sp.bool(False)) sp.else: self.delete_permit(permit_key) sp.result(sp.bool(True)) sp.else: sp.result(sp.bool(False)) class Viewer(sp.Contract): def __init__(self, t): self.init(last = sp.none) self.init_type(sp.TRecord(last = sp.TOption(t))) @sp.entry_point def target(self, params): self.data.last = sp.some(params) if "templates" not in __name__: @sp.add_test(name = "FA12") def test():
def all_tokens(self): if self.config.assume_consecutive_token_ids: sp.result(sp.range(0, self.data.all_tokens)) else: sp.result(self.data.all_tokens.elements())
def does_token_exist(self, tok): "Ask whether a token ID is exists." sp.set_type(tok, sp.TNat) sp.result(self.data.token_metadata.contains(tok))
def execution_payload(x): sp.set_type(x, sp.TUnit) honey_pot_contract = sp.contract(sp.TUnit, honeypot).open_some() sp.result([sp.transfer_operation(sp.unit, sp.mutez(0), honey_pot_contract)])
def total_supply(self, tok): if self.config.store_total_supply: sp.result(self.data.total_supply[tok]) else: sp.set_type(tok, sp.TNat) sp.result("total-supply not supported")
] ) @sp.sub_entry_point def find_powerup(self, powerup): powerup_to_send = sp.local( 'powerup_to_send', sp.record(power='', duration=sp.nat(0))) sp.for p in self.data.powerups: sp.if p.power == powerup: powerup_to_send.value.power = p.power powerup_to_send.value.duration = p.duration sp.result(powerup_to_send.value) @sp.entry_point def send_powerup(self, params): # remember to pass find_powerup the powerup sent in params. powerup_to_send = self.find_powerup(params.powerup) # transfer powerup_to_sent with 0 mutez to the cryptobot_contract sent through the params. sp.transfer(powerup_to_send, sp.mutez(0), params.cryptobot_contract) @sp.add_test(name="inter-contract") def test(): scenario = sp.test_scenario() # Class Invokation
def getBalance(self, params): sp.result(self.data.ledger[params].balance)
def getTotalSupply(self, params): sp.set_type(params, sp.TUnit) sp.result(self.data.totalSupply)
def f_on_request(req): self.token_meta_data.set_type_and_layout(self.data.tokens[req]) sp.result(self.data.tokens[req])
def getCounter(self, params): sp.set_type(params, sp.TUnit) sp.result(self.data.permit_data.counter)
def getAllowance(self, params): sp.result(self.data.ledger[params.owner].approvals[params.spender])
def getDefaultExpiry(self, params): sp.set_type(params, sp.TUnit) sp.result(self.data.permit_data.default_expiry)
def getAdministrator(self, params): sp.set_type(params, sp.TUnit) sp.result(self.data.administrator)
def getMaxExpiry(self, params): sp.set_type(params, sp.TUnit) sp.result(self.data.permit_data.max_expiry)