Skip to content

Commit

Permalink
Rename Meta to TokenInfo in cw20 contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Aug 12, 2020
1 parent 75ce281 commit 11f1d55
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 87 deletions.
4 changes: 2 additions & 2 deletions contracts/cw20-base/examples/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

use cw20::{BalanceResponse, MetaResponse};
use cw20::{BalanceResponse, TokenInfoResponse};
use cw20_base::msg::{HandleMsg, InitMsg, QueryMsg};

fn main() {
Expand All @@ -16,5 +16,5 @@ fn main() {
export_schema(&schema_for!(HandleMsg), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(BalanceResponse), &out_dir);
export_schema(&schema_for!(MetaResponse), &out_dir);
export_schema(&schema_for!(TokenInfoResponse), &out_dir);
}
6 changes: 3 additions & 3 deletions contracts/cw20-base/schema/query_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
}
},
{
"description": "Returns metadata on the contract - name, decimals, supply, etc. Return type: MetaResponse.",
"description": "Returns metadata on the contract - name, decimals, supply, etc. Return type: TokenInfoResponse.",
"type": "object",
"required": [
"meta"
"token_info"
],
"properties": {
"meta": {
"token_info": {
"type": "object"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MetaResponse",
"title": "TokenInfoResponse",
"type": "object",
"required": [
"decimals",
Expand Down
14 changes: 7 additions & 7 deletions contracts/cw20-base/src/allowances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cosmwasm_std::{
};
use cw20::{AllowanceResponse, Cw20ReceiveMsg, Expiration};

use crate::state::{allowance_remove, allowances, allowances_read, balances, meta};
use crate::state::{allowances, allowances_read, balances, token_info};

pub fn handle_increase_allowance<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
Expand Down Expand Up @@ -67,7 +67,7 @@ pub fn handle_decrease_allowance<S: Storage, A: Api, Q: Querier>(
}
bucket.save(spender_raw.as_slice(), &allowance)?;
} else {
allowance_remove(&mut deps.storage, owner_raw, spender_raw);
allowances(&mut deps.storage, owner_raw).remove(spender_raw.as_slice());
}

let res = HandleResponse {
Expand Down Expand Up @@ -173,7 +173,7 @@ pub fn handle_burn_from<S: Storage, A: Api, Q: Querier>(
balance.unwrap_or_default() - amount
})?;
// reduce total_supply
meta(&mut deps.storage).update(|mut meta| {
token_info(&mut deps.storage).update(|mut meta| {
meta.total_supply = (meta.total_supply - amount)?;
Ok(meta)
})?;
Expand Down Expand Up @@ -265,9 +265,9 @@ mod tests {

use cosmwasm_std::testing::{mock_dependencies, mock_env};
use cosmwasm_std::{coins, CosmosMsg, StdError, WasmMsg};
use cw20::MetaResponse;
use cw20::TokenInfoResponse;

use crate::contract::{handle, init, query_balance, query_meta};
use crate::contract::{handle, init, query_balance, query_token_info};
use crate::msg::{HandleMsg, InitMsg, InitialBalance};

fn get_balance<S: Storage, A: Api, Q: Querier, T: Into<HumanAddr>>(
Expand All @@ -282,7 +282,7 @@ mod tests {
deps: &mut Extern<S, A, Q>,
addr: &HumanAddr,
amount: Uint128,
) -> MetaResponse {
) -> TokenInfoResponse {
let init_msg = InitMsg {
name: "Auto Gen".to_string(),
symbol: "AUTO".to_string(),
Expand All @@ -295,7 +295,7 @@ mod tests {
};
let env = mock_env(&HumanAddr("creator".to_string()), &[]);
init(deps, env, init_msg).unwrap();
query_meta(&deps).unwrap()
query_token_info(&deps).unwrap()
}

#[test]
Expand Down
74 changes: 37 additions & 37 deletions contracts/cw20-base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ use cosmwasm_std::{
log, to_binary, Api, Binary, Env, Extern, HandleResponse, HumanAddr, InitResponse, Querier,
StdError, StdResult, Storage, Uint128,
};
use cw20::{BalanceResponse, Cw20ReceiveMsg, MetaResponse, MinterResponse};
use cw20::{BalanceResponse, Cw20ReceiveMsg, MinterResponse, TokenInfoResponse};

use crate::allowances::{
handle_burn_from, handle_decrease_allowance, handle_increase_allowance, handle_send_from,
handle_transfer_from, query_allowance,
};
use crate::msg::{HandleMsg, InitMsg, InitialBalance, QueryMsg};
use crate::state::{balances, balances_read, meta, meta_read, Meta, MinterData};
use crate::state::{balances, balances_read, token_info, token_info_read, MinterData, TokenInfo};

pub fn init<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
_env: Env,
msg: InitMsg,
) -> StdResult<InitResponse> {
// check valid meta-data
// check valid token info
msg.validate()?;
// create initial accounts
let total_supply = create_accounts(deps, &msg.initial_balances)?;
Expand All @@ -35,15 +35,15 @@ pub fn init<S: Storage, A: Api, Q: Querier>(
None => None,
};

// store metadata
let data = Meta {
// store token info
let data = TokenInfo {
name: msg.name,
symbol: msg.symbol,
decimals: msg.decimals,
total_supply,
mint,
};
meta(&mut deps.storage).save(&data)?;
token_info(&mut deps.storage).save(&data)?;
Ok(InitResponse::default())
}

Expand Down Expand Up @@ -143,9 +143,9 @@ pub fn handle_burn<S: Storage, A: Api, Q: Querier>(
balance.unwrap_or_default() - amount
})?;
// reduce total_supply
meta(&mut deps.storage).update(|mut meta| {
meta.total_supply = (meta.total_supply - amount)?;
Ok(meta)
token_info(&mut deps.storage).update(|mut info| {
info.total_supply = (info.total_supply - amount)?;
Ok(info)
})?;

let res = HandleResponse {
Expand All @@ -166,7 +166,7 @@ pub fn handle_mint<S: Storage, A: Api, Q: Querier>(
recipient: HumanAddr,
amount: Uint128,
) -> StdResult<HandleResponse> {
let mut config = meta_read(&deps.storage).load()?;
let mut config = token_info_read(&deps.storage).load()?;
if config.mint.is_none()
|| config.mint.as_ref().unwrap().minter
!= deps.api.canonical_address(&env.message.sender)?
Expand All @@ -181,7 +181,7 @@ pub fn handle_mint<S: Storage, A: Api, Q: Querier>(
return Err(StdError::generic_err("Minting cannot exceed the cap"));
}
}
meta(&mut deps.storage).save(&config)?;
token_info(&mut deps.storage).save(&config)?;

// add amount to recipient balance
let rcpt_raw = deps.api.canonical_address(&recipient)?;
Expand Down Expand Up @@ -250,7 +250,7 @@ pub fn query<S: Storage, A: Api, Q: Querier>(
) -> StdResult<Binary> {
match msg {
QueryMsg::Balance { address } => to_binary(&query_balance(deps, address)?),
QueryMsg::Meta {} => to_binary(&query_meta(deps)?),
QueryMsg::TokenInfo {} => to_binary(&query_token_info(deps)?),
QueryMsg::Minter {} => to_binary(&query_minter(deps)?),
QueryMsg::Allowance { owner, spender } => {
to_binary(&query_allowance(deps, owner, spender)?)
Expand All @@ -269,23 +269,23 @@ pub fn query_balance<S: Storage, A: Api, Q: Querier>(
Ok(BalanceResponse { balance })
}

pub fn query_meta<S: Storage, A: Api, Q: Querier>(
pub fn query_token_info<S: Storage, A: Api, Q: Querier>(
deps: &Extern<S, A, Q>,
) -> StdResult<MetaResponse> {
let meta = meta_read(&deps.storage).load()?;
let res = MetaResponse {
name: meta.name,
symbol: meta.symbol,
decimals: meta.decimals,
total_supply: meta.total_supply,
) -> StdResult<TokenInfoResponse> {
let info = token_info_read(&deps.storage).load()?;
let res = TokenInfoResponse {
name: info.name,
symbol: info.symbol,
decimals: info.decimals,
total_supply: info.total_supply,
};
Ok(res)
}

pub fn query_minter<S: Storage, A: Api, Q: Querier>(
deps: &Extern<S, A, Q>,
) -> StdResult<Option<MinterResponse>> {
let meta = meta_read(&deps.storage).load()?;
let meta = token_info_read(&deps.storage).load()?;
let minter = match meta.mint {
Some(m) => Some(MinterResponse {
minter: deps.api.human_address(&m.minter)?,
Expand Down Expand Up @@ -318,7 +318,7 @@ mod tests {
amount: Uint128,
minter: &HumanAddr,
cap: Option<Uint128>,
) -> MetaResponse {
) -> TokenInfoResponse {
_do_init(
deps,
addr,
Expand All @@ -335,7 +335,7 @@ mod tests {
deps: &mut Extern<S, A, Q>,
addr: &HumanAddr,
amount: Uint128,
) -> MetaResponse {
) -> TokenInfoResponse {
_do_init(deps, addr, amount, None)
}

Expand All @@ -345,7 +345,7 @@ mod tests {
addr: &HumanAddr,
amount: Uint128,
mint: Option<MinterResponse>,
) -> MetaResponse {
) -> TokenInfoResponse {
let init_msg = InitMsg {
name: "Auto Gen".to_string(),
symbol: "AUTO".to_string(),
Expand All @@ -360,10 +360,10 @@ mod tests {
let res = init(deps, env, init_msg).unwrap();
assert_eq!(0, res.messages.len());

let meta = query_meta(&deps).unwrap();
let meta = query_token_info(&deps).unwrap();
assert_eq!(
meta,
MetaResponse {
TokenInfoResponse {
name: "Auto Gen".to_string(),
symbol: "AUTO".to_string(),
decimals: 3,
Expand Down Expand Up @@ -394,8 +394,8 @@ mod tests {
assert_eq!(0, res.messages.len());

assert_eq!(
query_meta(&deps).unwrap(),
MetaResponse {
query_token_info(&deps).unwrap(),
TokenInfoResponse {
name: "Cash Token".to_string(),
symbol: "CASH".to_string(),
decimals: 9,
Expand Down Expand Up @@ -429,8 +429,8 @@ mod tests {
assert_eq!(0, res.messages.len());

assert_eq!(
query_meta(&deps).unwrap(),
MetaResponse {
query_token_info(&deps).unwrap(),
TokenInfoResponse {
name: "Cash Token".to_string(),
symbol: "CASH".to_string(),
decimals: 9,
Expand Down Expand Up @@ -582,8 +582,8 @@ mod tests {
assert_eq!(0, res.messages.len());

assert_eq!(
query_meta(&deps).unwrap(),
MetaResponse {
query_token_info(&deps).unwrap(),
TokenInfoResponse {
name: "Bash Shell".to_string(),
symbol: "BASH".to_string(),
decimals: 6,
Expand All @@ -603,7 +603,7 @@ mod tests {
let expected = do_init(&mut deps, &addr1, amount1);

// check meta query
let loaded = query_meta(&deps).unwrap();
let loaded = query_token_info(&deps).unwrap();
assert_eq!(expected, loaded);

// check balance query (full)
Expand Down Expand Up @@ -676,7 +676,7 @@ mod tests {
let remainder = (amount1 - transfer).unwrap();
assert_eq!(get_balance(&deps, &addr1), remainder);
assert_eq!(get_balance(&deps, &addr2), transfer);
assert_eq!(query_meta(&deps).unwrap().total_supply, amount1);
assert_eq!(query_token_info(&deps).unwrap().total_supply, amount1);
}

#[test]
Expand All @@ -697,7 +697,7 @@ mod tests {
StdError::Underflow { .. } => {}
e => panic!("Unexpected error: {}", e),
}
assert_eq!(query_meta(&deps).unwrap().total_supply, amount1);
assert_eq!(query_token_info(&deps).unwrap().total_supply, amount1);

// valid burn reduces total supply
let env = mock_env(addr1.clone(), &[]);
Expand All @@ -707,7 +707,7 @@ mod tests {

let remainder = (amount1 - burn).unwrap();
assert_eq!(get_balance(&deps, &addr1), remainder);
assert_eq!(query_meta(&deps).unwrap().total_supply, remainder);
assert_eq!(query_token_info(&deps).unwrap().total_supply, remainder);
}

#[test]
Expand Down Expand Up @@ -768,6 +768,6 @@ mod tests {
let remainder = (amount1 - transfer).unwrap();
assert_eq!(get_balance(&deps, &addr1), remainder);
assert_eq!(get_balance(&deps, &contract), transfer);
assert_eq!(query_meta(&deps).unwrap().total_supply, amount1);
assert_eq!(query_token_info(&deps).unwrap().total_supply, amount1);
}
}
4 changes: 2 additions & 2 deletions contracts/cw20-base/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ pub enum QueryMsg {
/// Return type: BalanceResponse.
Balance { address: HumanAddr },
/// Returns metadata on the contract - name, decimals, supply, etc.
/// Return type: MetaResponse.
Meta {},
/// Return type: TokenInfoResponse.
TokenInfo {},
/// Only with "mintable" extension.
/// Returns who can mint and how much.
/// Return type: MinterResponse.
Expand Down
Loading

0 comments on commit 11f1d55

Please sign in to comment.