Ejemplo n.º 1
0
    def start_auction(self, amount_to_sell: Rad, bid_amount: Wad) -> Transact:
        assert (isinstance(amount_to_sell, Rad))
        assert (isinstance(bid_amount, Wad))

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'startAuction',
                        [amount_to_sell.value, bid_amount.value])
Ejemplo n.º 2
0
    def remove_liquidity_eth(self, amounts: dict, token: Token,
                             eth_position: int) -> Transact:
        """ Remove liquidity from token-weth pair.

        Args:
            token_a: Address of pool token.
            amounts: dictionary[uint256, uint256, uint256]
        Returns:
            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(amounts, dict))
        assert (isinstance(token, Token))
        assert (isinstance(eth_position, int))

        # Will approve Uniswap Liquidity token if allowance not already set
        self.approve(self.pair_token)

        if eth_position == 0:
            token_min = amounts['amountBMin'].value
            eth_min = amounts['amountAMin'].value
        elif eth_position == 1:
            token_min = amounts['amountAMin'].value
            eth_min = amounts['amountBMin'].value

        removeLiquidityArgs = [
            token.address.address, amounts['liquidity'].value, token_min,
            eth_min, self.account_address.address,
            self._deadline()
        ]

        return Transact(self, self.web3, self.router_abi, self.router_address,
                        self._router_contract,
                        'removeLiquidityETHSupportingFeeOnTransferTokens',
                        removeLiquidityArgs)
Ejemplo n.º 3
0
    def set_user_role(self, who: Address, role: int, enabled=True) -> Transact:
        assert isinstance(who, Address)
        assert isinstance(role, int)
        assert 0 <= role <= int('0xFFFFFFFF')

        return Transact(self, self.web3, self.abi, self.address, self._contract,
                        "setUserRole", [who.address, role, enabled])
Ejemplo n.º 4
0
    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:`pyflex.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])
Ejemplo n.º 5
0
    def remove_liquidity(self, amounts: dict, token_a: Token,
                         token_b: Token) -> Transact:
        """ Remove liquidity from arbitrary token pair.

        Args:
            token_a: Address of pool token A.
            token_b: Address of pool token B.
            amounts: dictionary[uint256, uint256, uint256]
        Returns:
            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(token_a, Token))
        assert (isinstance(token_b, Token))
        assert (isinstance(amounts, dict))

        # Will approve Uniswap Liquidity token if allowance not already set
        self.approve(self.pair_token)

        removeLiquidityArgs = [
            token_a.address.address, token_b.address.address,
            amounts['liquidity'].value, amounts['amountAMin'].value,
            amounts['amountBMin'].value, self.account_address.address,
            self._deadline()
        ]

        return Transact(self, self.web3, self.router_abi, self.router_address,
                        self._router_contract, 'removeLiquidity',
                        removeLiquidityArgs)
Ejemplo n.º 6
0
    def execute_at(self, address: Address, calldata: Calldata) -> Transact:
        assert (isinstance(address, Address))
        assert (isinstance(calldata, Calldata))

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'execute(address,bytes)',
                        [address.address, calldata.as_bytes()])
