Skip to content

Commit

Permalink
Merge pull request #44 from InjectiveLabs/update-sdk
Browse files Browse the repository at this point in the history
Update sdk
  • Loading branch information
achilleas-kal authored Dec 6, 2021
2 parents 272ad72 + 3dd9e6f commit 211e3cc
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 18 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ python pyinjective/fetch_metadata.py


### Changelogs
0.5.4
* Added PortfolioRequest, GetTxByHashRequest, AuctionRequest, AuctionsRequest, StreamBidsRequest and provided examples
* Updated the composer with MsgIncreasePosition and MsgLiquidatePosition
* Added reduce-only orders to the composer and updated examples

0.5.3
* add skip, and limit to trade request

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ async def main() -> None:
fee_recipient=fee_recipient,
price=41027,
quantity=0.01,
is_buy=True,
leverage=0.7,
is_buy=True
),
composer.DerivativeOrder(
market_id=market_id,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=62140,
quantity=0.01,
leverage=1.4,
is_buy=False
is_buy=False,
is_reduce_only=True
),
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ async def main() -> None:
market_id=market_id,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=44054.48,
quantity=0.01,
leverage=0.7,
is_buy=True
price=50000,
quantity=0.1,
is_buy=False,
leverage=1,
is_reduce_only=False
)

# build sim tx
Expand Down
32 changes: 32 additions & 0 deletions examples/exchange_api_examples/accounts_rpc/8_PortfolioRequest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2021 Injective 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.
"""Injective Exchange API client for Python. Example only."""

import asyncio
import logging

from pyinjective.client import Client
from pyinjective.constant import Network


async def main() -> None:
network = Network.testnet()
client = Client(network, insecure=True)
account_address = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku"
portfolio = client.get_portfolio(account_address=account_address)
print(portfolio)

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
asyncio.get_event_loop().run_until_complete(main())
31 changes: 31 additions & 0 deletions examples/exchange_api_examples/auctions_rpc/10_Auction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021 Injective 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.
"""Injective Exchange API client for Python. Example only."""

import asyncio
import logging

from pyinjective.client import Client
from pyinjective.constant import Network

async def main() -> None:
network = Network.testnet()
client = Client(network, insecure=True)
bid_round = 135
auction = client.get_auction(bid_round=bid_round)
print(auction)

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
asyncio.get_event_loop().run_until_complete(main())
30 changes: 30 additions & 0 deletions examples/exchange_api_examples/auctions_rpc/11_Auctions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2021 Injective 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.
"""Injective Exchange API client for Python. Example only."""

import asyncio
import logging

from pyinjective.client import Client
from pyinjective.constant import Network

async def main() -> None:
network = Network.testnet()
client = Client(network, insecure=True)
auctions = client.get_auctions()
print(auctions)

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
asyncio.get_event_loop().run_until_complete(main())
31 changes: 31 additions & 0 deletions examples/exchange_api_examples/auctions_rpc/9_Streambids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021 Injective 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.
"""Injective Exchange API client for Python. Example only."""

import asyncio
import logging

from pyinjective.client import Client
from pyinjective.constant import Network

async def main() -> None:
network = Network.testnet()
client = Client(network, insecure=True)
bids = client.stream_bids()
for bid in bids:
print(bid)

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
asyncio.get_event_loop().run_until_complete(main())
31 changes: 31 additions & 0 deletions examples/exchange_api_examples/explorer_rpc/1_GetTxByHash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021 Injective 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.
"""Injective Exchange API client for Python. Example only."""

import asyncio
import logging

from pyinjective.client import Client
from pyinjective.constant import Network

async def main() -> None:
network = Network.testnet()
client = Client(network, insecure=True)
tx_hash = "0x298c3a789bb53b5978f8140fcfe9ec8a4447211b95618d8b108fcf521625fb1a"
account = client.get_tx_by_hash(tx_hash=tx_hash)
print(account)

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
asyncio.get_event_loop().run_until_complete(main())
37 changes: 35 additions & 2 deletions pyinjective/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
service_pb2_grpc as tx_service_grpc,
service_pb2 as tx_service,
)

