Skip to content

Commit

Permalink
chore: cw20 <> cw20 working
Browse files Browse the repository at this point in the history
Signed-off-by: aeryz <[email protected]>
  • Loading branch information
aeryz committed Jan 29, 2025
1 parent da4714c commit d9567de
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 63 deletions.
2 changes: 1 addition & 1 deletion cosmwasm/cw20-token-minter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
cosmwasm-schema = { version = "1.5" }
cosmwasm-std = { version = "1.5", features = [ "cosmwasm_1_3"] }
cosmwasm-std = { version = "1.5", features = ["cosmwasm_1_3"] }
cw-storage-plus = { version = "1.2" }
cw20 = "1.1.2"
cw20-base = { version = "1.1.2", features = ["library"] }
Expand Down
47 changes: 32 additions & 15 deletions cosmwasm/cw20-token-minter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use cosmwasm_std::{
WasmMsg,
};
use cw20::{Cw20QueryMsg, TokenInfoResponse};
use token_factory_api::TokenFactoryMsg;
use ucs03_zkgm_token_minter_api::{ExecuteMsg, LocalTokenMsg, MetadataResponse, QueryMsg};
use ucs03_zkgm_token_minter_api::{
BaseTokenResponse, ExecuteMsg, LocalTokenMsg, MetadataResponse, QueryMsg, WrappedTokenMsg,
};

use crate::{
error::Error,
state::{Config, CONFIG, DENOM_TO_ADDR, DENOM_TO_BE_STORED},
state::{Config, ADDR_TO_DENOM, CONFIG, DENOM_TO_ADDR, DENOM_TO_BE_STORED},
};

