Skip to content

Commit

Permalink
feat(cosmwasm): introduce token factory api library for contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-aitlahcen committed Sep 1, 2023
1 parent c5b6a81 commit 6d4d0a0
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ protos = { path = "generated/rust", default-features = false }
unionlabs = { path = "lib/unionlabs", default-features = false }
beacon-api = { path = "lib/beacon-api", default-features = false }

token-factory-api = { path = "cosmwasm/token-factory-api", default-features = false }

[patch.crates-io]
typenum = { git = "https://github.com/unionlabs/typenum", branch = "ben/192-const-generic-32-bit" }
8 changes: 8 additions & 0 deletions cosmwasm/token-factory-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "token-factory-api"
version = "0.1.0"
edition = "2021"

[dependencies]
cosmwasm-schema = { version = "1.3" }
cosmwasm-std = { version = "1.3" }
178 changes: 178 additions & 0 deletions cosmwasm/token-factory-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Coin, CosmosMsg, CustomMsg, CustomQuery, QueryRequest, Uint128};

/// A top-level Custom message for the token factory.
/// It is embedded like this to easily allow adding other variants that are custom
/// to your chain, or other "standardized" extensions along side it.
#[cw_serde]
pub enum TokenFactoryMsg {
Token(TokenMsg),
}

/// Special messages to be supported by any chain that supports token_factory
#[cw_serde]
pub enum TokenMsg {
/// 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./]
/// Empty subdenoms are valid.
/// The (creating contract address, subdenom) pair must be unique.
/// The created denom's admin is the creating contract address,
/// but this admin can be changed using the UpdateAdmin binding.
///
/// If you set an initial metadata here, this is equivalent
/// to calling SetMetadata directly on the returned denom.
CreateDenom {
subdenom: String,
metadata: Option<Metadata>,
},
/// ChangeAdmin changes the admin for a factory denom.
/// Can only be called by the current contract admin.
/// If the NewAdminAddress is empty, the denom will have no admin.
ChangeAdmin {
denom: String,
new_admin_address: String,
},
/// Contracts can mint native tokens for an existing factory denom
/// that they are the admin of.
MintTokens {
denom: String,
amount: Uint128,
mint_to_address: String,
},
/// Contracts can burn native tokens for an existing factory denom
/// that they are the admin of.
/// Currently, the burn from address must be the admin contract.
BurnTokens {
denom: String,
amount: Uint128,
burn_from_address: String,
},
SetMetadata {
denom: String,
metadata: Metadata,
},
}

/// This maps to cosmos.bank.v1beta1.Metadata protobuf struct
#[cw_serde]
pub struct Metadata {
pub description: Option<String>,
/// denom_units represents the list of DenomUnit's for a given coin
pub denom_units: Vec<DenomUnit>,
/// base represents the base denom (should be the DenomUnit with exponent = 0).
pub base: Option<String>,
/// display indicates the suggested denom that should be displayed in clients.
pub display: Option<String>,
/// name defines the name of the token (eg: Cosmos Atom)
pub name: Option<String>,
/// symbol is the token symbol usually shown on exchanges (eg: ATOM). This can
/// be the same as the display.
pub symbol: Option<String>,
}

/// This maps to cosmos.bank.v1beta1.DenomUnit protobuf struct
#[cw_serde]
pub struct DenomUnit {
/// denom represents the string name of the given denom unit (e.g uatom).
pub denom: String,
/// exponent represents power of 10 exponent that one must
/// raise the base_denom to in order to equal the given DenomUnit's denom
/// 1 denom = 1^exponent base_denom
/// (e.g. with a base_denom of uatom, one can create a DenomUnit of 'atom' with
/// exponent = 6, thus: 1 atom = 10^6 uatom).
exponent: u32,
/// aliases is a list of string aliases for the given denom
aliases: Vec<String>,
}

/// This maps to tokenfactory.v1beta1.Params protobuf struct
#[cw_serde]
pub struct Params {
pub denom_creation_fee: Vec<Coin>,
}

impl From<TokenMsg> for CosmosMsg<TokenFactoryMsg> {
fn from(msg: TokenMsg) -> CosmosMsg<TokenFactoryMsg> {
CosmosMsg::Custom(TokenFactoryMsg::Token(msg))
}
}

impl CustomMsg for TokenFactoryMsg {}

/// This is in the data field in the reply from a TokenFactoryMsg::CreateDenom SubMsg
/// Custom code to parse from protobuf with minimal wasm bytecode bloat
pub struct CreateDenomResponse {
pub new_token_denom: String,
}

#[cw_serde]
pub enum TokenFactoryQuery {
// Note: embded enums don't work with QueryResponses currently
Token(TokenQuery),
}

#[cw_serde]
#[derive(QueryResponses)]
pub enum TokenQuery {
/// Given a subdenom created by the address `creator_addr` via `TokenFactoryMsg::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
/// by the current contract.
#[returns(FullDenomResponse)]
FullDenom {
creator_addr: String,
subdenom: String,
},
/// Returns the metadata set for this denom, if present. May return None.
/// This will also return metadata for native tokens created outside
/// of the token factory (like staking tokens)
#[returns(MetadataResponse)]
Metadata { denom: String },
/// Returns info on admin of the denom, only if created/managed via token factory.
/// Errors if denom doesn't exist or was created by another module.
#[returns(AdminResponse)]
Admin { denom: String },
/// List all denoms that were created by the given creator.
/// This does not imply all tokens currently managed by the creator.
/// (Admin may have changed)
#[returns(DenomsByCreatorResponse)]
DenomsByCreator { creator: String },
/// Returns configuration params for TokenFactory modules
#[returns(ParamsResponse)]
Params {},
}

impl CustomQuery for TokenFactoryQuery {}

impl From<TokenQuery> for QueryRequest<TokenFactoryQuery> {
fn from(query: TokenQuery) -> Self {
QueryRequest::Custom(TokenFactoryQuery::Token(query))
}
}

#[cw_serde]
pub struct FullDenomResponse {
pub denom: String,
}

#[cw_serde]
pub struct MetadataResponse {
/// Empty if this was never set for the given denom
pub metadata: Option<Metadata>,
}

#[cw_serde]
pub struct AdminResponse {
pub admin: String,
}

#[cw_serde]
pub struct DenomsByCreatorResponse {
pub denoms: Vec<String>,
}

#[cw_serde]
pub struct ParamsResponse {
pub params: Params,
}
1 change: 1 addition & 0 deletions cosmwasm/ucs01-relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ schemars = "0.8"
semver = "1"
serde = { version = "1.0", default-features = false, features = ["derive"] }
thiserror = { version = "1.0" }
token-factory-api = { workspace = true }
2 changes: 1 addition & 1 deletion cosmwasm/ucs01-relay/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_schema::write_api;
use cw20_ics20::msg::{ExecuteMsg, InitMsg, QueryMsg};
use ucs01_relay::msg::{ExecuteMsg, InitMsg, QueryMsg};

fn main() {
write_api! {
Expand Down

0 comments on commit 6d4d0a0

Please sign in to comment.