diff --git a/Cargo.lock b/Cargo.lock index c4300de..dcf16af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1157,6 +1157,7 @@ version = "0.2.0" dependencies = [ "cosmwasm-schema", "cosmwasm-std", + "nibiru-macro", "prost 0.12.1", "prost-types", ] diff --git a/nibiru-std/Cargo.toml b/nibiru-std/Cargo.toml index 52c4e94..40a9ba7 100644 --- a/nibiru-std/Cargo.toml +++ b/nibiru-std/Cargo.toml @@ -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 " -] +authors = ["Unique Divine "] [lib] crate-type = ["cdylib", "rlib"] @@ -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" \ No newline at end of file +# path = "bin/script_name.rs" diff --git a/nibiru-std/src/bindings/mod.rs b/nibiru-std/src/bindings/mod.rs index 1eafa62..b8565c0 100644 --- a/nibiru-std/src/bindings/mod.rs +++ b/nibiru-std/src/bindings/mod.rs @@ -1,4 +1,4 @@ +pub mod msg; pub mod querier; pub mod query; -pub mod route; -pub mod state; \ No newline at end of file +pub mod state; diff --git a/nibiru-std/src/bindings/msg.rs b/nibiru-std/src/bindings/msg.rs new file mode 100644 index 0000000..bcaccfe --- /dev/null +++ b/nibiru-std/src/bindings/msg.rs @@ -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] +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] +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] +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, + }, + + DonateToInsuranceFund { + donation: Coin, + }, + + NoOp {}, +} + +#[cw_serde] +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 { + route: NibiruRoute::Perp, + msg: NibiruMsg::MarketOrder { + pair, + is_long, + quote_amount, + leverage, + base_amount_limit, + }, + } + .into() + } + + pub fn close_position(pair: String) -> CosmosMsg { + NibiruMsgWrapper { + route: NibiruRoute::Perp, + msg: NibiruMsg::ClosePosition { pair }, + } + .into() + } + + pub fn add_margin( + pair: String, + margin: Coin, + ) -> CosmosMsg { + NibiruMsgWrapper { + route: NibiruRoute::Perp, + msg: NibiruMsg::AddMargin { pair, margin }, + } + .into() + } + + pub fn remove_margin( + pair: String, + margin: Coin, + ) -> CosmosMsg { + NibiruMsgWrapper { + route: NibiruRoute::Perp, + msg: NibiruMsg::RemoveMargin { pair, margin }, + } + .into() + } + + pub fn multi_liquidate( + pair: String, + liquidations: Vec, + ) -> CosmosMsg { + NibiruMsgWrapper { + route: NibiruRoute::Perp, + msg: NibiruMsg::MultiLiquidate { pair, liquidations }, + } + .into() + } + + pub fn donate_to_insurance_fund( + donation: Coin, + ) -> CosmosMsg { + NibiruMsgWrapper { + route: NibiruRoute::Perp, + msg: NibiruMsg::DonateToInsuranceFund { donation }, + } + .into() + } + + pub fn no_op() -> CosmosMsg { + NibiruMsgWrapper { + route: NibiruRoute::NoOp, + msg: NibiruMsg::NoOp {}, + } + .into() + } +} diff --git a/nibiru-std/src/bindings/route.rs b/nibiru-std/src/bindings/route.rs deleted file mode 100644 index c6735b8..0000000 --- a/nibiru-std/src/bindings/route.rs +++ /dev/null @@ -1,16 +0,0 @@ -use cosmwasm_schema::cw_serde; - -/// Routes here refer to groups of operations that will be interpreted in -/// the x/wasm/binding packa . The idea here is to add -/// information on which module or group of modules a particular execute message -/// belongs to. -#[cw_serde] -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, -}