Skip to content

Commit

Permalink
chore: adapt for v0.20.0b5 (#224)
Browse files Browse the repository at this point in the history
* chore: adapt for v0.20.0b5

* fix: tests

* chore: linting

---------

Co-authored-by: Matthias <md2022@matrixsystems.co>
  • Loading branch information
k-yang and matthiasmatt authored May 23, 2023
1 parent 0b386a2 commit 13e6238
Show file tree
Hide file tree
Showing 21 changed files with 515 additions and 693 deletions.
190 changes: 168 additions & 22 deletions examples/1- Connect to nibiru py and query.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import nibiru"
"import nibiru\n",
"\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'0.20.0b5'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nibiru.__version__"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -40,14 +54,15 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"network = nibiru.Network.devnet()"
"network = nibiru.Network.testnet(1)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -56,23 +71,42 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:root:The mnemonic used for the newly generated key is: \n",
"coin cart alone walnut busy basket shiver october buddy lamp company diet nominee hover phrase acoustic forget repair intact noise assist mountain review win\n",
"WARNING:root:Please write down this key, it will NOT be recoverable otherwise\n"
]
}
],
"source": [
"trader = nibiru.Sdk.authorize().with_network(network)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"nibi17pz067rhcnyerjvn5lkzsf2ne3udtjmk7zjp3r\n"
]
}
],
"source": [
"print(trader.address)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -81,41 +115,153 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\n",
" {\n",
" \"id\": \"1\",\n",
" \"address\": \"nibi1w00c7pqkr5z7ptewg5z87j2ncvxd88w43ug679\",\n",
" \"poolParams\": {\n",
" \"swapFee\": 0.01,\n",
" \"exitFee\": 0.01,\n",
" \"A\": \"0\"\n",
" },\n",
" \"poolAssets\": [\n",
" {\n",
" \"token\": {\n",
" \"denom\": \"unibi\",\n",
" \"amount\": \"16434695127\"\n",
" },\n",
" \"weight\": \"1073741824\"\n",
" },\n",
" {\n",
" \"token\": {\n",
" \"denom\": \"unusd\",\n",
" \"amount\": \"2025755901517\"\n",
" },\n",
" \"weight\": \"1073741824\"\n",
" }\n",
" ],\n",
" \"totalWeight\": \"2147483648\",\n",
" \"totalShares\": {\n",
" \"denom\": \"nibiru/pool/1\",\n",
" \"amount\": \"16382308445118085345149\"\n",
" }\n",
" },\n",
" {\n",
" \"id\": \"2\",\n",
" \"address\": \"nibi1vm70ju9n2laspspsh6fs2hu50z3qzf4m2emr0p\",\n",
" \"poolParams\": {\n",
" \"swapFee\": 0.01,\n",
" \"exitFee\": 0.01,\n",
" \"A\": \"10\",\n",
" \"poolType\": \"STABLESWAP\"\n",
" },\n",
" \"poolAssets\": [\n",
" {\n",
" \"token\": {\n",
" \"denom\": \"unusd\",\n",
" \"amount\": \"336875701340\"\n",
" },\n",
" \"weight\": \"1073741824\"\n",
" },\n",
" {\n",
" \"token\": {\n",
" \"denom\": \"uusdt\",\n",
" \"amount\": \"9586964043151\"\n",
" },\n",
" \"weight\": \"1073741824\"\n",
" }\n",
" ],\n",
" \"totalWeight\": \"2147483648\",\n",
" \"totalShares\": {\n",
" \"denom\": \"nibiru/pool/2\",\n",
" \"amount\": \"379679519187813492101905\"\n",
" }\n",
" }\n",
"]\n"
]
}
],
"source": [
"trader.query.perp.params()"
"print(json.dumps(trader.query.spot.pools(), indent=4))"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"liquidity\": [\n",
" {\n",
" \"denom\": \"unibi\",\n",
" \"amount\": \"16434695127\"\n",
" },\n",
" {\n",
" \"denom\": \"unusd\",\n",
" \"amount\": \"2025755901517\"\n",
" }\n",
" ]\n",
"}\n"
]
}
],
"source": [
"trader.query.vpool.all_pools()"
"print(json.dumps(trader.query.spot.total_pool_liquidity(1), indent=4))"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"liquidity\": [\n",
" {\n",
" \"denom\": \"unibi\",\n",
" \"amount\": \"16434695127\"\n",
" },\n",
" {\n",
" \"denom\": \"unusd\",\n",
" \"amount\": \"2362631602857\"\n",
" },\n",
" {\n",
" \"denom\": \"uusdt\",\n",
" \"amount\": \"9586964043151\"\n",
" }\n",
" ]\n",
"}\n"
]
}
],
"source": [
"# Let's format the previous query\n",
"import json\n",
"print(json.dumps(trader.query.vpool.all_pools(), indent=4))"
"print(json.dumps(trader.query.spot.total_liquidity(), indent=4))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"You can find the documentation on query you can execute on the official documentation website: https://nibiru-py.readthedocs.io/en/latest/nibiru.query_clients.html"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": []
Expand All @@ -137,7 +283,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.8.16"
},
"orig_nbformat": 4,
"vscode": {
Expand Down
1 change: 0 additions & 1 deletion nibiru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
Network,
NetworkType,
PoolAsset,
Side,
TxConfig,
TxType,
)
Expand Down
12 changes: 3 additions & 9 deletions nibiru/event_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
from enum import Enum

