Beispiel #1
0
    def place_order(self,
                    pay_token: Address,
                    pay_amount: Wad,
                    buy_token: Address,
                    buy_amount: Wad) -> Order:
        """Places a new order.

        Args:
            pay_token: Address of the ERC20 token you want to put on sale.
            pay_amount: Amount of the `pay_token` token you want to put on sale.
            buy_token: Address of the ERC20 token you want to be paid with.
            buy_amount: Amount of the `buy_token` you want to receive.

        Returns:
            New order as an instance of the :py:class:`pyexchange.idex.Order` class.
        """
        assert(isinstance(pay_token, Address))
        assert(isinstance(pay_amount, Wad))
        assert(isinstance(buy_token, Address))
        assert(isinstance(buy_amount, Wad))

        expires = 0
        nonce = self.next_nonce()
        order_hash = keccak_256(encode_address(self.idex.address) +
                                encode_address(buy_token) +
                                encode_uint256(buy_amount.value) +
                                encode_address(pay_token) +
                                encode_uint256(pay_amount.value) +
                                encode_uint256(expires) +
                                encode_uint256(nonce) +
                                encode_address(Address(self._our_address()))).digest()

        signature = eth_sign(order_hash, self.idex.web3)
        v, r, s = to_vrs(signature)

        data = {
            'tokenBuy': buy_token.address,
            'amountBuy': str(buy_amount.value),
            'tokenSell': pay_token.address,
            'amountSell': str(pay_amount.value),
            'address': self._our_address(),
            'nonce': str(nonce),
            'expires': expires,
            'v': v,
            'r': bytes_to_hexstring(r),
            's': bytes_to_hexstring(s)
        }

        self.logger.info(f"Placing order selling {pay_amount} {pay_token} for {buy_amount} {buy_token}...")

        result = self._http_post("/order", data)
        order = self._json_to_order(result)

        self.logger.info(f"Placed order selling {pay_amount} {pay_token} for {buy_amount} {buy_token} as #{order.order_id}")

        return order
Beispiel #2
0
    def create_order(self,
                     pay_token: Address,
                     pay_amount: Wad,
                     buy_token: Address,
                     buy_amount: Wad,
                     expires: int) -> Order:
        """Creates a new off-chain order.

        Although it's not necessary to have any amount of `pay_token` deposited to EtherDelta
        before placing an order, nobody will be able to take this order until some balance of
        'pay_token' is provided.

        If you want to trade raw ETH, pass `Address('0x0000000000000000000000000000000000000000')`
        as either `pay_token` or `buy_token`.

        Args:
            pay_token: Address of the ERC20 token you want to put on sale.
            pay_amount: Amount of the `pay_token` token you want to put on sale.
            buy_token: Address of the ERC20 token you want to be paid with.
            buy_amount:  Amount of the `buy_token` you want to receive.
            expires: The block number after which the order will expire.

        Returns:
            Newly created order as an instance of the :py:class:`pymaker.etherdelta.Order` class.
        """

        assert(isinstance(pay_token, Address))
        assert(isinstance(pay_amount, Wad))
        assert(isinstance(buy_token, Address))
        assert(isinstance(buy_amount, Wad))
        assert(isinstance(expires, int) and (expires > 0))
        assert(pay_amount > Wad(0))
        assert(buy_amount > Wad(0))

        nonce = self.random_nonce()
        order_hash = hashlib.sha256(encode_address(self.address) +
                                    encode_address(buy_token) +
                                    encode_uint256(buy_amount.value) +
                                    encode_address(pay_token) +
                                    encode_uint256(pay_amount.value) +
                                    encode_uint256(expires) +
                                    encode_uint256(nonce)).digest()

        signature = eth_sign(order_hash, self.web3)
        v, r, s = to_vrs(signature)

        return Order(self, Address(self.web3.eth.defaultAccount), pay_token, pay_amount, buy_token, buy_amount,
                     expires, nonce, v, r, s)
Beispiel #3
0
    def cancel_order(self, order: Order) -> bool:
        assert(isinstance(order, Order))

        nonce = self.next_nonce()
        signed_data = keccak_256(encode_bytes(hexstring_to_bytes(order.order_hash)) +
                                 encode_uint256(nonce)).digest()

        signature = eth_sign(signed_data, self.idex.web3)
        v, r, s = to_vrs(signature)

        data = {
            'orderHash': order.order_hash,
            'nonce': str(nonce),
            'address': self._our_address(),
            'v': v,
            'r': bytes_to_hexstring(r),
            's': bytes_to_hexstring(s)
        }

        self.logger.info(f"Cancelling order #{order.order_id}...")

        result = self._http_post("/cancel", data)
        success = result['success'] == 1

        if success:
            self.logger.info(f"Cancelled order #{order.order_id}")
        else:
            self.logger.info(f"Failed to cancel order #{order.order_id}")

        return success