Ejemplo n.º 7
0
    def fill_order(self, order: Order, fill_buy_amount: Wad) -> Transact:
        """Fills an order.

        Args:
            order: The order to be filled.
            fill_buy_amount: The amount (in terms of `buy_token` of the original order) to be filled.

        Returns:
            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(order, Order))
        assert (isinstance(fill_buy_amount, Wad))

        method_signature = self.web3.keccak(
            text=f"fillOrder({self.ORDER_INFO_TYPE},uint256,bytes)")[0:4]
        method_parameters = encode_single(
            f"({self.ORDER_INFO_TYPE},uint256,bytes)", [
                self._order_tuple(order), fill_buy_amount.value,
                hexstring_to_bytes(order.signature)
            ])

        request = bytes_to_hexstring(method_signature + method_parameters)

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, None, [request])
Ejemplo n.º 8
0
    def execute(self, tokens: List[Address], invocations: List[Invocation]) -> Transact:
        """Executes multiple contract methods in one Ethereum transaction.

        Args:
            tokens: List of addresses of ERC20 token the invocations should be able to access.
            invocations: A list of invocations (contract methods) to be executed.

        Returns:
            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """
        def token_addresses() -> list:
            return list(map(lambda address: address.address, tokens))

        def script() -> bytes:
            return reduce(operator.add, map(lambda invocation: script_entry(invocation), invocations), bytes())

        def script_entry(invocation: Invocation) -> bytes:
            address = invocation.address.as_bytes()
            calldata = invocation.calldata.as_bytes()
            calldata_length = len(calldata).to_bytes(32, byteorder='big')
            return address + calldata_length + calldata

        assert(isinstance(tokens, list))
        assert(isinstance(invocations, list))

        return Transact(self, self.web3, self.abi, self.address, self._contract, 'execute', [token_addresses(), script()])
Ejemplo n.º 9
0
    def remove_liquidity(self, amount: Wad) -> Transact:
        assert (isinstance(amount, Wad))

        return Transact(
            self, self.web3, self.abi, self.exchange, self._contract,
            'removeLiquidity',
            [amount.value, 1, 1, self._deadline()])
Ejemplo n.º 10
0
 def calculate_cash_price(self,
                          collateral_type: CollateralType) -> Transact:
     """Calculate the `fix`, the cash price for a given collateral"""
     assert isinstance(collateral_type, CollateralType)
     return Transact(self, self.web3, self.abi, self.address,
                     self._contract, 'calculateCashPrice',
                     [collateral_type.toBytes()])
Ejemplo n.º 11
0
    def add_liquidity(self, amounts: dict, token_a: Token,
                      token_b: Token) -> Transact:
        """ Add liquidity to arbitrary token pair.

        Args:
            amounts: dictionary[Wad, Wad, Wad, Wad]
            token_a: First token in the pool
            token_b: Second token in the pool
        Returns:
            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(amounts, dict))
        assert (isinstance(token_a, Token))
        assert (isinstance(token_b, Token))

        addLiquidityArgs = [
            token_a.address.address, token_b.address.address,
            amounts['amount_a_desired'].value,
            amounts['amount_b_desired'].value, amounts['amount_a_min'].value,
            amounts['amount_b_min'].value, self.account_address.address,
            self._deadline()
        ]

        return Transact(self, self.web3, self.router_abi, self.router_address,
                        self._router_contract, 'addLiquidity',
                        addLiquidityArgs)
Ejemplo n.º 12
0
    def swap_exact_tokens_for_tokens(self, tokens_to_swap: Wad,
                                     min_amount_out: Wad,
                                     path: List) -> Transact:
        """Convert ERC20 to ERC20.

        Requires Approval to have already been called on the token to swap

        Args:
            tokens_to_swap: Amount of given token to swap for token.
            min_amount_out: Minimum amount of output token to set price
            path: array of token addresses used to form swap route
        Returns:
            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(tokens_to_swap, Wad))
        assert (isinstance(min_amount_out, Wad))
        assert (isinstance(path, List))

        swapArgs = [
            tokens_to_swap.value, min_amount_out.value, path,
            self.account_address.address,
            self._deadline()
        ]

        return Transact(self, self.web3, self.router_abi, self.router_address,
                        self._router_contract, 'swapExactTokensForTokens',
                        swapArgs)
Ejemplo n.º 13
0
    def open_safe(self, collateral_type: CollateralType,
                  address: Address) -> Transact:
        assert isinstance(collateral_type, CollateralType)
        assert isinstance(address, Address)

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'openSAFE',
                        [collateral_type.toBytes(), address.address])
Ejemplo n.º 14
0
    def update_result(self) -> Transact:
        """Populates this instance with a new value.

            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'updateResult', [])
Ejemplo n.º 15
0
 def redeem_collateral(self, collateral_type: CollateralType,
                       system_coin: Wad):
     """Exchange an amount of system coin (already `prepare_coins_for_redeemin`ed in the `coin_bag`) for collateral"""
     assert isinstance(collateral_type, CollateralType)
     assert isinstance(system_coin, Wad)
     return Transact(self, self.web3, self.abi, self.address,
                     self._contract, 'redeemCollateral',
                     [collateral_type.toBytes(), system_coin.value])
Ejemplo n.º 16
0
 def process_safe(self, collateral_type: CollateralType,
                  address: Address) -> Transact:
     """Cancels undercollateralized SAFE debt to determine collateral shortfall"""
     assert isinstance(collateral_type, CollateralType)
     assert isinstance(address, Address)
     return Transact(self, self.web3, self.abi, self.address,
                     self._contract, 'processSAFE',
                     [collateral_type.toBytes(), address.address])
Ejemplo n.º 17
0
    def freeze_collateral_type(self,
                               collateral_type: CollateralType) -> Transact:
        """Set the `shutdownSystem` price for the collateral"""
        assert isinstance(collateral_type, CollateralType)

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'freezeCollateralType(bytes32)',
                        [collateral_type.toBytes()])
