Skip to content

Commit

Permalink
Doc change (#3)
Browse files Browse the repository at this point in the history
* fixing the examples and cancel order

* documenting
  • Loading branch information
ibadia authored Aug 28, 2023
1 parent 7a75f00 commit 7ce182d
Show file tree
Hide file tree
Showing 26 changed files with 185 additions and 149 deletions.
17 changes: 17 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,20 @@
hash=hashlib.blake2b(intent,digest_size=32)
#then we finally sign the hash
```
## Margin Bank:
1. When you insert money to margin bank we give it in BASE6 but when we see balance it is in base9. internally the functions are implemented that way.
2. We expect you to give the arguments directly in DECIMALS.


## Making Direct Contract Calls:
1. for making contract calls. The one interacting with our contracts are implemented internally in `client.py`
2. we have exposed some generic functions through which you can make direct contract calls yourself
1. `def rpc_unsafe_moveCall(url,params, function_name: str, function_library: str, userAddress ,packageId, gasBudget=100000000 ):`
2. `def rpc_sui_executeTransactionBlock(url, txBytes, signature):`
3. The first one is used to serialise the contract calls you are going to make.
4. for second one you need to sign the result of first call with your signer and then make call along with serialised call and signature.
5. internally all our contract calls to packages uses these functions. for more information have a look at `rpc.py`
3. For making calls to SUI functions
1. For that we have exposed `def rpc_call_sui_function(url, params, method="suix_getCoins"):`
2. Here you can specify the params and the name of the sui function you are calling.
3. internally we use these calls to get the chain balance, usdc coins.
3 changes: 1 addition & 2 deletions examples/1.initialization.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import os
import sys

print(os.getcwd())
sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")

from config import TEST_ACCT_KEY, TEST_NETWORK

Expand Down
2 changes: 1 addition & 1 deletion examples/10.1.socket_readonly.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")

import time
from config import TEST_ACCT_KEY, TEST_NETWORK
Expand Down
2 changes: 1 addition & 1 deletion examples/10.sockets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")

import time
from config import TEST_ACCT_KEY, TEST_NETWORK
Expand Down
2 changes: 1 addition & 1 deletion examples/11.sub_accounts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")
from config import TEST_ACCT_KEY, TEST_SUB_ACCT_KEY, TEST_NETWORK
from bluefin_v2_client import (
BluefinClient,
Expand Down
6 changes: 3 additions & 3 deletions examples/12.open_order_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ async def place_limit_order(client: BluefinClient):
# creates a LIMIT order to be signed
signature_request = OrderSignatureRequest(
symbol=MARKET_SYMBOLS.ETH, # market symbol
price=1300000000000, # price at which you want to place order
quantity=10000000, # quantity
price=1300, # price at which you want to place order
quantity=0.01, # quantity
side=ORDER_SIDE.SELL,
orderType=ORDER_TYPE.LIMIT,
leverage=1000000000,
leverage=user_leverage,
)

# create signed order
Expand Down
14 changes: 6 additions & 8 deletions examples/14.web_sockets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")
import time
from config import TEST_ACCT_KEY, TEST_NETWORK
from bluefin_v2_client import (
Expand Down Expand Up @@ -33,7 +33,7 @@ def on_error(ws, error):

def on_close(ws):
# unsubscribe from global events
status = client.webSocketClient.unsubscribe_global_updates_by_symbol(
status = client.ws_client.unsubscribe_global_updates_by_symbol(
MARKET_SYMBOLS.ETH
)
print("Unsubscribed from global ETH events: {}".format(status))
Expand All @@ -42,21 +42,19 @@ def on_close(ws):

def on_open(ws):
# subscribe to global event updates for ETH market
status = client.webSocketClient.subscribe_global_updates_by_symbol(
MARKET_SYMBOLS.ETH
)
status = client.ws_client.subscribe_global_updates_by_symbol(MARKET_SYMBOLS.ETH)
print("Subscribed to global ETH events: {}".format(status))

# SOCKET_EVENTS contains all events that can be listened to
print("Listening to Exchange Health updates")
client.webSocketClient.listen(SOCKET_EVENTS.EXCHANGE_HEALTH.value, callback)
client.ws_client.listen(SOCKET_EVENTS.EXCHANGE_HEALTH.value, callback)

# logs event name and data for all markets and users that are subscribed.
# helpful for debugging
# client.socket.listen("default",callback)

print("Making socket connection to Bluefin exchange")
client.webSocketClient.initialize_socket(
client.ws_client.initialize_socket(
on_open=on_open, on_error=on_error, on_close=on_close
)

Expand All @@ -65,7 +63,7 @@ def on_open(ws):
while not event_received and time.time() < end_time:
time.sleep(1)

client.webSocketClient.stop()
client.ws_client.stop()
await client.close_connections()


Expand Down
2 changes: 1 addition & 1 deletion examples/15.get_funding_history.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")
from config import TEST_ACCT_KEY, TEST_NETWORK
from bluefin_v2_client import (
BluefinClient,
Expand Down
5 changes: 4 additions & 1 deletion examples/16.listening_events_using_sub_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
has no position of its own. So when placing orders or listening to position updates
the sub account must specify the parent address whose position its listening.
"""
import os, sys

# sys.path.append(os.getcwd() + "/src/")
import time, sys
from config import TEST_ACCT_KEY, TEST_NETWORK, TEST_SUB_ACCT_KEY
from bluefin_v2_client import (
Expand Down Expand Up @@ -35,7 +38,7 @@ async def main():

# # whitelist sub account
status = await clientParent.update_sub_account(
MARKET_SYMBOLS.ETH, clientChild.get_public_address(), True
clientChild.get_public_address(), True
)
print("Sub account created: {}\n".format(status))

Expand Down
2 changes: 1 addition & 1 deletion examples/17.1.get_orders_readonly.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")
from config import TEST_ACCT_KEY, TEST_NETWORK
from bluefin_v2_client import (
BluefinClient,
Expand Down
2 changes: 1 addition & 1 deletion examples/17.get_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")
from config import TEST_ACCT_KEY, TEST_NETWORK
from bluefin_v2_client import (
BluefinClient,
Expand Down
2 changes: 1 addition & 1 deletion examples/18.dms_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")
from config import TEST_ACCT_KEY, TEST_SUB_ACCT_KEY, TEST_NETWORK
from bluefin_v2_client import (
BluefinClient,
Expand Down
2 changes: 1 addition & 1 deletion examples/19.Generate_readonly_token.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# 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
Expand Down
2 changes: 1 addition & 1 deletion examples/2.user_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# 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
Expand Down
12 changes: 9 additions & 3 deletions examples/3.balance.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")

from config import TEST_ACCT_KEY, TEST_NETWORK
from bluefin_v2_client import BluefinClient, Networks
Expand Down Expand Up @@ -32,18 +32,24 @@ async def main():

# deposit usdc to margin bank
# must have native chain tokens to pay for gas fee
print("USDC deposited:", await client.deposit_margin_to_bank(10))
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))

# check margin bank balance
resp = await client.get_margin_bank_balance()
print("Margin bank balance:", resp)

# withdraw margin bank balance
print("USDC Withdrawn:", await client.withdraw_margin_from_bank(resp))
print("USDC Withdrawn:", await client.withdraw_margin_from_bank(10))

# check margin bank balance
print("Margin bank balance:", await client.get_margin_bank_balance())

print("Withdraw all", await client.withdraw_all_margin_from_bank())

print("Margin bank balance:", await client.get_margin_bank_balance())
await client.close_connections()


Expand Down
8 changes: 3 additions & 5 deletions examples/4.placing_orders.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")
from config import TEST_ACCT_KEY, TEST_NETWORK
from bluefin_v2_client import (
BluefinClient,
Expand Down Expand Up @@ -49,12 +49,11 @@ async def place_market_order(client: BluefinClient):
symbol=MARKET_SYMBOLS.ETH,
price=0,
quantity=1,
leverage=1,
leverage=user_leverage,
side=ORDER_SIDE.BUY,
reduceOnly=False,
postOnly=False,
orderbookOnly=True,
maker="0xa3c3504d90c428274beaa89f1238a769ea1d1c3516c31c0f4157f33787367af0",
expiration=1700530261000,
salt=1668690862116,
orderType=ORDER_TYPE.MARKET,
Expand Down Expand Up @@ -83,9 +82,8 @@ async def main():

await client.init(True)

# await place_limit_order(client)
await client
await place_limit_order(client)
await place_market_order(client)

await client.close_connections()

Expand Down
17 changes: 4 additions & 13 deletions examples/5.adjusting_leverage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")

from config import TEST_ACCT_KEY, TEST_NETWORK
from bluefin_v2_client import BluefinClient, Networks, MARKET_SYMBOLS
Expand All @@ -15,27 +15,18 @@ async def main():
TEST_ACCT_KEY, # private key of wallet
)

await client.init(True, symbol=MARKET_SYMBOLS.BTC)
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, 6)
await client.adjust_leverage(MARKET_SYMBOLS.BTC, 2)

print("Leverage on BTC market:", await client.get_user_leverage(MARKET_SYMBOLS.BTC))

# initialize client
client = BluefinClient(
True, # agree to terms and conditions
Networks[TEST_NETWORK], # network to connect with
TEST_ACCT_KEY, # private key of wallet
)

await client.init(True, symbol=MARKET_SYMBOLS.ETH)

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, 7)
await client.adjust_leverage(MARKET_SYMBOLS.ETH, 8)

print("Leverage on ETH market:", await client.get_user_leverage(MARKET_SYMBOLS.ETH))

Expand Down
14 changes: 9 additions & 5 deletions examples/6.adjusting_margin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")

from config import TEST_ACCT_KEY, TEST_NETWORK
from bluefin_v2_client import BluefinClient, Networks, MARKET_SYMBOLS, ADJUST_MARGIN
Expand All @@ -14,8 +14,6 @@
OrderSignatureRequest,
)

TEST_NETWORK = "SUI_STAGING"


async def place_limit_order(client: BluefinClient):
# default leverage of account is set to 3 on Bluefin
Expand Down Expand Up @@ -91,7 +89,10 @@ async def main():

# adding 100$ from our margin bank into our BTC position on-chain
# must have native chain tokens to pay for gas fee
await client.adjust_margin(MARKET_SYMBOLS.ETH, ADJUST_MARGIN.ADD, 100)
print(
"Adjusting Margin",
await client.adjust_margin(MARKET_SYMBOLS.ETH, ADJUST_MARGIN.ADD, 100),
)

# 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
Expand All @@ -100,7 +101,10 @@ async def main():
print("Current margin in position:", position["margin"])

# removing 100$ from margin
await client.adjust_margin(MARKET_SYMBOLS.ETH, ADJUST_MARGIN.REMOVE, 100)
print(
"Adjusting margin",
await client.adjust_margin(MARKET_SYMBOLS.ETH, ADJUST_MARGIN.REMOVE, 100),
)

position = await client.get_user_position({"symbol": MARKET_SYMBOLS.ETH})
print("Current margin in position:", int(position["margin"]))
Expand Down
7 changes: 4 additions & 3 deletions examples/7.cancelling_orders.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os, random

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")

import time
from config import TEST_ACCT_KEY, TEST_NETWORK
Expand All @@ -23,15 +23,16 @@ async def main():

# 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
order = OrderSignatureRequest(
symbol=MARKET_SYMBOLS.ETH, # market symbol
price=1636.8, # price at which you want to place order
price=2905, # price at which you want to place order
quantity=0.01, # quantity
side=ORDER_SIDE.BUY,
orderType=ORDER_TYPE.LIMIT,
leverage=1,
leverage=user_leverage,
salt=random.randint(0, 100000000),
expiration=int(time.time() + (30 * 24 * 60 * 60)) * 1000,
)
Expand Down
2 changes: 1 addition & 1 deletion examples/8.exchange_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# sys.path.append(os.getcwd() + "/src/")

from config import TEST_ACCT_KEY, TEST_NETWORK
from bluefin_v2_client import (
Expand Down
2 changes: 1 addition & 1 deletion examples/9.user_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys, os

sys.path.append(os.getcwd() + "/src/")
# 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
Expand Down
Loading

0 comments on commit 7ce182d

Please sign in to comment.