Exemplo n.º 1
0
 def __isub__(self, quantity: 'Quantity') -> 'Wallet':
     if quantity.is_locked and self.locked[quantity.path_id]:
         if quantity > self.locked[quantity.path_id]:
             raise InsufficientFunds(self.locked[quantity.path_id], quantity.size)
         self._locked[quantity.path_id] -= quantity
     elif not quantity.is_locked:
         if quantity > self._balance:
             raise InsufficientFunds(self.balance, quantity.size)
         self._balance -= quantity
     return self
Exemplo n.º 2
0
    def lock(self, quantity, order: 'Order', reason: str):
        if quantity.is_locked:
            raise DoubleLockedQuantity(quantity)

        if quantity > self._balance:
            raise InsufficientFunds(self.balance, quantity)

        self._balance -= quantity

        quantity = quantity.lock_for(order.path_id)

        if quantity.path_id not in self._locked:
            self._locked[quantity.path_id] = quantity
        else:
            self._locked[quantity.path_id] += quantity

        self._locked[quantity.path_id] = self._locked[quantity.path_id].quantize()
        self._balance = self._balance.quantize()

        self.ledger.commit(wallet=self,
                           quantity=quantity,
                           source="{}:{}/free".format(self.exchange.name, self.instrument),
                           target="{}:{}/locked".format(self.exchange.name, self.instrument),
                           memo="LOCK ({})".format(reason))

        return quantity
Exemplo n.º 3
0
    def __isub__(self, quantity: 'Quantity') -> 'Wallet':
        if quantity.is_locked and self.locked[quantity.path_id]:
            if quantity > self.locked[quantity.path_id]:
                raise InsufficientFunds(self.locked[quantity.path_id],
                                        quantity)
            self._locked[quantity.path_id] -= quantity
        elif not quantity.is_locked:
            if quantity > self._balance:
                raise InsufficientFunds(self.balance, quantity)
            self._balance -= quantity

        self.ledger.commit(
            Transaction(self.exchange.clock.step, self.exchange.name,
                        self.instrument, "WITHDRAW", quantity.path_id,
                        quantity.memo, quantity, self.balance,
                        self.locked_balance))
        return self
Exemplo n.º 4
0
    def withdraw(self, quantity: 'Quantity', reason: str):
        if quantity.is_locked and self._locked.get(quantity.path_id, False):
            locked_quantity = self._locked[quantity.path_id]
            if quantity > locked_quantity:
                raise InsufficientFunds(self._locked[quantity.path_id], quantity)
            self._locked[quantity.path_id] -= quantity

        elif not quantity.is_locked:
            if quantity > self._balance:
                raise InsufficientFunds(self.balance, quantity)
            self._balance -= quantity

        self._balance = self._balance.quantize()

        self.ledger.commit(wallet=self,
                           quantity=quantity,
                           source="{}:{}/locked".format(self.exchange.name, self.instrument),
                           target=self.exchange.name,
                           memo="WITHDRAWAL ({})".format(reason))

        return quantity
Exemplo n.º 5
0
    def unlock(self, quantity: 'Quantity', reason: str):
        if not quantity.is_locked:
            raise DoubleUnlockedQuantity(quantity)

        if quantity.path_id not in self._locked:
            raise QuantityNotLocked(quantity)

        if quantity > self._locked[quantity.path_id]:
            raise InsufficientFunds(self._locked[quantity.path_id], quantity)

        self._locked[quantity.path_id] -= quantity
        self._balance += quantity.free()

        self._locked[quantity.path_id] = self._locked[quantity.path_id].quantize()
        self._balance = self._balance.quantize()

        self.ledger.commit(wallet=self,
                           quantity=quantity,
                           source="{}:{}/locked".format(self.exchange.name, self.instrument),
                           target="{}:{}/free".format(self.exchange.name, self.instrument),
                           memo="UNLOCK {} ({})".format(self.instrument, reason))

        return quantity