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

Allow custom LP subdenom and setting denom metadata #10

Merged
merged 5 commits into from
Jun 20, 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
66 changes: 61 additions & 5 deletions contracts/transmuter/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
ensure, ensure_eq, BankMsg, Coin, Decimal, Deps, DepsMut, Env, MessageInfo, Reply, Response,
StdError, SubMsg, Uint128,
StdError, StdResult, SubMsg, Uint128,
};
use cw_controllers::{Admin, AdminResponse};
use cw_storage_plus::Item;
use osmosis_std::types::osmosis::tokenfactory::v1beta1::{
MsgBurn, MsgCreateDenom, MsgCreateDenomResponse, MsgMint,
use osmosis_std::types::{
cosmos::bank::v1beta1::Metadata,
osmosis::tokenfactory::v1beta1::{
MsgBurn, MsgCreateDenom, MsgCreateDenomResponse, MsgMint, MsgSetDenomMetadata,
},
};
use sylvia::contract;

Expand All @@ -23,6 +27,7 @@ pub struct Transmuter<'a> {
pub(crate) active_status: Item<'a, bool>,
pub(crate) pool: Item<'a, TransmuterPool>,
pub(crate) shares: Shares<'a>,
pub(crate) admin: Admin<'a>,
}

#[contract]
Expand All @@ -32,7 +37,8 @@ impl Transmuter<'_> {
Self {
active_status: Item::new("active_status"),
pool: Item::new("pool"),
shares: Shares::new(),
shares: Shares::new("shares"),
admin: Admin::new("admin"),
}
}

Expand All @@ -42,6 +48,8 @@ impl Transmuter<'_> {
&self,
ctx: (DepsMut, Env, MessageInfo),
pool_asset_denoms: Vec<String>,
lp_subdenom: String,
admin: Option<String>,
) -> Result<Response, ContractError> {
let (deps, env, _info) = ctx;

Expand All @@ -59,11 +67,17 @@ impl Transmuter<'_> {
let msg_create_lp_denom = SubMsg::reply_on_success(
MsgCreateDenom {
sender: env.contract.address.to_string(),
subdenom: "transmuter/poolshare".to_owned(),
subdenom: lp_subdenom,
},
CREATE_LP_DENOM_REPLY_ID,
);

// set admin if provided
if let Some(admin) = admin {
let admin = deps.api.addr_validate(&admin)?;
self.admin.set(deps, Some(admin))?;
}

Ok(Response::new()
.add_attribute("method", "instantiate")
.add_attribute("contract_name", CONTRACT_NAME)
Expand All @@ -87,6 +101,48 @@ impl Transmuter<'_> {
}
}

#[msg(exec)]
pub fn update_admin(
&self,
ctx: (DepsMut, Env, MessageInfo),
admin: Option<String>,
) -> Result<Response, ContractError> {
let (deps, _env, info) = ctx;

let new_admin = admin.map(|a| deps.api.addr_validate(&a)).transpose()?;

self.admin
.execute_update_admin(deps, info, new_admin)
.map_err(Into::into)
}

#[msg(query)]
pub fn admin(&self, ctx: (Deps, Env)) -> StdResult<AdminResponse> {
let (deps, _env) = ctx;
self.admin.query_admin(deps)
}

#[msg(exec)]
pub fn set_lp_denom_metadata(
&self,
ctx: (DepsMut, Env, MessageInfo),
metadata: Metadata,
) -> Result<Response, ContractError> {
let (deps, env, info) = ctx;

// ensure admin
self.admin.assert_admin(deps.as_ref(), &info.sender)?;

let msg_set_denom_metadata = MsgSetDenomMetadata {
sender: env.contract.address.to_string(),
metadata: Some(metadata),
};

Ok(Response::new()
.add_attribute("method", "set_lp_denom_metadata")
.add_message(msg_set_denom_metadata))
}

/// Join pool with tokens that exist in the pool.
/// Token used to join pool is sent to the contract via `funds` in `MsgExecuteContract`.
#[msg(exec)]
Expand Down
4 changes: 4 additions & 0 deletions contracts/transmuter/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use cosmwasm_std::{Coin, Decimal, StdError, Uint128};
use cw_controllers::AdminError;
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),

#[error("{0}")]
Admin(#[from] AdminError),

#[error("Funds must contain exactly one token")]
SingleTokenExpected {},

Expand Down
6 changes: 3 additions & 3 deletions contracts/transmuter/src/shares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ pub struct Shares<'a> {
share_denom: Item<'a, String>,
}

impl Shares<'_> {
pub const fn new() -> Self {
impl<'a> Shares<'a> {
pub const fn new(namespace: &'a str) -> Self {
Self {
share_denom: Item::new("share_denom"),
share_denom: Item::new(namespace),
}
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/transmuter/src/test/cases/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod comprehensive_flows;
mod scenarios;

#[macro_use]
mod units;
Loading