pub mod asset;
pub mod margin;
pub mod sub;
pub use asset::{DexSchema, MarginMode, NewAsset};
pub use margin::{MarginTable, MarginTier};
pub use sub::{PerpKind, SubDeployer};
use std::collections::{BTreeMap, btree_map::Entry};
use crate::deploy::annotation::Annotation;
use crate::deploy::error::BadDeploy;
use crate::deploy::name::DexName;
use crate::utils::sign::{L1Action, Map, Value};
use crate::utils::{Address, Dec};
pub type ByCoin<T> = BTreeMap<String, T>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DeployerFee {
pub scale: Dec,
pub growth_mode: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum PerpDeploy {
RegisterAsset2 {
max_gas: Option<u64>,
asset: NewAsset,
margin_mode: MarginMode,
dex: DexName,
schema: Option<DexSchema>,
},
RegisterAsset {
max_gas: Option<u64>,
asset: NewAsset,
only_isolated: bool,
dex: DexName,
schema: Option<DexSchema>,
},
SetOracle {
dex: DexName,
oracle_pxs: ByCoin<Dec>,
mark_pxs: Vec<ByCoin<Dec>>,
external_perp_pxs: ByCoin<Dec>,
},
SetFundingMultipliers(ByCoin<Dec>),
SetFundingInterestRates(ByCoin<Dec>),
SetFundingClamps(ByCoin<Dec>),
HaltTrading {
coin: String,
is_halted: bool,
},
InsertMarginTable {
dex: DexName,
margin_table: MarginTable,
},
SetMarginTableIds(ByCoin<u32>),
SetFeeRecipient {
dex: DexName,
fee_recipient: Address,
},
SetOpenInterestCaps(ByCoin<Option<u64>>),
SetSubDeployers {
dex: DexName,
sub_deployers: Vec<SubDeployer>,
},
SetMarginModes(ByCoin<MarginMode>),
SetDeployerFees(ByCoin<DeployerFee>),
SetPerpAnnotation {
coin: String,
annotation: Annotation,
},
DisableDex(DexName),
}
fn by_coin<K: Into<String>, T>(pairs: impl IntoIterator<Item = (K, T)>) -> Result<ByCoin<T>, BadDeploy> {
let mut coins = ByCoin::new();
for (coin, value) in pairs {
match coins.entry(coin.into()) {
Entry::Occupied(taken) => return Err(BadDeploy::Twice(taken.key().clone())),
Entry::Vacant(slot) => {
slot.insert(value);
}
}
}
Ok(coins)
}
fn within(
what: &'static str,
value: Dec,
(least, least_text): (Dec, &'static str),
(most, most_text): (Dec, &'static str),
) -> Result<Dec, BadDeploy> {
if value < least || value > most {
return Err(BadDeploy::OutOfRange { what, least: least_text, most: most_text, value: value.to_wire() });
}
Ok(value)
}
fn checked<T: Copy>(coins: &ByCoin<T>, check: impl Fn(T) -> Result<(), BadDeploy>) -> Result<(), BadDeploy> {
coins.values().try_for_each(|value| check(*value))
}
fn not_negative(what: &'static str, value: Dec) -> Result<(), BadDeploy> {
if value.is_negative() {
return Err(BadDeploy::OutOfRange { what, least: "0", most: "any", value: value.to_wire() });
}
Ok(())
}
fn tuples<T>(coins: &ByCoin<T>, value: impl Fn(&T) -> Value) -> Value {
Value::array(coins.iter().map(|(coin, one)| Value::Array(vec![coin.clone().into(), value(one)])))
}
fn decimals(coins: &ByCoin<Dec>) -> Value {
tuples(coins, |value| value.to_wire().into())
}
const ZERO: (Dec, &str) = (Dec::ZERO, "0");
const TEN: (Dec, &str) = (Dec::lit::<0>(10), "10");
const RATE: Dec = Dec::lit::<2>(1);
impl PerpDeploy {
pub fn register_asset2(
dex: DexName,
max_gas: Option<u64>,
asset: NewAsset,
margin_mode: MarginMode,
schema: Option<DexSchema>,
) -> Result<Self, BadDeploy> {
not_negative("oraclePx", asset.oracle_px)?;
Ok(PerpDeploy::RegisterAsset2 { max_gas, asset, margin_mode, dex, schema })
}
pub fn register_asset(
dex: DexName,
max_gas: Option<u64>,
asset: NewAsset,
only_isolated: bool,
schema: Option<DexSchema>,
) -> Result<Self, BadDeploy> {
not_negative("oraclePx", asset.oracle_px)?;
Ok(PerpDeploy::RegisterAsset { max_gas, asset, only_isolated, dex, schema })
}
pub fn set_oracle<K: Into<String>>(
dex: DexName,
oracle_pxs: impl IntoIterator<Item = (K, Dec)>,
mark_pxs: Vec<Vec<(K, Dec)>>,
external_perp_pxs: impl IntoIterator<Item = (K, Dec)>,
) -> Result<Self, BadDeploy> {
if mark_pxs.len() > 2 {
return Err(BadDeploy::MarkPxs(mark_pxs.len()));
}
let oracle_pxs = by_coin(oracle_pxs)?;
let mark_pxs = mark_pxs.into_iter().map(by_coin).collect::<Result<Vec<_>, _>>()?;
let external_perp_pxs = by_coin(external_perp_pxs)?;
checked(&oracle_pxs, |px| not_negative("an oracle price", px))?;
mark_pxs.iter().try_for_each(|marks| checked(marks, |px| not_negative("a mark price", px)))?;
checked(&external_perp_pxs, |px| not_negative("an external perp price", px))?;
Ok(PerpDeploy::SetOracle { dex, oracle_pxs, mark_pxs, external_perp_pxs })
}
pub fn set_funding_multipliers<K: Into<String>>(
multipliers: impl IntoIterator<Item = (K, Dec)>,
) -> Result<Self, BadDeploy> {
let multipliers = by_coin(multipliers)?;
checked(&multipliers, |value| within("a funding multiplier", value, ZERO, TEN).map(drop))?;
Ok(PerpDeploy::SetFundingMultipliers(multipliers))
}
pub fn set_funding_interest_rates<K: Into<String>>(
rates: impl IntoIterator<Item = (K, Dec)>,
) -> Result<Self, BadDeploy> {
let rates = by_coin(rates)?;
checked(&rates, |value| {
within("a funding interest rate", value, (RATE.negate(), "-0.01"), (RATE, "0.01")).map(drop)
})?;
Ok(PerpDeploy::SetFundingInterestRates(rates))
}
pub fn set_funding_clamps<K: Into<String>>(clamps: impl IntoIterator<Item = (K, Dec)>) -> Result<Self, BadDeploy> {
let clamps = by_coin(clamps)?;
checked(&clamps, |value| within("a funding clamp", value, ZERO, (RATE, "0.01")).map(drop))?;
Ok(PerpDeploy::SetFundingClamps(clamps))
}
pub fn set_margin_table_ids<K: Into<String>>(ids: impl IntoIterator<Item = (K, u32)>) -> Result<Self, BadDeploy> {
let ids = by_coin(ids)?;
if let Some((coin, _)) = ids.iter().find(|(_, id)| **id == 0) {
return Err(BadDeploy::MarginTableZero(coin.clone()));
}
Ok(PerpDeploy::SetMarginTableIds(ids))
}
pub fn set_open_interest_caps<K: Into<String>>(
caps: impl IntoIterator<Item = (K, Option<u64>)>,
) -> Result<Self, BadDeploy> {
Ok(PerpDeploy::SetOpenInterestCaps(by_coin(caps)?))
}
pub fn set_sub_deployers(dex: DexName, sub_deployers: Vec<SubDeployer>) -> Result<Self, BadDeploy> {
if sub_deployers.is_empty() {
return Err(BadDeploy::NoGrants);
}
Ok(PerpDeploy::SetSubDeployers { dex, sub_deployers })
}
pub fn set_margin_modes<K: Into<String>>(
modes: impl IntoIterator<Item = (K, MarginMode)>,
) -> Result<Self, BadDeploy> {
Ok(PerpDeploy::SetMarginModes(by_coin(modes)?))
}
pub fn set_deployer_fees<K: Into<String>>(
fees: impl IntoIterator<Item = (K, DeployerFee)>,
) -> Result<Self, BadDeploy> {
let fees = by_coin(fees)?;
checked(&fees, |fee| {
if fee.growth_mode {
if fee.scale >= TEN.0 {
return Err(BadDeploy::OutOfRange {
what: "a growth mode fee scale",
least: "0",
most: "just under 10",
value: fee.scale.to_wire(),
});
}
not_negative("a growth mode fee scale", fee.scale)
} else {
within("a fee scale", fee.scale, ZERO, (Dec::lit::<0>(3), "3")).map(drop)
}
})?;
Ok(PerpDeploy::SetDeployerFees(fees))
}
#[must_use]
pub const fn kind(&self) -> PerpKind {
match self {
PerpDeploy::RegisterAsset2 { .. } => PerpKind::RegisterAsset2,
PerpDeploy::RegisterAsset { .. } => PerpKind::RegisterAsset,
PerpDeploy::SetOracle { .. } => PerpKind::SetOracle,
PerpDeploy::SetFundingMultipliers(_) => PerpKind::SetFundingMultipliers,
PerpDeploy::SetFundingInterestRates(_) => PerpKind::SetFundingInterestRates,
PerpDeploy::SetFundingClamps(_) => PerpKind::SetFundingClamps,
PerpDeploy::HaltTrading { .. } => PerpKind::HaltTrading,
PerpDeploy::InsertMarginTable { .. } => PerpKind::InsertMarginTable,
PerpDeploy::SetMarginTableIds(_) => PerpKind::SetMarginTableIds,
PerpDeploy::SetFeeRecipient { .. } => PerpKind::SetFeeRecipient,
PerpDeploy::SetOpenInterestCaps(_) => PerpKind::SetOpenInterestCaps,
PerpDeploy::SetSubDeployers { .. } => PerpKind::SetSubDeployers,
PerpDeploy::SetMarginModes(_) => PerpKind::SetMarginModes,
PerpDeploy::SetDeployerFees(_) => PerpKind::SetDeployerFees,
PerpDeploy::SetPerpAnnotation { .. } => PerpKind::SetPerpAnnotation,
PerpDeploy::DisableDex(_) => PerpKind::DisableDex,
}
}
#[must_use]
pub fn body(&self) -> Value {
let gas = |max_gas: &Option<u64>| max_gas.map_or(Value::Nil, Value::from);
let schema = |schema: &Option<DexSchema>| schema.as_ref().map_or(Value::Nil, |one| one.wire().into());
match self {
PerpDeploy::RegisterAsset2 { max_gas, asset, margin_mode, dex, schema: new } => Map::new()
.put("maxGas", gas(max_gas))
.put("assetRequest", asset.wire().put("marginMode", margin_mode.wire()))
.put("dex", dex.as_str().to_owned())
.put("schema", schema(new))
.into(),
PerpDeploy::RegisterAsset { max_gas, asset, only_isolated, dex, schema: new } => Map::new()
.put("maxGas", gas(max_gas))
.put("assetRequest", asset.wire().put("onlyIsolated", *only_isolated))
.put("dex", dex.as_str().to_owned())
.put("schema", schema(new))
.into(),
PerpDeploy::SetOracle { dex, oracle_pxs, mark_pxs, external_perp_pxs } => Map::new()
.put("dex", dex.as_str().to_owned())
.put("oraclePxs", decimals(oracle_pxs))
.put("markPxs", Value::array(mark_pxs.iter().map(decimals)))
.put("externalPerpPxs", decimals(external_perp_pxs))
.into(),
PerpDeploy::SetFundingMultipliers(values)
| PerpDeploy::SetFundingInterestRates(values)
| PerpDeploy::SetFundingClamps(values) => decimals(values),
PerpDeploy::HaltTrading { coin, is_halted } => {
Map::new().put("coin", coin.clone()).put("isHalted", *is_halted).into()
}
PerpDeploy::InsertMarginTable { dex, margin_table } => {
Map::new().put("dex", dex.as_str().to_owned()).put("marginTable", margin_table.wire()).into()
}
PerpDeploy::SetMarginTableIds(ids) => tuples(ids, |id| (*id).into()),
PerpDeploy::SetFeeRecipient { dex, fee_recipient } => {
Map::new().put("dex", dex.as_str().to_owned()).put("feeRecipient", fee_recipient.to_hex()).into()
}
PerpDeploy::SetOpenInterestCaps(caps) => tuples(caps, |cap| cap.map_or(Value::Nil, Value::from)),
PerpDeploy::SetSubDeployers { dex, sub_deployers } => Map::new()
.put("dex", dex.as_str().to_owned())
.put("subDeployers", Value::array(sub_deployers.iter().map(SubDeployer::wire)))
.into(),
PerpDeploy::SetMarginModes(modes) => tuples(modes, |mode| mode.wire().into()),
PerpDeploy::SetDeployerFees(fees) => tuples(fees, |fee| {
Map::new().put("scale", fee.scale.to_wire()).put("growthMode", fee.growth_mode).into()
}),
PerpDeploy::SetPerpAnnotation { coin, annotation } => {
annotation.wire_after(Map::new().put("coin", coin.clone())).into()
}
PerpDeploy::DisableDex(dex) => dex.as_str().to_owned().into(),
}
}
}
impl L1Action for PerpDeploy {
fn action(&self) -> Map {
Map::new().put("type", "perpDeploy").put(self.kind().wire(), self.body())
}
}
#[cfg(test)]
#[path = "tests/perp.rs"]
mod tests;