Skip to content

Commit

Permalink
docs(README): Add usage examples
Browse files Browse the repository at this point in the history
Also, enlarge the "Open in Colab" button
  • Loading branch information
Unique-Divine authored Dec 20, 2022
1 parent fe57ab1 commit 8a9758e
Showing 1 changed file with 78 additions and 3 deletions.
81 changes: 78 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ The package is intended to be used by coders, developers, technically-skilled tr

## Python SDK Tutorial

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)][Colab link]

[Colab link]: https://colab.research.google.com/github/NibiruChain/py-sdk/blob/master/examples/collab_notebook.ipynb
<a href="https://colab.research.google.com/github/NibiruChain/py-sdk/blob/master/examples/collab_notebook.ipynb" target="_blank">
<p align="center">
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="width: 300px;">
</p>
</a>

## Installation from `PyPI`

Expand All @@ -55,6 +57,79 @@ You may need to update `pip` to get this to run:
python -m pip install --upgrade pip
```

## Usage

#### Ex: Creating a wallet and SDK client

```python
from nibiru import wallet

# Save the mnemonic for later
mnemonic, private_key = wallet.PrivateKey.generate()
```

After, creating an account, you can create an `Sdk` instance.

```python
import nibiru

network = nibiru.network.Network.testnet(2)
sdk = nibiru.Sdk.authorize(mnemonic)
.with_network(network)
```

The `Sdk` class creates an interface to sign and send transactions or execute
queries. It is associated with:
- A transaction signer (wallet), which is configured from existing mnemonic to recover a `PrivateKey`.
- A `Network`, which specifies the RPC, LCD, and gRPC endpoints for connecting to Nibiru Chain.
- An optional `TxConfig` for changing gas parameters.

#### Ex: Using the faucet

```python
import requests

requests.post(
"https://faucet.testnet-2.nibiru.fi/",
json={
"address": agent.address,
"coins": ["10000000unibi", "100000000000unusd"],
},
)
```

#### Ex: Querying chain state

```python
# Querying the token balances of the account
sdk.query.get_bank_balances(sdk.address)

# Querying from the vpool module
query_resp = sdk.query.vpool.all_pools()
print(query_resp)
# Queries from other modules can be accessed from "sdk.query.module"
```

#### Ex: Submitting transactions

```python
import nibiru
from nibiru.msg

tx_resp = sdk.tx.execute_msgs(
nibiru.msg.MsgOpenPosition(
sender=sdk.address,
token_pair="ubtc:unusd",
side=nibiru.common.Side.BUY,
quote_asset_amount=10,
leverage=10,
base_asset_amount_limit=0,
)
)
```

You can broadcast any available transaction by passing its corresponding `Msg` to the `sdk.tx.execute_msgs` function.

## Documentation Website

Documentation can be found here: [Nibiru-py documentation](https://nibiru-py.readthedocs.io/en/latest/index.html)
Expand Down

0 comments on commit 8a9758e

Please sign in to comment.