Skip to content

Commit

Permalink
chore: update eth proto to latest (#386)
Browse files Browse the repository at this point in the history
* chore: update eth proto to latest

* refactor: updating to latest graphql schema since featureflags don't include spot & perp (#385)

* fix: test

* fix: test

---------

Co-authored-by: Calico Nino <54007257+CalicoNino@users.noreply.github.com>
  • Loading branch information
cgilbe27 and CalicoNino authored Oct 24, 2024
1 parent d40508d commit 20b645d
Showing 9 changed files with 129 additions and 226 deletions.
68 changes: 1 addition & 67 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

@@ -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
@@ -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 |
2 changes: 1 addition & 1 deletion nibiru
Submodule nibiru updated 571 files
10 changes: 2 additions & 8 deletions src/gql/heart-monitor/heart-monitor.test.ts
Original file line number Diff line number Diff line change
@@ -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"])
}
}

@@ -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"])
}
})

5 changes: 1 addition & 4 deletions src/gql/utils/consts.test.ts
Original file line number Diff line number Diff line change
@@ -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"])
}
})

133 changes: 1 addition & 132 deletions src/gql/utils/defaultObjects.ts
Original file line number Diff line number Diff line change
@@ -14,18 +14,10 @@ import {
GQLInflationDistribution,
GQLInflationInfo,
GQLInflationRewards,
GQLMarkPriceCandle,
GQLOracleEntry,
GQLOraclePrice,
GQLPerpLeaderboard,
GQLPerpMarket,
GQLPerpPosition,
GQLPerpPositionChange,
GQLProxies,
GQLRedelegation,
GQLSpotLpPosition,
GQLSpotPool,
GQLSpotPoolSwap,
GQLStakingActionType,
GQLStakingHistoryItem,
GQLToken,
@@ -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,
@@ -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,
}

@@ -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: "",
@@ -342,8 +213,6 @@ export const defaultInflationInfo: GQLInflationInfo = {
export const defaultFeatureFlags: GQLFeatureFlags = {
gov: true,
oracle: true,
perp: false,
spot: false,
staking: true,
wasm: true,
}
58 changes: 48 additions & 10 deletions src/sdk/msg/eth.test.ts
Original file line number Diff line number Diff line change
@@ -11,6 +11,12 @@ describe("setupEthMsgExtension", () => {
UpdateParams: jest.fn().mockResolvedValue({
test: "Test",
}),
CreateFunToken: jest.fn().mockResolvedValue({
test: "Test",
}),
ConvertCoinToEvm: jest.fn().mockResolvedValue({
test: "Test",
}),
} as unknown as query.MsgClientImpl)

test("should setup extension correctly", () => {
@@ -54,30 +60,62 @@ describe("setupEthMsgExtension", () => {
const result = await extension.updateParams({
authority: "",
params: {
evmDenom: "",
createFuntokenFee: "",
enableCreate: true,
enableCall: true,
extraEips: [new Long(0)],
allowUnprotectedTxs: true,
activePrecompiles: [""],
evmChannels: [""],
},
})
expect(msgUpdateParams).toHaveBeenCalledWith({
authority: "",
params: {
evmDenom: "",
createFuntokenFee: "",
enableCreate: true,
enableCall: true,
extraEips: [new Long(0)],
allowUnprotectedTxs: true,
activePrecompiles: [""],
evmChannels: [""],
},
})
expect(result).toEqual({ test: "Test" })
})
})

describe("createFunToken", () => {
test("should call MsgCreateFunToken and return the response", async () => {
const msgCreateFunToken = jest
.spyOn(query.MsgCreateFunToken, "fromPartial")
.mockReturnValue({} as query.MsgCreateFunToken)

const extension = setupEthMsgExtension(mockBaseQueryClient)
const result = await extension.createFunToken({
fromBankDenom: "",
fromErc20: "",
sender: "",
})
expect(msgCreateFunToken).toHaveBeenCalledWith({
fromBankDenom: "",
fromErc20: "",
sender: "",
})
expect(result).toEqual({ test: "Test" })
})
})

describe("convertCoinToEVM", () => {
test("should call MsgConvertCoinToEvm and return the response", async () => {
const msgConvertCoinToEvm = jest
.spyOn(query.MsgConvertCoinToEvm, "fromPartial")
.mockReturnValue({} as query.MsgConvertCoinToEvm)

const extension = setupEthMsgExtension(mockBaseQueryClient)
const result = await extension.convertCoinToEVM({
toEthAddr: "",
bankCoin: { denom: "", amount: "" },
sender: "",
})
expect(msgConvertCoinToEvm).toHaveBeenCalledWith({
toEthAddr: "",
bankCoin: { denom: "", amount: "" },
sender: "",
})
expect(result).toEqual({ test: "Test" })
})
})
})
Loading

1 comment on commit 20b645d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines Statements Branches Functions
Coverage: 94%
95.42% (876/918) 80.66% (171/212) 94.04% (300/319)

Please sign in to comment.