def do_approve(self, storage: StorageAPI, t_owner, t_spender, amount): if not CheckWitness(t_owner): print("Incorrect permission") return False if amount < 0: print("Negative amount") return False from_balance = storage.get(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) if len(approval_key) != 40: return False if amount == 0: storage.delete(approval_key) else: storage.put(approval_key, amount) OnApprove(t_owner, t_spender, amount) return True return False
def kyc_deregister(self, args, token: Token): """ :param args:list a list of addresses to deregister :param token: Token A token object with your ICO settings :return: int: The number of addresses deregistered from KYC """ ok_count = 0 storage = StorageAPI() owner = storage.get(token.owner_key) if CheckWitness(owner): for address in args: if len(address) == 20: kyc_storage_key = concat(self.kyc_key, address) storage.delete(kyc_storage_key) OnKYCDeregister(address) ok_count += 1 return ok_count
def cancel_change_owner(token: Token): """ Cancel a pending ownership transfer request :param token: Token The token to cancel the ownership transfer for :return: bool: Whether the operation was successful """ storage = StorageAPI() new_owner = storage.get(token.new_owner_key) if not new_owner: print( "Can't cancel_change_owner unless an owner change is already pending" ) return False owner = storage.get(token.owner_key) if not CheckWitness(owner): print("Must be owner to cancel change_owner") return False # delete the new owner to cancel the transfer. storage.delete(token.new_owner_key) return True
def do_transfer_from(self, storage: StorageAPI, t_from, t_to, amount): if amount <= 0: return False available_key = concat(t_from, t_to) if len(available_key) != 40: return False available_to_to_addr = storage.get(available_key) if available_to_to_addr < amount: print("Insufficient funds approved") return False from_balance = storage.get(t_from) if from_balance < amount: print("Insufficient tokens in from balance") return False to_balance = storage.get(t_to) new_from_balance = from_balance - amount new_to_balance = to_balance + amount storage.put(t_to, new_to_balance) storage.put(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(available_key) else: print("updating allowance to new allowance") storage.put(available_key, new_allowance) OnTransfer(t_from, t_to, amount) return True
def resume_sale(token: Token): """ Resume the sale :param token: Token The token of the sale to resume :return: bool: Whether the operation was successful """ storage = StorageAPI() owner = storage.get(token.owner_key) if not CheckWitness(owner): print("Must be owner to resume sale") return False # mark the sale as active storage.delete(token.sale_paused_key) return True
def do_transfer(self, storage: StorageAPI, t_from, t_to, amount): if amount <= 0: return False if len(t_to) != 20: return False if CheckWitness(t_from): if t_from == t_to: print("transfer to self!") return True from_val = storage.get(t_from) if from_val < amount: print("insufficient funds") return False if from_val == amount: storage.delete(t_from) else: difference = from_val - amount storage.put(t_from, difference) to_value = storage.get(t_to) to_total = to_value + amount storage.put(t_to, to_total) OnTransfer(t_from, t_to, amount) return True else: print("from address is not the tx sender") return False
def accept_owner(token: Token): """ Change the owner of this smart contract who will be able to perform protected operations :param token: Token The token to change the owner for :return: bool: Whether the operation was successful """ storage = StorageAPI() new_owner = storage.get(token.new_owner_key) if not new_owner: print("Must call change_owner before accept_owner") return False if not CheckWitness(new_owner): print("Must be new_owner to accept owner") return False # setup the new owner. storage.put(token.owner_key, new_owner) # now that it's accepted, make the change official by removing the pending new_owner storage.delete(token.new_owner_key) return True