def getInvestOngBalance(account): return Get(GetContext(), concatKey(INVEST_BALANCE_PREFFIX, account))
def getReferralBalance(account): return Get(GetContext(), concatKey(REFERRAL_BALANCE_OF_PREFIX, account))
def getCurrentRound(): return Get(GetContext(), CURRET_ROUND_NUM_KEY)
def getPaperBalance(account): return Get(GetContext(), concatKey(PAPER_BALANCE_PREFIX, account))
def getAwardVault(roundNum): return Get(GetContext(), concatKey(concatKey(ROUND_PREFIX, roundNum), AWARD_VAULT_KEY))
def GetInfoOf(account): return Get(ctx, account)
def reinvest(account, paperAmount): RequireWitness(account) currentRound = getCurrentRound() Require(getGameStatus(currentRound) == STATUS_ON) ongAmount = paperToONG(currentRound, paperAmount) updateDividendBalance(account) dividendBalance = getDividendBalance(account) awardBalance = getAwardBalance(account) referralBalance = getReferralBalance(account) assetToBeReinvest = Add(Add(dividendBalance, awardBalance), referralBalance) Require(assetToBeReinvest >= ongAmount) dividend1 = Div(Mul(ongAmount, PaperHolderPercentage), 100) # update referral balance referral = Get(GetContext(), concatKey(REFERRAL_PREFIX, account)) referralAmount = 0 if referral: referralAmount = Div(Mul(ongAmount, ReferralAwardPercentage), 100) Put(GetContext(), concatKey(REFERRAL_BALANCE_OF_PREFIX, referral), Add(referralAmount, getReferralBalance(referral))) dividend = Sub(dividend1, referralAmount) # update next vault nextVaultToBeAdd = Div(Mul(ongAmount, NextPercentage), 100) nextVaultKey = concatKey(concatKey(ROUND_PREFIX, currentRound), NEXT_VAULT_KEY) Put(GetContext(), nextVaultKey, Add(nextVaultToBeAdd, getNextVault(currentRound))) # update award vault awardVaultToBeAdd = Div(Mul(ongAmount, AwardPercentage), 100) awardVaultKey = concatKey(concatKey(ROUND_PREFIX, currentRound), AWARD_VAULT_KEY) Put(GetContext(), awardVaultKey, Add(awardVaultToBeAdd, getAwardVault(currentRound))) # update gas vault gasVaultToBeAdd = Sub(Sub(Sub(ongAmount, dividend1), nextVaultToBeAdd), awardVaultToBeAdd) Put(GetContext(), GAS_VAULT_KEY, Add(gasVaultToBeAdd, getGasVault())) # update total paper amount Put(GetContext(), TOTAL_PAPER, Add(paperAmount, getTotalPaper())) # update sold paper amount for the usage of calculating the price key = concatKey(concatKey(ROUND_PREFIX, currentRound), ROUND_PAPER_AMOUNT) Put(GetContext(), key, Add(paperAmount, getRoundSoldPaperAmount(currentRound))) # update paper balance of account Put(GetContext(), concatKey(PAPER_BALANCE_PREFIX, account), Add(paperAmount, getPaperBalance(account))) updateDividendBalance(account) # update profitPerPaper oldProfitPerPaper = Get(GetContext(), PROFIT_PER_PAPER_KEY) profitPerPaperToBeAdd = Div(dividend, getTotalPaper()) Put(GetContext(), PROFIT_PER_PAPER_KEY, Add(profitPerPaperToBeAdd, oldProfitPerPaper)) # update profitPerPaperFrom of account # key = concatKey(PROFIT_PER_PAPER_FROM_PREFIX, account) # Put(GetContext(), key, oldProfitPerPaper) # update the account balances of dividend, award, referral Delete(GetContext(), concatKey(AWARD_BALANCE_OF_PREFFIX, account)) Delete(GetContext(), concatKey(REFERRAL_BALANCE_OF_PREFIX, account)) ongAmountLeft = Sub(assetToBeReinvest, ongAmount) Put(GetContext(), concatKey(TOTAL_DIVIDEND_OF_PREFIX, account), ongAmountLeft) # update the invested ong amount for account Put(GetContext(), concatKey(INVEST_BALANCE_PREFFIX, account), Add(ongAmount, getInvestOngBalance(account))) # PurchaseEvent(account, ongAmount, paperAmount) Notify(["reBuyPaper", account, ongAmount, paperAmount, GetTime()])
def getReferral(account): return Get(GetContext, concatKey(REFERRAL_PREFIX, account))
def endCurrentRound(): RequireWitness(Admin) # transfer Gas vault to admin to prepare for calculating winner of current round gasVault = getGasVault() if gasVault: Require(transferONGFromContact(Admin, gasVault)) currentRound = getCurrentRound() Require(getGameStatus(currentRound) == STATUS_ON) numberListKey = concatKey(concatKey(ROUND_PREFIX, currentRound), FILLED_NUMBER_LIST_KEY) numberListInfo = Get(GetContext(), numberListKey) if numberListInfo: numberList = Deserialize(numberListInfo) else: # if no one participate this round of game Put(GetContext(), concatKey(concatKey(ROUND_PREFIX, currentRound), ROUND_STATUS_KEY), STATUS_OFF) Notify(["endRound", currentRound]) startNewRound() return True # to record the minimum distance minDistance = 10000 # to record the number corresponding with minimum distance minIndex = 10000 luckyNumber = getLuckyNumber() for number in numberList: # get the L1 norm of the distance between number and luckyNumber distance = ASub(number, luckyNumber) if distance < minDistance: minDistance = distance minIndex = number winnersListKey = concatKey(concatKey(ROUND_PREFIX, currentRound), concatKey(FILLED_NUMBER_KEY, minIndex)) winnersListInfo = Get(GetContext(), winnersListKey) winnersList = Deserialize(winnersListInfo) winnersTotalPaper = 0 for winner in winnersList: winnersTotalPaper = Add(winnersTotalPaper, getPaperBalance(winner)) # split the Award Vault to the winners awardVault = getAwardVault(currentRound) totalTaxedAward = 0 for winner in winnersList: paperBalance = getPaperBalance(winner) winnerAward = Div(Mul(awardVault, paperBalance), winnersTotalPaper) winnerPercentage = winnerAward * 100 / getInvestOngBalance(winner) fee = 0 if winnerPercentage > 100: fee = Div(Mul(winnerAward, WinnerFeeLarge), 100) else: fee = Div(Mul(winnerAward, WinnerFeeSmall), 100) pureWinnerAwardToBeAdd = Sub(winnerAward, fee) totalTaxedAward = Add(totalTaxedAward, pureWinnerAwardToBeAdd) Put(GetContext(), concatKey(AWARD_BALANCE_OF_PREFFIX, winner), Add(pureWinnerAwardToBeAdd, getAwardBalance(winner))) # give Admin some fee from winner totalFee = Sub(awardVault, totalTaxedAward) Put(GetContext(), concatKey(TOTAL_DIVIDEND_OF_PREFIX, Admin), Add(totalFee, getDividendBalance(Admin))) # delete the filled paper in current round and update their PROFIT_PER_PAPER_FROM_PREFIX for number in numberList: numberPlayersKey = concatKey(concatKey(ROUND_PREFIX, currentRound), concatKey(FILLED_NUMBER_KEY, number)) numberPlayersInfo = Get(GetContext(), numberPlayersKey) numberPlayers = Deserialize(numberPlayersInfo) for player in numberPlayers: if getFilledPaperBalance(player, currentRound): updateDividendBalance(player) # update the player's paper balance Put(GetContext(), concatKey(PAPER_BALANCE_PREFIX, player), Sub(getPaperBalance(player), getFilledPaperBalance(player, currentRound))) # delete the filled paper balance of this round key = concatKey(concatKey(ROUND_PREFIX, currentRound), concatKey(FILLED_PAPER_BALANCE_PREFIX, player)) Delete(GetContext(), key) Notify(["destroyPaper", player, getFilledPaperBalance(player, currentRound), GetTime()]) # update the paper total amount Put(GetContext(), TOTAL_PAPER, Sub(getTotalPaper(), getFilledPaperAmount(currentRound))) # Notify(["destroy",getFilledPaperAmount(currentRound), GetTime()]) # mark this round game as END Put(GetContext(), concatKey(concatKey(ROUND_PREFIX, currentRound), ROUND_STATUS_KEY), STATUS_OFF) Notify(["endRound", currentRound]) startNewRound() return True
def buyPaper(account, paperAmount): RequireWitness(account) currentRound = getCurrentRound() Require(getGameStatus(currentRound) == STATUS_ON) ongAmount = paperToONG(currentRound, paperAmount) Require(transferONG(account, ContractAddress, ongAmount)) # PaperHolderPercentage = 50 dividend1 = Div(Mul(ongAmount, PaperHolderPercentage), 100) # update referral balance <---> Get(GetContext(), concatKey(REFERRAL_PREFIX, account)) referral = getReferral(account) referralAmount = 0 if referral: # ReferralAwardPercentage = 1 referralAmount = Div(Mul(ongAmount, ReferralAwardPercentage), 100) Put(GetContext(), concatKey(REFERRAL_BALANCE_OF_PREFIX, referral), Add(referralAmount, getReferralBalance(referral))) dividend = Sub(dividend1, referralAmount) # update next vault, NextPercentage = 10 nextVaultToBeAdd = Div(Mul(ongAmount, NextPercentage), 100) nextVaultKey = concatKey(concatKey(ROUND_PREFIX, currentRound), NEXT_VAULT_KEY) Put(GetContext(), nextVaultKey, Add(nextVaultToBeAdd, getNextVault(currentRound))) # update award vault, AwardPercentage = 35 awardVaultToBeAdd = Div(Mul(ongAmount, AwardPercentage), 100) awardVaultKey = concatKey(concatKey(ROUND_PREFIX, currentRound), AWARD_VAULT_KEY) Put(GetContext(), awardVaultKey, Add(awardVaultToBeAdd, getAwardVault(currentRound))) # update gas vault gasVaultToBeAdd = Sub(Sub(Sub(ongAmount, dividend1), nextVaultToBeAdd), awardVaultToBeAdd) Put(GetContext(), GAS_VAULT_KEY, Add(gasVaultToBeAdd, getGasVault())) # update total paper amount Put(GetContext(), TOTAL_PAPER, Add(paperAmount, getTotalPaper())) # update total invest ONG amount Put(GetContext(), concatKey(INVEST_BALANCE_PREFFIX, account), Add(ongAmount, getInvestOngBalance(account))) # update sold paper amount for the usage of calculating the price key = concatKey(concatKey(ROUND_PREFIX, currentRound), ROUND_PAPER_AMOUNT) Put(GetContext(), key, Add(paperAmount, getRoundSoldPaperAmount(currentRound))) # update paper balance of account Put(GetContext(), concatKey(PAPER_BALANCE_PREFIX, account), Add(paperAmount, getPaperBalance(account))) updateDividendBalance(account) # Notify(["111_buy", dividend1, dividend, nextVaultToBeAdd, awardVaultToBeAdd, gasVaultToBeAdd, getGasVault()]) # update profitPerPaper oldProfitPerPaper = Get(GetContext(), PROFIT_PER_PAPER_KEY) totalPaper = getTotalPaper() profitPerPaperToBeAdd = Div(dividend, totalPaper) Put(GetContext(), PROFIT_PER_PAPER_KEY, Add(profitPerPaperToBeAdd, oldProfitPerPaper)) # Notify(["222_buy", getGasVault()]) # # update profitPerPaperFrom of account # Put(GetContext(), concatKey(PROFIT_PER_PAPER_FROM_PREFIX, account), oldProfitPerPaper) # PurchaseEvent(account, ongAmount, paperAmount) Notify(["buyPaper", account, ongAmount, paperAmount, GetTime()]) return True
def get(): b = Get(ctx, 'test') a = Deserialize(b) Log(a['a']) Notify(a['b'])
def balanceOf(account): """ :param account: :return: the token balance of account """ return Get(ctx,concat(BALANCE_PREFIX,account))
def totalSupply(): """ :return: the total supply of the token """ return Get(ctx, SUPPLY_KEY)
def getAwardBalance(account): return Get(GetContext(), concatKey(AWARD_BALANCE_OF_PREFFIX, account))
def fillPaper(account, guessNumberList): """ :param account: :param guessNumberList: can be a list of numbers :return: """ RequireWitness(account) currentRound = getCurrentRound() Require(getGameStatus(currentRound) == STATUS_ON) guessNumberLen = len(guessNumberList) Require(guessNumberLen >= 1) currentFilledPaperBalance = getFilledPaperBalance(account, currentRound) # make sure his balance is greater or equal to current filled paper balance + guessNumberList length Require(getPaperBalance(account) >= Add(currentFilledPaperBalance, guessNumberLen)) numberListKey = concatKey(concatKey(ROUND_PREFIX, currentRound), FILLED_NUMBER_LIST_KEY) numberListInfo = Get(GetContext(), numberListKey) numberList = [] if numberListInfo: numberList = Deserialize(numberListInfo) for guessNumber in guessNumberList: Require(guessNumber < 10000 and guessNumber >= 0) numberPlayersKey = concatKey(concatKey(ROUND_PREFIX, currentRound), concatKey(FILLED_NUMBER_KEY, guessNumber)) numberPlayersInfo = Get(GetContext(), numberPlayersKey) numberPlayers = [] if numberPlayersInfo: numberPlayers = Deserialize(numberPlayersInfo) # make sure account has NOT filled the number before in this round for player in numberPlayers: Require(player != account) else: numberList.append(guessNumber) # add account to the players list that filled the number in this round numberPlayers.append(account) # Store the numberPlayers List numberPlayersInfo = Serialize(numberPlayers) Put(GetContext(), numberPlayersKey, numberPlayersInfo) # Store the numberList numberListInfo = Serialize(numberList) Put(GetContext(), numberListKey, numberListInfo) # update the filled paper amount in current round key = concatKey(concatKey(ROUND_PREFIX, currentRound), FILLED_PAPER_AMOUNT) Put(GetContext(), key, Add(guessNumberLen, getFilledPaperAmount(currentRound))) # update the filled paper balance in current round key1 = concatKey(ROUND_PREFIX, currentRound) key2 = concatKey(FILLED_PAPER_BALANCE_PREFIX, account) key = concatKey(key1, key2) Put(GetContext(), key, Add(guessNumberLen, currentFilledPaperBalance)) Notify(["fillPaper", account, guessNumberList, GetTime()]) return True
def getWithdrawnBalance(account): return Get(GetContext(), concatKey(WITHDRAWN_BALANCEOF_PREFFIX, account))
def getTotalPaper(): return Get(GetContext(), TOTAL_PAPER)
def getFilledPaperBalance(account, roundNum): key1 = concatKey(ROUND_PREFIX, roundNum) key2 = concatKey(FILLED_PAPER_BALANCE_PREFIX, account) return Get(GetContext(), concatKey(key1, key2))
def getGasVault(): return Get(GetContext(), GAS_VAULT_KEY)
def getNextVault(roundNum): return Get(GetContext(), concatKey(concatKey(ROUND_PREFIX, roundNum), NEXT_VAULT_KEY))
def GetStorage(key): return Get(ctx,key)