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 (#385)
  • Loading branch information
CalicoNino authored Oct 23, 2024
1 parent 96014f2 commit cbb36d7
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 211 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
10 changes: 2 additions & 8 deletions src/gql/heart-monitor/heart-monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,7 @@ const testFeatureFlags = async (fields: GQLFeatureFlags) => {
if (resp.featureFlags) {
const { featureFlags } = resp

checkFields(
[featureFlags],
["gov", "oracle", "perp", "spot", "staking", "wasm"]
)
checkFields([featureFlags], ["gov", "oracle", "staking", "wasm"])
}
}

Expand Down Expand Up @@ -398,10 +395,7 @@ test("queryBatchHandler", async () => {
if (resp.featureFlags) {
const { featureFlags } = resp

checkFields(
[featureFlags],
["gov", "oracle", "perp", "spot", "staking", "wasm"]
)
checkFields([featureFlags], ["gov", "oracle", "staking", "wasm"])
}
})

Expand Down
5 changes: 1 addition & 4 deletions src/gql/utils/consts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ describe("queryBatchHandler tests", () => {
if (resp.featureFlags) {
const { featureFlags } = resp

checkFields(
[featureFlags],
["gov", "oracle", "perp", "spot", "staking", "wasm"]
)
checkFields([featureFlags], ["gov", "oracle", "staking", "wasm"])
}
})

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

0 comments on commit cbb36d7

Please sign in to comment.