def TezToToken(self, recipient: sp.TAddress, tezIn: sp.TNat, minTokensOut: sp.TNat): this = self.data.address sp.verify(tezIn > sp.mutez(0), message="Wrong tezIn") sp.verify(minTokensOut > 0, message="Wrong minTokensOut") fee = sp.fst(sp.ediv(tezIn, self.data.feeRate).open_some()) # TODO: ???? newTezPool = sp.local("newTezPool", self.data.tezPool).value + tezIn tempTezPool = abs(newTezPool - fee) newTokenPool = sp.fst(sp.ediv(sp.local( "newTokenPool", self.data.invariant).value, tempTezPool).open_some()) tokensOut = abs( sp.local("tokensOut", self.data.tokenPool).value - newTokenPool) sp.verify(tokensOut >= minTokensOut, message="Wrong minTokensOut") sp.verify(tokensOut <= self.data.tokenPool, message="Wrong tokenPool") self.data.tezPool = newTezPool self.data.tokenPool = newTokenPool self.data.invariant = sp.split_tokens(newTezPool, newTokenPool, sp.nat(1)) 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=this, destination=recipient, value=tokensOut), sp.mutez(0), token_contract)
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 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 custClose(self, params): sp.verify(self.data.custAddr == sp.sender) sp.verify((self.data.status == OPEN) | (self.data.status == MERCH_CLOSE)) # custClose inputs custBal = params.custBal merchBal = params.merchBal revLock = params.revLock s1 = params.s1 s2 = params.s2 # Prepare pairing check inputs g2 = self.data.g2 merchPk0 = self.data.merchPk0 merchPk1 = self.data.merchPk1 merchPk2 = self.data.merchPk2 merchPk3 = self.data.merchPk3 merchPk4 = self.data.merchPk4 chanID = self.data.chanID cust_b = sp.local('cust_b', sp.fst(sp.ediv(custBal, sp.mutez(1)).open_some())) one = sp.local('one', sp.bls12_381_fr("0x01")) cust_bal_b = sp.local("cust_bal_b", sp.mul(cust_b.value, one.value)) merch_b = sp.local('merch_b', sp.fst(sp.ediv(merchBal, sp.mutez(1)).open_some())) merch_bal_b = sp.local("merch_bal_b", sp.mul(merch_b.value, one.value)) revLockConcat = sp.local('revLockConcat', sp.concat([sp.bytes("0x050a00000020"), revLock])) rev_lock_b = sp.local('rev_lock_b', sp.unpack(revLockConcat.value, t = sp.TBls12_381_fr).open_some()) # Verify signature val1 = sp.local("val1", sp.mul(merchPk0, chanID)) val2 = sp.local("val2", sp.mul(merchPk1, rev_lock_b.value)) val3 = sp.local("val3", sp.mul(merchPk2, cust_bal_b.value)) val4 = sp.local("val4", sp.mul(merchPk3, merch_bal_b.value)) prod1 = sp.local("prod1", val1.value + val2.value + val3.value + val4.value + merchPk4) g2_negated = - g2 pair_list = sp.local("pair_list", [sp.pair(s1, prod1.value), sp.pair(s2, g2_negated)]) out = sp.local('out', False) sp.verify(sp.pairing_check(pair_list.value)) # Update on-chain state and transfer merchant's balance self.data.custBal = custBal self.data.revLock = revLock self.data.delayExpiry = sp.now.add_seconds(self.data.selfDelay) sp.send(self.data.merchAddr, merchBal) self.data.merchBal = sp.tez(0) self.data.status = CUST_CLOSE
def deposit(self, params): sp.verify(sp.amount > sp.mutez(0), message = "Deposit too low") contractbal = sp.ediv(sp.balance, sp.tez(1)) sp.if (contractbal.is_some() ): bal = sp.fst(contractbal.open_some()) val = sp.split_tokens( sp.amount, self.data.totalTokens, bal) _natVal = sp.ediv(val, sp.tez(1)) sp.if (_natVal.is_some() ): natVal = sp.fst(_natVal.open_some()) self.data.withdrawBalances[params] = sp.to_int(natVal) self.data.totalTokens += natVal
def default(self, params): sp.verify(sp.amount == LEADERSHIP_PAYMENT_AMOUNT) sp.verify(self.data.leadership_start_timestamp.add_seconds(sp.to_int(self.data.countdown_milliseconds/1000)) > sp.now) self.data.leader = sp.sender self.data.leadership_start_timestamp = sp.now balance_weight_tenthtez = sp.fst(sp.ediv(sp.balance-INITIAL_BALANCE,sp.mutez(1)).open_some())/sp.nat(100000) # mutez becomes tenth of a tez countdown_drop_milliseconds = (COUNTDOWN_DROP_FACTOR+balance_weight_tenthtez)/balance_weight_tenthtez sp.if self.data.countdown_milliseconds - countdown_drop_milliseconds > sp.to_int(MINIMAL_COUNT_DOWN_MILLISECONDS): self.data.countdown_milliseconds = sp.as_nat(self.data.countdown_milliseconds - countdown_drop_milliseconds)
def collect(self, params): sp.verify( (params.objkt_amount > 0) & (sp.sender != self.data.swaps[params.swap_id].issuer) ) sp.if (self.data.swaps[params.swap_id].xtz_per_objkt != sp.tez(0)): self.objkt_amount = sp.fst(sp.ediv(sp.amount, self.data.swaps[params.swap_id].xtz_per_objkt).open_some()) self.amount = self.objkt_amount * sp.fst(sp.ediv(self.data.swaps[params.swap_id].xtz_per_objkt, sp.mutez(1)).open_some()) sp.verify((params.objkt_amount == self.objkt_amount) & (sp.amount == sp.utils.nat_to_mutez(self.amount)) & (sp.amount > sp.tez(0))) # calculate fees and royalties self.fee = sp.fst(sp.ediv(sp.utils.nat_to_mutez(self.amount), sp.utils.nat_to_mutez(1)).open_some()) * (self.data.royalties[self.data.swaps[params.swap_id].objkt_id].royalties + 25) / 1000 self.royalties = self.data.royalties[self.data.swaps[params.swap_id].objkt_id].royalties * self.fee / (self.data.royalties[self.data.swaps[params.swap_id].objkt_id].royalties + 25) # send royalties to NFT creator sp.send(self.data.royalties[self.data.swaps[params.swap_id].objkt_id].issuer, sp.utils.nat_to_mutez(self.royalties)) # send management fees sp.send(self.data.manager, sp.utils.nat_to_mutez(abs(self.fee - self.royalties))) # send value to issuer sp.send(self.data.swaps[params.swap_id].issuer, sp.amount - sp.utils.nat_to_mutez(self.fee))
def getResponseFromHarbinger(self,response): sp.verify(self.data.admin.contains(sp.source) , "Un-authorized") sp.set_type(response , sp.TPair(sp.TString , sp.TPair(sp.TTimestamp , sp.TNat))) currentPrice=sp.local("currentPrice",sp.int(0)) currentPrice = sp.to_int(sp.fst(sp.ediv(sp.snd(sp.snd(response)) , sp.nat(1000)).open_some())) currentCycle = sp.local("currentCycle" ,sp.int(0)) currentCycle = sp.fst(sp.ediv(sp.level,self.data.blocksPerCycle).open_some()) sp.verify(~self.data.cycleData.contains(currentCycle+self.data.stakingPeriod)) rangeMap = sp.local("rangeMap" , sp.map(tkey = sp.TPair(sp.TInt, sp.TInt) , tvalue = sp.TMutez)) iterator = sp.local("iterator" , sp.int(0)) sp.while iterator.value<=self.data.rangeEnd: sp.if iterator.value+self.data.rangeStep<=self.data.rangeEnd: rangeMap.value[sp.pair(iterator.value,iterator.value+self.data.rangeStep)] =sp.mutez(0) rangeMap.value[(sp.int(-1)*(iterator.value+self.data.rangeStep),sp.int(-1)*iterator.value)] =sp.mutez(0)
def TokenToTokenOut(self, buyer: sp.TAddress, recipient: sp.TAddress, tokensIn: sp.TNat, minTokensOut: sp.TNat, tokenOutAddress: sp.TAddress): this = self.data.address sp.verify(tokensIn > 0, message="Wrong tokensIn") sp.verify(minTokensOut > 0, message="Wrong minTokensOut") fee = tokensIn / self.data.feeRate # TODO: ???? newTokenPool = sp.local( "newTokenPool", self.data.tokenPool).value + tokensIn tempTokenPool = abs(newTokenPool - fee) newTezPool = sp.fst(sp.ediv(sp.local( "newTezPool", self.data.invariant).value, tempTokenPool).open_some()) tezOut = abs(sp.local("tezOut", self.data.tezPool).value - newTezPool) sp.verify(sp.mutez(tezOut) <= self.data.tezPool, message="Wrong tezPool") self.data.tezPool = newTezPool self.data.tokenPool = newTokenPool self.data.invariant = sp.mutez(newTezPool * newTokenPool) 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() factory_contract = sp.contract( sp.TRecord(tokenOutAddress=sp.TAddress, recipient=sp.TAddress, minTokensOut=sp.TNat), address=self.data.factoryAddress, entry_point="TokenToExchangeLookup" ).open_some() sp.transfer(sp.record(account_from=buyer, destination=this, value=tokensIn), sp.mutez(0), token_contract) sp.transfer(sp.record(tokenOutAddress=tokenOutAddress, destination=recipient, value=minTokensOut), sp.mutez(tezOut), factory_contract)
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 checkIfWinnerAndDisburse(self,cycle): change = sp.local("change",sp.int(0)) change = self.data.cycleData[cycle].endingPrice - self.data.cycleData[cycle].referencePrice changePercentQuotient = sp.local("changePercentQuotient",sp.int(0)) changePercentQuotient.value = sp.mul(change , 10000) changePercentQuotient.value = sp.fst(sp.ediv(changePercentQuotient.value,self.data.cycleData[cycle].referencePrice).open_some()) upperLimit = sp.local("upperLimit",sp.int(0)) lowerLimit = sp.local("lowerLimit",sp.int(0)) betsByCycle = self.data.bettors[sp.sender] lowerLimit = sp.fst(betsByCycle[cycle].range) upperLimit = sp.snd(betsByCycle[cycle].range) sp.if upperLimit != lowerLimit: sp.if lowerLimit<= changePercentQuotient.value: sp.if changePercentQuotient.value<upperLimit: self.hasWon(cycle,betsByCycle[cycle].range)
def placeBet(self,params): self.checkBettingPaused() self.bettorNotAContract() sp.verify(sp.amount > sp.mutez(0) , message = "Amount should not be 0") currentCycle = sp.local("currentCycle" ,sp.int(0)) currentCycle = sp.fst(sp.ediv(sp.level,self.data.blocksPerCycle).open_some()) self.initializeInternalMapForNewBettor() self.betAlreadyPlacedForParticularCycle(currentCycle+self.data.stakingPeriod) self.betBelowBetLimit() self.isRangeValid(currentCycle+self.data.stakingPeriod , params.top , params.bottom) self.isCycleInitiated(currentCycle+self.data.stakingPeriod) betsByCycle = self.data.bettors[sp.sender] betsByCycle[currentCycle+self.data.stakingPeriod] = sp.record(amount = sp.amount , range = sp.pair(params.top , params.bottom) , withdrawn = sp.bool(False) , withdrawnAmount = sp.mutez(0) , stakedAt = currentCycle) self.data.cycleData[currentCycle+self.data.stakingPeriod].amountByRange[sp.pair(params.top , params.bottom)] += sp.amount self.data.cycleData[currentCycle+self.data.stakingPeriod].totalAmount += sp.amount
def create_certificate(self, params): # 0) verify that lock period is not more than 2 years sp.verify(params.months < 25) # 1) get tez value mintAmount = sp.split_tokens(sp.amount, 100, 100) coins = sp.ediv(mintAmount, sp.mutez(1) ) principal = sp.to_int( sp.fst(coins.open_some()) ) # 2) get timestamp end_time = sp.now.add_days(params.months*30) # 3) calculate payout w = sp.local('w', 1) y = sp.local('y', 0) sp.while y.value < params.months: w.value = 10033*w.value y.value = y.value + 1
def join_hDAO(self, params): sp.verify((sp.amount > sp.mutez(0)) & (sp.balance < sp.tez(730000))) c = sp.contract( sp.TRecord( address=sp.TAddress, amount=sp.TNat, token_id=sp.TNat, token_info=sp.TMap(sp.TString, sp.TBytes) ), self.data.ung, entry_point = "mint").open_some() sp.transfer( sp.record( address=sp.sender, amount=sp.fst(sp.ediv(sp.amount, sp.mutez(1)).open_some()), token_id=1, token_info={"": sp.pack("ipfs://QmS87PA42aKj6WgPM1vQMHxyavKJkswa5ycgAn1wbSrNgi")} ), sp.mutez(0), c) self.data.balance += sp.amount
@sp.entry_point def setManager(self, params): sp.verify(sp.sender == self.data.administrator) self.addAddressIfNecessary(params) self.data.saleManager = params @sp.entry_point def getManager(self, params): sp.transfer(self.data.saleManager, sp.tez(0), sp.contract(sp.TAddress, params.target).open_some()) @sp.entry_point def sale(self, params): sp.verify(self.data.saleStatus) sp.if self.data.balances.contains(sp.sender): sp.verify(~ self.data.balances[sp.sender].lock) natMutez = sp.fst(sp.ediv(sp.amount, sp.mutez(1)).open_some()) intMutez = sp.to_int(natMutez) self.mintSale(sp.sender, intMutez * self.data.ratio) @sp.entry_point def offchainSale(self, params): sp.verify(sp.sender == self.data.saleManager) sp.if self.data.balances.contains(params.address): sp.verify(~ self.data.balances[params.address].lock) self.mintSale(params.address, params.amount) def mintSale(self, address, nbMutoken): sp.verify(self.data.soldToken + nbMutoken <= self.data.saleLimit) self.addAddressIfNecessary(address) self.data.balances[address].balance += nbMutoken self.data.circulatingSupply += nbMutoken
def list_job(self, _job_id, _company, _job_description, _contact, _max_hires): # Cannot post another job with the same ID sp.verify(~self.data.jobs.contains(_job_id)) sp.verify(_max_hires > 0) sp.verify(sp.amount > sp.mutez(0)) self.data.jobs[_job_id] = sp.record(owner=sp.source, company=_company, job_description=_job_description, stipend=sp.mutez(sp.fst(sp.ediv( sp.amount, sp.mutez(sp.as_nat(_max_hires))).open_some())), contact=_contact, status=0, applications={}, selected={}, max_hires=_max_hires, cur_hires=0)