def Main(operation, args): if operation == "deposit": assert (len(args) == 4) address = args[0] amount = args[1] ongOrOnt = args[2] userId = args[3] return deposit(address, amount, ongOrOnt, userId) if operation == "preWithdraw": assert (len(args) == 4) address = args[0] amount = args[1] ongOrOnt = args[2] userId = args[3] return preWithdraw(address, amount, ongOrOnt, userId) if operation == "checkIn": assert (len(args) == 2) address = args[0] userId = args[1] return checkIn(address, userId) if operation == "canCheckIn": assert (len(args) == 1) address = args[0] return canCheckIn(address) if operation == "withdraw": assert (len(args) == 3) to = args[0] amount = args[1] ongOrOnt = args[2] return withdraw(to, amount, ongOrOnt) return False
def _onlyCLevel(): isCEO = CheckWitness(CEOAddress) isCTO = None isCOO = None CTOAddress = getCTO() if len(CTOAddress) == 20: isCTO = CheckWitness(CTOAddress) COOAddress = getCOO() if len(COOAddress) == 20: isCOO = CheckWitness(COOAddress) return isCEO or isCTO or isCOO
def Main(operation, args): ########## for Owner to invoke Begin ########## if operation == "init": return init() if operation == "addAdmin": Require(len(args) == 1) Account = args[0] return addAdmin(Account) if operation == "removeAdmin": Require(len(args) == 1) Account = args[0] return removeAdmin(Account) if operation == "migrateContract": Require(len(args) == 7) code = args[0] needStorage = args[1] name = args[2] version = args[3] author = args[4] email = args[5] description = args[6] return migrateContract(code, needStorage, name, version, author, email, description) ########## for Owner to invoke End ########## ########## for Owner and Admin to invoke Begin ########## if operation == "createProperty": Require(len(args) == 2) Account = args[0] createList = args[1] return createProperty(Account, createList) ########## for Owner and Admin to invoke End ########## ########## for Everyone to invoke Begin ########## if operation == "transferProperty": Require(len(args) == 1) transferList = args[0] return transferProperty(transferList) if operation == "removeProperty": Require(len(args) == 1) removeList = args[0] return removeProperty(removeList) if operation == "getPlayerAllDNA": Require(len(args) == 1) Account = args[0] return getPlayerAllDNA(Account) if operation == "getPlayerDNAFromRange": Require(len(args) == 3) Account = args[0] fromNum = args[1] toNum = args[2] return getPlayerDNAFromRange(Account, fromNum, toNum) if operation == "getPlayerDNANum": Require(len(args) == 1) Account = args[0] return getPlayerDNANum(Account)
def removeAuthorizedLevel(account): assert (CheckWitness(CEOAddress)) assert (len(account) == 20) authorizedAddressListInfo = Get(GetContext(), AUTHORIZED_ADDRESS_LIST_KEY) assert (authorizedAddressListInfo) authorizedAddressList = Deserialize(authorizedAddressListInfo) assert (_checkInList(account, authorizedAddressList)) index = _findInList(account, authorizedAddressList) # make sure index did exist in authorizedAddressList assert (index < len(authorizedAddressList)) authorizedAddressList.remove(index) Put(GetContext(), AUTHORIZED_ADDRESS_LIST_KEY, Serialize(authorizedAddressList)) Notify(["removeAuthorizedLevel", account]) return True
def purchase_bank(address, amount): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False else: # check if user has been created. if not, create the user user_info = Get(ctx, USERKEY) all_users = Deserialize(user_info) if address not in all_users: create_user(address) # if the user has been created, that means the bank map exists. update the user's bank and wallet else: bank_info = Get(ctx, BANKEY) bank_map = Deserialize(bank_info) bank_map[address] += amount bank_info = Serialize(bank_map) Put(ctx, BANKEY, bank_info) add_bank(address, amount) return True
def purchase_bank(address, amount): byte_address = Base58ToAddress(address) assert (CheckWitness(byte_address)) if len(byte_address) != 20: Notify(['Invalid address']) return False else: # check if user has been created. if not, create the user user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['No existing users']) create_user(address) if address not in all_users: Notify(['not a registered user']) create_user(address) # if the user has been created, that means the bank map exists. update the user's bank and wallet else: bank_info = Get(ctx, BANKEY) bank_map = Deserialize(bank_info) bank_map[address] += amount bank_info = Serialize(bank_map) Put(ctx, BANKEY, bank_info) Notify(['bank_map updated']) add_bank(address, amount) Notify(['user wallet updated']) return True
def removeProperty(removeList): """ :param removeList: [DNA1, DNA2] :return: bool """ DNACheck = removeList[0] account = Get(context, concatKey(DNA_PRE_KEY, DNACheck)) RequireScriptHash(account) RequireWitness(account) DNAlist = Get(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account)) if DNAlist: DNAlist = Deserialize(DNAlist) else: raise Exception("NO DNA") removeListLen = len(removeList) removeListIndex = 0 while removeListIndex < removeListLen: DNA = removeList[removeListIndex] findInList = _findInList(DNA, DNAlist) if findInList >= 0: Delete(context, concatKey(DNA_PRE_KEY, DNA)) DNAlist.remove(findInList) else: raise Exception("Not found DNA to be removed") removeListIndex += 1 Put(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account), Serialize(DNAlist)) Notify(["Remove property successfully"]) return True
def add_ong(address, amount): byte_address = Base58ToAddress(address) assert (CheckWitness(byte_address)) if len(byte_address) != 20: Notify(['Invalid address']) return False if amount < 0: Notify(['Negative amount']) return False supply = Get(ctx, ONGKEY) if supply < amount: Notify(['Not enough ONG in supply']) return False else: from_acct = contract_address to_acct = byte_address supply -= amount Put(ctx, ONGKEY, supply) Notify(['ONG supply decreased by', amount]) params = state(from_acct, to_acct, amount) return Invoke(1, contract_address_ONG, 'transfer', [params])
def view_bank(address): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not yet created']) return False # if the user has been created, the bank map exists. Check the user's bank else: bank_info = Get(ctx, BANKEY) bank_map = Deserialize(bank_info) bank_balance = bank_map[address] Notify([bank_balance]) return bank_balance
def add_bank(address, amount): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False if token_supply() < amount: Notify(['Not enough tokens']) return False else: from_acct = contract_address to_acct = byte_address supply = Get(ctx, SUPPKEY) if supply < amount: Notify(['Not enough tokens in supply']) return False else: supply -= amount Put(ctx, SUPPKEY, supply) Notify(['Supply decreased by', amount]) params = [from_acct, to_acct, amount] return RepContract('transfer', params)
def createProperty(createList): """ :param createList: [[account1, DNA1],[account2, DNA2]] :return: bool """ RequireWitness(Owner) for createE in createList: account = createE[0] DNA = createE[1] accountCheck = Get(context, concatKey(DNA_PRE_KEY, DNA)) Require(len(account) == 20) Require(DNA > 100000000000000) Require(DNA < 1000000000000000) Require(not accountCheck) DNAlist = Get(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account)) if not DNAlist: DNAlist = [] else: DNAlist = Deserialize(DNAlist) DNAlist.append(DNA) Put(context, concatKey(DNA_PRE_KEY, DNA), account) Put(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account), Serialize(DNAlist)) Notify(["Create property successfully."]) return True
def view_ong(address): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not yet created']) return False # if the above checks hold, we can check the user's wallet. else: param = state(byte_address) ong_balance = Invoke(0, contract_address_ONG, 'balanceOf', param) if ong_balance == '': ong_balance = 0 Notify([ong_balance]) return ong_balance
def approve(owner, spender, tokenId, amount): """ approve amount of the tokenId token to toAcct address, it can overwrite older approved amount :param owner: :param spender: :param tokenId: :param amount: :return: """ Require(_whenNotPaused()) # make sure the invoker is the owner address RequireWitness(owner) # make sure the address is legal Require(len(spender) == 20) Require(_tokenExist(tokenId)) ownerBalance = balanceOf(owner, tokenId) # you can use "if" to notify the corresponding message, or use assert to raise exception Require(ownerBalance >= amount) Require(amount > 0) key = _concatkey(_concatkey(_concatkey(APPROVE_PREFIX, tokenId), owner), spender) Put(GetContext(), key, amount) ApprovalEvent(owner, spender, tokenId, amount) return True
def add_bank(address, amount): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False if amount < 0: Notify(['Negative amount']) return False supply = Get(ctx, AESKEY) if supply < amount: Notify(['Not enough AES in supply']) return False else: from_acct = contract_address to_acct = byte_address supply -= amount Put(ctx, AESKEY, supply) Notify(['AES supply decreased by', amount]) params = [from_acct, to_acct, amount] return AesContract('transfer', params)
def create_user(address, username, bio): # only the address can invoke the method assert (CheckWitness(address)) # check if username has been created. if not, initialize it. Otherwise, rollback the transaction assert (not user_exist(address)) # check if user has been created. if not, initialize it. Otherwise, rollback the transaction assert (not get_address_by_username(username)) # mark username as being used one Put(ctx, concatkey(username, UN_EXIST_PREFIX), address) if len(bio) > 100: Notify(['Bio length exceeds 100 characters']) # create profile info list profile = [username, bio] profile_info = Serialize(profile) Put(ctx, concatkey(address, PI_PREFIX), profile_info) # update reputation of address Put(ctx, concatkey(address, REPKEY_PREFIX), (100 + 100000000) * AES_FACTOR) # distribute a certain amount of token to the newly registered user token_amount = 100 * AES_FACTOR assert (withdraw(address, token_amount)) Notify(["userCreated", address, username, bio]) return True
def subtract_bank(address, amount): byte_address = Base58ToAddress(address) assert (CheckWitness(byte_address)) if len(byte_address) != 20: Notify(['Invalid address']) return False if amount < 0: Notify(['Negative amount']) return False if RepContract('balanceOf', [byte_address]) < amount: Notify(['Not enough AES in wallet']) return False else: from_acct = byte_address to_acct = contract_address supply = Get(ctx, AESKEY) supply += amount Put(ctx, AESKEY, supply) Notify(['AES supply increased by', amount]) params = [from_acct, to_acct, amount] return RepContract('transfer', params)
def view_rep(address): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not yet created']) return False # if the user has been created, the rep map exists. Check the user's rep else: rep_info = Get(ctx, REPKEY) rep_map = Deserialize(rep_info) rep_balance = rep_map[address] Notify([rep_balance]) return rep_balance
def transferProperty(transferList): """ :param transferList: [[toAccount1, DNA1],[toAccount2, DNA2]] :return: bool """ DNACheck = transferList[0][1] account = Get(context, concatKey(DNA_PRE_KEY, DNACheck)) RequireWitness(account) for transferE in transferList: toAccount = transferE[0] DNA = transferE[1] DNAlist = Get(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account)) DNAlist = Deserialize(DNAlist) toDNAlist = Get(context, concatKey(PLAYER_ADDRESS_PRE_KEY, toAccount)) if not toDNAlist: toDNAlist = [] else: toDNAlist = Deserialize(DNAlist) num = 0 while num < len(DNAlist): if DNAlist[num] == DNA: Put(context, concatKey(DNA_PRE_KEY, DNA), toAccount) DNAlist.remove(num) toDNAlist.append(DNA) Put(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account), Serialize(DNAlist)) Put(context, concatKey(PLAYER_ADDRESS_PRE_KEY, toAccount), Serialize(toDNAlist)) num += 1 Notify(["Transfer property successfully"]) return True
def subtract_ong(address, amount): byte_address = Base58ToAddress(address) assert (CheckWitness(byte_address)) if len(byte_address) != 20: Notify(['Invalid address']) return False if amount < 0: Notify(['Negative amount']) return False param = state(byte_address) if Invoke(0, contract_address_ONG, 'balanceOf', param) < amount: Notify(['Not enough ONG in wallet']) return False else: from_acct = byte_address to_acct = contract_address supply = Get(ctx, ONGKEY) supply += amount Put(ctx, ONGKEY, supply) Notify(['ONG supply increased by', amount]) params = state(from_acct, to_acct, amount) return Invoke(1, contract_address_ONG, 'transfer', [params])
def profile(address): byte_address = Base58ToAddress(address) # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not created']) return False # check if address is valid if len(byte_address) != 20: Notify(['Invalid address']) return False profile_info = Get(ctx, concatkey(address, PI_PREFIX)) if profile_info: profile = Deserialize(profile_info) else: Notify(['The profile does not exist']) return False Notify(['profile', profile]) return profile
def user_tab(address): byte_address = Base58ToAddress(address) # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not created']) return False # check if address is valid if len(byte_address) != 20: Notify(['Invalid address']) return False total_rep = view_rep(address) total_wallet = view_wallet(address) total_ong = view_ong(address) Notify([total_rep, total_wallet, total_ong]) return [total_rep, total_wallet, total_ong]
def check_result(betId, current_price): """ :param bet: :param current_price: :return: 0 means against wins, 1 means for wins, 2 means a draw, 3 means bet inactive """ if not Get(ctx, concatkey(betId, ACTIVE_BET_PREFIX)): return 3 # sign, margin, target_price, vote_end_time, stock_ticker bet_details = get_bet_details(betId) assert (len(bet_details) == 5) sign = bet_details[0] margin = bet_details[1] target_price = bet_details[2] if margin == 0 and current_price == target_price: return 2 # case where user bets that stock will rise if sign > 0 and current_price >= target_price: return 1 # case where user bets that stock will fall if sign < 0 and current_price <= target_price: return 1 return 0
def Main(operation, args): if operation == "addAdmin": Account = args[0] return addAdmin(Account) if operation == "createProperty": createList = args[0] return createProperty(createList) if operation == "transferProperty": transferList = args[0] return transferProperty(transferList) if operation == "removeProperty": removeList = args[0] return removeProperty(removeList) if operation == "getPlayerAllDNA": account = args[0] return getPlayerAllDNA(account) if operation == "migrateContract": Require(len(args) == 8) code = args[0] needStorage = args[1] name = args[2] version = args[3] author = args[4] email = args[5] description = args[6] return migrateContract(code, needStorage, name, version, author, email, description)
def view_wallet(address): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not yet created']) return False # if the above checks hold, we can check the user's wallet. else: params = [byte_address] wallet_balance = RepContract("balanceOf", params) if wallet_balance == '': wallet_balance = 0 Notify([wallet_balance]) return wallet_balance
def createProperty(Account, createList): """ only contract owner or admin can create property cannot create more than 1000 property once :param createList: [Account, [[account1, DNA1],[account2, DNA2]]] :return: bool """ Require(Get(context, concatKey(ADMIN_ADDRESS_KEY, Account)) == 1) RequireWitness(Account) DNAlist = Get(context, concatKey(PLAYER_ADDRESS_PRE_KEY, createList[0][0])) if not DNAlist: DNAlist = [] else: DNAlist = Deserialize(DNAlist) Require(len(createList) <= 1000) for createE in createList: account = createE[0] DNA = createE[1] accountCheck = Get(context, concatKey(DNA_PRE_KEY, DNA)) Require(len(account) == 20) # check len Require(DNA >= 100000000000000) Require(DNA < 10000000000000000) # check kind Require(Div(DNA, 100000000000000) % 100 > 0) Require(Div(DNA, 100000000000000) % 100 < 100) # check grade Require(Div(DNA, 1000000000000) % 100 > 0) Require(Div(DNA, 1000000000000) % 100 < 100) # check name Require(Div(DNA, 1000000000) % 1000 > 0) Require(Div(DNA, 1000000000) % 1000 < 1000) # check number Require(Div(DNA, 1000) % 1000000 > 0) Require(Div(DNA, 1000) % 1000000 < 1000000) # check random Require(DNA % 1000 >= 0) Require(DNA % 1000 < 1000) # check DNA Require(not accountCheck) DNAlist.append(DNA) Put(context, concatKey(DNA_PRE_KEY, DNA), account) Put(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account), Serialize(DNAlist)) Notify(["Create property successfully."]) return True
def removeAuthorizedLevel(account): RequireWitness(CEOAddress) Require(len(account) == 20) # make sure account is authorized before. Require(isAuthorizedLevel(account)) Delete(GetContext(), _concatkey(AUTHORIZED_LEVEL_PREFIX, account)) Notify(["removeAuthorizedLevel", account]) return True
def _findInList(DNA, DNAlist): DNAListIndex = 0 DNAListLen = len(DNAlist) while DNAListIndex < DNAListLen: if DNA == DNAlist[DNAListIndex]: return DNAListIndex DNAListIndex += 1 return -1
def create_user(address): byte_address = Base58ToAddress(address) # only the address can invoke the method assert (CheckWitness(byte_address)) # check if user list has been created. if not, initialize it user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: all_users = [] if address in all_users: Notify(['User already created']) return False if len(byte_address) != 20: Notify(['Invalid address']) return False else: # check if rep map has been created. if not, initialize it rep_info = Get(ctx, REPKEY) if rep_info: rep_map = Deserialize(rep_info) else: rep_map = {} # update and put rep map rep_map[address] = (100 + 100000000) * FACTOR rep_info = Serialize(rep_map) Put(ctx, REPKEY, rep_info) Notify(['rep_map updated']) # update and put user list all_users.append(address) user_info = Serialize(all_users) Put(ctx, USERKEY, user_info) Notify(['all_users updated']) # check if bank map has been created. if not, initialize it bank_info = Get(ctx, BANKEY) if bank_info: bank_map = Deserialize(bank_info) else: bank_map = {} # update and put bank map bank_map[address] = 100 * FACTOR bank_info = Serialize(bank_map) Put(ctx, BANKEY, bank_info) Notify(['bank_map updated']) # add to wallet of user add_bank(address, 100 * FACTOR) Notify(['user wallet updated']) return True
def RequireScriptHash(key): """ Checks the bytearray parameter is script hash or not. Script Hash length should be equal to 20. :param key: bytearray parameter to check script hash format. :return: True if script hash or revert the transaction. """ Require(len(key) == 20) return True
def transferFromMulti(args): """ multiple transferFrom :param args: args:[[spender1, fromAcct1, toAcct1, tokenId1, amount1],[spender2, fromAcct2, toAcct2, tokenId2, amount2]] :return: """ for p in args: assert (len(p) == 5) assert (transferFrom(p[0], p[1], p[2], p[3], p[4])) return True