def checkPokerHash(gameId, pokerNum): """ can only check hash after end :param gameId: game's id :param pokeNum: player's poker num :return: pokeHash """ assert (pokerNum >= 1) assert (pokerNum <= 52) salt = getSaltAfterEnd(gameId) pokeHash = abs(sha256(pokerNum) ^ sha256(salt)) return pokeHash
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
def createOneToken(name, url, type): ''' create a new token :param name: :param url: :param type: :return: ''' # Notify(["111_createOneToken begins"]) # generate tokenID timestamp = GetTime() totalSupply = Get(ctx, TOTAL_SUPPLY) newTotalSupply = totalSupply + 1 Put(ctx, TOTAL_SUPPLY, newTotalSupply) tmp = concatkey(concatkey(selfAddr, timestamp), newTotalSupply) tokenID = sha256(tmp) # construct token map token = {'ID': tokenID, 'Name': name, 'Image': url, 'Type': type} Notify([ "111_createOneToken", newTotalSupply, tokenID, concatkey(TOKEN_ID_PREFIX, tokenID) ]) Put(ctx, concatkey(TOKEN_INDEX_PREFIX, newTotalSupply), tokenID) ownerKey = concatkey(OWNER_OF_TOKEN_PREFIX, tokenID) Put(ctx, ownerKey, admin) Put(ctx, concatkey(TOKEN_ID_PREFIX, tokenID), Serialize(token)) # add to adminBalance adminBalance = Get(ctx, concatkey(OWNER_BALANCE_PREFIX, admin)) Put(ctx, concatkey(OWNER_BALANCE_PREFIX, admin), adminBalance + 1) # Notify(["333_createOneToken ends"]) return True #################### For testing usage only ends ######################
def getRandom(): time = GetTime() height = GetHeight() header = GetHeader(height) # return sha256(abs(GetBlockHash(header)) % time) tmp = sha256(abs(GetBlockHash(header)) % time) Notify(["getRandom", tmp]) return tmp
def sendChainlinkRequestTo(caller, oracle, req, payment): RequireWitness(caller) requestCount = Get(GetContext(), REQUEST_COUNT) requestId = sha256(Serialize([req[1], caller, requestCount])) req[3] = requestCount Put(GetContext(), concatKey(PENDING_REQUESTS_PREFIX, requestId), oracle) ChainlinkRequestedEvent(requestId) link = Get(GetContext(), LINK_ADDRESS) params = [caller, oracle, payment, [SENDER_OVERRIDE, AMOUNT_OVERRIDE, req[0], req[1], req[2], req[3], ARGS_VERSION, req[4], 'oracleRequest']] assert (DynamicCallFunction(bytearray_reverse(link), "transferAndCall", params)) Put(GetContext(), REQUEST_COUNT, requestCount + 1) return True
def oracleRequest(spender, payment, specId, callbackAddress, callbackFunctionId, nonce, dataVersion, data, callFunctionId): onlyLINK() RequireWitness(spender) payment = payment + 0 requestId = sha256(Serialize([callbackAddress, spender, nonce])) assert (not Get(GetContext(), concatKey(COMMITMENTS_PRIFX, requestId))) expiration = GetTime() + EXPIRY_TIME Put(GetContext(), concatKey(COMMITMENTS_PRIFX, requestId), Serialize([payment, callbackAddress, callbackFunctionId, expiration])) OracleRequestEvent(specId, spender, requestId, payment, callbackAddress, callbackFunctionId, expiration, dataVersion, data, callFunctionId) return True