def claimUnits(self, uid, units_to_claim): storage_api = StorageApi() consume_type = storage_api.getValue(self.STORAGE_KEY_CONSUME_TYPE, uid) if consume_type == self.CONSUME_TYPE_TIME: Notify('error: you cant claim units for a time contract') return False current_units = storage_api.getValue(self.STORAGE_KEY_CURRENT_UNITS, uid) open_units = storage_api.getValue(self.STORAGE_KEY_OPEN_UNITS, uid) total_units = storage_api.getValue(self.STORAGE_KEY_TOTAL_UNITS, uid) if units_to_claim == 0: Notify('error: you cant claim 0 units') return False if units_to_claim > total_units: Notify('error: number of claimed units is higher than total units') return False claimable_units = total_units - current_units - open_units if units_to_claim > claimable_units: Notify( 'error: number of claimed units is higher than available units' ) return False open_units += units_to_claim storage_api.putValue(self.STORAGE_KEY_OPEN_UNITS, uid, open_units) return open_units
def releaseAttachedAssets(self, address, attached_assets_value): storage_api = StorageApi() released_assets = storage_api.getValue( self.STORAGE_KEY_RELEASED_ASSETS, address) released_assets += attached_assets_value storage_api.putValue(self.STORAGE_KEY_RELEASED_ASSETS, address, released_assets) return released_assets
def reserveUnitsForWithdrawal(self, uid, units): storage_api = StorageApi() reserved_units = storage_api.getValue(self.STORAGE_KEY_RESERVED_UNITS, uid) reserved_units += units storage_api.putValue(self.STORAGE_KEY_RESERVED_UNITS, uid, reserved_units) return reserved_units
def spentAssets(self, address, spent_assets_value): storage_api = StorageApi() spent_assets_value = spent_assets_value * 100000000 released_assets = storage_api.getValue( self.STORAGE_KEY_RELEASED_ASSETS, address) released_assets -= spent_assets_value storage_api.putValue(self.STORAGE_KEY_RELEASED_ASSETS, address, released_assets) return released_assets
def authorizeOpenUnits(self, uid, units_to_authorize): storage_api = StorageApi() open_units = storage_api.getValue(self.STORAGE_KEY_OPEN_UNITS, uid) if units_to_authorize == 0: return 'error: you cant authorize 0 units' if units_to_authorize > open_units: return 'error: you cant authorize more units than there are open ones' current_units = storage_api.getValue(self.STORAGE_KEY_CURRENT_UNITS, uid) open_units -= units_to_authorize current_units += units_to_authorize storage_api.putValue(self.STORAGE_KEY_OPEN_UNITS, uid, open_units) storage_api.putValue(self.STORAGE_KEY_CURRENT_UNITS, uid, current_units) return current_units
def spentUnitsAfterWithdrawalAuthorize(self, uid, released_assets, reduce_reserved): storage_api = StorageApi() overall_value = storage_api.getValue(self.STORAGE_KEY_OVERALL_VALUE, uid) total_units = storage_api.getValue(self.STORAGE_KEY_TOTAL_UNITS, uid) value_factor = (overall_value * FACTOR) / released_assets units = (total_units * FACTOR) / value_factor spent_units = storage_api.getValue(self.STORAGE_KEY_SPENT_UNITS, uid) spent_units += units storage_api.putValue(self.STORAGE_KEY_SPENT_UNITS, uid, spent_units) # for an up-front payment contract, units were never reserved, but immediately released if reduce_reserved: reserved_units = storage_api.getValue( self.STORAGE_KEY_RESERVED_UNITS, uid) reserved_units -= units storage_api.putValue(self.STORAGE_KEY_RESERVED_UNITS, uid, reserved_units) return spent_units
def createNewWithdrawalRequest(self, uid, units, units_value): storage_api = StorageApi() storage_api.putValue(self.STORAGE_KEY_WITHDRAWAL_REQUEST, uid, 'ACTIVE') storage_api.putValue(self.STORAGE_KEY_WITHDRAWAL_REQUEST_UNITS, uid, units) storage_api.putValue(self.STORAGE_KEY_WITHDRAWAL_REQUEST_VALUE, uid, units_value) return True
def initialize(self, uid, receiver): storage_api = StorageApi() storage_api.putValue(self.STORAGE_KEY_RELEASED_ASSETS, receiver, 0)
def initialize(self, args, consume_type): if not consume_type: return "error: no consume type defined" uid = args[0] principal_address = args[1] receiver_address = args[2] overall_value = args[3] from_timestamp = args[4] to_timestamp = args[5] payment_type = args[6] storage_api = StorageApi() overall_value = overall_value * FACTOR # when initializing an up-front contract, the total asset amount has to be attached if payment_type == self.PAYMENT_TYPE_UP_FRONT: contract_script_hash = GetExecutingScriptHash() attached_assets = getAttachedAssets(contract_script_hash) if attached_assets.gas_attached == 0: return 'error: no gas attached' if attached_assets.gas_attached != overall_value: return 'error: attached gas is not matching overall value' storage_api.putValue(self.STORAGE_KEY_UP_FRONT_HOLDINGS, uid, attached_assets.gas_attached) storage_api.putValue(self.STORAGE_KEY_PRINCIPAL_ADDRESS, uid, principal_address) storage_api.putValue(self.STORAGE_KEY_RECEIVER_ADDRESS, uid, receiver_address) storage_api.putValue(self.STORAGE_KEY_OVERALL_VALUE, uid, overall_value) storage_api.putValue(self.STORAGE_KEY_FROM_TIMESTAMP, uid, from_timestamp) storage_api.putValue(self.STORAGE_KEY_TO_TIMESTAMP, uid, to_timestamp) storage_api.putValue(self.STORAGE_KEY_PAYMENT_TYPE, uid, payment_type) storage_api.putValue(self.STORAGE_KEY_SPENT_UNITS, uid, 0) storage_api.putValue(self.STORAGE_KEY_OPEN_UNITS, uid, 0) storage_api.putValue(self.STORAGE_KEY_RESERVED_UNITS, uid, 0) storage_api.putValue(self.STORAGE_KEY_CURRENT_UNITS, uid, 0) storage_api.putValue(self.STORAGE_KEY_CONSUME_TYPE, uid, consume_type) # for TimeContracts the total unit count is defined by the timestamps if consume_type == self.CONSUME_TYPE_TIME: total_units = to_timestamp - from_timestamp storage_api.putValue(self.STORAGE_KEY_TOTAL_UNITS, uid, total_units) return True # for UnitContracts the total unit count is set by the invoker elif consume_type == self.CONSUME_TYPE_UNITS: total_units = args[7] storage_api.putValue(self.STORAGE_KEY_TOTAL_UNITS, uid, total_units) return True else: return False return True