Exemplo n.º 1
0
    def bite(self, cdps: List):
        bytes32CDPs = []
        for cdp in cdps:
            bytes32CDPs.append(int_to_bytes32(cdp))

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'bite(bytes32[])', [bytes32CDPs])
Exemplo n.º 2
0
class DSGuard(Contract):
    """A client for the `DSGuard` contract.

    You can find the source code of the `DSGuard` contract here:
    <https://github.com/dapphub/ds-guard>.

    Attributes:
        web3: An instance of `Web` from `web3.py`.
        address: Ethereum address of the `DSGuard` contract.
    """

    abi = Contract._load_abi(__name__, 'abi/DSGuard.abi')
    bin = Contract._load_bin(__name__, 'abi/DSGuard.bin')

    ANY = int_to_bytes32(2**256 - 1)

    def __init__(self, web3: Web3, address: Address):
        assert (isinstance(web3, Web3))
        assert (isinstance(address, Address))

        self.web3 = web3
        self.address = address
        self._contract = self._get_contract(web3, self.abi, address)

    @staticmethod
    def deploy(web3: Web3):
        return DSGuard(web3=web3,
                       address=Contract._deploy(web3, DSGuard.abi, DSGuard.bin,
                                                []))

    def permit(self, src, dst, sig: bytes) -> Transact:
        """Grant access to a function call.

        Args:
            src: Address of the caller, or `ANY`.
            dst: Address of the called contract, or `ANY`.
            sig: Signature of the called function, or `ANY`.

        Returns:
            A :py:class:`pymaker.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(src, Address) or isinstance(src, bytes))
        assert (isinstance(dst, Address) or isinstance(dst, bytes))
        assert (isinstance(sig, bytes) and len(sig) in (4, 32))

        if isinstance(src, Address) and isinstance(dst, Address):
            method = 'permit(address,address,bytes32)'
            src = src.address
            dst = dst.address

        else:
            method = 'permit(bytes32,bytes32,bytes32)'

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, method, [src, dst, sig])

    def __repr__(self):
        return f"DSGuard('{self.address}')"
Exemplo n.º 3
0
    def tab(self, cup_id: int) -> Wad:
        """Get the amount of debt in a cup.

        Args:
            cup_id: Id of the cup.

        Returns:
            Amount of debt in the cup, in SAI.
        """
        assert isinstance(cup_id, int)
        return Wad(self._contract.functions.tab(int_to_bytes32(cup_id)).call())
Exemplo n.º 4
0
    def ink(self, cup_id: int) -> Wad:
        """Get the amount of SKR collateral locked in a cup.

        Args:
            cup_id: Id of the cup.

        Returns:
            Amount of SKR collateral locked in the cup, in SKR.
        """
        assert isinstance(cup_id, int)
        return Wad(self._contract.functions.ink(int_to_bytes32(cup_id)).call())
Exemplo n.º 5
0
    def lad(self, cup_id: int) -> Address:
        """Get the owner of a cup.

        Args:
            cup_id: Id of the cup.

        Returns:
            Address of the owner of the cup.
        """
        assert isinstance(cup_id, int)
        return Address(self._contract.call().lad(int_to_bytes32(cup_id)))
Exemplo n.º 6
0
    def safe(self, cup_id: int) -> bool:
        """Determine if a cup is safe.

        Args:
            cup_id: Id of the cup

        Returns:
            `True` if the cup is safe. `False` otherwise.
        """
        assert isinstance(cup_id, int)
        return self._contract.functions.safe(int_to_bytes32(cup_id)).call()
Exemplo n.º 7
0
def test_int_to_bytes32():
    assert int_to_bytes32(0) == bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])

    assert int_to_bytes32(1) == bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01])

    assert int_to_bytes32(512) == bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00])
    
    assert int_to_bytes32(2**256-1) == bytes([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
                                              0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
                                              0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
                                              0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
Exemplo n.º 8
0
    def bite(self, cup_id: int) -> Transact:
        """Initiate liquidation of an undercollateralized cup.

        Args:
            cup_id: Id of the cup to liquidate.

        Returns:
            A :py:class:`pymaker.Transact` instance, which can be used to trigger the transaction.
        """
        assert isinstance(cup_id, int)
        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'bite', [int_to_bytes32(cup_id)])
Exemplo n.º 9
0
    def cups(self, cup_id: int) -> Cup:
        """Get the cup details.

        Args:
            cup_id: Id of the cup to get the details of.

        Returns:
            Class encapsulating cup details.
        """
        assert isinstance(cup_id, int)
        array = self._contract.functions.cups(int_to_bytes32(cup_id)).call()
        return Cup(cup_id, Address(array[0]), Wad(array[1]), Wad(array[2]))
Exemplo n.º 10
0
    def free(self, cup_id: int, amount_in_skr: Wad) -> Transact:
        """Remove excess SKR collateral from a cup.

        Args:
            cup_id: Id of the cup to remove the collateral from.
            amount_in_skr: The amount of collateral to remove, in SKR.

        Returns:
            A :py:class:`pymaker.Transact` instance, which can be used to trigger the transaction.
        """
        assert isinstance(cup_id, int)
        assert isinstance(amount_in_skr, Wad)
        return Transact(self, self.web3, self.abi, self.address, self._contract, 'free',
                        [int_to_bytes32(cup_id), amount_in_skr.value])
Exemplo n.º 11
0
    def give(self, cup_id: int, new_lad: Address) -> Transact:
        """Transfer ownership of a cup.

        Args:
            cup_id: Id of the cup to transfer ownership of.
            new_lad: New owner of the cup.

        Returns:
            A :py:class:`pymaker.Transact` instance, which can be used to trigger the transaction.
        """
        assert isinstance(cup_id, int)
        assert isinstance(new_lad, Address)
        return Transact(self, self.web3, self.abi, self.address, self._contract, 'give',
                        [int_to_bytes32(cup_id), new_lad.address])
Exemplo n.º 12
0
    def wipe(self, cup_id: int, amount_in_sai: Wad) -> Transact:
        """Repay some portion of existing SAI debt.

        Args:
            cup_id: Id of the cup to repay the SAI to.
            amount_in_sai: The amount SAI to be repaid.

        Returns:
            A :py:class:`pymaker.Transact` instance, which can be used to trigger the transaction.
        """
        assert isinstance(cup_id, int)
        assert isinstance(amount_in_sai, Wad)
        return Transact(self, self.web3, self.abi, self.address, self._contract, 'wipe',
                        [int_to_bytes32(cup_id), amount_in_sai.value])
Exemplo n.º 13
0
    def draw(self, cup_id: int, amount_in_sai: Wad) -> Transact:
        """Issue the specified amount of SAI stablecoins.

        Args:
            cup_id: Id of the cup to issue the SAI from.
            amount_in_sai: The amount SAI to be issued.

        Returns:
            A :py:class:`pymaker.Transact` instance, which can be used to trigger the transaction.
        """
        assert isinstance(cup_id, int)
        assert isinstance(amount_in_sai, Wad)
        return Transact(self, self.web3, self.abi, self.address, self._contract, 'draw',
                        [int_to_bytes32(cup_id), amount_in_sai.value])
Exemplo n.º 14
0
    def lock(self, cup_id: int, amount_in_skr: Wad) -> Transact:
        """Post additional SKR collateral to a cup.

        Args:
            cup_id: Id of the cup to post the collateral into.
            amount_in_skr: The amount of collateral to post, in SKR.

        Returns:
            A :py:class:`pymaker.Transact` instance, which can be used to trigger the transaction.
        """
        assert isinstance(cup_id, int)
        assert isinstance(amount_in_skr, Wad)
        return Transact(self, self.web3, self.abi, self.address, self._contract, 'lock',
                        [int_to_bytes32(cup_id), amount_in_skr.value])
Exemplo n.º 15
0
    def shut(self, cup_id: int) -> Transact:
        """Close a cup.

        Involves calling `wipe()` and `free()` internally in order to clear all remaining SAI debt and free
        all remaining SKR collateral.

        Args:
            cup_id: Id of the cup to close.

        Returns:
            A :py:class:`pymaker.Transact` instance, which can be used to trigger the transaction.
        """
        assert isinstance(cup_id, int)
        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'shut', [int_to_bytes32(cup_id)])
Exemplo n.º 16
0
    def kill(self, order_id: int) -> Transact:
        """Cancels an existing order.

        Orders can be cancelled only by their owners.

        Args:
            order_id: Id of the order you want to cancel.

        Returns:
            A :py:class:`pymaker.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(order_id, int))

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'kill(bytes32)',
                        [int_to_bytes32(order_id)])
Exemplo n.º 17
0
    def bump(self, order_id: int) -> Transact:
        """Bumps an order.

        Bumping an order generates a `LogBump` event, which can make the order reappear
        in some front-ends relying on the events.

        Args:
            order_id: Id of the order you want to bump.

        Returns:
            A :py:class:`pymaker.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(order_id, int))

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'bump', [int_to_bytes32(order_id)])
Exemplo n.º 18
0
    def kill(self, order_id: int) -> Transact:
        """Cancels an existing order.

        Orders can be cancelled only by their owners. In addition to that, in case of expiring markets,
        after the market has expired all orders can be cancelled by anyone.

        Args:
            order_id: Id of the order you want to cancel.

        Returns:
            A :py:class:`pymaker.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(order_id, int))

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'kill', [int_to_bytes32(order_id)])
Exemplo n.º 19
0
    def take(self, order_id: int, quantity: Wad) -> Transact:
        """Takes (buys) an order.

        If `quantity` is equal to `pay_amount`, the whole order will be taken (bought) which will make it
        disappear from the order book. If you want to buy a fraction of the order, set `quantity` to a number
        lower than `pay_amount`.

        Args:
            order_id: Id of the order you want to take (buy).
            quantity: Quantity of `pay_token` that you want to buy.

        Returns:
            A :py:class:`pymaker.Transact` instance, which can be used to trigger the transaction.
        """
        assert(isinstance(order_id, int))
        assert(isinstance(quantity, Wad))

        return Transact(self, self.web3, self.abi, self.address, self._contract, 'take',
                        [int_to_bytes32(order_id), quantity.value])
Exemplo n.º 20
0
 def _to_hex(self, num: int) -> str:
     return bytes_to_hexstring(int_to_bytes32(num))