Ejemplo n.º 18
0
 def fast_track_auction(self, collateral_type: CollateralType,
                        collateral_auction_id: int) -> Transact:
     """Cancel a collateral auction and seize it's collateral"""
     assert isinstance(collateral_type, CollateralType)
     assert isinstance(collateral_auction_id, int)
     return Transact(self, self.web3, self.abi, self.address,
                     self._contract, 'fastTrackAuction',
                     [collateral_type.toBytes(), collateral_auction_id])
Ejemplo n.º 19
0
    def restart_value(self) -> Transact:
        """Removes the current value from this instance.

        Returns:
            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """
        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'restartValue', [])
Ejemplo n.º 20
0
    def increase_bid_size(self, id: int, amount_to_sell: Rad,
                          bid_amount: Wad) -> Transact:
        assert (isinstance(id, int))
        assert (isinstance(amount_to_sell, Rad))
        assert (isinstance(bid_amount, Wad))

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'increaseBidSize',
                        [id, amount_to_sell.value, bid_amount.value])
Ejemplo n.º 21
0
    def decrease_sold_amount(self, id: int, amount_to_sell: Wad,
                             bid_amount: Rad) -> Transact:
        assert (isinstance(id, int))
        assert (isinstance(amount_to_sell, Wad))
        assert (isinstance(bid_amount, Rad))

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'decreaseSoldAmount',
                        [id, amount_to_sell.value, bid_amount.value])
Ejemplo n.º 22
0
    def write(self, code: str):
        assert (isinstance(code, str))

        if code.startswith('0x'):
            b32_code = hexstring_to_bytes(code)
        else:
            b32_code = hexstring_to_bytes('0x' + code)

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'write', [b32_code])
Ejemplo n.º 23
0
    def start_auction(self, initial_bidder: Address, amount_to_sell: Wad,
                      bid_amount: Wad) -> Transact:
        assert (isinstance(initial_bidder, Address))
        assert (isinstance(amount_to_sell, Wad))
        assert (isinstance(bid_amount, Wad))

        return Transact(
            self, self.web3, self.abi, self.address, self._contract,
            'startAuction',
            [initial_bidder.address, amount_to_sell.value, bid_amount.value])
Ejemplo n.º 24
0
    def set_authority(self, address: Address) -> Transact:
        """Set the `authority` of a `DSAuth`-ed contract.

        Args:
            address: The address of the new `authority`.

        Returns:
            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(address, Address))
        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'setAuthority', [address.address])
Ejemplo n.º 25
0
    def eth_to_token_swap_input(self, eth_sold: Wad) -> Transact:
        """Convert ETH to Tokens.

        Args:
            eth_sold: Amount of ETH to swap for token.

        Returns:
            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """
        return Transact(self, self.web3, self.abi, self.exchange,
                        self._contract, 'ethToTokenSwapInput',
                        [1, self._deadline()], {'value': eth_sold.value})
Ejemplo n.º 26
0
    def add_liquidity(self, amount: Wad) -> Transact:
        assert (isinstance(amount, Wad))

        min_liquidity = Wad.from_number(0.5) * amount
        max_token = amount * self.get_exchange_rate() * Wad.from_number(
            1.00000001)

        return Transact(
            self, self.web3, self.abi, self.exchange, self._contract,
            'addLiquidity',
            [min_liquidity.value, max_token.value,
             self._deadline()], {'value': amount.value})
Ejemplo n.º 27
0
    def execute(self, code: str, calldata: Calldata) -> Transact:
        assert (isinstance(code, str))
        assert (isinstance(calldata, Calldata))

        if code.startswith('0x'):
            b32_code = hexstring_to_bytes(code)
        else:
            b32_code = hexstring_to_bytes('0x' + code)

        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'execute(bytes,bytes)',
                        [b32_code, calldata.as_bytes()])
Ejemplo n.º 28
0
    def burn(self, amount: Wad) -> Transact:
        """Decrease the total supply of the token.

        Args:
            amount: The amount to decrease the total supply by.

        Returns:
            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(amount, Wad))
        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'burn(uint256)', [amount.value])
Ejemplo n.º 29
0
    def deposit(self, amount: Wad) -> Transact:
        """Deposits `amount` of raw ETH to `DSEthToken`.

        Args:
            amount: Amount of raw ETH to be deposited to `DSEthToken`.

        Returns:
            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(amount, Wad))
        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'deposit', [], {'value': amount.value})
Ejemplo n.º 30
0
    def update_result(self, new_value: int) -> Transact:
        """Populates this instance with a new value.

        Args:
            new_value: An integer of the new value to be set.

        Returns:
            A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction.
        """
        assert (isinstance(new_value, int))
        #assert(len(new_value) == 32)
        return Transact(self, self.web3, self.abi, self.address,
                        self._contract, 'updateResult', [new_value])