diff --git a/README.md b/README.md index f8b426a..12264b4 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Users can import predefined networks from [constants](https://github.com/firefly from bluefin_v2_client import Networks ``` -For testing purposes use `Networks[SUI_STAGING]` and for production use `Networks[SUI_PROD]`. +For testing purposes use `Networks["SUI_STAGING"]` and for production use `Networks["SUI_PROD"]`. ## Initialization example​ diff --git a/examples/1.initialization.py b/examples/1.initialization.py index 4bc64f7..51f7f10 100644 --- a/examples/1.initialization.py +++ b/examples/1.initialization.py @@ -1,14 +1,9 @@ -import os -import sys - -# sys.path.append(os.getcwd() + "/src/") - from config import TEST_ACCT_KEY, TEST_NETWORK - -from bluefin_v2_client import BluefinClient, Networks from pprint import pprint import asyncio +from bluefin_v2_client import BluefinClient, Networks + async def main(): # initialize client @@ -23,13 +18,12 @@ async def main(): print("Account Address:", client.get_public_address()) - # # gets user account data on-chain + # gets user account data on-chain data = await client.get_user_account_data() + pprint(data) await client.close_connections() - pprint(data) - if __name__ == "__main__": loop = asyncio.new_event_loop() diff --git a/examples/10.1.socket_readonly.py b/examples/10.1.socket_readonly.py index 375e7dc..e1c7aea 100644 --- a/examples/10.1.socket_readonly.py +++ b/examples/10.1.socket_readonly.py @@ -1,14 +1,8 @@ -import sys, os - -# sys.path.append(os.getcwd() + "/src/") - import time from config import TEST_ACCT_KEY, TEST_NETWORK from bluefin_v2_client import BluefinClient, Networks, MARKET_SYMBOLS, SOCKET_EVENTS import asyncio -TEST_NETWORK = "SUI_STAGING" - event_received = False diff --git a/examples/10.sockets.py b/examples/10.sockets.py index 05fb66d..1ff9b37 100644 --- a/examples/10.sockets.py +++ b/examples/10.sockets.py @@ -1,13 +1,8 @@ -import sys, os - -# sys.path.append(os.getcwd() + "/src/") - import time from config import TEST_ACCT_KEY, TEST_NETWORK from bluefin_v2_client import BluefinClient, Networks, MARKET_SYMBOLS, SOCKET_EVENTS import asyncio -TEST_NETWORK = "SUI_STAGING" event_received = False diff --git a/examples/12.open_order_event.py b/examples/12.open_order_event.py index a753fc1..9ef622e 100644 --- a/examples/12.open_order_event.py +++ b/examples/12.open_order_event.py @@ -19,8 +19,6 @@ ) import asyncio -TEST_NETWORK = "SUI_STAGING" - event_received = False diff --git a/examples/13.orderbook_updates.py b/examples/13.orderbook_updates.py index 947c309..fbdfc5f 100644 --- a/examples/13.orderbook_updates.py +++ b/examples/13.orderbook_updates.py @@ -7,6 +7,7 @@ sys.path.append(os.getcwd() + "/src/") import time from config import TEST_ACCT_KEY, TEST_NETWORK +import asyncio from bluefin_v2_client import ( BluefinClient, Networks, @@ -16,9 +17,7 @@ ORDER_TYPE, OrderSignatureRequest, ) -import asyncio -TEST_NETWORK = "SUI_STAGING" event_received = False diff --git a/examples/2.user_info.py b/examples/2.user_info.py index dc1e6e3..04ae8ca 100644 --- a/examples/2.user_info.py +++ b/examples/2.user_info.py @@ -1,14 +1,10 @@ -import sys, os - -# sys.path.append(os.getcwd() + "/src/") -from config import TEST_ACCT_KEY, TEST_NETWORK -from bluefin_v2_client import BluefinClient, Networks, MARKET_SYMBOLS from pprint import pprint import asyncio +from config import TEST_ACCT_KEY, TEST_NETWORK +from bluefin_v2_client import BluefinClient, Networks, MARKET_SYMBOLS async def main(): - # create client instance client = BluefinClient( True, # agree to terms and conditions Networks[TEST_NETWORK], # network to connect with @@ -21,18 +17,13 @@ async def main(): # gets user account data on Bluefin exchange data = await client.get_user_account_data() - pprint(data) position = await client.get_user_position({"symbol": MARKET_SYMBOLS.ETH}) - - # returns {} when user has no position - pprint(position) + pprint(position) # returns {} when user has no position position = await client.get_user_position({"symbol": MARKET_SYMBOLS.BTC}) - - # returns user position if exists - pprint(position) + pprint(position) # returns user position if exists await client.close_connections() diff --git a/examples/3.balance.py b/examples/3.balance.py index c0e418f..67d0712 100644 --- a/examples/3.balance.py +++ b/examples/3.balance.py @@ -1,10 +1,7 @@ -import sys, os - -# sys.path.append(os.getcwd() + "/src/") - +from pprint import pprint +import asyncio from config import TEST_ACCT_KEY, TEST_NETWORK from bluefin_v2_client import BluefinClient, Networks -import asyncio async def main(): @@ -19,37 +16,34 @@ async def main(): # on boards user on Bluefin. Must be set to true for first time use await client.init(True) - # checks chain native token balance. - # A user must have native tokens to perform contract calls - + # checks SUI token balance print("Chain token balance:", await client.get_native_chain_token_balance()) - # check margin bank balance on-chain + # check usdc balance deposited to Bluefin Margin Bank print("Margin bank balance:", await client.get_margin_bank_balance()) - # check usdc balance user has on-chain + # check usdc balance deposited to USDC contract print("USDC balance:", await client.get_usdc_balance()) - # deposit usdc to margin bank - # must have native chain tokens to pay for gas fee - usdc_coins = client.get_usdc_coins() - ## we expect that you have some coins in your usdc, - coin_obj_id = usdc_coins["data"][1]["coinObjectId"] - print("USDC deposited:", await client.deposit_margin_to_bank(5000, coin_obj_id)) + usdc_coins = await client.get_usdc_coins() + pprint(usdc_coins) + + # deposit 100 usdc to margin bank + print("USDC deposited:", await client.deposit_margin_to_bank(100)) # check margin bank balance resp = await client.get_margin_bank_balance() - print("Margin bank balance:", resp) + print("Margin bank balance after deposit:", resp) # withdraw margin bank balance - print("USDC Withdrawn:", await client.withdraw_margin_from_bank(10)) + print("USDC Withdrawn:", await client.withdraw_margin_from_bank(100)) # check margin bank balance - print("Margin bank balance:", await client.get_margin_bank_balance()) + print("Margin bank balance after withdraw:", await client.get_margin_bank_balance()) - print("Withdraw all", await client.withdraw_all_margin_from_bank()) + # print("Withdraw all", await client.withdraw_all_margin_from_bank()) - print("Margin bank balance:", await client.get_margin_bank_balance()) + # print("Margin bank balance:", await client.get_margin_bank_balance()) await client.close_connections() diff --git a/examples/4.placing_orders.py b/examples/4.placing_orders.py index 52eda7f..cc65a4e 100644 --- a/examples/4.placing_orders.py +++ b/examples/4.placing_orders.py @@ -1,6 +1,6 @@ -import sys, os - -# sys.path.append(os.getcwd() + "/src/") +from pprint import pprint +import asyncio +import time from config import TEST_ACCT_KEY, TEST_NETWORK from bluefin_v2_client import ( BluefinClient, @@ -10,65 +10,48 @@ ORDER_TYPE, OrderSignatureRequest, ) -import asyncio -async def place_limit_order(client: BluefinClient): +async def place_orders(client: BluefinClient): # default leverage of account is set to 3 on Bluefin user_leverage = await client.get_user_leverage(MARKET_SYMBOLS.ETH) - print("User Leverage", user_leverage) + print("User Default Leverage", user_leverage) - # creates a LIMIT order to be signed + # Sign and place a limit order at 4x leverage. Order is signed using the account seed phrase set on the client + adjusted_leverage = 4 + await client.adjust_leverage(MARKET_SYMBOLS.ETH, adjusted_leverage) signature_request = OrderSignatureRequest( symbol=MARKET_SYMBOLS.ETH, # market symbol price=1636.8, # price at which you want to place order quantity=0.01, # quantity side=ORDER_SIDE.BUY, orderType=ORDER_TYPE.LIMIT, - leverage=user_leverage, + leverage=adjusted_leverage, + expiration=int( + (time.time() + 864000) * 1000 + ), # expiry after 10 days, default expiry is a month ) - - # create signed order signed_order = client.create_signed_order(signature_request) - - print("Placing a limit order") - # place signed order on orderbook resp = await client.post_signed_order(signed_order) + pprint({"msg": "placing limit order", "resp": resp}) - # returned order with PENDING state - print(resp) - - return - - -async def place_market_order(client: BluefinClient): - # default leverage of account is set to 3 on Bluefin - user_leverage = await client.get_user_leverage(MARKET_SYMBOLS.ETH) - + # sign and place a market order at 2x leverage + adjusted_leverage = 2 + await client.adjust_leverage(MARKET_SYMBOLS.BTC, adjusted_leverage) signature_request = OrderSignatureRequest( - symbol=MARKET_SYMBOLS.ETH, + symbol=MARKET_SYMBOLS.BTC, price=0, quantity=1, - leverage=user_leverage, + leverage=adjusted_leverage, side=ORDER_SIDE.BUY, reduceOnly=False, postOnly=False, orderbookOnly=True, - expiration=1700530261000, - salt=1668690862116, orderType=ORDER_TYPE.MARKET, ) - - # create signed order signed_order = client.create_signed_order(signature_request) - - print("Placing a market order") - # place signed order on orderbook resp = await client.post_signed_order(signed_order) - - # returned order with PENDING state - print(resp) - + pprint({"msg": "placing market order", "resp": resp}) return @@ -81,9 +64,7 @@ async def main(): ) await client.init(True) - - await place_limit_order(client) - await place_market_order(client) + await place_orders(client) await client.close_connections() diff --git a/examples/5.adjusting_leverage.py b/examples/5.adjusting_leverage.py deleted file mode 100644 index 38b78c5..0000000 --- a/examples/5.adjusting_leverage.py +++ /dev/null @@ -1,39 +0,0 @@ -import sys, os - -# sys.path.append(os.getcwd() + "/src/") - -from config import TEST_ACCT_KEY, TEST_NETWORK -from bluefin_v2_client import BluefinClient, Networks, MARKET_SYMBOLS -import asyncio - - -async def main(): - # initialize client - client = BluefinClient( - True, # agree to terms and conditions - Networks[TEST_NETWORK], # network to connect with - TEST_ACCT_KEY, # seed phrase of the wallet - ) - - await client.init(True) - - print("Leverage on BTC market:", await client.get_user_leverage(MARKET_SYMBOLS.BTC)) - # we have a position on BTC so this will perform on-chain leverage update - # must have native chain tokens to pay for gas fee - await client.adjust_leverage(MARKET_SYMBOLS.BTC, 2) - - print("Leverage on BTC market:", await client.get_user_leverage(MARKET_SYMBOLS.BTC)) - - print("Leverage on ETH market:", await client.get_user_leverage(MARKET_SYMBOLS.ETH)) - # since we don't have a position on-chain, it will perform off-chain leverage adjustment - await client.adjust_leverage(MARKET_SYMBOLS.ETH, 8) - - print("Leverage on ETH market:", await client.get_user_leverage(MARKET_SYMBOLS.ETH)) - - await client.close_connections() - - -if __name__ == "__main__": - loop = asyncio.new_event_loop() - loop.run_until_complete(main()) - loop.close() diff --git a/examples/7.cancelling_orders.py b/examples/5.cancelling_orders.py similarity index 54% rename from examples/7.cancelling_orders.py rename to examples/5.cancelling_orders.py index 6e26967..4dc93a7 100644 --- a/examples/7.cancelling_orders.py +++ b/examples/5.cancelling_orders.py @@ -1,8 +1,7 @@ -import sys, os, random - -# sys.path.append(os.getcwd() + "/src/") - +from pprint import pprint +import asyncio import time +import random from config import TEST_ACCT_KEY, TEST_NETWORK from bluefin_v2_client import ( BluefinClient, @@ -13,56 +12,60 @@ ORDER_STATUS, OrderSignatureRequest, ) -from pprint import pprint -import asyncio async def main(): client = BluefinClient(True, Networks[TEST_NETWORK], TEST_ACCT_KEY) await client.init(True) - # client.create_order_to_sign() - await client.adjust_leverage(MARKET_SYMBOLS.ETH, 1) - user_leverage = await client.get_user_leverage(MARKET_SYMBOLS.ETH) - - # creates a LIMIT order to be signed + leverage = 1 + await client.adjust_leverage(MARKET_SYMBOLS.ETH, leverage) order = OrderSignatureRequest( symbol=MARKET_SYMBOLS.ETH, # market symbol price=2905, # price at which you want to place order quantity=0.01, # quantity side=ORDER_SIDE.BUY, orderType=ORDER_TYPE.LIMIT, - leverage=user_leverage, + leverage=leverage, salt=random.randint(0, 100000000), expiration=int(time.time() + (30 * 24 * 60 * 60)) * 1000, ) - signed_order = client.create_signed_order(order) - resp = await client.post_signed_order(signed_order) - print("sleeping for two seconds") + post_resp = await client.post_signed_order(signed_order) + pprint(post_resp) + time.sleep(2) - # sign order for cancellation using order hash - # you can pass a list of hashes to be signed for cancellation, good to be used when multiple orders are to be cancelled + # cancel placed order cancellation_request = client.create_signed_cancel_orders( - MARKET_SYMBOLS.ETH, order_hash=[resp["hash"]] + MARKET_SYMBOLS.ETH, order_hash=[post_resp["hash"]] ) - pprint(cancellation_request) - - # # or sign the order for cancellation using order data cancellation_request = client.create_signed_cancel_order(order) - pprint(cancellation_request) # same as above cancellation request - - # post order to exchange for cancellation - resp = await client.post_cancel_order(cancellation_request) + pprint(cancellation_request) + cancel_resp = await client.post_cancel_order(cancellation_request) + pprint(cancel_resp) - pprint(resp) + # post order again + await client.adjust_leverage(MARKET_SYMBOLS.ETH, leverage) + order = OrderSignatureRequest( + symbol=MARKET_SYMBOLS.ETH, # market symbol + price=2905, # price at which you want to place order + quantity=0.01, # quantity + side=ORDER_SIDE.BUY, + orderType=ORDER_TYPE.LIMIT, + leverage=leverage, + salt=random.randint(0, 100000000), + expiration=int(time.time() + (30 * 24 * 60 * 60)) * 1000, + ) + signed_order = client.create_signed_order(order) + post_resp = await client.post_signed_order(signed_order) + pprint(post_resp) + time.sleep(2) - # cancels all open orders, returns false if there is no open order to cancel + # this time cancel all open orders resp = await client.cancel_all_orders( MARKET_SYMBOLS.ETH, [ORDER_STATUS.OPEN, ORDER_STATUS.PARTIAL_FILLED] ) - - if resp == False: + if resp is False: print("No open order to cancel") else: pprint(resp) diff --git a/examples/6.adjusting_margin.py b/examples/6.adjusting_margin.py index 2d75243..2914fbf 100644 --- a/examples/6.adjusting_margin.py +++ b/examples/6.adjusting_margin.py @@ -1,98 +1,33 @@ -import sys, os - -# sys.path.append(os.getcwd() + "/src/") - +from pprint import pprint from config import TEST_ACCT_KEY, TEST_NETWORK -from bluefin_v2_client import BluefinClient, Networks, MARKET_SYMBOLS, ADJUST_MARGIN import asyncio from bluefin_v2_client import ( BluefinClient, + ADJUST_MARGIN, Networks, MARKET_SYMBOLS, - ORDER_SIDE, - ORDER_TYPE, - OrderSignatureRequest, ) -async def place_limit_order(client: BluefinClient): - # default leverage of account is set to 3 on Bluefin - user_leverage = await client.get_user_leverage(MARKET_SYMBOLS.ETH) - await client.adjust_leverage(MARKET_SYMBOLS.ETH, 3) - user_leverage = await client.get_user_leverage(MARKET_SYMBOLS.ETH) - - print("User Leverage", user_leverage) - - # creates a LIMIT order to be signed - signature_request = OrderSignatureRequest( - symbol=MARKET_SYMBOLS.ETH, # market symbol - price=1636.8, # price at which you want to place order - quantity=0.01, # quantity - side=ORDER_SIDE.BUY, - orderType=ORDER_TYPE.LIMIT, - leverage=user_leverage, - ) - - # create signed order - signed_order = client.create_signed_order(signature_request) - - print("Placing a limit order") - # place signed order on orderbook - resp = await client.post_signed_order(signed_order) - - # returned order with PENDING state - print(resp) - - return - - -async def place_market_order(client: BluefinClient): - # default leverage of account is set to 3 on Bluefin - user_leverage = await client.get_user_leverage(MARKET_SYMBOLS.ETH) - - signature_request = OrderSignatureRequest( - symbol=MARKET_SYMBOLS.ETH, - price=0, - quantity=1, - leverage=user_leverage, - side=ORDER_SIDE.BUY, - orderType=ORDER_TYPE.MARKET, - ) - - # create signed order - signed_order = client.create_signed_order(signature_request) - - print("Placing a market order") - # place signed order on orderbook - resp = await client.post_signed_order(signed_order) - - # returned order with PENDING state - print(resp) - - return - - async def main(): client = BluefinClient(True, Networks[TEST_NETWORK], TEST_ACCT_KEY) await client.init(True) print(await client.get_usdc_balance()) - # usdc_coins=client.get_usdc_coins() - # coin_obj_id=usdc_coins["data"][1]["coinObjectId"] - # await client.deposit_margin_to_bank(1000000000000, coin_obj_id) - - print(await client.get_margin_bank_balance()) - await place_market_order(client) - + # Already open positions on exchange can be queried using position = await client.get_user_position({"symbol": MARKET_SYMBOLS.ETH}) - print("Current margin in position:", position) + pprint(position) - # adding 100$ from our margin bank into our BTC position on-chain - # must have native chain tokens to pay for gas fee - print( - "Adjusting Margin", - await client.adjust_margin(MARKET_SYMBOLS.ETH, ADJUST_MARGIN.ADD, 100), - ) + if not position: + print("Account has no open position") + return + + # Add 100 USD from our margin bank into our ETH position on-chain + print("Adding Margin") + resp = await client.adjust_margin(MARKET_SYMBOLS.ETH, ADJUST_MARGIN.ADD, 100) + if resp is False: + print("Failed to add margin to position") + pprint(resp) # get updated position margin. Note it can take a few seconds to show updates # to on-chain positions on exchange as off-chain infrastructure waits for blockchain @@ -100,7 +35,7 @@ async def main(): position = await client.get_user_position({"symbol": MARKET_SYMBOLS.ETH}) print("Current margin in position:", position["margin"]) - # removing 100$ from margin + # Remove 100 USD from margin print( "Adjusting margin", await client.adjust_margin(MARKET_SYMBOLS.ETH, ADJUST_MARGIN.REMOVE, 100), @@ -109,12 +44,6 @@ async def main(): position = await client.get_user_position({"symbol": MARKET_SYMBOLS.ETH}) print("Current margin in position:", int(position["margin"])) - try: - # will throw as user does not have any open position on BTC to adjust margin on - await client.adjust_margin(MARKET_SYMBOLS.BTC, ADJUST_MARGIN.ADD, 100) - except Exception as e: - print("Error:", e) - await client.close_connections() diff --git a/examples/7.onboarding_signer.py b/examples/7.onboarding_signer.py new file mode 100644 index 0000000..f43d2df --- /dev/null +++ b/examples/7.onboarding_signer.py @@ -0,0 +1,27 @@ +from config import TEST_NETWORK +from pprint import pprint +import asyncio +from bluefin_v2_client import BluefinClient, Networks + + +async def main(): + seed_phrase = "person essence firm tail chapter forest return pulse dismiss unlock zebra amateur" + + client = BluefinClient( + True, # agree to terms and conditions + Networks[TEST_NETWORK], # network to connect i.e. SUI_STAGING or SUI_PROD + seed_phrase, # seed phrase of the wallet + ) + + await client.init(True) + signature = await client.onboard_user() + pprint("User onboarding Signature:") + pprint(signature) + + await client.close_connections() + + +if __name__ == "__main__": + loop = asyncio.new_event_loop() + loop.run_until_complete(main()) + loop.close() diff --git a/examples/9.user_data.py b/examples/9.user_data.py index 38023a5..2f104ba 100644 --- a/examples/9.user_data.py +++ b/examples/9.user_data.py @@ -1,14 +1,8 @@ -import sys, os - -# sys.path.append(os.getcwd() + "/src/") - from config import TEST_ACCT_KEY, TEST_NETWORK from bluefin_v2_client import BluefinClient, Networks, MARKET_SYMBOLS, ORDER_STATUS from pprint import pprint import asyncio -TEST_NETWORK = "SUI_STAGING" - async def main(): client = BluefinClient(True, Networks[TEST_NETWORK], TEST_ACCT_KEY) diff --git a/examples/config.py b/examples/config.py index 77f4011..25ee8e3 100644 --- a/examples/config.py +++ b/examples/config.py @@ -1,6 +1,3 @@ -TEST_ACCT_KEY = ( - "negative repeat fold noodle symptom spirit spend trophy merge ethics math erupt" -) -# TEST_ACCT_KEY = "milk fit tape notable input seek circle define deny rally camera sorry" +TEST_ACCT_KEY = "cigar tip purchase gym income crumble short hobby model rocket push twelve" # public key 0x6579b6b4edd3d89ba088ea9d4fcdc3d9e030605054ebff2def870e601bc3eb98 TEST_SUB_ACCT_KEY = "7540d48032c731b3a17947b63a04763492d84aef854246d355a703adc9b54ce9" TEST_NETWORK = "SUI_STAGING"