Skip to content

Commit

Permalink
refactor: updating to latest graphql schema since featureflags don't …
Browse files Browse the repository at this point in the history
…include spot & perp
  • Loading branch information
CalicoNino committed Oct 23, 2024
1 parent b89a700 commit 236bdf0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 199 deletions.
68 changes: 1 addition & 67 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ The `nibijs` source code can be found in the `src` directory.
- [Example: Creating a wallet](#example-creating-a-wallet)
- [Example: Querying](#example-querying)
- [Example: Sending funds](#example-sending-funds)
- [Example: Transaction with arbitrary messages](#example-transaction-with-arbitrary-messages)
- [Codebase structure](#codebase-structure)
- [Development Quick Start](#development-quick-start)
- [🔓 License](#%F0%9F%94%93-license)
- [🔓 License](#-license)

To learn more about Nibiru, see [nibiru.fi/docs](https://nibiru.fi/docs)

Expand Down Expand Up @@ -100,14 +99,6 @@ console.log("balances: %o", balances)
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
Expand Down Expand Up @@ -151,63 +142,6 @@ balances = await querier.getAllBalances(exampleAddress)
console.log("balances: %o", balances)
```

### Example: Transaction with arbitrary messages

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

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 = [
Msg.perp.openPosition({
sender: fromAddr,
pair: pair,
quoteAssetAmount: 10,
leverage: 1,
goLong: true,
baseAssetAmountLimit: 0,
}),
Msg.perp.addMargin({
sender: fromAddr,
pair: pair,
margin: coin("20", "unusd"),
}),
Msg.perp.removeMargin({
sender: fromAddr,
pair: pair,
margin: coin("5", "unusd"),
}),
// 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

| Directories of `@nibiruchain/nibijs` | Purpose/Utility |
Expand Down
133 changes: 1 addition & 132 deletions src/gql/utils/defaultObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,10 @@ import {
GQLInflationDistribution,
GQLInflationInfo,
GQLInflationRewards,
GQLMarkPriceCandle,
GQLOracleEntry,
GQLOraclePrice,
GQLPerpLeaderboard,
GQLPerpMarket,
GQLPerpPosition,
GQLPerpPositionChange,
GQLProxies,
GQLRedelegation,
GQLSpotLpPosition,
GQLSpotPool,
GQLSpotPoolSwap,
GQLStakingActionType,
GQLStakingHistoryItem,
GQLToken,
Expand Down Expand Up @@ -82,68 +74,6 @@ export const defaultActor: GQLUser = {

export const defaultUser = defaultActor

export const defaultPerpMarket: GQLPerpMarket = {
base_reserve: 0,
ecosystem_fund_fee_ratio: 0,
max_funding_rate: 0,
enabled: true,
exchange_fee_ratio: 0,
funding_rate_epoch_id: "",
index_price_twap: 0,
is_deleted: false,
latest_cumulative_premium_fraction: 0,
liquidation_fee_ratio: 0,
maintenance_margin_ratio: 0,
mark_price: 0,
mark_price_twap: 0,
max_leverage: 0,
pair: "",
partial_liquidation_ratio: 0,
prepaid_bad_debt: defaultToken,
price_multiplier: 0,
quote_reserve: 0,
sqrt_depth: 0,
total_long: 0,
total_short: 0,
twap_lookback_window: "",
}

export const defaultPerpPosition: GQLPerpPosition = {
bad_debt: 0,
last_updated_block: defaultBlock,
latest_cumulative_premium_fraction: 0,
margin: 0,
margin_ratio: 0,
open_notional: 0,
pair: "",
liquidation_price: 0,
position_notional: 0,
size: 0,
trader_address: "",
unrealized_funding_payment: 0,
unrealized_pnl: 0,
}

export const defaultPool: GQLSpotPool = {
amplification: 0,
created_block: defaultBlock,
exit_fee: 0,
swap_fee: 0,
pool_id: 0,
tokens: [defaultToken],
pool_type: "",
total_shares: defaultToken,
total_weight: 0,
weights: [defaultToken],
}

export const defaultSpotPool = {
block: defaultBlock,
pool: defaultPool,
pool_shares: defaultToken,
user: defaultUser,
}

export const defaultGovProposal: GQLGovProposal = {
depositEndTime: "",
finalTallyResultAbstain: 0,
Expand Down Expand Up @@ -176,58 +106,14 @@ export const defaultGovVote: GQLGovVote = {
sender: defaultUser,
}

export const defaultMarkPriceCandles: GQLMarkPriceCandle = {
close: 0,
high: 0,
low: 0,
open: 0,
volume: 0,
volumeNotional: 0,
pair: "",
period: 0,
periodInterval: "",
periodStartTs: "",
indexPriceTwapClose: 0,
}

export const defaultPerpPositionChanges: GQLPerpPositionChange = {
badDebt: defaultToken,
block: defaultBlock,
changeReason: "",
eventSeqNo: 0,
exchangedNotional: 0,
exchangedSize: 0,
fundingPayment: 0,
latestCumulativePremiumFraction: 0,
margin: 0,
marginToUser: 0,
openNotional: 0,
pair: "",
positionNotional: 0,
realizedPnl: 0,
size: 0,
traderAddress: "",
transactionFee: defaultToken,
txSeqNo: 0,
}

export const defaultPerpLeaderboard: GQLPerpLeaderboard = {
avg_pct_pnl_rank: 0,
avg_pct_pnl: 0,
input_margin: 0,
raw_pnl: 0,
raw_pnl_with_unrealized: 0,
trader_address: "",
}

export const defaultGovernance: GQLGovernance = {
govDeposits: [defaultGovDeposit],
govProposals: [defaultGovProposal],
govVotes: [defaultGovVote],
}

export const defaultDistributionCommission: GQLDistributionCommission = {
commission: [defaultToken],
commission: defaultToken,
validator: defaultValidator,
}

Expand All @@ -246,21 +132,6 @@ export const defaultRedelegations: GQLRedelegation = {
creation_block: defaultBlock,
}

export const defaultSpotLpPosition: GQLSpotLpPosition = {
created_block: defaultBlock,
pool: defaultPool,
pool_shares: defaultToken,
user: defaultUser,
}

export const defaultSpotPoolSwap: GQLSpotPoolSwap = {
block: defaultBlock,
pool: defaultPool,
token_in: defaultToken,
token_out: defaultToken,
user: defaultUser,
}

export const defaultUnbondings: GQLUnbonding = {
amount: 0,
completion_time: "",
Expand Down Expand Up @@ -342,8 +213,6 @@ export const defaultInflationInfo: GQLInflationInfo = {
export const defaultFeatureFlags: GQLFeatureFlags = {
gov: true,
oracle: true,
perp: false,
spot: false,
staking: true,
wasm: true,
}
Expand Down
6 changes: 6 additions & 0 deletions src/sdk/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @file Automatically generated by barrelsby.
*/

export * from "./cosmwasmclient"
export * from "./signingcosmwasmclient"
1 change: 1 addition & 0 deletions src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @file Automatically generated by barrelsby.
*/

export * from "./core/index"
export * from "./msg/index"
export * from "./query/index"
export * from "./tx/index"
Expand Down

0 comments on commit 236bdf0

Please sign in to comment.