Skip to content

Commit

Permalink
fix: wrong plugin msgs and queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamle2 committed Jul 6, 2024
1 parent 8953004 commit 17c3027
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 69 deletions.
58 changes: 32 additions & 26 deletions contracts/tokenfactory/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cw2::set_contract_version;
use crate::error::TokenFactoryError;
use crate::msg::{ExecuteMsg, GetDenomResponse, InstantiateMsg, QueryMsg};
use crate::state::{State, STATE};
use token_bindings::{TokenFactoryMsg, TokenFactoryQuery, TokenQuerier};
use token_bindings::{TokenFactoryMsg, TokenFactoryMsgOptions, TokenFactoryQuery, TokenQuerier};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:tokenfactory-demo";
Expand Down Expand Up @@ -69,10 +69,10 @@ pub fn create_denom(subdenom: String) -> Result<Response<TokenFactoryMsg>, Token
return Err(TokenFactoryError::InvalidSubdenom { subdenom });
}

let create_denom_msg = TokenFactoryMsg::CreateDenom {
let create_denom_msg = TokenFactoryMsg::Token(TokenFactoryMsgOptions::CreateDenom {
subdenom,
metadata: None,
};
});

let res = Response::new()
.add_attribute("method", "create_denom")
Expand All @@ -90,10 +90,10 @@ pub fn change_admin(

validate_denom(deps, denom.clone())?;

let change_admin_msg = TokenFactoryMsg::ChangeAdmin {
let change_admin_msg = TokenFactoryMsg::Token(TokenFactoryMsgOptions::ChangeAdmin {
denom,
new_admin_address,
};
});

let res = Response::new()
.add_attribute("method", "change_admin")
Expand Down Expand Up @@ -243,7 +243,7 @@ mod tests {
SystemError, SystemResult,
};
use std::marker::PhantomData;
use token_bindings::TokenFactoryQuery;
use token_bindings::{TokenFactoryQuery, TokenFactoryQueryEnum};
use token_bindings_test::TokenFactoryApp;

const DENOM_NAME: &str = "mydenom";
Expand All @@ -264,10 +264,10 @@ mod tests {
) -> OwnedDeps<MockStorage, MockApi, MockQuerier<TokenFactoryQuery>, TokenFactoryQuery> {
let custom_querier: MockQuerier<TokenFactoryQuery> =
MockQuerier::new(&[(MOCK_CONTRACT_ADDR, &[])]).with_custom_handler(|a| match a {
TokenFactoryQuery::FullDenom {
TokenFactoryQuery::Token(TokenFactoryQueryEnum::FullDenom {
creator_addr,
subdenom,
} => {
}) => {
let binary_request = to_binary(a).unwrap();

if creator_addr.eq("") {
Expand Down Expand Up @@ -333,10 +333,12 @@ mod tests {

assert_eq!(1, res.messages.len());

let expected_message = CosmosMsg::from(TokenFactoryMsg::CreateDenom {
subdenom: String::from(DENOM_NAME),
metadata: None,
});
let expected_message = CosmosMsg::from(TokenFactoryMsg::Token(
TokenFactoryMsgOptions::CreateDenom {
subdenom: String::from(DENOM_NAME),
metadata: None,
},
));
let actual_message = res.messages.get(0).unwrap();
assert_eq!(expected_message, actual_message.msg);

Expand Down Expand Up @@ -385,10 +387,12 @@ mod tests {

assert_eq!(1, res.messages.len());

let expected_message = CosmosMsg::from(TokenFactoryMsg::ChangeAdmin {
denom: String::from(full_denom_name),
new_admin_address: String::from(NEW_ADMIN_ADDR),
});
let expected_message = CosmosMsg::from(TokenFactoryMsg::Token(
TokenFactoryMsgOptions::ChangeAdmin {
denom: String::from(full_denom_name),
new_admin_address: String::from(NEW_ADMIN_ADDR),
},
));
let actual_message = res.messages.get(0).unwrap();
assert_eq!(expected_message, actual_message.msg);

Expand Down Expand Up @@ -483,11 +487,12 @@ mod tests {

assert_eq!(1, res.messages.len());

let expected_message = CosmosMsg::from(TokenFactoryMsg::MintTokens {
denom: String::from(full_denom_name),
amount: mint_amount,
mint_to_address: String::from(NEW_ADMIN_ADDR),
});
let expected_message =
CosmosMsg::from(TokenFactoryMsg::Token(TokenFactoryMsgOptions::MintTokens {
denom: String::from(full_denom_name),
amount: mint_amount,
mint_to_address: String::from(NEW_ADMIN_ADDR),
}));
let actual_message = res.messages.get(0).unwrap();
assert_eq!(expected_message, actual_message.msg);

Expand Down Expand Up @@ -543,11 +548,12 @@ mod tests {
let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();

assert_eq!(1, res.messages.len());
let expected_message = CosmosMsg::from(TokenFactoryMsg::BurnTokens {
denom: String::from(full_denom_name),
amount: mint_amount,
burn_from_address: String::from(""),
});
let expected_message =
CosmosMsg::from(TokenFactoryMsg::Token(TokenFactoryMsgOptions::BurnTokens {
denom: String::from(full_denom_name),
amount: mint_amount,
burn_from_address: String::from(""),
}));
let actual_message = res.messages.get(0).unwrap();
assert_eq!(expected_message, actual_message.msg);

Expand Down
61 changes: 31 additions & 30 deletions packages/bindings-test/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use cw_storage_plus::Map;

use token_bindings::{
AdminResponse, CreateDenomResponse, DenomsByCreatorResponse, FullDenomResponse, Metadata,
MetadataResponse, TokenFactoryMsg, TokenFactoryQuery,
MetadataResponse, TokenFactoryMsg, TokenFactoryMsgOptions, TokenFactoryQuery,
TokenFactoryQueryEnum,
};

use crate::error::ContractError;
Expand Down Expand Up @@ -76,7 +77,7 @@ impl Module for TokenFactoryModule {
QueryC: CustomQuery + DeserializeOwned + 'static,
{
match msg {
TokenFactoryMsg::CreateDenom { subdenom, metadata } => {
TokenFactoryMsg::Token(TokenFactoryMsgOptions::CreateDenom { subdenom, metadata }) => {
let new_token_denom = self.build_denom(&sender, &subdenom)?;

// errors if the denom was already created
Expand All @@ -102,11 +103,11 @@ impl Module for TokenFactoryModule {
events: vec![],
})
}
TokenFactoryMsg::MintTokens {
TokenFactoryMsg::Token(TokenFactoryMsgOptions::MintTokens {
denom,
amount,
mint_to_address,
} => {
}) => {
// ensure we are admin of this denom (and it exists)
let admin = ADMIN
.may_load(storage, &denom)?
Expand All @@ -121,21 +122,21 @@ impl Module for TokenFactoryModule {
router.sudo(api, storage, block, mint.into())?;
Ok(AppResponse::default())
}
TokenFactoryMsg::BurnTokens {
denom: _,
amount: _,
burn_from_address: _,
} => todo!(),
TokenFactoryMsg::ForceTransfer {
denom: _,
amount: _,
from_address: _,
to_address: _,
} => todo!(),
TokenFactoryMsg::ChangeAdmin {
TokenFactoryMsg::Token(TokenFactoryMsgOptions::BurnTokens {
denom,
amount,
burn_from_address,
}) => todo!(),
TokenFactoryMsg::Token(TokenFactoryMsgOptions::ForceTransfer {
denom,
amount,
from_address,
to_address,
}) => todo!(),
TokenFactoryMsg::Token(TokenFactoryMsgOptions::ChangeAdmin {
denom,
new_admin_address,
} => {
}) => {
// ensure we are admin of this denom (and it exists)
let admin = ADMIN
.may_load(storage, &denom)?
Expand All @@ -148,7 +149,7 @@ impl Module for TokenFactoryModule {
ADMIN.save(storage, &denom, &new_admin)?;
Ok(AppResponse::default())
}
TokenFactoryMsg::SetMetadata { denom, metadata } => {
TokenFactoryMsg::Token(TokenFactoryMsgOptions::SetMetadata { denom, metadata }) => {
// ensure we are admin of this denom (and it exists)
let admin = ADMIN
.may_load(storage, &denom)?
Expand Down Expand Up @@ -187,31 +188,31 @@ impl Module for TokenFactoryModule {
request: Self::QueryT,
) -> anyhow::Result<Binary> {
match request {
TokenFactoryQuery::FullDenom {
TokenFactoryQuery::Token(TokenFactoryQueryEnum::FullDenom {
creator_addr,
subdenom,
} => {
}) => {
let contract = api.addr_validate(&creator_addr)?;
let denom = self.build_denom(&contract, &subdenom)?;
let res = FullDenomResponse { denom };
Ok(to_binary(&res)?)
}
TokenFactoryQuery::Metadata { denom } => {
TokenFactoryQuery::Token(TokenFactoryQueryEnum::Metadata { denom }) => {
let metadata = METADATA.may_load(storage, &denom)?;
Ok(to_binary(&MetadataResponse { metadata })?)
}
TokenFactoryQuery::Admin { denom } => {
TokenFactoryQuery::Token(TokenFactoryQueryEnum::Admin { denom }) => {
let admin = ADMIN.load(storage, &denom)?.to_string();
Ok(to_binary(&AdminResponse { admin })?)
}
TokenFactoryQuery::DenomsByCreator { creator } => {
TokenFactoryQuery::Token(TokenFactoryQueryEnum::DenomsByCreator { creator }) => {
let creator = api.addr_validate(&creator)?;
let denoms = DENOMS_BY_CREATOR
.may_load(storage, &creator)?
.unwrap_or_default();
Ok(to_binary(&DenomsByCreatorResponse { denoms })?)
}
TokenFactoryQuery::Params {} => todo!(),
TokenFactoryQuery::Token(TokenFactoryQueryEnum::Params {}) => todo!(),
}
}
}
Expand Down Expand Up @@ -324,10 +325,10 @@ mod tests {
let FullDenomResponse { denom } = app
.wrap()
.query(
&TokenFactoryQuery::FullDenom {
&TokenFactoryQuery::Token(TokenFactoryQueryEnum::FullDenom {
creator_addr: contract.to_string(),
subdenom: subdenom.to_string(),
}
})
.into(),
)
.unwrap();
Expand All @@ -336,11 +337,11 @@ mod tests {

// prepare to mint
let amount = Uint128::new(1234567);
let msg = TokenFactoryMsg::MintTokens {
let msg = TokenFactoryMsg::Token(TokenFactoryMsgOptions::MintTokens {
denom: denom.to_string(),
amount,
mint_to_address: rcpt.to_string(),
};
});

// fails to mint token before creating it
let err = app
Expand All @@ -352,7 +353,7 @@ mod tests {
);

// create the token now
let create = TokenFactoryMsg::CreateDenom {
let create = TokenFactoryMsg::Token(TokenFactoryMsgOptions::CreateDenom {
subdenom: subdenom.to_string(),
metadata: Some(Metadata {
description: Some("Awesome token, get it now!".to_string()),
Expand All @@ -362,7 +363,7 @@ mod tests {
name: Some("Fundz pays".to_string()),
symbol: Some("FUNDZ".to_string()),
}),
};
});
app.execute(contract.clone(), create.into()).unwrap();

// now we can mint
Expand Down
4 changes: 2 additions & 2 deletions packages/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ mod querier;
mod query;
mod types;

pub use msg::{CreateDenomResponse, TokenFactoryMsg};
pub use msg::{CreateDenomResponse, TokenFactoryMsg, TokenFactoryMsgOptions};
pub use querier::TokenQuerier;
pub use query::{
AdminResponse, DenomsByCreatorResponse, FullDenomResponse, MetadataResponse, ParamsResponse,
TokenFactoryQuery,
TokenFactoryQuery, TokenFactoryQueryEnum,
};
pub use types::{DenomUnit, Metadata, Params};
19 changes: 12 additions & 7 deletions packages/bindings/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ use crate::types::Metadata;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Binary, CosmosMsg, CustomMsg, StdResult, Uint128};

/// Special messages to be supported by any chain that supports token_factory
#[cw_serde]
pub enum TokenFactoryMsg {
Token(TokenFactoryMsgOptions),
}

/// Special messages to be supported by any chain that supports token_factory
#[cw_serde]
pub enum TokenFactoryMsgOptions {
/// CreateDenom creates a new factory denom, of denomination:
/// factory/{creating contract bech32 address}/{Subdenom}
/// Subdenom can be of length at most 44 characters, in [0-9a-zA-Z./]
Expand Down Expand Up @@ -57,19 +62,19 @@ pub enum TokenFactoryMsg {

impl TokenFactoryMsg {
pub fn mint_contract_tokens(denom: String, amount: Uint128, mint_to_address: String) -> Self {
TokenFactoryMsg::MintTokens {
TokenFactoryMsg::Token(TokenFactoryMsgOptions::MintTokens {
denom,
amount,
mint_to_address,
}
})
}

pub fn burn_contract_tokens(denom: String, amount: Uint128, burn_from_address: String) -> Self {
TokenFactoryMsg::BurnTokens {
TokenFactoryMsg::Token(TokenFactoryMsgOptions::BurnTokens {
denom,
amount,
burn_from_address,
}
})
}

pub fn force_transfer_tokens(
Expand All @@ -78,12 +83,12 @@ impl TokenFactoryMsg {
from_address: String,
to_address: String,
) -> Self {
TokenFactoryMsg::ForceTransfer {
TokenFactoryMsg::Token(TokenFactoryMsgOptions::ForceTransfer {
denom,
amount,
from_address,
to_address,
}
})
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/bindings/src/querier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{QuerierWrapper, StdResult};

use crate::query::{FullDenomResponse, TokenFactoryQuery};
use crate::query::{FullDenomResponse, TokenFactoryQuery, TokenFactoryQueryEnum};

/// This is a helper wrapper to easily use our custom queries
pub struct TokenQuerier<'a> {
Expand All @@ -17,10 +17,10 @@ impl<'a> TokenQuerier<'a> {
creator_addr: String,
subdenom: String,
) -> StdResult<FullDenomResponse> {
let full_denom_query = TokenFactoryQuery::FullDenom {
let full_denom_query = TokenFactoryQuery::Token(TokenFactoryQueryEnum::FullDenom {
creator_addr,
subdenom,
};
});
self.querier.query(&full_denom_query.into())
}
}
Loading

0 comments on commit 17c3027

Please sign in to comment.