import google.protobuf.message
from nibiru_proto.proto.oracle.v1beta1 import event_pb2 as oracle_events
from nibiru_proto.proto.perp.v1 import event_pb2 as perp_events
from nibiru_proto.proto.oracle.v1 import event_pb2 as oracle_events
from nibiru_proto.proto.perp.v2 import event_pb2 as perp_events
from nibiru_proto.proto.spot.v1 import event_pb2 as spot_events
from nibiru_proto.proto.stablecoin import events_pb2 as stablecoin_events # noqa
from nibiru_proto.proto.vpool.v1 import event_pb2 as vpool_events
from nibiru_proto.proto.stablecoin.v1 import events_pb2 as stablecoin_events # noqa


class EventType(Enum):
Expand All @@ -20,11 +19,6 @@ class EventType(Enum):
PositionLiquidatedEvent = perp_events.PositionLiquidatedEvent
FundingRateChangedEvent = perp_events.FundingRateChangedEvent

# Vpool events
ReserveSnapshotSavedEvent = vpool_events.ReserveSnapshotSavedEvent
SwapOnVpoolEvent = vpool_events.SwapOnVpoolEvent
MarkPriceChanged = vpool_events.MarkPriceChangedEvent

# Spot event
PoolJoinedEvent = spot_events.EventPoolJoined
PoolCreatedEvent = spot_events.EventPoolCreated
Expand Down
2 changes: 0 additions & 2 deletions nibiru/grpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ def __init__(
# Query services
self.spot = query_clients.SpotQueryClient(self.chain_channel)
self.perp = query_clients.PerpQueryClient(self.chain_channel)
self.vpool = query_clients.VpoolQueryClient(self.chain_channel)
self.epoch = query_clients.EpochQueryClient(self.chain_channel)
self.auth = query_clients.AuthQueryClient(self.chain_channel)
self.staking = query_clients.StakingQueryClient(self.chain_channel)
self.util = query_clients.UtilQueryClient(self.chain_channel)
self.stablecoin = query_clients.StablecoinQueryClient(self.chain_channel)

if not bypass_version_check:
Expand Down
21 changes: 10 additions & 11 deletions nibiru/msg/perp.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import dataclasses
from typing import List

from nibiru_proto.proto.perp.v1 import state_pb2 as state_pb
from nibiru_proto.proto.perp.v1 import tx_pb2 as pb
from nibiru_proto.proto.perp.v2 import state_pb2 as state_pb
from nibiru_proto.proto.perp.v2 import tx_pb2 as pb

from nibiru.pytypes import Coin, PythonMsg, Side
from nibiru.pytypes import Coin, Direction, PythonMsg
from nibiru.utils import to_sdk_dec, to_sdk_int


Expand Down Expand Up @@ -51,15 +51,10 @@ def open_position(
base_asset_amount_limit (float): The minimum amount of base you are willing to receive for this amount of
quote.
"""
side: Side
if is_long:
side = Side.BUY
else:
side = Side.SELL
return MsgOpenPosition(
sender=sender,
pair=pair,
side=side,
dir=Direction.LONG if is_long else Direction.SHORT,
quote_asset_amount=quote_asset_amount,
leverage=leverage,
base_asset_amount_limit=base_asset_amount_limit,
Expand Down Expand Up @@ -221,7 +216,7 @@ class MsgOpenPosition(PythonMsg):

sender: str
pair: str
side: Side
dir: Direction
quote_asset_amount: float
leverage: float
base_asset_amount_limit: float
Expand All @@ -234,7 +229,11 @@ def to_pb(self) -> pb.MsgOpenPosition:
pb.MsgOpenPosition: The proto object.
"""
pb_side = state_pb.Side.BUY if self.side == Side.BUY else state_pb.SELL
pb_side = (
state_pb.Direction.LONG
if self.dir == Direction.LONG
else state_pb.Direction.SHORT
)
quote_asset_amount_pb = to_sdk_int(self.quote_asset_amount)
base_asset_amount_limit_pb = to_sdk_int(self.base_asset_amount_limit)
leverage_pb = to_sdk_dec(self.leverage)
Expand Down
1 change: 0 additions & 1 deletion nibiru/pytypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
PoolAsset,
PoolType,
PythonMsg,
Side,
TxConfig,
TxType,
)
Expand Down
Loading

0 comments on commit 13e6238

Please sign in to comment.