Skip to content

Instantly share code, notes, and snippets.

@Unique-Divine
Last active March 4, 2024 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Unique-Divine/f2692c42a758afb98db55be3c4304f40 to your computer and use it in GitHub Desktop.
Save Unique-Divine/f2692c42a758afb98db55be3c4304f40 to your computer and use it in GitHub Desktop.
NibiJS entrypoint examples
import { useFaucet } from "@nibiruchain/nibijs" // v0.7.2
import { newRandomWallet, WalletHD } from "@nibiruchain/nibijs/dist/tx"
async function runExample() {
const wallet: WalletHD = await newRandomWallet()
const [{ address }] = await wallet.getAccounts()
// Save the mnemonic somewhere to re-use the account
console.log("mnemonic: ", wallet.mnemonic)
console.log("address: ", address)
await useFaucet(address)
}
runExample().then(() => {
console.log("Completed 01_new-wallet-and-faucet.ts")
})
import { Testnet } from "@nibiruchain/nibijs" // nibijs v0.7.2
import { initQueryCmd } from "@nibiruchain/nibijs/dist/query"
async function runExample() {
const { client: query } = await initQueryCmd(Testnet)
const perpParamsResp = await query.perp.params()
console.log("perpParams: %o", perpParamsResp)
// Use your address instead here.
const address = "nibi1fm80fe48g0tp2aztltr6q9g987ejtnllt75qsm"
const balances = await query.bank.allBalances(address)
console.log("balances: %o", balances)
const allPools = await query.vpool.allPools()
console.log("allPools: %o", allPools)
}
runExample().then(async () => {
console.log("Completed example: 02_queries.ts")
})
import { newCoin, newSdk, Testnet } from "@nibiruchain/nibijs" // nibijs v0.7.2
import { Msg, TxMessage } from "@nibiruchain/nibijs/dist/msg"
import { newSignerFromMnemonic } from "@nibiruchain/nibijs/dist/tx"
async function runExample() {
const mnemonic = "..." // fill in the blank
const signer = await newSignerFromMnemonic(mnemonic!)
const sdk = await newSdk(Testnet, signer)
const [{ address: fromAddr }] = await sdk.tx.getAccounts()
const pair = "ubtc:unusd"
const msgs: TxMessage[] = [
Msg.perp.openPosition({
tokenPair: pair,
baseAssetAmountLimit: 0,
leverage: 1,
quoteAssetAmount: 10,
sender: fromAddr,
goLong: true,
}),
Msg.perp.addMargin({
sender: fromAddr,
tokenPair: pair,
margin: newCoin("20", "unusd"),
}),
Msg.perp.removeMargin({
tokenPair: pair,
sender: fromAddr,
margin: newCoin("5", "unusd"),
}),
// final margin value of 10 (open) + 20 (add) - 5 (remove) = 25
]
const txResp = await sdk.tx.signAndBroadcast(...msgs)
console.log("txResp: %o", txResp)
}
runExample().then(async () => {
console.log("Completed example: 03_messages.ts")
})
FAUCET_URL="https://faucet.testnet-1.nibiru.fi/"
ADDR="..." # ← with your address
curl -X POST -d '{"address": "'"$ADDR"'", "coins": ["10000000unibi","100000000000unusd"]}' $FAUCET_URL
@teguhsy
Copy link

teguhsy commented Mar 4, 2024

good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment