def render_POST(self, request): """ .. http:post:: /wallets/(string:wallet identifier)/transfer A POST request to this endpoint will transfer some units from a wallet to another address. **Example request**: .. sourcecode:: none curl -X POST http://localhost:8085/wallets/BTC/transfer --data "amount=0.3&destination=mpC1DDgSP4PKc5HxJzQ5w9q6CGLBEQuLsN" **Example response**: .. sourcecode:: javascript { "txid": "abcd" } """ parameters = http.parse_qs(request.content.read(), 1) if self.identifier != "BTC" and self.identifier != "TBTC": request.setResponseCode(http.BAD_REQUEST) return json.twisted_dumps({ "error": "currently, currency transfers using the API " "is only supported for Bitcoin" }) wallet = self.get_market_community().wallets[self.identifier] if not wallet.created: request.setResponseCode(http.BAD_REQUEST) return json.twisted_dumps({"error": "this wallet is not created"}) if b'amount' not in parameters or b'destination' not in parameters: request.setResponseCode(http.BAD_REQUEST) return json.twisted_dumps( {"error": "an amount and a destination address are required"}) def on_transferred(txid): request.write(json.twisted_dumps({"txid": txid})) request.finish() def on_transfer_error(error): request.setResponseCode(http.INTERNAL_SERVER_ERROR) request.write( json.twisted_dumps({ "txid": "", "error": error.getErrorMessage() })) request.finish() wallet.transfer(parameters[b'amount'][0], parameters[b'destination'][0]).addCallback(on_transferred)\ .addErrback(on_transfer_error) return NOT_DONE_YET
def render_PUT(self, request): """ .. http:put:: /market/asks A request to this endpoint will create a new ask order. **Example request**: .. sourcecode:: none curl -X PUT http://localhost:8085/market/asks --data "first_asset_amount=10&second_asset_amount=10&first_asset_type=BTC&second_asset_type=MB" **Example response**: .. sourcecode:: javascript { "timestamp": 1547587907.887339, "order_number": 12, "assets": { "second": { "amount": 1000, "type": "MB" }, "first": { "amount": 100000, "type": "BTC" } }, "timeout": 3600, "trader_id": "9695c9e15201d08586e4230f4a8524799ebcb2d7" } """ parameters = http.parse_qs(request.content.read(), 1) if not has_param(parameters, b'first_asset_amount') or not has_param(parameters, b'second_asset_amount'): request.setResponseCode(http.BAD_REQUEST) return json.twisted_dumps({"error": "asset amount parameter missing"}) if not has_param(parameters, b'first_asset_type') or not has_param(parameters, b'second_asset_type'): request.setResponseCode(http.BAD_REQUEST) return json.twisted_dumps({"error": "asset type parameter missing"}) def on_ask_created(ask): if not request.finished: request.write(json.twisted_dumps({ 'assets': ask.assets.to_dictionary(), 'timestamp': int(ask.timestamp), 'trader_id': ask.order_id.trader_id.as_hex(), 'order_number': int(ask.order_id.order_number), 'timeout': int(ask.timeout) })) request.finish() self.get_market_community().create_ask(*BaseAsksBidsEndpoint.create_ask_bid_from_params(parameters))\ .addCallback(on_ask_created) return NOT_DONE_YET
def render_GET(self, request): """ .. http:get:: /market/matchmakers A GET request to this endpoint will return all known matchmakers. **Example request**: .. sourcecode:: none curl -X GET http://localhost:8085/market/matchmakers **Example response**: .. sourcecode:: javascript { "matchmakers": [{ "ip": "131.249.48.3", "port": 7008 }] } """ matchmakers = self.get_market_community().matchmakers matchmakers_json = [{"ip": mm.address[0], "port": mm.address[1]} for mm in matchmakers] return json.twisted_dumps({"matchmakers": matchmakers_json})
def on_transfer_error(error): request.setResponseCode(http.INTERNAL_SERVER_ERROR) request.write( json.twisted_dumps({ "txid": "", "error": error.getErrorMessage() })) request.finish()
def render_GET(self, request): """ .. http:get:: /market/transactions/(string:transaction_id)/payments A GET request to this endpoint will return all payments tied to a specific transaction. **Example request**: .. sourcecode:: none curl -X GET http://localhost:8085/market/transactions/ 12c406358ba05e5883a75da3f009477e4ca699a9/3/payments **Example response**: .. sourcecode:: javascript { "payments": [{ "trader_id": "12c406358ba05e5883a75da3f009477e4ca699a9", "transaction_number": 3, "price": 10, "price_type": "MC", "quantity": 10, "quantity_type": "BTC", "transferred_quantity": 4, "payment_id": "abcd", "address_from": "my_mc_address", "address_to": "my_btc_address", "timestamp": 1493906434.627721, ] } """ transaction = self.get_market_community( ).transaction_manager.find_by_id(self.transaction_id) if not transaction: request.setResponseCode(http.NOT_FOUND) return json.twisted_dumps({"error": "transaction not found"}) return json.twisted_dumps({ "payments": [payment.to_dictionary() for payment in transaction.payments] })
def on_bid_created(bid): if not request.finished: request.write(json.twisted_dumps({ 'assets': bid.assets.to_dictionary(), 'timestamp': int(bid.timestamp), 'trader_id': bid.order_id.trader_id.as_hex(), 'order_number': int(bid.order_id.order_number), 'timeout': int(bid.timeout) })) request.finish()
def render_GET(self, request): """ .. http:get:: /market/transactions A GET request to this endpoint will return all performed transactions in the market community. **Example request**: .. sourcecode:: none curl -X GET http://localhost:8085/market/transactions **Example response**: .. sourcecode:: javascript { "transactions": [{ "trader_id": "12c406358ba05e5883a75da3f009477e4ca699a9", "order_number": 4, "partner_trader_id": "34c406358ba05e5883a75da3f009477e4ca699a9", "partner_order_number": 1, "assets" { "first": { "amount": 3, "type": "BTC", }, "second": { "amount": 3, "type": "MB", } }, "transferred" { "first": { "amount": 3, "type": "BTC", }, "second": { "amount": 3, "type": "MB", } } "timestamp": 1493906434.627721, "payment_complete": False ] } """ transactions = self.get_market_community( ).transaction_manager.find_all() return json.twisted_dumps({ "transactions": [ transaction.to_block_dictionary() for transaction in transactions ] })
def render_POST(self, request): """ .. http:get:: /market/orders/(string:order_number)/cancel A POST request to this endpoint will cancel a specific order. **Example request**: .. sourcecode:: none curl -X GET http://localhost:8085/market/orders/3/cancel **Example response**: .. sourcecode:: javascript { "cancelled": True } """ market_community = self.get_market_community() order_id = OrderId(TraderId(market_community.mid), OrderNumber(int(self.order_number))) order = market_community.order_manager.order_repository.find_by_id( order_id) if not order: request.setResponseCode(http.NOT_FOUND) return json.twisted_dumps({"error": "order not found"}) if order.status != "open" and order.status != "unverified": request.setResponseCode(http.BAD_REQUEST) return json.twisted_dumps( {"error": "only open and unverified orders can be cancelled"}) market_community.cancel_order(order_id) return json.twisted_dumps({"cancelled": True})
def render_GET(self, request): """ .. http:get:: /market/orders A GET request to this endpoint will return all your orders in the market community. **Example request**: .. sourcecode:: none curl -X GET http://localhost:8085/market/orders **Example response**: .. sourcecode:: javascript { "orders": [{ "trader_id": "12c406358ba05e5883a75da3f009477e4ca699a9", "timestamp": 1493906434.627721, "assets" { "first": { "amount": 3, "type": "BTC", }, "second": { "amount": 3, "type": "MB", } } "reserved_quantity": 0, "is_ask": False, "timeout": 3600, "traded": 0, "order_number": 1, "completed_timestamp": null, "cancelled": False, "status": "open" }] } """ orders = self.get_market_community( ).order_manager.order_repository.find_all() return json.twisted_dumps( {"orders": [order.to_dictionary() for order in orders]})
def render_GET(self, request): """ .. http:get:: /market/asks A GET request to this endpoint will return all ask ticks in the order book of the market community. **Example request**: .. sourcecode:: none curl -X GET http://localhost:8085/market/asks **Example response**: .. sourcecode:: javascript { "asks": [{ "asset1": "BTC", "asset2": "MB", "ticks": [{ "trader_id": "12c406358ba05e5883a75da3f009477e4ca699a9", "timeout": 3600, "assets": { "first": { "amount": 10, "type": "BTC" }, "second": { "amount": 10, "type": "MB" } }, "traded": 5, "timestamp": 1493905920.68573, "order_number": 1}, ...] }, ...] } """ return json.twisted_dumps({"asks": self.get_market_community().order_book.asks.get_list_representation()})
def render_PUT(self, request): """ .. http:put:: /wallets/(string:wallet identifier) A request to this endpoint will create a new wallet. **Example request**: .. sourcecode:: none curl -X PUT http://localhost:8085/wallets/BTC **Example response**: .. sourcecode:: javascript { "created": True } """ if self.get_market_community().wallets[self.identifier].created: request.setResponseCode(http.BAD_REQUEST) return json.twisted_dumps({"error": "this wallet already exists"}) def on_wallet_created(_): request.write(json.twisted_dumps({"created": True})) request.finish() def on_create_error(error): request.setResponseCode(http.INTERNAL_SERVER_ERROR) request.write( json.twisted_dumps({"error": error.getErrorMessage()})) request.finish() self.get_market_community().wallets[self.identifier].create_wallet()\ .addCallbacks(on_wallet_created, on_create_error) return NOT_DONE_YET
def on_received_balances(balances): for _, balance_info in balances: wallets[balance_info[0]]['balance'] = balance_info[1] request.write(json.twisted_dumps({"wallets": wallets})) request.finish()
def on_wallet_created(_): request.write(json.twisted_dumps({"created": True})) request.finish()
def render_GET(self, request): return json.twisted_dumps({"version": VERSION})
def on_balance(balance): request.write(json.twisted_dumps({"balance": balance})) request.finish()
def on_transactions(transactions): request.write(json.twisted_dumps({"transactions": transactions})) request.finish()
def on_transferred(txid): request.write(json.twisted_dumps({"txid": txid})) request.finish()