示例#1
0
def getInvestOngBalance(account):
    return Get(GetContext(), concatKey(INVEST_BALANCE_PREFFIX, account))
示例#2
0
def getReferralBalance(account):
    return Get(GetContext(), concatKey(REFERRAL_BALANCE_OF_PREFIX, account))
示例#3
0
def getCurrentRound():
    return Get(GetContext(), CURRET_ROUND_NUM_KEY)
示例#4
0
def getPaperBalance(account):
    return Get(GetContext(), concatKey(PAPER_BALANCE_PREFIX, account))
示例#5
0
def getAwardVault(roundNum):
    return Get(GetContext(), concatKey(concatKey(ROUND_PREFIX, roundNum), AWARD_VAULT_KEY))
示例#6
0
def GetInfoOf(account):
    return Get(ctx, account)
示例#7
0
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()])
示例#8
0
def getReferral(account):
    return Get(GetContext, concatKey(REFERRAL_PREFIX, account))
示例#9
0
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
示例#10
0
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
示例#11
0
def get():
    b = Get(ctx, 'test')
    a = Deserialize(b)
    Log(a['a'])
    Notify(a['b'])
示例#12
0
def balanceOf(account):
    """
    :param account:
    :return: the token balance of account
    """
    return Get(ctx,concat(BALANCE_PREFIX,account))
示例#13
0
def totalSupply():
    """
    :return: the total supply of the token
    """
    return Get(ctx, SUPPLY_KEY)
示例#14
0
def getAwardBalance(account):
    return Get(GetContext(), concatKey(AWARD_BALANCE_OF_PREFFIX, account))
示例#15
0
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
示例#16
0
def getWithdrawnBalance(account):
    return Get(GetContext(), concatKey(WITHDRAWN_BALANCEOF_PREFFIX, account))
示例#17
0
def getTotalPaper():
    return Get(GetContext(), TOTAL_PAPER)
示例#18
0
def getFilledPaperBalance(account, roundNum):
    key1 = concatKey(ROUND_PREFIX, roundNum)
    key2 = concatKey(FILLED_PAPER_BALANCE_PREFIX, account)
    return Get(GetContext(), concatKey(key1, key2))
示例#19
0
def getGasVault():
    return Get(GetContext(), GAS_VAULT_KEY)
示例#20
0
def getNextVault(roundNum):
    return Get(GetContext(), concatKey(concatKey(ROUND_PREFIX, roundNum), NEXT_VAULT_KEY))
示例#21
0
def GetStorage(key):
    return Get(ctx,key)