Exemple #1
0
    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
Exemple #3
0
    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
Exemple #5
0
    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
Exemple #6
0
    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