diff --git a/contracts/tokenfactory/src/contract.rs b/contracts/tokenfactory/src/contract.rs index d91ff1f..9a545d5 100644 --- a/contracts/tokenfactory/src/contract.rs +++ b/contracts/tokenfactory/src/contract.rs @@ -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"; @@ -69,10 +69,10 @@ pub fn create_denom(subdenom: String) -> Result, 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") @@ -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") @@ -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"; @@ -264,10 +264,10 @@ mod tests { ) -> OwnedDeps, TokenFactoryQuery> { let custom_querier: MockQuerier = 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("") { @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/packages/bindings-test/src/multitest.rs b/packages/bindings-test/src/multitest.rs index c989860..b094db4 100644 --- a/packages/bindings-test/src/multitest.rs +++ b/packages/bindings-test/src/multitest.rs @@ -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; @@ -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 @@ -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)? @@ -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)? @@ -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)? @@ -187,31 +188,31 @@ impl Module for TokenFactoryModule { request: Self::QueryT, ) -> anyhow::Result { 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!(), } } } @@ -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(); @@ -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 @@ -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()), @@ -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 diff --git a/packages/bindings/src/lib.rs b/packages/bindings/src/lib.rs index 4ce759b..b6cd8d3 100644 --- a/packages/bindings/src/lib.rs +++ b/packages/bindings/src/lib.rs @@ -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}; diff --git a/packages/bindings/src/msg.rs b/packages/bindings/src/msg.rs index 5f7efde..e6ce2d6 100644 --- a/packages/bindings/src/msg.rs +++ b/packages/bindings/src/msg.rs @@ -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./] @@ -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( @@ -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, - } + }) } } diff --git a/packages/bindings/src/querier.rs b/packages/bindings/src/querier.rs index fbbac37..9b93132 100644 --- a/packages/bindings/src/querier.rs +++ b/packages/bindings/src/querier.rs @@ -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> { @@ -17,10 +17,10 @@ impl<'a> TokenQuerier<'a> { creator_addr: String, subdenom: String, ) -> StdResult { - let full_denom_query = TokenFactoryQuery::FullDenom { + let full_denom_query = TokenFactoryQuery::Token(TokenFactoryQueryEnum::FullDenom { creator_addr, subdenom, - }; + }); self.querier.query(&full_denom_query.into()) } } diff --git a/packages/bindings/src/query.rs b/packages/bindings/src/query.rs index d9d5524..2784ed3 100644 --- a/packages/bindings/src/query.rs +++ b/packages/bindings/src/query.rs @@ -5,6 +5,15 @@ use cosmwasm_std::CustomQuery; #[cw_serde] #[derive(QueryResponses)] pub enum TokenFactoryQuery { + #[returns(TokenQueryResponse)] + Token(TokenFactoryQueryEnum), +} + +impl CustomQuery for TokenFactoryQuery {} + +#[cw_serde] +#[derive(QueryResponses)] +pub enum TokenFactoryQueryEnum { /// Given a subdenom created by the address `creator_addr` via `OsmosisMsg::CreateDenom`, /// returns the full denom as used by `BankMsg::Send`. /// You may call `FullDenom { creator_addr: env.contract.address, subdenom }` to find the denom issued @@ -33,7 +42,14 @@ pub enum TokenFactoryQuery { Params {}, } -impl CustomQuery for TokenFactoryQuery {} +#[cw_serde] +pub enum TokenQueryResponse { + FullDenomResponse(FullDenomResponse), + MetadataResponse(MetadataResponse), + AdminResponse(AdminResponse), + DenomsByCreatorResponse(DenomsByCreatorResponse), + ParamsResponse(ParamsResponse), +} #[cw_serde] pub struct FullDenomResponse {