Esempio n. 1
0
 def test_limit_order_create(self):
     self.op = operations.Limit_order_create(
         **{
             "fee": {
                 "amount": 100,
                 "asset_id": "1.3.0"
             },
             "seller": "1.2.29",
             "amount_to_sell": {
                 "amount": 100000,
                 "asset_id": "1.3.0"
             },
             "min_to_receive": {
                 "amount": 10000,
                 "asset_id": "1.3.105"
             },
             "expiration": "2016-05-18T09:22:05",
             "fill_or_kill": False,
             "extensions": [],
         })
     self.cm = ("f68585abf4dce7c8045701016400000000000000001da08601000"
                "0000000001027000000000000693d343c57000000011f75cbfd49"
                "ae8d9b04af76cc0a7de8b6e30b71167db7fe8e2197ef9d858df18"
                "77043493bc24ffdaaffe592357831c978fd8a296b913979f106de"
                "be940d60d77b50")
     self.doit()
 def test_limit_order_create(self):
     op = operations.Limit_order_create(
         **{
             "fee": {
                 "amount": 100,
                 "asset_id": "1.3.0"
             },
             "seller": "1.2.29",
             "amount_to_sell": {
                 "amount": 100000,
                 "asset_id": "1.3.0"
             },
             "min_to_receive": {
                 "amount": 10000,
                 "asset_id": "1.3.105"
             },
             "expiration": "2016-05-18T09:22:05",
             "fill_or_kill": False,
             "extensions": []
         })
     ops = [Operation(op)]
     tx = Signed_Transaction(ref_block_num=ref_block_num,
                             ref_block_prefix=ref_block_prefix,
                             expiration=expiration,
                             operations=ops)
     tx = tx.sign([wif], chain=prefix)
     tx.verify([PrivateKey(wif).pubkey], "BTS")
     txWire = hexlify(bytes(tx)).decode("ascii")
     compare = ("f68585abf4dce7c8045701016400000000000000001da08601000"
                "0000000001027000000000000693d343c57000000011f75cbfd49"
                "ae8d9b04af76cc0a7de8b6e30b71167db7fe8e2197ef9d858df18"
                "77043493bc24ffdaaffe592357831c978fd8a296b913979f106de"
                "be940d60d77b50")
     self.assertEqual(compare[:-130], txWire[:-130])
Esempio n. 3
0
    def sell(self,
             price,
             amount,
             expiration=None,
             killfill=False,
             account=None,
             returnOrderId=False,
             **kwargs):
        """ Places a sell order in a given market

            :param float price: price denoted in ``base``/``quote``
            :param number amount: Amount of ``quote`` to sell
            :param number expiration: (optional) expiration time of the order in seconds (defaults to 7 days)
            :param bool killfill: flag that indicates if the order shall be killed if it is not filled (defaults to False)
            :param string account: Account name that executes that order
            :param string returnOrderId: If set to "head" or "irreversible" the call will wait for the tx to appear in
                                        the head/irreversible block and add the key "orderid" to the tx output

            Prices/Rates are denoted in 'base', i.e. the USD_BTS market
            is priced in BTS per USD.

            **Example:** in the USD_BTS market, a price of 300 means
            a USD is worth 300 BTS

            .. note::

                All prices returned are in the **reversed** orientation as the
                market. I.e. in the BTC/BTS market, prices are BTS per BTC.
                That way you can multiply prices with `1.05` to get a +5%.
        """
        if not expiration:
            expiration = self.blockchain.config["order-expiration"]
        if not account:
            if "default_account" in self.blockchain.config:
                account = self.blockchain.config["default_account"]
        if not account:
            raise ValueError("You need to provide an account")
        account = Account(account, blockchain_instance=self.blockchain)
        if isinstance(price, Price):
            price = price.as_base(self["base"]["symbol"])

        if isinstance(amount, Amount):
            amount = Amount(amount, blockchain_instance=self.blockchain)
            assert (amount["asset"]["symbol"] == self["quote"]["symbol"]
                    ), "Price: {} does not match amount: {}".format(
                        str(price), str(amount))
        else:
            amount = Amount(amount,
                            self["quote"]["symbol"],
                            blockchain_instance=self.blockchain)

        order = operations.Limit_order_create(
            **{
                "fee": {
                    "amount": 0,
                    "asset_id": "1.3.0"
                },
                "seller": account["id"],
                "amount_to_sell": {
                    "amount":
                    int(round(float(amount) * 10**self["quote"]["precision"])),
                    "asset_id":
                    self["quote"]["id"],
                },
                "min_to_receive": {
                    "amount":
                    int(
                        round(
                            float(amount) * float(price) *
                            10**self["base"]["precision"])),
                    "asset_id":
                    self["base"]["id"],
                },
                "expiration": formatTimeFromNow(expiration),
                "fill_or_kill": killfill,
            })
        if returnOrderId:
            # Make blocking broadcasts
            prevblocking = self.blockchain.blocking
            self.blockchain.blocking = returnOrderId

        tx = self.blockchain.finalizeOp(order, account["name"], "active",
                                        **kwargs)

        if returnOrderId:
            tx["orderid"] = tx["operation_results"][0][1]
            self.blockchain.blocking = prevblocking

        return tx
