Skip to content

Commit

Permalink
Updated protos and fixed perp sdk wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
john-connor84 committed Jul 7, 2022
1 parent bd2f69e commit d89fb83
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 85 deletions.
25 changes: 9 additions & 16 deletions examples/chain_client/dex/create_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@
import asyncio
import logging

from nibiru.composer import Composer, PoolAsset
from nibiru.client import Client
from nibiru.transaction import Transaction
from nibiru.network import Network
from nibiru.wallet import PrivateKey

from nibiru import Composer, PoolAsset, Client, Transaction, Network, PrivateKey
from nibiru.constant import GAS_PRICE

async def main() -> None:
# select network: local, testnet, mainnet
network = Network.local()
composer = Composer(network=network.string())

# initialize grpc client
client = Client(network, insecure=True)
Expand All @@ -36,13 +31,13 @@ async def main() -> None:
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)

# prepare tx msg
msg = composer.dex.create_pool(
msg = Composer.dex.create_pool(
creator=address.to_acc_bech32(),
swap_fee="2",
exit_fee="3",
assets=[
PoolAsset(token=composer.Coin(4, "unusd"),weight="3"),
PoolAsset(token=composer.Coin(5, "uusdc"),weight="4"),
PoolAsset(token=Composer.coin(4, "unusd"),weight="3"),
PoolAsset(token=Composer.coin(5, "uusdc"),weight="4"),
],
)

Expand All @@ -64,11 +59,9 @@ async def main() -> None:
return

# build tx
gas_price = 1
gas_limit = sim_res.gas_info.gas_used + 2 # add 2 for gas, fee computation
gas_fee = '{:.18f}'.format((gas_price * gas_limit) / pow(10, 18)).rstrip('0')
fee = [composer.Coin(
amount=gas_price * gas_limit,
gas_limit = sim_res.gas_info.gas_used * 1.25
fee = [Composer.coin(
amount=int(GAS_PRICE * gas_limit),
denom=network.fee_denom,
)]
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height)
Expand All @@ -78,7 +71,7 @@ async def main() -> None:
res = await client.send_tx_sync_mode(tx_raw_bytes)
print(res)
print("gas wanted: {}".format(gas_limit))
print("gas fee: {} unibi".format(gas_fee))
print("gas fee: {} unibi".format(res.gas_used * GAS_PRICE))

if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
Expand Down
35 changes: 35 additions & 0 deletions examples/chain_client/dex/create_pool2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2022 Nibiru Labs
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import asyncio
import logging

from nibiru import Sdk, Composer, PoolAsset

async def main() -> None:
trader = Sdk.authorize("guard cream sadness conduct invite crumble clock pudding hole grit liar hotel maid produce squeeze return argue turtle know drive eight casino maze host")
res = await trader.tx.dex.create_pool(
creator=trader.address,
swap_fee="2",
exit_fee="3",
assets=[
PoolAsset(token=Composer.coin(4, "unusd"),weight="3"),
PoolAsset(token=Composer.coin(5, "uusdc"),weight="4"),
],
)
print(res)

if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
asyncio.get_event_loop().run_until_complete(main())
2 changes: 1 addition & 1 deletion examples/chain_client/msg_send2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import asyncio
import logging

from nibiru.sdk import Sdk
from nibiru import Sdk

async def main() -> None:
trader = Sdk.authorize("guard cream sadness conduct invite crumble clock pudding hole grit liar hotel maid produce squeeze return argue turtle know drive eight casino maze host")
Expand Down
2 changes: 2 additions & 0 deletions nibiru/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from .client import Client
from .network import Network
from .sdk import Sdk
from .composer import Composer
from .transaction import Transaction
from .wallet import PrivateKey, PublicKey, Address
Expand Down
6 changes: 3 additions & 3 deletions nibiru/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class Composer:
perp = PerpComposer

@staticmethod
def Coin(amount: float, denom: str):
def coin(amount: float, denom: str):
return cosmos_base_coin_pb.Coin(amount=str(amount), denom=denom)

@staticmethod
def msg_send(from_address: str, to_address: str, amount: int, denom: str):
return cosmos_bank_tx_pb.MsgSend(
from_address=from_address,
to_address=to_address,
amount=[Composer.Coin(amount=amount, denom=denom)],
amount=[Composer.coin(amount=amount, denom=denom)],
)

@staticmethod
Expand All @@ -53,7 +53,7 @@ def msg_delegate( delegator_address: str, validator_address: str, amount: float)
return cosmos_staking_tx_pb.MsgDelegate(
delegator_address=delegator_address,
validator_address=validator_address,
amount=Composer.Coin(amount=amount, denom="inj"),
amount=Composer.coin(amount=amount, denom="inj"),
)

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions nibiru/composers/dex_composer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List

from nibiru.proto.dex.v1 import (
tx_pb2 as dex_tx_pb,
pool_pb2 as pool_tx_pb,
Expand All @@ -6,8 +8,6 @@

from nibiru.proto.cosmos.base.v1beta1 import coin_pb2 as coin_pb

from typing import List


class DexComposer:
@staticmethod
Expand Down
16 changes: 11 additions & 5 deletions nibiru/proto/perp/v1/query_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions nibiru/proto/perp/v1/query_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,29 @@ global___QueryTraderPositionRequest = QueryTraderPositionRequest
class QueryTraderPositionResponse(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor
POSITION_FIELD_NUMBER: builtins.int
POSITION_NOTIONAL_FIELD_NUMBER: builtins.int
UNREALIZED_PNL_FIELD_NUMBER: builtins.int
MARGIN_RATIO_FIELD_NUMBER: builtins.int
@property
def position(self) -> perp.v1.state_pb2.Position: ...
def position(self) -> perp.v1.state_pb2.Position:
"""The position as it exists in the blockchain state"""
pass
position_notional: typing.Text
"""The position's current notional value, if it were to be entirely closed (in margin units)."""

unrealized_pnl: typing.Text
"""The position's unrealized PnL."""

margin_ratio: typing.Text
"""The position's margin ratio, calculated from margin, unrealized PnL, and position notional."""

def __init__(self,
*,
position: typing.Optional[perp.v1.state_pb2.Position] = ...,
position_notional: typing.Text = ...,
unrealized_pnl: typing.Text = ...,
margin_ratio: typing.Text = ...,
) -> None: ...
def HasField(self, field_name: typing_extensions.Literal["position",b"position"]) -> builtins.bool: ...
def ClearField(self, field_name: typing_extensions.Literal["position",b"position"]) -> None: ...
def ClearField(self, field_name: typing_extensions.Literal["margin_ratio",b"margin_ratio","position",b"position","position_notional",b"position_notional","unrealized_pnl",b"unrealized_pnl"]) -> None: ...
global___QueryTraderPositionResponse = QueryTraderPositionResponse
Loading

0 comments on commit d89fb83

Please sign in to comment.