Skip to content

Commit

Permalink
Merge pull request #52 from geniusyield/51-python-sdk-add-direct-fill…
Browse files Browse the repository at this point in the history
…-support

51 python sdk add direct fill support
  • Loading branch information
4TT1L4 authored May 28, 2024
2 parents c3dedeb + 7cb1aab commit 1b03bde
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 26 deletions.
69 changes: 63 additions & 6 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from client.api.assets import get_assets_id
from client.api.orders import post_orders
from client.api.orders import delete_orders
from client.api.orders import post_orders_fill
from client.api.orders import get_order_books_market_id
from client.api.historical_prices import get_historical_prices_maestro_market_dex
import time
Expand All @@ -18,6 +19,11 @@ def __init__(self, status_code, response):
self.response = response
super().__init__(f"API request failed with status {status_code}: {response}")

class FillRequest:
def __init__(self, order_ref: str, amount: str) -> None:
self.order_ref = order_ref
self.amount = amount

class Api:

def __init__(self, client, own_address : str, wait_for_confirmation : int, logger):
Expand Down Expand Up @@ -80,9 +86,16 @@ def place_order(self, offered_amount : str, offered_token : str, price_token : s
body.price_token = price_token
body.price_amount = price_amount
response : Response[ErrorResponse | PostOrderResponse] = post_orders.sync_detailed(client=self.client, body=body)
self.logger.info(f"[PLACE-ORDER] Waiting {self.wait_for_confirmation} seconds for confirmation...")
time.sleep(self.wait_for_confirmation)
self.logger.info(f"[PLACE-ORDER] [OK] Done!")

if isinstance(response.parsed, PostOrderResponse):
self.logger.info(f"[PLACE-ORDER] Placed order: {response.parsed.transaction_id}")
self.logger.info(f"[PLACE-ORDER] Waiting {self.wait_for_confirmation} seconds for confirmation...")
time.sleep(self.wait_for_confirmation)
self.logger.info(f"[PLACE-ORDER] [OK] Done!")

if isinstance(response.parsed, ErrorResponse):
self.logger.info(f"[PLACE-ORDER] [FAILED] ⚠️ [{response.parsed.error_code}] {response.parsed.message}")

return cast(PostOrderResponse, self.process_response(response))

def cancel_order(self, order_reference : str):
Expand All @@ -92,7 +105,51 @@ def cancel_order(self, order_reference : str):
body.address=self.own_address
body.order_references=[order_reference]
response: Response[ErrorResponse | DeleteOrderResponse] = delete_orders.sync_detailed(client=self.client, body=body)
self.logger.info(f"[CANCEL-ORDER] Waiting {self.wait_for_confirmation} seconds for confirmation...")
time.sleep(self.wait_for_confirmation)
self.logger.info(f"[CANCEL-ORDER] [OK] Done!")

if isinstance(response.parsed, DeleteOrderResponse):
self.logger.info(f"[CANCEL-ORDER] [OK] Canceled: {response.parsed.transaction_id}")
self.logger.info(f"[CANCEL-ORDER] Waiting {self.wait_for_confirmation} seconds for confirmation...")
time.sleep(self.wait_for_confirmation)
self.logger.info(f"[CANCEL-ORDER] [OK] Done!")

if isinstance(response.parsed, ErrorResponse):
self.logger.info(f"[CANCEL-ORDER] [FAILED] ⚠️ [{response.parsed.error_code}] {response.parsed.message}")

return cast(DeleteOrderResponse, self.process_response(response))

def direct_fill(self, *fills: FillRequest):

self.logger.info(f"[DIRECT-FILL] Direct filling from on-chain orders...")

# Verify that the we do not try to fill from too many on-chain orders:
if len(fills) > 5:
self.logger.error("[DIRECT-FILL] Cannot fill from more than 5 on-chain orders.")
raise ValueError("[DIRECT-FILL] Cannot fill from more than 5 on-chain orders.")

# Verify FillRequest instances:
for index, fill in enumerate(fills):
if isinstance(fill, FillRequest):
self.logger.info(f"[DIRECT-FILL] #{index+1} ref: {fill.order_ref} ")
self.logger.info(f"[DIRECT-FILL] #{index+1} amount: {fill.amount} ")
else:
self.logger.error("[DIRECT-FILL] Each fill must be an instance of Fill.")
raise TypeError("[DIRECT-FILL] Each fill must be an instance of Fill.")

# Build the request body:
body = PostOrderFillParameters()
body.order_references_with_amount = []
for fill in fills:
body.order_references_with_amount.append([fill.order_ref, fill.amount])

# Send the request:
response : Response[ErrorResponse | PostOrderFillResponse] = post_orders_fill.sync_detailed(client=self.client, body=body)

if isinstance(response.parsed, PostOrderFillResponse):
self.logger.info(f"[DIRECT-FILL] [OK] SUCCESS. transaction_id: {response.parsed.transaction_id}")
self.logger.info(f"[DIRECT-FILL] Waiting {self.wait_for_confirmation} seconds for confirmation...")
time.sleep(self.wait_for_confirmation)

if isinstance(response.parsed, ErrorResponse):
self.logger.info(f"[DIRECT-FILL] [FAILED] ⚠️ [{response.parsed.error_code}] {response.parsed.message}")

return cast(PostOrderFillResponse, self.process_response(response))
71 changes: 51 additions & 20 deletions bot-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,14 @@ components:
ErrorResponse:
type: object
properties:
error:
type: object
properties:
code:
type: integer
minimum: 0
maximum: 9999
format: int32
description: Numeric error code indicating the type of error.
instance:
type: string
description: Unique identifier of the indicent instance (used for troubleshooting).
message:
type: string
description: Human-readable message providing more details about the error.
details:
type: array
items:
type: string
description: Optional array of detailed error messages or information.
errorCode:
type: string
examples: ["MULTI_FILL_NOT_SAME_PAIR"]
message:
type: string
examples: ["Given orders are not having same payment token"]
required: [code, message]
examples: [{"errorCode": "MULTI_FILL_NOT_SAME_PAIR", "message": "Given orders are not having same payment token"}]
Settings:
type: object
properties: {
Expand Down Expand Up @@ -176,6 +163,35 @@ components:
transaction: {type: string, examples: ["hexa"] }
transaction_id: {type: string, examples: ["lovelace"] }
transaction_fee: {type: string, examples: ["1"]}
PostOrderFillParameters:
type: object
properties:
order_references_with_amount:
type: array
items:
type: array
items:
type: string
example: ["0018dbaa1611531b9f11a31765e8abe875f9c43750b82b5f321350f31e1ea747#0", "100"]
example: { "order_references_with_amount": [ [ "0018dbaa1611531b9f11a31765e8abe875f9c43750b82b5f321350f31e1ea747#0", "100" ], [ "0018dbaa1611531b9f11a31765e8abe875f9c43750b82b5f321350f31e144444#0", "100" ] ] }
PostOrderFillResponse:
type: object
properties:
taker_lovelace_flat_fee:
type: string
examples: ["123456789123456789123456789123456789123456789"]
taker_offered_percent_fee:
type: string
examples: ["0.125"]
taker_offered_percent_fee_amount:
type: string
examples: ["123456789123456789123456789123456789123456789"]
transaction:
type: string
examples: ["84a70082825820975e4c7f8d7937f8102e500714feb3f014c8766fcf287a11c10c686154fcb27501825820c887cba672004607a0f60ab28091d5c24860dbefb92b1a8776272d752846574f000d818258207a67cd033169e330c9ae9b8d0ef8b71de9eb74bbc8f3f6be90446dab7d1e8bfd00018282583900fd040c7a10744b79e5c80ec912a05dbdb3009e372b7f4b0f026d16b0c663651ffc046068455d2994564ba9d4b3e9b458ad8ab5232aebbf401a1abac7d882583900fd040c7a10744b79e5c80ec912a05dbdb3009e372b7f4b0f026d16b0c663651ffc046068455d2994564ba9d4b3e9b458ad8ab5232aebbf40821a0017ad4aa2581ca6bb5fd825455e7c69bdaa9d3a6dda9bcbe9b570bc79bd55fa50889ba1466e69636b656c1911d7581cb17cb47f51d6744ad05fb937a762848ad61674f8aebbaec67be0bb6fa14853696c6c69636f6e190258021a00072f3c0e8009a1581cb17cb47f51d6744ad05fb937a762848ad61674f8aebbaec67be0bb6fa14853696c6c69636f6e1902580b5820291b4e4c5f189cb896674e02e354028915b11889687c53d9cf4c1c710ff5e4aea203815908d45908d101000033332332232332232323232323232323232323232323232322223232323235500222222222225335333553024120013232123300122333500522002002001002350012200112330012253350021001102d02c25335325335333573466e3cd400488008d404c880080b40b04ccd5cd19b873500122001350132200102d02c102c3500122002102b102c00a132635335738921115554784f206e6f7420636f6e73756d65640002302115335333573466e3c048d5402c880080ac0a854cd4ccd5cd19b8701335500b2200102b02a10231326353357389210c77726f6e6720616d6f756e740002302113263533573892010b77726f6e6720746f6b656e00023021135500122222222225335330245027007162213500222253350041335502d00200122161353333573466e1cd55cea8012400046644246600200600464646464646464646464646666ae68cdc39aab9d500a480008cccccccccc888888888848cccccccccc00402c02802402001c01801401000c008cd40548c8c8cccd5cd19b8735573aa0049000119910919800801801180f1aba15002301a357426ae8940088c98d4cd5ce01381401301289aab9e5001137540026ae854028cd4054058d5d0a804999aa80c3ae501735742a010666aa030eb9405cd5d0a80399a80a80f1aba15006335015335502101f75a6ae854014c8c8c8cccd5cd19b8735573aa00490001199109198008018011919191999ab9a3370e6aae754009200023322123300100300233502475a6ae854008c094d5d09aba2500223263533573805605805405226aae7940044dd50009aba150023232323333573466e3c048d5402c880080ac0a854cd4ccd5cd19b8701335500b2200102b02a10231326353357389210c77726f6e6720616d6f756e740002302113263533573892010b77726f6e6720746f6b656e00023021135500122222222225335330245027007162213500222253350041335502d00200122161353333573466e1cd55cea8012400046644246600200600464646464646464646464646666ae68cdc39aab9d500a480008cccccccccc888888888848cccccccccc00402c02802402001c01801401000c008cd40548c8c8cccd5cd19b8735573aa004900011991091980080180118099aba1500233500c012357426ae8940088c98d4cd5ce00b00b80a80a09aab9e5001137540026ae85400cccd5401dd728031aba1500233500875c6ae84d5d1280111931a99ab9c012013011010135744a00226aae7940044dd5000899aa800bae75a224464460046eac004c8004d5405c88c8cccd55cf8011280b919a80b19aa80c18031aab9d5002300535573ca00460086ae8800c0444d5d080089119191999ab9a3370ea0029000119091180100198029aba135573ca00646666ae68cdc3a801240044244002464c6a66ae7004004403c0380344d55cea80089baa001357426ae8940088c98d4cd5ce00b80c00b00a9100109aab9e5001137540022464460046eb0004c8004d5406488cccd55cf8009280c119a80b98021aba100230033574400402446464646666ae68cdc39aab9d5003480008ccc88848ccc00401000c008c8c8c8cccd5cd19b8735573aa004900011991091980080180118099aba1500233500c012357426ae8940088c98d4cd5ce00b00b80a80a09aab9e5001137540026ae85400cccd5401dd728031aba1500233500875c6ae84d5d1280111931a99ab9c012013011010135744a00226aae7940044dd5000899aa800bae75a224464460046eac004c8004d5405c88c8cccd55cf8011280b919a80b19aa80c18031aab9d5002300535573ca00460086ae8800c0444d5d080089119191999ab9a3370ea0029000119091180100198029aba135573ca00646666ae68cdc3a801240044244002464c6a66ae7004004403c0380344d55cea80089baa001232323333573466e1cd55cea80124000466442466002006004600a6ae854008dd69aba135744a004464c6a66ae7003403803002c4d55cf280089baa0012323333573466e1cd55cea800a400046eb8d5d09aab9e500223263533573801601801401226ea8004488c8c8cccd5cd19b87500148010848880048cccd5cd"]
transaction_id: {type: string, examples: ["0018dbaa1611531b9f11a31765e8abe875f9c43750b82b5f321350f31e1ea747"] }
transaction_fee: {type: string, examples: ["1"]}
examples: [{ "taker_lovelace_flat_fee": "123456789123456789123456789123456789123456789", "taker_offered_percent_fee": "0.125", "taker_offered_percent_fee_amount": "123456789123456789123456789123456789123456789", "transaction": "84a70082825820975e4c7f8d7937f8102e500714feb3f014c8766fcf287a11c10c686154fcb27501825820c887cba672004607a0f60ab28091d5c24860dbefb92b1a8776272d752846574f000d818258207a67cd033169e330c9ae9b8d0ef8b71de9eb74bbc8f3f6be90446dab7d1e8bfd00018282583900fd040c7a10744b79e5c80ec912a05dbdb3009e372b7f4b0f026d16b0c663651ffc046068455d2994564ba9d4b3e9b458ad8ab5232aebbf401a1abac7d882583900fd040c7a10744b79e5c80ec912a05dbdb3009e372b7f4b0f026d16b0c663651ffc046068455d2994564ba9d4b3e9b458ad8ab5232aebbf40821a0017ad4aa2581ca6bb5fd825455e7c69bdaa9d3a6dda9bcbe9b570bc79bd55fa50889ba1466e69636b656c1911d7581cb17cb47f51d6744ad05fb937a762848ad61674f8aebbaec67be0bb6fa14853696c6c69636f6e190258021a00072f3c0e8009a1581cb17cb47f51d6744ad05fb937a762848ad61674f8aebbaec67be0bb6fa14853696c6c69636f6e1902580b5820291b4e4c5f189cb896674e02e354028915b11889687c53d9cf4c1c710ff5e4aea203815908d45908d101000033332332232332232323232323232323232323232323232323232222223232323235500222222222225335333553024120013232123300122333500522002002001002350012200112330012253350021001102d02c25335325335333573466e3cd400488008d404c880080b40b04ccd5cd19b873500122001350132200102d02c102c3500122002102b102c00a132635335738921115554784f206e6f7420636f6e73756d65640002302115335333573466e3c048d5402c880080ac0a854cd4ccd5cd19b8701335500b2200102b02a10231326353357389210c77726f6e6720616d6f756e740002302113263533573892010b77726f6e6720746f6b656e00023021135500122222222225335330245027007162213500222253350041335502d00200122161353333573466e1cd55cea8012400046644246600200600464646464646464646464646666ae68cdc39aab9d500a480008cccccccccc888888888848cccccccccc00402c02802402001c01801401000c008cd40548c8c8cccd5cd19b8735573aa0049000119910919800801801180f1aba15002301a357426ae8940088c98d4cd5ce01381401301289aab9e5001137540026ae854028cd4054058d5d0a804999aa80c3ae501735742a010666aa030eb9405cd5d0a80399a80a80f1aba15006335015335502101f75a6ae854014c8c8c8cccd5cd19b8735573aa00490001199109198008018011919191999ab9a3370e6aae754009200023322123300100300233502475a6ae854008c094d5d09aba2500223263533573805605805405226aae7940044dd50009aba150023232323333573466e1cd55cea8012400046644246600200600466a048eb4d5d0a80118129aba135744a004464c6a66ae700ac0b00a80a44d55cf280089baa001357426ae8940088c98d4cd5ce01381401301289aab9e5001137540026ae854010cd4055d71aba15003335015335502175c40026ae854008c06cd5d09aba2500223263533573804604804404226ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aba25001135744a00226aae7940044dd50009aba150023232323333573466e1d400520062321222230040053016357426aae79400c8cccd5cd19b875002480108c848888c008014c060d5d09aab9e500423333573466e1d400d20022321222230010053014357426aae7940148cccd5cd19b875004480008c848888c00c014dd71aba135573ca00c464c6a66ae7007807c07407006c0680644d55cea80089baa001357426ae8940088c98d4cd5ce00b80c00b00a9100109aab9e5001137540022464460046eb0004c8004d5406488cccd55cf8009280c119a80b98021aba100230033574400402446464646666ae68cdc39aab9d5003480008ccc88848ccc00401000c008c8c8c8cccd5cd19b8735573aa004900011991091980080180118099aba1500233500c012357426ae8940088c98d4cd5ce00b00b80a80a09aab9e5001137540026ae85400cccd5401dd728031aba1500233500875c6ae84d5d1280111931a99ab9c012013011010135744a00226aae7940044dd5000899aa800bae75a224464460046eac004c8004d5405c88c8cccd55cf8011280b919a80b19aa80c18031aab9d5002300535573ca00460086ae8800c0444d5d080089119191999ab9a3370ea0029000119091180100198029aba135573ca00646666ae68cdc3a801240044244002464c6a66ae7004004403c0380344d55cea80089baa001232323333573466e1cd55cea80124000466442466002006004600a6ae854008dd69aba135744a004464c6a66ae7003403803002c4d55cf280089baa0012323333573466e1cd55cea800a400046eb8d5d09aab9e500223263533573801601801401226ea8004488c8c8cccd5cd19b87500148010848880048cccd5cd19b875002480088c84888c00c010c018d5d09aab9e500423333573466e1d400d20002122200223263533573801c01e01a01801601426aae7540044dd50009191999ab9a3370ea0029001100911999ab9a3370ea0049000100911931a99ab9c00a00b009008007135573a6ea80048c8c8c8c8c8cccd5cd19b8750014803084888888800c8cccd5cd19b875002480288488888880108cccd5cd19b875003480208cc8848888888cc004024020dd71aba15005375a6ae84d5d1280291999ab9a3370ea00890031199109111111198010048041bae35742a00e6eb8d5d09aba2500723333573466e1d40152004233221222222233006009008300c35742a0126eb8d5d09aba2500923333573466e1d40192002232122222223007008300d357426aae79402c8cccd5cd19b875007480008c848888888c014020c038d5d09aab9e500c23263533573802402602202001e01c01a01801601426aae7540104d55cf280189aab9e5002135573ca00226ea80048c8c8c8c8cccd5cd19b875001480088ccc888488ccc00401401000cdd69aba15004375a6ae85400cdd69aba135744a00646666ae68cdc3a80124000464244600400660106ae84d55cf280311931a99ab9c00b00c00a009008135573aa00626ae8940044d55cf280089baa001232323333573466e1d400520022321223001003375c6ae84d55cf280191999ab9a3370ea004900011909118010019bae357426aae7940108c98d4cd5ce00400480380300289aab9d5001137540022244464646666ae68cdc39aab9d5002480008cd5403cc018d5d0a80118029aba135744a004464c6a66ae7002002401c0184d55cf280089baa00149924103505431001200132001355008221122253350011350032200122133350052200230040023335530071200100500400132001355007222533500110022213500222330073330080020060010033200135500622225335001100222135002225335333573466e1c005200000d00c13330080070060031333008007335009123330010080030020060031122002122122330010040031122123300100300212200212200111232300100122330033002002001482c0252210853696c6c69636f6e003351223300248920975e4c7f8d7937f8102e500714feb3f014c8766fcf287a11c10c686154fcb27500480088848cc00400c00880050581840100d87980821a001f372a1a358a2b14f5f6", "transaction_fee": "123456789123456789123456789123456789123456789", "transaction_id": "a8d75b90a052302c1232bedd626720966b1697fe38de556c617c340233688935" }]
BuildTxRequestBase:
type: object
properties:
Expand Down Expand Up @@ -442,6 +458,21 @@ paths:
"400": {description: Bad Request, content: {application/json: {schema: {$ref: '#/components/schemas/ErrorResponse'}}}}
"401": {description: Anauthorized access, content: {application/json: {schema: {$ref: '#/components/schemas/ErrorResponse'}}}}
"501": {description: Internal Server Error, content: {application/json: {schema: {$ref: '#/components/schemas/ErrorResponse'}}}}
/orders/fill:
post:
tags: [orders]
description: Direct fill from existing on-chain orders.
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PostOrderFillParameters'
responses:
"200": {description: Success, content: {application/json: {schema: {$ref: '#/components/schemas/PostOrderFillResponse' }}}}
"400": {description: Bad Request, content: {application/json: {schema: {$ref: '#/components/schemas/ErrorResponse'}}}}
"401": {description: Anauthorized access, content: {application/json: {schema: {$ref: '#/components/schemas/ErrorResponse'}}}}
"501": {description: Internal Server Error, content: {application/json: {schema: {$ref: '#/components/schemas/ErrorResponse'}}}}
/order-books/{market_id}:
parameters:
- name: market_id
Expand Down

0 comments on commit 1b03bde

Please sign in to comment.