from .proto.exchange import (
injective_accounts_rpc_pb2 as exchange_accounts_rpc_pb,
injective_accounts_rpc_pb2_grpc as exchange_accounts_rpc_grpc,
Expand All @@ -33,7 +34,11 @@
injective_derivative_exchange_rpc_pb2 as derivative_exchange_rpc_pb,
injective_derivative_exchange_rpc_pb2_grpc as derivative_exchange_rpc_grpc,
injective_meta_rpc_pb2 as exchange_meta_rpc_pb,
injective_meta_rpc_pb2_grpc as exchange_meta_rpc_grpc
injective_meta_rpc_pb2_grpc as exchange_meta_rpc_grpc,
injective_explorer_rpc_pb2 as explorer_rpc_pb,
injective_explorer_rpc_pb2_grpc as explorer_rpc_grpc,
injective_auction_rpc_pb2 as auction_rpc_pb,
injective_auction_rpc_pb2_grpc as auction_rpc_grpc
)

from .constant import Network
Expand Down Expand Up @@ -73,6 +78,8 @@ def __init__(
self.stubInsurance = insurance_rpc_grpc.InjectiveInsuranceRPCStub(self.exchange_channel)
self.stubSpotExchange = spot_exchange_rpc_grpc.InjectiveSpotExchangeRPCStub(self.exchange_channel)
self.stubDerivativeExchange = derivative_exchange_rpc_grpc.InjectiveDerivativeExchangeRPCStub(self.exchange_channel)
self.stubExplorer = explorer_rpc_grpc.InjectiveExplorerRPCStub(self.exchange_channel)
self.stubAuction = auction_rpc_grpc.InjectiveAuctionRPCStub(self.exchange_channel)

# default client methods
async def get_latest_block(self) -> tendermint_query.GetLatestBlockResponse:
Expand Down Expand Up @@ -135,6 +142,21 @@ async def get_chain_id(self) -> str:

# Injective Exchange client methods

# Auction RPC

async def get_auction(self, bid_round: int):
req = auction_rpc_pb.AuctionRequest(round=bid_round)
return await self.stubAuction.AuctionEndpoint(req)

async def get_auctions(self):
req = auction_rpc_pb.AuctionsRequest()
return await self.stubAuction.Auctions(req)

async def stream_bids(self):
req = auction_rpc_pb.StreamBidsRequest()
return await self.stubAuction.StreamBids(req)


# Meta RPC

async def ping(self):
Expand All @@ -155,6 +177,12 @@ async def stream_keepalive(self):
req = exchange_meta_rpc_pb.StreamKeepaliveRequest()
return self.stubMeta.StreamKeepalive(req)

# Explorer RPC

async def get_tx_by_hash(self, tx_hash: str):
req = explorer_rpc_pb.GetTxByTxHashRequest(hash=tx_hash)
return await self.stubExplorer.GetTxByTxHash(req)

#AccountsRPC

async def stream_subaccount_balance(self, subaccount_id: str):
Expand Down Expand Up @@ -192,6 +220,10 @@ async def get_order_states(
)
return await self.stubExchangeAccount.OrderStates(req)

async def get_portfolio(self, account_address: str):
req = exchange_accounts_rpc_pb.PortfolioRequest(account_address=account_address)
return await self.stubExchangeAccount.Portfolio(req)


# OracleRPC

Expand All @@ -218,6 +250,7 @@ async def get_redemptions(self, redeemer: str = '', redemption_denom: str = '',
req = insurance_rpc_pb.RedemptionsRequest(redeemer=redeemer, redemption_denom=redemption_denom, status=status)
return await self.stubInsurance.Redemptions(req)


# SpotRPC

async def get_spot_market(self, market_id: str):
Expand Down Expand Up @@ -333,4 +366,4 @@ async def get_derivative_subaccount_trades(self, subaccount_id: str, market_id:

async def get_funding_payments(self, subaccount_id: str, market_id: str = ''):
req = derivative_exchange_rpc_pb.FundingPaymentsRequest(subaccount_id=subaccount_id, market_id=market_id)
return await self.stubDerivativeExchange.FundingPayments(req)
return await self.stubDerivativeExchange.FundingPayments(req)
34 changes: 32 additions & 2 deletions pyinjective/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
injective_derivative_exchange_rpc_pb2_grpc as derivative_exchange_rpc_grpc,
injective_meta_rpc_pb2 as exchange_meta_rpc_pb,
injective_meta_rpc_pb2_grpc as exchange_meta_rpc_grpc,
injective_explorer_rpc_pb2 as explorer_rpc_pb,
injective_explorer_rpc_pb2_grpc as explorer_rpc_grpc,
injective_auction_rpc_pb2 as auction_rpc_pb,
injective_auction_rpc_pb2_grpc as auction_rpc_grpc
)

from .constant import Network
Expand Down Expand Up @@ -82,6 +86,8 @@ def __init__(
exchange_channel
)
)
self.stubExplorer = explorer_rpc_grpc.InjectiveExplorerRPCStub(exchange_channel)
self.stubAuction = auction_rpc_grpc.InjectiveAuctionRPCStub(exchange_channel)

# default client methods
def get_latest_block(self) -> tendermint_query.GetLatestBlockResponse:
Expand Down Expand Up @@ -158,6 +164,20 @@ def get_chain_id(self) -> str:

# Injective Exchange client methods

# Auction RPC

def get_auction(self, bid_round: int):
req = auction_rpc_pb.AuctionRequest(round=bid_round)
return self.stubAuction.AuctionEndpoint(req)

def get_auctions(self):
req = auction_rpc_pb.AuctionsRequest()
return self.stubAuction.Auctions(req)

def stream_bids(self):
req = auction_rpc_pb.StreamBidsRequest()
return self.stubAuction.StreamBids(req)

# Meta RPC

def ping(self):
Expand All @@ -178,6 +198,12 @@ def stream_keepalive(self):
req = exchange_meta_rpc_pb.StreamKeepaliveRequest()
return self.stubMeta.StreamKeepalive(req)

# Explorer RPC

def get_tx_by_hash(self, tx_hash: str):
req = explorer_rpc_pb.GetTxByTxHashRequest(hash=tx_hash)
return self.stubExplorer.GetTxByTxHash(req)

# AccountsRPC

def stream_subaccount_balance(self, subaccount_id: str):
Expand Down Expand Up @@ -233,6 +259,10 @@ def get_order_states(
)
return self.stubExchangeAccount.OrderStates(req)

def get_portfolio(self, account_address: str):
req = exchange_accounts_rpc_pb.PortfolioRequest(account_address=account_address)
return self.stubExchangeAccount.Portfolio(req)

# OracleRPC

def stream_oracle_prices(
Expand Down Expand Up @@ -276,7 +306,7 @@ def get_redemptions(
)
return self.stubInsurance.Redemptions(req)

# SpotRPC
# SpotRPC

def get_spot_market(self, market_id: str):
req = spot_exchange_rpc_pb.MarketRequest(market_id=market_id)
Expand Down Expand Up @@ -478,4 +508,4 @@ def get_funding_payments(self, subaccount_id: str, market_id: str = ""):
req = derivative_exchange_rpc_pb.FundingPaymentsRequest(
subaccount_id=subaccount_id, market_id=market_id
)
return self.stubDerivativeExchange.FundingPayments(req)
return self.stubDerivativeExchange.FundingPayments(req)
Loading

0 comments on commit 211e3cc

Please sign in to comment.