pub const NATIVE_TOKEN_STORE_PREFIX: u32 = 0x1;
Expand Down Expand Up @@ -60,13 +61,12 @@ pub fn execute(

let response = match msg {
ExecuteMsg::Wrapped(msg) => match msg {
TokenFactoryMsg::CreateDenom { metadata, .. } => {
let metadata = metadata.expect("metadata exists");
WrappedTokenMsg::CreateDenom { metadata, .. } => {
// the first denom is always the same as the generated denom
let denom = metadata.denom_units[0].denom.clone();
DENOM_TO_BE_STORED.save(deps.storage, &denom)?;
let name = metadata.name.expect("metadata name exists");
let symbol = metadata.symbol.expect("metadata symbol exists");
let name = metadata.name;
let symbol = metadata.symbol;
let msg = WasmMsg::Instantiate {
admin: Some(env.contract.address.to_string()),
code_id: config.cw20_code_id,
Expand Down Expand Up @@ -95,10 +95,7 @@ pub fn execute(
reply_on: ReplyOn::Success,
})
}
TokenFactoryMsg::ChangeAdmin { .. } => {
panic!("admin is always this contract")
}
TokenFactoryMsg::MintTokens {
WrappedTokenMsg::MintTokens {
denom,
amount,
mint_to_address,
Expand All @@ -116,14 +113,25 @@ pub fn execute(
)?;
Response::new().add_message(msg)
}
TokenFactoryMsg::BurnTokens { denom, amount, .. } => {
WrappedTokenMsg::BurnTokens {
denom,
amount,
sender,
..
} => {
let addr = DENOM_TO_ADDR
.load(deps.storage, denom.clone())
.map_err(|_| Error::CantMint(denom))?;
let msg = wasm_execute(addr, &cw20::Cw20ExecuteMsg::Burn { amount }, vec![])?;
let msg = wasm_execute(
addr,
&cw20::Cw20ExecuteMsg::BurnFrom {
owner: sender.to_string(),
amount,
},
vec![],
)?;
Response::new().add_message(msg)
}
_ => return Err(Error::UnexpectedExecuteMsg(msg)),
},
ExecuteMsg::Local(msg) => match msg {
LocalTokenMsg::TakeFunds {
Expand Down Expand Up @@ -223,7 +231,10 @@ pub fn reply(deps: DepsMut, _: Env, reply: Reply) -> Result<Response, Error> {
.ok_or(Error::ContractCreationEventNotFound)?
.value;

DENOM_TO_ADDR.save(deps.storage, denom, &Addr::unchecked(addr))?;
let addr = deps.api.addr_validate(&addr)?;

DENOM_TO_ADDR.save(deps.storage, denom.clone(), &addr)?;
ADDR_TO_DENOM.save(deps.storage, addr, &denom)?;

Ok(Response::new())
} else {
Expand All @@ -234,6 +245,12 @@ pub fn reply(deps: DepsMut, _: Env, reply: Reply) -> Result<Response, Error> {
#[entry_point]
pub fn query(deps: Deps, _: Env, msg: QueryMsg) -> Result<Binary, Error> {
match msg {
QueryMsg::BaseToken { base_token } => {
let base_token = ADDR_TO_DENOM
.load(deps.storage, Addr::unchecked(base_token.clone()))
.unwrap_or(base_token);
Ok(to_json_binary(&BaseTokenResponse { base_token })?)
}
QueryMsg::Metadata { denom } => match DENOM_TO_ADDR.load(deps.storage, denom.clone()) {
Ok(addr) => {
let TokenInfoResponse { name, symbol, .. } = query_token_info(deps, addr.as_str())?;
Expand Down
4 changes: 2 additions & 2 deletions cosmwasm/cw20-token-minter/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::StdError;
use token_factory_api::TokenFactoryMsg;
use ucs03_zkgm_token_minter_api::WrappedTokenMsg;

#[derive(Debug, thiserror::Error)]
pub enum Error {
Expand Down Expand Up @@ -28,5 +28,5 @@ pub enum Error {
TokenDoesNotExist(String),

#[error("unexpected execute msg: {0:?}")]
UnexpectedExecuteMsg(TokenFactoryMsg),
UnexpectedExecuteMsg(WrappedTokenMsg),
}
2 changes: 2 additions & 0 deletions cosmwasm/cw20-token-minter/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ pub const CONFIG: Item<Config> = Item::new("conf");

pub const DENOM_TO_ADDR: Map<String, Addr> = Map::new("dta");

pub const ADDR_TO_DENOM: Map<Addr, String> = Map::new("atd");

pub const DENOM_TO_BE_STORED: Item<String> = Item::new("dtbs");
69 changes: 28 additions & 41 deletions cosmwasm/ibc-union/app/ucs03-zkgm/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ use base58::ToBase58;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, to_json_string, wasm_execute, Addr, BankMsg, Coin, CosmosMsg, DepsMut, Env,
MessageInfo, QueryRequest, Reply, Response, StdError, StdResult, SubMsg, SubMsgResult, Uint128,
Uint256, WasmMsg,
to_json_binary, to_json_string, wasm_execute, Addr, Coin, CosmosMsg, DepsMut, Env, MessageInfo,
QueryRequest, Reply, Response, StdError, StdResult, SubMsg, SubMsgResult, Uint128, Uint256,
WasmMsg,
};
use ibc_union_msg::{
module::IbcUnionMsg,
msg::{MsgSendPacket, MsgWriteAcknowledgement},
};
use ibc_union_spec::types::Packet;
use token_factory_api::{DenomUnit, Metadata, TokenFactoryMsg};
use ucs03_zkgm_token_minter_api::{LocalTokenMsg, MetadataResponse};
use token_factory_api::DenomUnit;
use ucs03_zkgm_token_minter_api::{
BaseTokenResponse, LocalTokenMsg, Metadata, MetadataResponse, WrappedTokenMsg,
};
use unionlabs::{
ethereum::keccak256,
primitives::{Bytes, H256},
Expand Down Expand Up @@ -400,7 +402,7 @@ fn refund(
// TODO: handle forward path
if order.base_token_path == source_channel.try_into().unwrap() {
messages.push(make_wasm_msg(
TokenFactoryMsg::MintTokens {
WrappedTokenMsg::MintTokens {
denom: base_denom,
amount: base_amount.into(),
mint_to_address: sender.into_string(),
Expand Down Expand Up @@ -458,7 +460,7 @@ fn acknowledge_fungible_asset_order(
// TODO: handle forward path
if order.base_token_path == packet.source_channel_id.try_into().unwrap() {
messages.push(make_wasm_msg(
TokenFactoryMsg::MintTokens {
WrappedTokenMsg::MintTokens {
denom: base_denom,
amount: base_amount.into(),
mint_to_address: market_maker.into_string(),
Expand Down Expand Up @@ -731,22 +733,18 @@ fn execute_fungible_asset_order(
&Vec::from(order.base_token.clone()).into(),
)?;
messages.push(make_wasm_msg(
TokenFactoryMsg::CreateDenom {
WrappedTokenMsg::CreateDenom {
subdenom: wrapped_denom,
metadata: Some(Metadata {
description: None,
metadata: Metadata {
denom_units: vec![DenomUnit {
denom: subdenom.clone(),
exponent: 0,
aliases: vec![],
}],
base: None,
display: Some(subdenom.clone()),
name: Some(order.base_token_name),
symbol: Some(order.base_token_symbol),
uri: None,
uri_hash: None,
}),
display: subdenom.clone(),
name: order.base_token_name,
symbol: order.base_token_symbol,
},
},
&minter,
vec![],
Expand All @@ -758,7 +756,7 @@ fn execute_fungible_asset_order(
)?;
};
messages.push(make_wasm_msg(
TokenFactoryMsg::MintTokens {
WrappedTokenMsg::MintTokens {
denom: subdenom.clone(),
amount: quote_amount.into(),
mint_to_address: receiver.into_string(),
Expand All @@ -768,7 +766,7 @@ fn execute_fungible_asset_order(
)?);
if fee_amount > 0 {
messages.push(make_wasm_msg(
TokenFactoryMsg::MintTokens {
WrappedTokenMsg::MintTokens {
denom: subdenom,
amount: fee_amount.into(),
mint_to_address: relayer.into_string(),
Expand Down Expand Up @@ -929,23 +927,32 @@ fn transfer(
if base_amount.is_zero() {
return Err(ContractError::InvalidAmount);
}
let minter = TOKEN_MINTER.load(deps.storage)?;
// If the origin exists, the preimage exists
let BaseTokenResponse { base_token } =
deps.querier
.query(&QueryRequest::Wasm(cosmwasm_std::WasmQuery::Smart {
contract_addr: minter.to_string(),
msg: to_json_binary(&ucs03_zkgm_token_minter_api::QueryMsg::BaseToken {
base_token,
})?,
}))?;
let unwrapped_asset = HASH_TO_FOREIGN_TOKEN.may_load(deps.storage, base_token.clone())?;
let mut messages = Vec::<CosmosMsg>::new();
// TODO: handle forward path
let mut origin = TOKEN_ORIGIN.may_load(deps.storage, base_token.clone())?;
let minter = TOKEN_MINTER.load(deps.storage)?;
match origin {
// Burn as we are going to unescrow on the counterparty
Some(path)
if path == Uint256::from(channel_id)
&& unwrapped_asset == Some(quote_token.clone()) =>
{
messages.push(make_wasm_msg(
TokenFactoryMsg::BurnTokens {
WrappedTokenMsg::BurnTokens {
denom: base_token.clone(),
amount: base_amount,
burn_from_address: minter.to_string(),
sender: info.sender.clone(),
},
&minter,
info.funds,
Expand Down Expand Up @@ -1044,23 +1051,3 @@ fn make_wasm_msg(
let msg = msg.into();
Ok(CosmosMsg::Wasm(wasm_execute(minter, &msg, funds)?))
}

#[test]
pub fn test_fucking() {
let t = hex_literal::hex!("79e489e8a9267d8ef2ae96b1d0965e69e42be338c603e330da8a64ce5e6490cc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000003400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002a62626e31786530726e6c6833753035716b7779746b776d797a6c383661306d767077667867663274377500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002c756e696f6e3164383467743663777839333873616e306874687a37793666307234663030676a71776835397700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000073666163746f72792f62626e3163633330686a30376d617061383565323963636171386a326a38767234676b7032746d7a637668703672643939656564353430736666647a6b682f4366446259716e5a4e5a734e6b447544706f3662665244336f7a66724a414537717534666257756b6f51474b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000046d756e6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000046d756e6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000046d756e6f00000000000000000000000000000000000000000000000000000000");
let packet = ZkgmPacket::abi_decode_params(t.as_slice(), false).unwrap();
let inst = FungibleAssetOrder::abi_decode_params(&packet.instruction.operand, false).unwrap();
panic!("{inst:?}");
}

#[test]
pub fn aedlnaesnd() {
panic!(
"{}",
predict_wrapped_denom(
"0".parse().unwrap(),
22,
b"factory/union1gk3qcw5tlajduwez3uxgpzsydymht2q0qtjyfz6zeq0azsju8c3qqu8hds/QbT8RvrS1NmUfkGmEynCigyFpWBxgarbkbijuYT9369".into()
)
);
}
8 changes: 7 additions & 1 deletion cosmwasm/native-token-minter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use cosmwasm_std::{
QueryRequest, Response, StdResult,
};
use token_factory_api::{TokenFactoryMsg, TokenFactoryQuery};
use ucs03_zkgm_token_minter_api::{ExecuteMsg, LocalTokenMsg, MetadataResponse, QueryMsg};
use ucs03_zkgm_token_minter_api::{
BaseTokenResponse, ExecuteMsg, LocalTokenMsg, MetadataResponse, QueryMsg,
};

use crate::{error::Error, state::ADMIN};

Expand Down Expand Up @@ -47,6 +49,7 @@ pub fn execute(

let resp = match msg {
ExecuteMsg::Wrapped(msg) => {
let msg: TokenFactoryMsg = msg.into();
if let TokenFactoryMsg::BurnTokens { denom, amount, .. } = &msg {
let contains_base_token = info
.funds
Expand Down Expand Up @@ -88,6 +91,9 @@ pub fn execute(
#[entry_point]
pub fn query(deps: Deps<TokenFactoryQuery>, _: Env, msg: QueryMsg) -> Result<Binary, Error> {
match msg {
QueryMsg::BaseToken { base_token } => {
Ok(to_json_binary(&BaseTokenResponse { base_token })?)
}
QueryMsg::Metadata { denom } => {
let denom_metadata =
deps.querier
Expand Down
Loading

0 comments on commit d9567de

Please sign in to comment.