def getRandomNumber():
    callerHash = GetCallingScriptHash()
    entryHash = GetEntryScriptHash()
    Require(callerHash == entryHash)
    randomHash = GetCurrentBlockHash()
    randomNumber = abs(randomHash) % 100
    return randomNumber
Exemplo n.º 2
0
def _rollANumber():
    blockHash = GetCurrentBlockHash()
    tx = GetScriptContainer()
    txhash = GetTransactionHash(tx)
    theNumber = abs(blockHash ^ txhash) % 100
    theNumber = abs(theNumber) + 1
    return theNumber
Exemplo n.º 3
0
def getRandomNumber():
    callerHash = GetCallingScriptHash()
    entryHash = GetEntryScriptHash()
    Require(callerHash == entryHash)
    randomHash = GetCurrentBlockHash()
    winers = getWiners()
    randomNumber = abs(randomHash) % len(winers)
    return randomNumber
Exemplo n.º 4
0
def startGame(pokerHashList, playerList, gameId):
    """
    admin send param to start game
    algorithm: pokeHash = abs(sha256(pokerNum) ^ sha256(salt))
    :param pokerHashList: [pokerHash1, pokerHash2, pokerHash3, ..., pokerHash52] send a list include 52 pokerHash
    :param playerList: [address1, address2, address3...] send all player address in this game
    :param gameId: game's id
    :return: bool
    """
    assert (CheckWitness(Admin))
    playerNum = len(playerList)
    pokerNum = len(pokerHashList)
    helperRandom = abs(GetCurrentBlockHash()) % pokerNum
    # deal poker to player
    playerPokerList = []

    tmp = 0
    while tmp < playerNum:
        # deal first poker
        playerHandPokerList = []
        poker = pokerHashList[helperRandom]
        pokerHashList.remove(helperRandom)
        playerHandPokerList.append(poker)
        pokerNum = Sub(pokerNum, 1)

        # deal second poker
        helperRandom = abs(sha256(concat(helperRandom, poker))) % pokerNum
        poker = pokerHashList[helperRandom]
        pokerHashList.remove(helperRandom)
        playerHandPokerList.append(poker)
        pokerNum = Sub(pokerNum, 1)

        helperRandom = abs(sha256(concat(helperRandom, poker))) % pokerNum
        tmp += 1
        playerPokerList.append(playerHandPokerList)

    # deal common poker
    commonPokerList = []
    commonPokerNum = 0
    while commonPokerNum < 5:
        poker = pokerHashList[helperRandom]
        pokerHashList.remove(helperRandom)
        commonPokerList.append(poker)
        pokerNum = Sub(pokerNum, 1)
        helperRandom = abs(sha256(concat(helperRandom, poker))) % pokerNum
        commonPokerNum += 1

    Notify(
        ["startGame", pokerHashList, commonPokerList, playerPokerList, gameId])
    return True
Exemplo n.º 5
0
def createExchange(token_hash):
    # Ensure token is a contract with nonzero contract hash
    assert (token_hash != ZERO_ADDRESS and len(token_hash) == 20)

    # Ensure templateCode existgetExchange
    templateScript = Get(GetContext(), EXCHANGE_TEMPLATE_KEY)
    assert (len(templateScript) > 0)
    # Make sure the token_hash has not been used to create exchange before
    assert (len(getExchange(token_hash)) == 0)

    tokenCount = Get(GetContext(), TOKEN_COUNT_KEY)

    # Append unused byte code to avm code to produce different contract hash, yet same executable opcode
    newTokenCount = tokenCount + 1
    templateScript = concat(
        templateScript,
        concat(
            concat(concat(GetExecutingScriptHash(), GetCurrentBlockHash()),
                   GetTime()), newTokenCount))

    # Deploy replica contract
    assert (Create(
        templateScript, True, "uniswap_exchange", "1.0", "uniswap_factory",
        "email",
        "uniswap_exchange contract created by uniswap_factory contract"))

    # Invoke the newly deployed contract to set up the token exchange pair
    exchangeHash = AddressFromVmCode(templateScript)
    exchangeAddr = bytearray_reverse(exchangeHash)
    tokenAddr = bytearray_reverse(token_hash)
    assert (DynamicAppCall(exchangeAddr, "setup",
                           [tokenAddr, GetExecutingScriptHash()]))

    # Store the map between token and exchange contract hash
    Put(GetContext(), concat(TOKEN_TO_EXCHANGE_PREFIX, token_hash),
        exchangeAddr)
    Put(GetContext(), concat(EXCHANGE_TO_TOKEN_PREFIX, exchangeHash),
        tokenAddr)

    # Add the token count
    Put(GetContext(), TOKEN_COUNT_KEY, newTokenCount)

    # Map token with token id
    Put(GetContext(), concat(ID_TO_TOKEN_PREFIX, newTokenCount), tokenAddr)

    # Fire the event
    NewExchangeEvent(tokenAddr, exchangeAddr)
    return True
def getRandomNumber():
    randomHash = GetCurrentBlockHash()
    randomNumber = abs(randomHash) % 100000000
    return randomNumber