Esempio n. 4
0
    def buy(self,
            price,
            amount,
            expiration=None,
            killfill=False,
            account=None,
            returnOrderId=False):
        """ Places a buy order in a given market

            :param float price: price denoted in ``base``/``quote``
            :param number amount: Amount of ``quote`` to buy
            :param number expiration: (optional) expiration time of the order in seconds (defaults to 7 days)
            :param bool killfill: flag that indicates if the order shall be killed if it is not filled (defaults to False)
            :param string account: Account name that executes that order
            :param string returnOrderId: If set to "head" or "irreversible" the call will wait for the tx to appear in
                                        the head/irreversible block and add the key "orderid" to the tx output

            Prices/Rates are denoted in 'base', i.e. the USD_BTS market
            is priced in BTS per USD.

            **Example:** in the USD_BTS market, a price of 300 means
            a USD is worth 300 BTS

            .. note::

                All prices returned are in the **reversed** orientation as the
                market. I.e. in the BTC/BTS market, prices are BTS per BTC.
                That way you can multiply prices with `1.05` to get a +5%.

            .. warning::

                Since buy orders are placed as
                limit-sell orders for the base asset,
                you may end up obtaining more of the
                buy asset than you placed the order
                for. Example:

                    * You place and order to buy 10 USD for 100 BTS/USD
                    * This means that you actually place a sell order for 1000 BTS in order to obtain **at least** 10 USD
                    * If an order on the market exists that sells USD for cheaper, you will end up with more than 10 USD
        """
        if not expiration:
            expiration = self.bitshares.config["order-expiration"]
        if not account:
            if "default_account" in self.bitshares.config:
                account = self.bitshares.config["default_account"]
        if not account:
            raise ValueError("You need to provide an account")
        account = Account(account, bitshares_instance=self.bitshares)
        if isinstance(price, Price):
            if (price["quote"]["symbol"] == self["quote"]["symbol"]
                    and price["base"]["symbol"] == self["base"]["symbol"]):
                pass
            elif (price["base"]["symbol"] == self["quote"]["symbol"]
                  and price["quote"]["symbol"] == self["base"]["symbol"]):
                price = price.invert()
            else:
                raise ValueError(
                    "The assets in the price do not match the market!")

        if isinstance(amount, Amount):
            amount = Amount(amount, bitshares_instance=self.bitshares)
            assert (amount["asset"]["symbol"] == self["quote"]["symbol"])
        else:
            amount = Amount(amount,
                            self["quote"]["symbol"],
                            bitshares_instance=self.bitshares)

        order = operations.Limit_order_create(
            **{
                "fee": {
                    "amount": 0,
                    "asset_id": "1.3.0"
                },
                "seller": account["id"],
                "amount_to_sell": {
                    "amount":
                    int(
                        float(amount) * float(price) *
                        10**self["base"]["precision"]),
                    "asset_id":
                    self["base"]["id"]
                },
                "min_to_receive": {
                    "amount": int(
                        float(amount) * 10**self["quote"]["precision"]),
                    "asset_id": self["quote"]["id"]
                },
                "expiration": formatTimeFromNow(expiration),
                "fill_or_kill": killfill,
            })

        if returnOrderId:
            # Make blocking broadcasts
            prevblocking = self.bitshares.blocking
            self.bitshares.blocking = returnOrderId

        tx = self.bitshares.finalizeOp(order, account["name"], "active")

        if returnOrderId:
            tx["orderid"] = tx["operation_results"][0][1]
            self.bitshares.blocking = prevblocking

        return tx