def do_approve(self, project_id, t_owner, t_spender, amount): storage = StorageManager() if not CheckWitness(t_owner): print("Incorrect permission") return False from_balance = storage.get_double(project_id, t_owner) # cannot approve an amount that is # currently greater than the from balance if from_balance >= amount: approval_key = concat(t_owner, t_spender) current_approved_balance = storage.get_double( project_id, approval_key) new_approved_balance = current_approved_balance + amount storage.put_double(project_id, approval_key, new_approved_balance) OnApprove(project_id, t_owner, t_spender, amount) return True return False
def fr_list_append(project_id, key, new_item): """Adds the input list of funding stages to storage (as serialized array) Args: project_id (list): ID for referencing the project new_list (list): list of funding stages to save to storage """ storage = StorageManager() print('new_item') print(new_item) # Gets current stored list current_serialized_list = storage.get_double(project_id, key) # Converts serialized list to normal list current_list = storage.deserialize_bytearray(current_serialized_list) current_list_len = len(current_list) print('current_list_len') print(current_list_len) if current_list_len == 0: output_list = [new_item] else: output_list = current_list output_list.append(new_item) # Serializes list serialized_output_list = storage.serialize_array(output_list) print('serialized_output_list') print(serialized_output_list) # Saves updated serialized list to storage storage.put_double(project_id, key, serialized_output_list)
def sts_get(project_id) -> SmartTokenShare: """ Get the info list Args: project_id (str): ID for referencing the project Return: (SmartTokenShare): Returns a Smart Token Share object containing attributes """ storage = StorageManager() sts = SmartTokenShare() # Pull STS info sts_info_serialized = storage.get_double('STS', project_id) if sts_info_serialized: sts_info = storage.deserialize_bytearray(sts_info_serialized) # Saves vars to object sts.project_id = project_id sts.symbol = sts_info[0] sts.decimals = sts_info[1] sts.owner = sts_info[2] sts.total_supply = sts_info[3] sts.total_in_circulation = sts_info[4] print('sts_get') print(sts.owner) return sts
def handle_sts(self, operation, args): # project_id always first arg project_id = args[0] sts = sts_get(project_id) storage = StorageManager() arg_error = 'Incorrect Arg Length' if operation == 'decimals': return sts.decimals elif operation == 'symbol': return sts.symbol elif operation == 'totalSupply': return sts.total_supply elif operation == 'balanceOf': if len(args) == 2: account = args[1] return storage.get_double(project_id, account) return arg_error elif operation == 'transfer': if len(args) == 4: t_from = args[1] t_to = args[2] t_amount = args[3] return self.do_transfer(project_id, t_from, t_to, t_amount) return arg_error elif operation == 'transferFrom': if len(args) == 4: t_from = args[1] t_to = args[2] t_amount = args[3] return self.do_transfer_from(project_id, t_from, t_to, t_amount) return arg_error elif operation == 'approve': if len(args) == 4: t_owner = args[1] t_spender = args[2] t_amount = args[3] return self.do_approve(project_id, t_owner, t_spender, t_amount) return arg_error elif operation == 'allowance': if len(args) == 3: t_owner = args[1] t_spender = args[2] return self.do_allowance(project_id, t_owner, t_spender) return arg_error return False
def do_allowance(self, project_id, t_owner, t_spender): storage = StorageManager() allowance_key = concat(t_owner, t_spender) amount = storage.get_double(project_id, allowance_key) return amount
def test_exchange(self): fs = FundingStage() attachments = get_asset_attachments() # Test vars tokens_per_gas = 100 test_exchanged_sts = attachments.gas_attached * tokens_per_gas / 100000000 # Sets balance to 0 storage = StorageManager() storage.put_double(self.test_project_id, attachments.sender_addr, 0) # Registers KYC address storage.put_triple(self.test_project_id, 'KYC_address', self.test_address1, True) # Setting default info storage = StorageManager() storage.delete_triple('FS', self.test_project_id, self.test_funding_stage_id) # Creates new test fund fs.create(self.test_project_id, self.test_funding_stage_id, 1, 999999, 10000, tokens_per_gas) # Testing exchange method and checking stored balance fs.exchange(self.test_project_id, self.test_funding_stage_id) result1 = storage.get_double(self.test_project_id, attachments.sender_addr) # Testing exchange method and checking stored balance again (should double) fs.exchange(self.test_project_id, self.test_funding_stage_id) result2 = storage.get_double(self.test_project_id, attachments.sender_addr) # Check Test print('CHECK') if result1 == test_exchanged_sts and result2 == test_exchanged_sts * 2: print('test_exchange PASSED') return True print('test_exchange FAILED') return False
def claim(): storage = StorageManager() attachments = get_asset_attachments() claim_amount = storage.get_double('CLAIM', attachments.receiver_addr) if claim_amount == attachments.gas_attached: return True return False
def do_transfer_from(self, project_id, t_from, t_to, amount): storage = StorageManager() if amount <= 0: return False available_key = concat(t_from, t_to) available_to_to_addr = storage.get_double(project_id, available_key) if available_to_to_addr < amount: print("Insufficient funds approved") return False from_balance = storage.get_double(project_id, t_from) if from_balance < amount: print("Insufficient tokens in from balance") return False to_balance = storage.get_double(project_id, t_to) new_from_balance = from_balance - amount new_to_balance = to_balance + amount storage.put_double(project_id, t_to, new_to_balance) storage.put_double(project_id, t_from, new_from_balance) print("transfer complete") new_allowance = available_to_to_addr - amount if new_allowance == 0: print("removing all balance") storage.delete_double(project_id, available_key) else: print("updating allowance to new allowance") storage.put_double(project_id, available_key, new_allowance) OnTransfer(project_id, t_from, t_to, amount) return True
def do_transfer(self, project_id, t_from, t_to, amount): storage = StorageManager() # Pointless if amount <= 0: return False # Validate address if CheckWitness(t_from): # Pointless if t_from == t_to: print("transfer to self!") return True from_val = storage.get_double(project_id, t_from) if from_val < amount: print("insufficient funds") return False if from_val == amount: storage.delete_double(project_id, t_from) else: difference = from_val - amount storage.put_double(project_id, t_from, difference) to_value = storage.get_double(project_id, t_to) to_total = to_value + amount storage.put_double(project_id, t_to, to_total) OnTransfer(project_id, t_from, t_to, amount) return True else: print("from address is not the tx sender") return False
def fr_get_active_index(project_id): """ Gets the active index Args: project_id (list): ID for referencing the project Return: (int): Index """ storage = StorageManager() idx = storage.get_double(project_id, 'FR_active_idx') return idx
def sts_create(project_id, symbol, decimals, owner, total_supply) -> SmartTokenShare: """ Args: project_id (str): ID for referencing the project symbol (str): Representation symbol decimals (int): Amount of decimal places, default 8 owner (bytes): Owner of the token total_supply (int): total supply of the token Return: (SmartTokenShare): Returns a Smart Token Share object containing these attributes """ # init objects storage = StorageManager() sts = SmartTokenShare() # Saves vars to object sts.project_id = project_id sts.symbol = symbol sts.decimals = decimals sts.owner = owner sts.total_supply = total_supply # Default circulation sts.total_in_circulation = 0 # Info structure sts_info = [symbol, decimals, owner, total_supply, 0] # Will only save to storage if none exsits for this project_id if not storage.get_double('STS', project_id): sts_info_serialized = storage.serialize_array(sts_info) storage.put_double('STS', project_id, sts_info_serialized) return sts
def test_create(self): sts = SmartTokenShare() # Running create() sts.create(self.test_project_id, self.test_symbol, self.test_decimals, self.test_owner, self.test_total_supply) # Pull output from storage storage = StorageManager() output = storage.get_double('STS', self.test_project_id) # Check Test if output == self.test_sts_info: print('test_create PASSED') return True print('test_create FAILED') return False
def fr_get_list(project_id, key): """ Registers all input addresses Args: project_id (list): ID for referencing the project Return: (list): Output list for key """ storage = StorageManager() # Gets current stored list serialized_list = storage.get_double(project_id, key) # Converts serialized list to normal list output_list = storage.deserialize_bytearray(serialized_list) # output_list = ['sdad','sdads'] return output_list
def Main(operation, args): """Entry point for the smart contract. Args: operation (str): UUID used as the first part of the key for Storage.Put(). args (str): UUID used as the second part of the key for Storage.Put(). Return: (bytearray): The result of the operation """ # Gets the transaction trigger trigger = GetTrigger() storage = StorageManager() invalid_args_msg = 'INVALID ARGS' invaild_op_msg = 'INVALID OPERATION' if trigger == Verification: print('Verification') attachments = get_asset_attachments() prev_attachments = get_asset_attachments_for_prev() gas_requested = prev_attachments.gas_attached - attachments.gas_attached print(gas_requested) # Get amount avaliable for address claim_amount = storage.get_double('CLAIM', attachments.receiver_addr) # If the request is the EXACT amount (not less), approve the tx if claim_amount == gas_requested: print('Successfully send claim tx') return True elif trigger == Application: print('Application') kyc = KYC() # F U N D I N G R O A D M A P # project_id = args[0] sts = sts_get(project_id) # ARGS: project_id, refund_addr if operation == 'check_claim_owed': OnOperationInvoke('check_claim_owed') print('execute:check_claim_owed') if len(args) == 2: refund_addr = args[1] return storage.get_double('CLAIM', refund_addr) # ARGS: project_id, refund_addr if operation == 'reset_claim_owed': OnOperationInvoke('reset_claim_owed') print('execute:reset_claim_owed') if len(args) == 2: refund_addr = args[1] return storage.put_double('CLAIM', refund_addr, 0) # ARGS: project_id, new_admin if operation == 'add_project_admins': OnOperationInvoke('add_project_admins') print('execute:add_project_admins') if len(args) == 2: if CheckWitness(sts.owner): new_admin = args[1] fr_add_project_admin(project_id, new_admin) return True return invalid_args_msg # ARGS: project_id if operation == 'get_active_index': OnOperationInvoke('get_active_index') print('execute:get_active_index') if len(args) == 1: return fr_get_active_index(project_id) return invalid_args_msg # ARGS: project_id if operation == 'get_funding_stages': OnOperationInvoke('get_funding_stages') print('execute:get_funding_stages') if len(args) == 1: funding_stages = fr_get_funding_stages(project_id) return funding_stages return invalid_args_msg # ARGS: project_id if operation == 'get_active_fs': OnOperationInvoke('get_active_fs') print('execute:get_active_fs') if len(args) == 1: active_idx = fr_get_active_index(project_id) funding_stages = fr_get_funding_stages(project_id) active_funding_stage = funding_stages[active_idx] return active_funding_stage return invalid_args_msg # ARGS: project_id if operation == 'get_milestones': OnOperationInvoke('get_milestones') print('execute:get_milestones') if len(args) == 1: milestones = fr_get_milestones(project_id) return milestones return invalid_args_msg # ARGS: project_id if operation == 'get_active_ms': OnOperationInvoke('get_active_ms') print('execute:get_active_ms') if len(args) == 1: active_idx = fr_get_active_index(project_id) milestones = fr_get_milestones(project_id) active_milestone = milestones[active_idx] return active_milestone return invalid_args_msg # ARGS: project_id, updated_progress if operation == 'update_active_ms_progress': OnOperationInvoke('update_active_ms_progress') print('execute:update_active_ms_progress') if len(args) == 2: if CheckWitness(sts.owner): updated_progress = args[1] progress = fr_update_milestone_progress( project_id, updated_progress) return progress return invalid_args_msg # S M A R T T O K E N S H A R E # # ARGS: project_id, symbol, decimals, owner, total_supply if operation == 'create_sts': OnOperationInvoke('create_sts') print('execute:create_sts') if len(args) == 5: symbol = args[1] decimals = 8 # hardcoded to 8 owner = args[3] total_supply = args[4] sts_create(project_id, symbol, decimals, owner, total_supply) fr_set_active_index(project_id, 0) return project_id return invalid_args_msg # ARGS: project_id, attribute: {'project_id', 'symbol', 'decimals', 'owner', 'total_supply', 'total_in_circulation'} if operation == 'sts_attribute': OnOperationInvoke('sts_attribute') print('execute:sts_attribute') if len(args) == 2: attr = args[1] sts = sts_get(project_id) return sts_get_attr(sts, attr) return invalid_args_msg # ARGS: project_id if operation == 'total_tokens_available': OnOperationInvoke('total_tokens_available') print('execute:total_tokens_available') if len(args) == 1: sts = sts_get(project_id) return sts_total_available_amount(sts) return invalid_args_msg # F U N D I N G S T A G E # funding_stage_id = args[1] # ARGS: project_id, funding_stage_id, start_block, end_block, supply, tokens_per_gas if operation == 'create_fs': OnOperationInvoke('create_fs') print('execute:create_fs') if len(args) == 6: if CheckWitness(sts.owner): start_block = args[2] end_block = args[3] supply = args[4] tokens_per_gas = args[5] fs_create(project_id, funding_stage_id, start_block, end_block, supply, tokens_per_gas) fr_add_funding_stage(project_id, funding_stage_id) return funding_stage_id return invalid_args_msg # ARGS: project_id, funding_stage_id, attribute: {'project_id', 'funding_stage_id', 'start_block', 'end_block', 'supply', 'tokens_per_gas', 'in_circulation'} if operation == 'fs_attribute': OnOperationInvoke('fs_attribute') print('execute:fs_attribute') if len(args) == 3: attr = args[2] fs = fs_get(project_id, funding_stage_id) return fs_get_attr(fs, attr) # ARGS: project_id, funding_stage_id if operation == 'fs_tokens_available': OnOperationInvoke('fs_tokens_available') print('execute:fs_tokens_available') if len(args) == 2: fs = fs_get(project_id, funding_stage_id) return fs_available_amount(fs) return invalid_args_msg # ARGS: project_id, funding_stage_id if operation == 'fs_status': OnOperationInvoke('fs_status') print('execute:fs_status') if len(args) == 2: fs = fs_get(project_id, funding_stage_id) return fs_status(fs) return invalid_args_msg # ARGS: project_id, funding_stage_id if operation == 'fs_contribute': OnOperationInvoke('fs_contribute') print('execute:fs_contribute') if len(args) == 2: fs = fs_get(project_id, funding_stage_id) return fs_contribute(fs) return invalid_args_msg # ARGS: project_id, funding_stage_id, addr if operation == 'fs_addr_balance': OnOperationInvoke('fs_addr_balance') print('execute:fs_addr_balance') if len(args) == 2: addr = args[2] fs = fs_get(project_id, funding_stage_id) return fs_get_addr_balance(fs, addr) return invalid_args_msg # M I L E S T O N E # milestone_id = args[1] # ARGS: project_id, milestone_id, title, subtitle, extra_info_hash if operation == 'create_ms': OnOperationInvoke('create_ms') print('execute:create_ms') if len(args) == 5: if CheckWitness(sts.owner): title = args[2] subtitle = args[3] extra_info_hash = args[4] ms_create(project_id, milestone_id, title, subtitle, extra_info_hash) fr_add_milestone(project_id, milestone_id) return milestone_id return invalid_args_msg # ARGS: project_id, milestone_id, attribute: {'project_id', 'milestone_id', 'title', 'subtitle', 'extra_info_hash', 'progress'} if operation == 'ms_attribute': OnOperationInvoke('ms_attribute') print('execute:ms_attribute') if len(args) == 3: attr = args[2] ms = ms_get(project_id, milestone_id) return ms_get_attr(ms, attr) # ARGS: project_id, milestone_id if operation == 'get_ms_progess': OnOperationInvoke('get_ms_progess') print('execute:get_ms_progess') if len(args) == 2: ms = ms_get(project_id, milestone_id) return ms_get_progress(ms) return invalid_args_msg # C L A I M S # funding_stage_id = args[1] # ARGS: project_id, funding_stage_id, refund_addr if operation == 'claim_fs_refund': OnOperationInvoke('claim_fs_refund') print('execute:claim_fs_refund') if len(args) == 3: refund_addr = args[2] fs = fs_get(project_id, funding_stage_id) return fs_refund(fs, refund_addr) return invalid_args_msg # ARGS: project_id, funding_stage_id, owner_addr if operation == 'claim_fs_contributions': OnOperationInvoke('claim_fs_contributions') print('execute:claim_fs_contributions') if len(args) == 3: owner_addr = args[2] fs = fs_get(project_id, funding_stage_id) return fs_claim_contributions(fs, owner_addr) return invalid_args_msg # ARGS: project_id, funding_stage_id, system_owner_addr if operation == 'claim_fs_system_fee': OnOperationInvoke('claim_fs_system_fee') print('execute:claim_fs_system_fee') if len(args) == 3: system_owner_addr = args[2] fs = fs_get(project_id, funding_stage_id) return fs_claim_system_fee(fs, system_owner_addr) return invalid_args_msg # K Y C # # ARGS: project_id, address, phys_address, first_name, last_name, id_type, id_number, id_expiry, file_location, file_hash if operation == 'kyc_submit': OnOperationInvoke('kyc_submit') print('execute:kyc_submit') if len(args) == 10: address = args[1] phys_address = args[2] first_name = args[3] last_name = args[4] id_type = args[5] id_number = args[6] id_expiry = args[7] file_location = args[8] file_hash = args[9] kyc.kyc_submit(project_id, address, phys_address, first_name, last_name, id_type, id_number, id_expiry, file_location, file_hash) return address return invalid_args_msg # ARGS: project_id, addresses -> if operation == 'kyc_register': if CheckWitness(sts.owner): OnOperationInvoke('kyc_register') print('execute:kyc_register') if len(args) > 1: # addresses = args[1:] return kyc.kyc_register(project_id, args) return invalid_args_msg # ARGS: project_id, address if operation == 'kyc_status': OnOperationInvoke('kyc_status') print('execute:kyc_status') if len(args) == 2: address = args[1] return kyc.kyc_status(project_id, address) return invalid_args_msg # ARGS: project_id, address if operation == 'get_kyc_submission': OnOperationInvoke('get_kyc_submission') print('execute:get_kyc_submission') if len(args) == 2: address = args[1] return kyc.get_kyc_submission(project_id, address) return invalid_args_msg return invaild_op_msg