Skip to content

Commit

Permalink
fix: readme code examples adjusted (#278)
Browse files Browse the repository at this point in the history
* fix: readme code examples adjusted

* chore: removed unused examples folder

* chore: removed testing examples from gh workflow

* fix: removed examples from install:packages
  • Loading branch information
onikonychev authored Jan 11, 2024
1 parent c0dea51 commit 113ca01
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 1,024 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/test-ts-sdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,6 @@ jobs:
with:
cmd: test

- name: "Run examples"
run: |
cd examples
yarn run:examples
cd ..
# tsx is a TS script executor similar to ts-node, except it runs more
# consistently and has a more lightweight implementation.
- name: Jest Coverage Comment
uses: MishaKav/jest-coverage-comment@main

Expand Down
103 changes: 63 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,45 +86,69 @@ console.log("address: ", address)
### Example: Querying

```js
import { NibiruQuerier, NibiruTxClient, Localnet } from "@nibiruchain/nibijs"
import { NibiruQuerier, Testnet } from "@nibiruchain/nibijs"

export const CHAIN: Chain = Localnet
export const CHAIN = Testnet(2)
const querier = await NibiruQuerier.connect(CHAIN.endptTm)

const perpParamsResp = await querier.nibiruExtensions.perp.params()
console.log("perpParams: %o", perpParamsResp)
// Query balances
const exampleAddress = "nibi17dz4cdw5fmm2cxd4ht9xvjmpw3ycmpkpcc6js9"
const balances = await querier.getAllBalances(exampleAddress)
console.log("balances: %o", balances)

const allMarkets = await querier.nibiruExtensions.perp.markets({
pair: "ueth:unusd",
})
console.log("allMarkets: %o", allMarkets)

const blockHeight = 1
// Query block
const blockHeight = 200000
const block = await querier.getBlock(blockHeight)
console.log("block: %o", block)

// Query PERP markets
const perpMarkets = await querier.nibiruExtensions.perp.markets()
console.log("perpMarkets: %o", perpMarkets)

// Query SPOT pools
const spotPools = await querier.nibiruExtensions.spot.pools()
console.log("spotPools: %o", spotPools)
```

### Example: Sending funds

```js
import {
Coin,
NibiruTxClient,
newSignerFromMnemonic,
Localnet
Testnet,
NibiruQuerier,
} from "@nibiruchain/nibijs"
import { coins } from "@cosmjs/proto-signing"

export const CHAIN: Chain = Localnet
const signer = await newSignerFromMnemonic(mnemonic!)
const txClient = await NibiruTxClient.connectWithSigner(
CHAIN.endptTm,
signer,
)
export const CHAIN = Testnet(2)
const mnemonic = "your mnemonic here..."
const signer = await newSignerFromMnemonic(mnemonic)
const querier = await NibiruQuerier.connect(CHAIN.endptTm)
const txClient = await NibiruTxClient.connectWithSigner(CHAIN.endptTm, signer)
const [{ address: fromAddr }] = await signer.getAccounts()

const tokens: Coin[] = coins(5, "unibi")
const toAddr: string = "..." // bech32 address of the receiving party
const txResp = await txClient.sendTokens(fromAddr, toAddr, tokens, "auto")
// Check balance before sending tokens
const exampleAddress = "nibi1mzjkw9z5ugajxchl884y0c28lk2627hpuljuw4"
let balances = await querier.getAllBalances(exampleAddress)
console.log("balances: %o", balances)

const tokens = coins(5, "unibi")
const txResp = await txClient.sendTokens(
fromAddr,
exampleAddress,
tokens,
5000 // gas fee 5000 unibi
)
console.log(txResp)

// Execution could take several seconds
const delay = (ms) => new Promise((res) => setTimeout(res, ms))
await delay(10000)

// Check balance after send tokens
balances = await querier.getAllBalances(exampleAddress)
console.log("balances: %o", balances)
```

### Example: Transaction with arbitrary messages
Expand All @@ -134,30 +158,21 @@ import {
NibiruTxClient,
newSignerFromMnemonic,
Msg,
TxMessage,
StdFee,
toSdkInt,
parseEventLogs,
Localnet
Testnet,
NibiruQuerier,
} from "@nibiruchain/nibijs"
import { Msg, TxMessage } from "@nibiruchain/nibijs/dist/msg"
import { coin } from "@cosmjs/proto-signing"

// const mnemonic = "..." <--
export const CHAIN: Chain = Localnet
const signer = await newSignerFromMnemonic(mnemonic!)
signer.getAccounts()
const txClient = await NibiruTxClient.connectWithSigner(
CHAIN.endptTm,
signer,
)
const mnemonic = "Your mnemonic here"
export const CHAIN = Testnet(2)
const signer = await newSignerFromMnemonic(mnemonic)
const querier = await NibiruQuerier.connect(CHAIN.endptTm)
const txClient = await NibiruTxClient.connectWithSigner(CHAIN.endptTm, signer)
const [{ address: fromAddr }] = await signer.getAccounts()
const pair = "ubtc:unusd"

// ------------------------------------
// Construct tx msgs
// ------------------------------------
const msgs: TxMessage[] = [
const msgs = [
Msg.perp.openPosition({
sender: fromAddr,
pair: pair,
Expand All @@ -179,10 +194,18 @@ const msgs: TxMessage[] = [
// final margin value of 10 (open) + 20 (add) - 5 (remove) = 25
]

// ------------------------------------
// Broadcast tx
// ------------------------------------
const txResp = await txClient.signAndBroadcast(fromAddr, msgs, "auto")
console.log(txResp)

// Check your open PERP positions
const delay = (ms) => new Promise((res) => setTimeout(res, ms))
await delay(5000)

const perpPositions = await querier.nibiruExtensions.perp.positions({
trader: fromAddr,
})
console.log("perpPositions: %o", perpPositions)
```

## Codebase structure
Expand Down
16 changes: 0 additions & 16 deletions examples/README.md

This file was deleted.

Loading

0 comments on commit 113ca01

Please sign in to comment.