Skip to content

Commit

Permalink
feat(bindings): add canonical perp bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Oct 9, 2023
1 parent 5ef46ff commit 65e7193
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

7 changes: 3 additions & 4 deletions nibiru-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ name = "nibiru-std"
version = "0.2.0"
edition = "2021"
description = "Standard library for Nibiru Chain CosmWasm smart contracts"
authors = [
"Unique Divine <[email protected]>"
]
authors = ["Unique Divine <[email protected]>"]

[lib]
crate-type = ["cdylib", "rlib"]
Expand All @@ -20,8 +18,9 @@ cosmwasm-std = "1.2.3"
cosmwasm-schema = "1.2.3"
prost = "0.12"
prost-types = "0.12"
nibiru-macro = { path = "../packages/macro" }

# cargo run --bin script-name
# [[bin]]
# name = "rust script name"
# path = "bin/script_name.rs"
# path = "bin/script_name.rs"
4 changes: 2 additions & 2 deletions nibiru-std/src/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod msg;
pub mod querier;
pub mod query;
pub mod route;
pub mod state;
pub mod state;
152 changes: 152 additions & 0 deletions nibiru-std/src/bindings/msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Coin, CosmosMsg, CustomMsg, Decimal, Uint128};
use nibiru_macro::cw_custom;

/// NibiruMsg is an override of CosmosMsg::Custom. Using this msg
/// wrapper for the NibiruMsg handlers show that their return values are valid
/// instances of CosmosMsg::Custom in a type-safe manner. It also shows how
/// NibiruMsg can be extended in the contract.
#[cw_serde]
#[cw_custom]

Check warning on line 10 in nibiru-std/src/bindings/msg.rs

View check run for this annotation

Codecov / codecov/patch

nibiru-std/src/bindings/msg.rs#L9-L10

Added lines #L9 - L10 were not covered by tests
pub struct NibiruMsgWrapper {
pub route: NibiruRoute,
pub msg: NibiruMsg,
}

/// Routes here refer to groups of operations that will be interpreted in
/// the x/wasmbinding package. The idea here is to add
/// information on which module or group of modules a particular execute message
/// belongs to.
#[cw_serde]

Check warning on line 20 in nibiru-std/src/bindings/msg.rs

View check run for this annotation

Codecov / codecov/patch

nibiru-std/src/bindings/msg.rs#L20

Added line #L20 was not covered by tests
pub enum NibiruRoute {
/// "perp" is the route corresponding to bindings for the x/perp module.
Perp,
Oracle,

/// "no_op" is a valid route that doesn't do anything. It's necessary for
/// formatting in the custom Wasm execute handler.
NoOp,
}

#[cw_serde]

Check warning on line 31 in nibiru-std/src/bindings/msg.rs

View check run for this annotation

Codecov / codecov/patch

nibiru-std/src/bindings/msg.rs#L31

Added line #L31 was not covered by tests
pub enum NibiruMsg {
MarketOrder {
pair: String,
is_long: bool,
quote_amount: Uint128,
leverage: Decimal,
base_amount_limit: Uint128,
},

ClosePosition {
pair: String,
},

AddMargin {
pair: String,
margin: Coin,
},

RemoveMargin {
pair: String,
margin: Coin,
},

MultiLiquidate {
pair: String,
liquidations: Vec<LiquidationArgs>,
},

DonateToInsuranceFund {
donation: Coin,
},

NoOp {},
}

#[cw_serde]

Check warning on line 67 in nibiru-std/src/bindings/msg.rs

View check run for this annotation

Codecov / codecov/patch

nibiru-std/src/bindings/msg.rs#L67

Added line #L67 was not covered by tests
pub struct LiquidationArgs {
pub pair: String,
pub trader: String,
}

impl NibiruMsgWrapper {
pub fn market_order(
pair: String,
is_long: bool,
quote_amount: Uint128,
leverage: Decimal,
base_amount_limit: Uint128,
) -> CosmosMsg<NibiruMsgWrapper> {
NibiruMsgWrapper {
route: NibiruRoute::Perp,
msg: NibiruMsg::MarketOrder {
pair,
is_long,
quote_amount,
leverage,
base_amount_limit,
},
}
.into()
}

Check warning on line 92 in nibiru-std/src/bindings/msg.rs

View check run for this annotation

Codecov / codecov/patch

nibiru-std/src/bindings/msg.rs#L74-L92

Added lines #L74 - L92 were not covered by tests

pub fn close_position(pair: String) -> CosmosMsg<NibiruMsgWrapper> {
NibiruMsgWrapper {
route: NibiruRoute::Perp,
msg: NibiruMsg::ClosePosition { pair },
}
.into()
}

Check warning on line 100 in nibiru-std/src/bindings/msg.rs

View check run for this annotation

Codecov / codecov/patch

nibiru-std/src/bindings/msg.rs#L94-L100

Added lines #L94 - L100 were not covered by tests

pub fn add_margin(
pair: String,
margin: Coin,
) -> CosmosMsg<NibiruMsgWrapper> {
NibiruMsgWrapper {
route: NibiruRoute::Perp,
msg: NibiruMsg::AddMargin { pair, margin },
}
.into()
}

Check warning on line 111 in nibiru-std/src/bindings/msg.rs

View check run for this annotation

Codecov / codecov/patch

nibiru-std/src/bindings/msg.rs#L102-L111

Added lines #L102 - L111 were not covered by tests

pub fn remove_margin(
pair: String,
margin: Coin,
) -> CosmosMsg<NibiruMsgWrapper> {
NibiruMsgWrapper {
route: NibiruRoute::Perp,
msg: NibiruMsg::RemoveMargin { pair, margin },
}
.into()
}

Check warning on line 122 in nibiru-std/src/bindings/msg.rs

View check run for this annotation

Codecov / codecov/patch

nibiru-std/src/bindings/msg.rs#L113-L122

Added lines #L113 - L122 were not covered by tests

pub fn multi_liquidate(
pair: String,
liquidations: Vec<LiquidationArgs>,
) -> CosmosMsg<NibiruMsgWrapper> {
NibiruMsgWrapper {
route: NibiruRoute::Perp,
msg: NibiruMsg::MultiLiquidate { pair, liquidations },
}
.into()
}

Check warning on line 133 in nibiru-std/src/bindings/msg.rs

View check run for this annotation

Codecov / codecov/patch

nibiru-std/src/bindings/msg.rs#L124-L133

Added lines #L124 - L133 were not covered by tests

pub fn donate_to_insurance_fund(
donation: Coin,
) -> CosmosMsg<NibiruMsgWrapper> {
NibiruMsgWrapper {
route: NibiruRoute::Perp,
msg: NibiruMsg::DonateToInsuranceFund { donation },
}
.into()
}

Check warning on line 143 in nibiru-std/src/bindings/msg.rs

View check run for this annotation

Codecov / codecov/patch

nibiru-std/src/bindings/msg.rs#L135-L143

Added lines #L135 - L143 were not covered by tests

pub fn no_op() -> CosmosMsg<NibiruMsgWrapper> {
NibiruMsgWrapper {
route: NibiruRoute::NoOp,
msg: NibiruMsg::NoOp {},
}
.into()
}

Check warning on line 151 in nibiru-std/src/bindings/msg.rs

View check run for this annotation

Codecov / codecov/patch

nibiru-std/src/bindings/msg.rs#L145-L151

Added lines #L145 - L151 were not covered by tests
}
16 changes: 0 additions & 16 deletions nibiru-std/src/bindings/route.rs

This file was deleted.

0 comments on commit 65e7193

Please sign in to comment.