Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

epic(nibiru-std): proto module with tests + scripts #72

Merged
merged 13 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[workspace]
members = ["packages/*", "contracts/*"]
members = ["packages/*", "contracts/*", "nibiru-std"]
exclude = ["scripts"]
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ Smart contract sandbox for Nibiru Chain.
```

<!-- 🚧 Work in progress 🚧 -->

## Hacking

Install `just` to run project-specific commands.
```bash
cargo install just
```

You can view the list of available development commands with `just -ls`.

Ref: [github.com/casey/just](https://github.com/casey/just)
66 changes: 66 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
workspaces := "./packages ./contracts "

# Displays available recipes by running `just -l`.
setup:
#!/usr/bin/env bash
just -l

install:
rustup component add clippy

wasm-all:
bash scripts/wasm-out.sh

# Move binding artifacts to teh local nibiru wasmbin
wasm-export:
bash scripts/wasm-export.sh

# Compiles a single CW contract to wasm bytecode.
# wasm-single:
# bash scripts/wasm-out.sh --single

# Runs rustfmt
fmt:
cargo fmt --all

# Runs rustfmt without updating
fmt-check:
cargo fmt --all -- --check

# Compiles Rust code
build:
cargo build

build-update:
cargo update
cargo build

# Clean target files and temp files
clean:
cargo clean

# Run linter + fix
clippy:
cargo clippy --fix --allow-dirty --allow-staged

# Run linter + check only
clippy-check:
cargo clippy

# Test a specific package or contract
test *pkg:
#!/usr/bin/env bash
set -e;
RUST_BACKGTRACE="1" cargo test --package "{{pkg}}"

# Test everything in the workspace.
test-all:
cargo test

# Format, lint, and test
tidy: fmt clippy test
just

# Format, lint, update dependencies, and test
tidy-update: build-update
just tidy
27 changes: 27 additions & 0 deletions nibiru-std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "nibiru-std"
version = "0.2.0"
edition = "2021"
description = "Standard library for Nibiru Chain CosmWasm smart contracts"
authors = [
"Unique Divine <[email protected]>"
]

[lib]
crate-type = ["cdylib", "rlib"]

[features]
backtraces = ["cosmwasm-std/backtraces"]
library = []

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cosmwasm-std = "1.2.3"
cosmwasm-schema = "1.2.3"
prost = "0.12"
prost-types = "0.12"

# cargo run --bin script-name
# [[bin]]
# name = "rust script name"
# path = "bin/script_name.rs"
4 changes: 4 additions & 0 deletions nibiru-std/src/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod querier;
pub mod query;
pub mod route;
pub mod state;
104 changes: 104 additions & 0 deletions nibiru-std/src/bindings/querier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use cosmwasm_std::{QuerierWrapper, StdResult, Uint256};

use crate::bindings::query::{
AllMarketsResponse, BasePriceResponse, MetricsResponse,
ModuleAccountsResponse, ModuleParamsResponse, OraclePricesResponse,
PositionResponse, PositionsResponse, PremiumFractionResponse, QueryPerpMsg,
ReservesResponse,
};
use std::collections::HashMap;

/// NibiruQuerier makes it easy to export the functions that correspond to each
/// request without needing to know as much about the underlying types.
pub struct NibiruQuerier<'a> {
querier: &'a QuerierWrapper<'a, QueryPerpMsg>,
}

impl<'a> NibiruQuerier<'a> {
pub fn new(querier: &'a QuerierWrapper<QueryPerpMsg>) -> Self {
NibiruQuerier { querier }
}

pub fn all_markets(&self) -> StdResult<AllMarketsResponse> {
let request = QueryPerpMsg::AllMarkets {};
self.querier.query(&request.into())
}

pub fn base_price(
&self,
pair: String,
is_long: bool,
base_amount: Uint256,
) -> StdResult<BasePriceResponse> {
let request = QueryPerpMsg::BasePrice {
pair,
is_long,
base_amount,
};
self.querier.query(&request.into())
}

pub fn position(
&self,
trader: String,
pair: String,
) -> StdResult<PositionResponse> {
let request = QueryPerpMsg::Position { trader, pair };
self.querier.query(&request.into())
}

pub fn positions(&self, trader: String) -> StdResult<PositionsResponse> {
let request = QueryPerpMsg::Positions { trader };
self.querier.query(&request.into())
}

pub fn reserves(&self, pair: String) -> StdResult<ReservesResponse> {
let request = QueryPerpMsg::Reserves { pair };
self.querier.query(&request.into())
}

pub fn oracle_prices(
&self,
pairs: Option<Vec<String>>,
) -> StdResult<OraclePricesResponse> {
let request = QueryPerpMsg::OraclePrices {};
let price_map: OraclePricesResponse =
self.querier.query(&request.into())?;

match pairs {
Some(pair_vec) if !pair_vec.is_empty() => {
let mut out_price_map: OraclePricesResponse = HashMap::new();
for p in pair_vec.iter() {
if let Some(rate) = price_map.get(p) {
out_price_map.insert(p.clone(), *rate);
}
}
Ok(out_price_map)
}
_ => Ok(price_map),
}
}

pub fn premium_fraction(
&self,
pair: String,
) -> StdResult<PremiumFractionResponse> {
let request = QueryPerpMsg::PremiumFraction { pair };
self.querier.query(&request.into())
}

pub fn metrics(&self, pair: String) -> StdResult<MetricsResponse> {
let request = QueryPerpMsg::Metrics { pair };
self.querier.query(&request.into())
}

pub fn module_params(&self) -> StdResult<ModuleParamsResponse> {
let request = QueryPerpMsg::ModuleParams {};
self.querier.query(&request.into())
}

pub fn module_accounts(&self) -> StdResult<ModuleAccountsResponse> {
let request = QueryPerpMsg::ModuleAccounts {};
self.querier.query(&request.into())
}
}
Loading