From 924d828a7672526d5db13231ccfbf0e7ea93a73f Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Mon, 18 Jul 2022 11:33:32 -0400 Subject: [PATCH] Update some contracts for `cosmwasm-proposal` (#66) --- Cargo.lock | 47 -------- contracts/anchor-handler/Cargo.toml | 2 - contracts/anchor-handler/src/contract.rs | 4 +- contracts/anchor/Cargo.toml | 2 - contracts/mixer/Cargo.toml | 1 - contracts/signature-bridge/Cargo.toml | 2 - contracts/signature-bridge/src/contract.rs | 41 +++---- contracts/tokenwrapper-handler/Cargo.toml | 2 - .../tokenwrapper-handler/src/contract.rs | 4 +- contracts/tokenwrapper-handler/src/tests.rs | 12 +- contracts/tokenwrapper/Cargo.toml | 4 - contracts/tokenwrapper/src/contract.rs | 44 ++++++-- contracts/tokenwrapper/src/tests.rs | 10 +- contracts/treasury-handler/Cargo.toml | 2 - contracts/treasury-handler/src/contract.rs | 4 +- contracts/treasury/Cargo.toml | 5 - contracts/treasury/src/contract.rs | 104 ++++++++++-------- contracts/treasury/src/tests.rs | 14 +-- contracts/vanchor/Cargo.toml | 2 - packages/protocol_cosmwasm/Cargo.toml | 1 - .../protocol_cosmwasm/src/signature_bridge.rs | 8 +- .../protocol_cosmwasm/src/token_wrapper.rs | 26 +++-- packages/protocol_cosmwasm/src/treasury.rs | 9 +- .../src/processes/tests/signatureBridge.ts | 27 ++--- test-scripts/src/processes/tests/testnet.ts | 4 +- 25 files changed, 169 insertions(+), 212 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4077ead..6f3fcd7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -697,7 +697,6 @@ dependencies = [ "arkworks-setups 1.0.0", "cosmwasm-schema", "cosmwasm-std", - "cosmwasm-storage", "cosmwasm-vm", "cw-storage-plus", "cw2", @@ -707,7 +706,6 @@ dependencies = [ "protocol-cosmwasm", "schemars", "serde", - "thiserror", "wasm-utils", ] @@ -717,14 +715,12 @@ version = "0.1.0" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cosmwasm-storage", "cw-storage-plus", "cw2", "getrandom 0.2.6", "protocol-cosmwasm", "schemars", "serde", - "thiserror", ] [[package]] @@ -764,7 +760,6 @@ dependencies = [ "arkworks-setups 1.0.0", "cosmwasm-schema", "cosmwasm-std", - "cosmwasm-storage", "cosmwasm-vm", "cw-storage-plus", "cw2", @@ -794,14 +789,12 @@ dependencies = [ "arkworks-setups 1.0.0", "cosmwasm-schema", "cosmwasm-std", - "cosmwasm-storage", "cw-storage-plus", "cw2", "getrandom 0.2.6", "protocol-cosmwasm", "schemars", "serde", - "thiserror", ] [[package]] @@ -821,34 +814,20 @@ dependencies = [ "uint", ] -[[package]] -name = "cosmwasm-storage" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d18403b07304d15d304dad11040d45bbcaf78d603b4be3fb5e2685c16f9229b5" -dependencies = [ - "cosmwasm-std", - "serde", -] - [[package]] name = "cosmwasm-tokenwrapper" version = "0.1.0" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cosmwasm-storage", "cosmwasm-vm", - "cw-controllers", "cw-storage-plus", - "cw-utils", "cw2", "cw20", "cw20-base", "protocol-cosmwasm", "schemars", "serde", - "thiserror", ] [[package]] @@ -857,14 +836,12 @@ version = "0.1.0" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cosmwasm-storage", "cw-storage-plus", "cw2", "getrandom 0.2.6", "protocol-cosmwasm", "schemars", "serde", - "thiserror", ] [[package]] @@ -873,18 +850,13 @@ version = "0.1.0" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cosmwasm-storage", "cosmwasm-vm", - "cw-controllers", "cw-storage-plus", - "cw-utils", "cw2", "cw20", - "cw20-base", "protocol-cosmwasm", "schemars", "serde", - "thiserror", ] [[package]] @@ -893,14 +865,12 @@ version = "0.1.0" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cosmwasm-storage", "cw-storage-plus", "cw2", "getrandom 0.2.6", "protocol-cosmwasm", "schemars", "serde", - "thiserror", ] [[package]] @@ -918,7 +888,6 @@ dependencies = [ "arkworks-setups 1.0.0", "cosmwasm-schema", "cosmwasm-std", - "cosmwasm-storage", "cosmwasm-vm", "cw-storage-plus", "cw2", @@ -928,7 +897,6 @@ dependencies = [ "schemars", "serde", "sp-core", - "thiserror", "wasm-utils", ] @@ -1140,20 +1108,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "cw-controllers" -version = "0.13.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f0bc6019b4d3d81e11f5c384bcce7173e2210bd654d75c6c9668e12cca05dfa" -dependencies = [ - "cosmwasm-std", - "cw-storage-plus", - "cw-utils", - "schemars", - "serde", - "thiserror", -] - [[package]] name = "cw-storage-plus" version = "0.13.4" @@ -2402,7 +2356,6 @@ dependencies = [ "arkworks-native-gadgets 1.0.0", "arkworks-setups 1.0.0", "cosmwasm-std", - "cosmwasm-storage", "cw20", "cw20-base", "schemars", diff --git a/contracts/anchor-handler/Cargo.toml b/contracts/anchor-handler/Cargo.toml index 6bbed49..a488701 100644 --- a/contracts/anchor-handler/Cargo.toml +++ b/contracts/anchor-handler/Cargo.toml @@ -23,12 +23,10 @@ library = [] [dependencies] cosmwasm-std = "1.0.0" -cosmwasm-storage = "1.0.0" cw-storage-plus = "0.13.4" cw2 = "0.13.4" schemars = "0.8.3" serde = { version = "1.0.127", default-features = false, features = ["derive"] } -thiserror = { version = "1.0.26" } getrandom = { version = "0.2", features = ["js"] } protocol-cosmwasm = { version = "0.1.0", path = "../../packages/protocol_cosmwasm"} diff --git a/contracts/anchor-handler/src/contract.rs b/contracts/anchor-handler/src/contract.rs index 72f8c55..b1fa124 100644 --- a/contracts/anchor-handler/src/contract.rs +++ b/contracts/anchor-handler/src/contract.rs @@ -129,7 +129,9 @@ fn execute_proposal( ) -> Result { // Parse the (proposal)`data`. let parsed_resource_id = element_encoder(&data[0..32]); - let base64_encoded_proposal = &data[32..]; + let _func_sig = &data[32..36]; + let _nonce = &data[36..40]; + let base64_encoded_proposal = &data[40..]; let bridge_addr = STATE.load(deps.storage)?.bridge_addr; diff --git a/contracts/anchor/Cargo.toml b/contracts/anchor/Cargo.toml index a1acf21..e7741a1 100644 --- a/contracts/anchor/Cargo.toml +++ b/contracts/anchor/Cargo.toml @@ -24,13 +24,11 @@ library = [] [dependencies] cosmwasm-std = "1.0.0" -cosmwasm-storage = "1.0.0" cw-storage-plus = "0.13.4" cw2 = "0.13.4" cw20 = "0.13.4" schemars = "0.8.3" serde = { version = "1.0.127", default-features = false, features = ["derive"] } -thiserror = { version = "1.0.26" } codec = {package = "parity-scale-codec", version = "2.3.0", default-features = false, features = ["derive", "max-encoded-len"]} hex = "0.4" diff --git a/contracts/mixer/Cargo.toml b/contracts/mixer/Cargo.toml index 46d5092..9df23dd 100644 --- a/contracts/mixer/Cargo.toml +++ b/contracts/mixer/Cargo.toml @@ -24,7 +24,6 @@ library = [] [dependencies] cosmwasm-std = "1.0.0" -cosmwasm-storage = "1.0.0" cw-storage-plus = "0.13.4" cw2 = "0.13.4" cw20 = "0.13.4" diff --git a/contracts/signature-bridge/Cargo.toml b/contracts/signature-bridge/Cargo.toml index 9a93883..7a87a3e 100644 --- a/contracts/signature-bridge/Cargo.toml +++ b/contracts/signature-bridge/Cargo.toml @@ -23,12 +23,10 @@ library = [] [dependencies] cosmwasm-std = "1.0.0" -cosmwasm-storage = "1.0.0" cw-storage-plus = "0.13.4" cw2 = "0.13.4" schemars = "0.8.3" serde = { version = "1.0.127", default-features = false, features = ["derive"] } -thiserror = { version = "1.0.26" } getrandom = { version = "0.2", features = ["js"] } arkworks-setups = { version = "1.0.0", features = ["r1cs"], default-features = false } diff --git a/contracts/signature-bridge/src/contract.rs b/contracts/signature-bridge/src/contract.rs index 66199e6..d45a8bf 100644 --- a/contracts/signature-bridge/src/contract.rs +++ b/contracts/signature-bridge/src/contract.rs @@ -1,8 +1,8 @@ #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - attr, to_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, StdError, - StdResult, WasmMsg, + attr, from_slice, to_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, + StdError, StdResult, WasmMsg, }; use cw2::set_contract_version; @@ -10,8 +10,8 @@ use crate::state::{State, RESOURCEID2HANDLERADDR, STATE}; use protocol_cosmwasm::error::ContractError; use protocol_cosmwasm::executor::ExecuteMsg as ExecutorExecMsg; use protocol_cosmwasm::signature_bridge::{ - ExecProposalWithSigMsg, ExecuteMsg, InstantiateMsg, QueryMsg, SetResourceWithSigMsg, - StateResponse, + ExecProposalWithSigMsg, ExecuteMsg, InstantiateMsg, QueryMsg, ResourceIdUpdateData, + SetResourceWithSigMsg, StateResponse, }; use protocol_cosmwasm::utils::{ compute_chain_id, compute_chain_id_type, element_encoder, get_chain_id_type, @@ -85,42 +85,43 @@ fn admin_set_resource_with_signature( ) -> Result { let mut state = STATE.load(deps.storage)?; + let ResourceIdUpdateData { + resource_id: _, + function_sig, + nonce, + new_resource_id, + handler_addr, + execution_context_addr, + } = from_slice(&msg.data)?; + // Validations - let mut data: Vec = Vec::new(); - data.extend_from_slice(&msg.resource_id); - data.extend_from_slice(&msg.function_sig); - data.extend_from_slice(&msg.nonce.to_be_bytes()); - data.extend_from_slice(&msg.new_resource_id); - data.extend_from_slice(msg.handler_addr.as_bytes()); - data.extend_from_slice(msg.execution_context_addr.as_bytes()); - - if !signed_by_governor(deps.branch(), &data, &msg.sig, &state.governor)? { + if !signed_by_governor(deps.branch(), &msg.data, &msg.sig, &state.governor)? { return Err(ContractError::Std(StdError::GenericErr { msg: "Invalid sig from governor".to_string(), })); } - if msg.nonce <= state.proposal_nonce || state.proposal_nonce + 1048 < msg.nonce { + if nonce <= state.proposal_nonce || state.proposal_nonce + 1048 < nonce { return Err(ContractError::InvalidNonce); } - if msg.function_sig != [0u8; 4] { + if function_sig != [0u8; 4] { return Err(ContractError::InvalidArbitraryData); } // Save the info of "resource_id -> handler(contract)" in this contract. - RESOURCEID2HANDLERADDR.save(deps.storage, &msg.new_resource_id, &msg.handler_addr)?; + RESOURCEID2HANDLERADDR.save(deps.storage, &new_resource_id, &handler_addr)?; - state.proposal_nonce = msg.nonce; + state.proposal_nonce = nonce; STATE.save(deps.storage, &state)?; // Save the "resource" info in "handler" contract. let msgs: Vec = vec![CosmosMsg::Wasm(WasmMsg::Execute { - contract_addr: msg.handler_addr, + contract_addr: handler_addr, funds: vec![], msg: to_binary(&ExecutorExecMsg::SetResource { - resource_id: msg.new_resource_id, - contract_addr: msg.execution_context_addr, + resource_id: new_resource_id, + contract_addr: execution_context_addr, }) .unwrap(), })]; diff --git a/contracts/tokenwrapper-handler/Cargo.toml b/contracts/tokenwrapper-handler/Cargo.toml index 6648b31..2e2f70f 100644 --- a/contracts/tokenwrapper-handler/Cargo.toml +++ b/contracts/tokenwrapper-handler/Cargo.toml @@ -23,12 +23,10 @@ library = [] [dependencies] cosmwasm-std = "1.0.0" -cosmwasm-storage = "1.0.0" cw-storage-plus = "0.13.2" cw2 = "0.13.2" schemars = "0.8.3" serde = { version = "1.0.127", default-features = false, features = ["derive"] } -thiserror = { version = "1.0.26" } getrandom = { version = "0.2", features = ["js"] } protocol-cosmwasm = { version = "0.1.0", path = "../../packages/protocol_cosmwasm"} diff --git a/contracts/tokenwrapper-handler/src/contract.rs b/contracts/tokenwrapper-handler/src/contract.rs index 10fdf83..f13cc66 100644 --- a/contracts/tokenwrapper-handler/src/contract.rs +++ b/contracts/tokenwrapper-handler/src/contract.rs @@ -132,7 +132,9 @@ fn execute_proposal( ) -> Result { // Parse the (proposal)`data`. let parsed_resource_id = element_encoder(&data[0..32]); - let base64_encoded_proposal = &data[32..]; + let _func_sig = &data[32..36]; + let _nonce = &data[36..40]; + let base64_encoded_proposal = &data[40..]; let bridge_addr = STATE.load(deps.storage)?.bridge_addr; diff --git a/contracts/tokenwrapper-handler/src/tests.rs b/contracts/tokenwrapper-handler/src/tests.rs index 8ca5501..d1e7b51 100644 --- a/contracts/tokenwrapper-handler/src/tests.rs +++ b/contracts/tokenwrapper-handler/src/tests.rs @@ -6,9 +6,7 @@ use cosmwasm_std::{attr, from_binary, to_binary, OwnedDeps}; use protocol_cosmwasm::error::ContractError; use protocol_cosmwasm::structs::BridgeAddrResponse; -use protocol_cosmwasm::token_wrapper::{ - ExecuteMsg as GovernedTokenWrapperExecMsg, UpdateConfigMsg, -}; +use protocol_cosmwasm::token_wrapper::ExecuteMsg as GovernedTokenWrapperExecMsg; use protocol_cosmwasm::tokenwrapper_handler::{ExecuteMsg, InstantiateMsg, QueryMsg}; const BRIDGE_ADDR: &str = "bridge-contract"; @@ -123,13 +121,9 @@ fn test_handler_execute_proposal() { // Try to set a new handler for tokenwrapper contract let info = mock_info(BRIDGE_ADDR, &[]); - let set_handler_proposal = GovernedTokenWrapperExecMsg::UpdateConfig(UpdateConfigMsg { - governor: Some("new-governor".to_string()), + let set_handler_proposal = GovernedTokenWrapperExecMsg::ConfigureNativeAllowed { is_native_allowed: Some(true), - wrapping_limit: None, - fee_percentage: None, - fee_recipient: None, - }); + }; let exec_data = proposal_to_exec_data(RESOURCE_ID, set_handler_proposal); let exec_proposal_msg = ExecuteMsg::ExecuteProposal { resource_id: RESOURCE_ID, diff --git a/contracts/tokenwrapper/Cargo.toml b/contracts/tokenwrapper/Cargo.toml index b8fa2a0..2b6e1bc 100644 --- a/contracts/tokenwrapper/Cargo.toml +++ b/contracts/tokenwrapper/Cargo.toml @@ -23,16 +23,12 @@ library = [] [dependencies] cosmwasm-std = "1.0.0" -cosmwasm-storage = "1.0.0" cw-storage-plus = "0.13.4" -cw-utils = "0.13.4" cw2 = "0.13.4" cw20 = "0.13.4" -cw-controllers = "0.13.4" cw20-base = { version = "0.13.4", features = ["library"] } schemars = "0.8.3" serde = { version = "1.0.127", default-features = false, features = ["derive"] } -thiserror = { version = "1.0.26" } protocol-cosmwasm = { version = "0.1.0", path = "../../packages/protocol_cosmwasm"} diff --git a/contracts/tokenwrapper/src/contract.rs b/contracts/tokenwrapper/src/contract.rs index d4d1453..96b17b0 100644 --- a/contracts/tokenwrapper/src/contract.rs +++ b/contracts/tokenwrapper/src/contract.rs @@ -22,7 +22,7 @@ use cw20_base::state::{MinterData, TokenInfo, TOKEN_INFO}; use protocol_cosmwasm::error::ContractError; use protocol_cosmwasm::token_wrapper::{ ConfigResponse, Cw20HookMsg, ExecuteMsg, FeeFromAmountResponse, GetAmountToWrapResponse, - InstantiateMsg, QueryMsg, UpdateConfigMsg, WRAP_FEE_CALC_DENOMINATOR, + InstantiateMsg, QueryMsg, WRAP_FEE_CALC_DENOMINATOR, }; use crate::state::{Config, CONFIG, HISTORICAL_TOKENS, TOKENS}; @@ -116,8 +116,30 @@ pub fn execute( /* ------------------------------------- */ /* ----- Governance functionality ----- */ - // Resets the config. Only the governor can execute this entry. - ExecuteMsg::UpdateConfig(msg) => update_config(deps, info, msg), + // Update the `governor`. Only the governor can execute this entry. + ExecuteMsg::ConfigureGovernor { governor } => { + update_config(deps, info, governor, None, None, None, None) + } + + // Update the `is_native_allowed`. Only the governor can execute this entry. + ExecuteMsg::ConfigureNativeAllowed { is_native_allowed } => { + update_config(deps, info, None, is_native_allowed, None, None, None) + } + + // Update the `wrapping_limit`. Only the owner can execute this entry. + ExecuteMsg::ConfigureWrappingLimit { wrapping_limit } => { + update_config(deps, info, None, None, wrapping_limit, None, None) + } + + // Update the `fee_percentage`. Only the owner can execute this entry. + ExecuteMsg::ConfigureFeePercentage { fee_percentage } => { + update_config(deps, info, None, None, None, fee_percentage, None) + } + + // Update the `wrapping_limit`. Only the owner can execute this entry. + ExecuteMsg::ConfigureFeeRecipient { fee_recipient } => { + update_config(deps, info, None, None, None, None, fee_recipient) + } // Add new cw20 token address to wrapping list ExecuteMsg::AddCw20TokenAddr { token, nonce } => add_token_addr(deps, info, token, nonce), @@ -427,7 +449,11 @@ fn wrap_cw20( fn update_config( deps: DepsMut, info: MessageInfo, - msg: UpdateConfigMsg, + governor: Option, + is_native_allowed: Option, + wrapping_limit: Option, + fee_percentage: Option, + fee_recipient: Option, ) -> Result { let mut config = CONFIG.load(deps.storage)?; // Validate the tx sender. @@ -436,19 +462,19 @@ fn update_config( } // Update the config - if let Some(new_governor) = msg.governor { + if let Some(new_governor) = governor { config.governor = deps.api.addr_validate(&new_governor)?; } - if let Some(is_native_allowed) = msg.is_native_allowed { + if let Some(is_native_allowed) = is_native_allowed { config.is_native_allowed = is_native_allowed; } - if let Some(wrapping_limit) = msg.wrapping_limit { + if let Some(wrapping_limit) = wrapping_limit { config.wrapping_limit = wrapping_limit; } - if let Some(fee_percentage) = msg.fee_percentage { + if let Some(fee_percentage) = fee_percentage { if fee_percentage > WRAP_FEE_CALC_DENOMINATOR { return Err(ContractError::Std(StdError::generic_err( "Fee percenage cannot be greater than 100", @@ -457,7 +483,7 @@ fn update_config( config.fee_percentage = fee_percentage; } - if let Some(fee_recipient) = msg.fee_recipient { + if let Some(fee_recipient) = fee_recipient { config.fee_recipient = deps.api.addr_validate(&fee_recipient)?; } diff --git a/contracts/tokenwrapper/src/tests.rs b/contracts/tokenwrapper/src/tests.rs index 5465f35..fa7bcf6 100644 --- a/contracts/tokenwrapper/src/tests.rs +++ b/contracts/tokenwrapper/src/tests.rs @@ -7,7 +7,7 @@ use cw20::{BalanceResponse, Cw20ReceiveMsg, TokenInfoResponse}; use protocol_cosmwasm::token_wrapper::{ ConfigResponse, Cw20HookMsg, ExecuteMsg, FeeFromAmountResponse, GetAmountToWrapResponse, - InstantiateMsg, QueryMsg, UpdateConfigMsg, + InstantiateMsg, QueryMsg, }; use crate::contract::{execute, instantiate, query}; @@ -329,13 +329,9 @@ fn test_update_config() { // Update the config let info = mock_info("creator", &[]); - let update_config_msg = ExecuteMsg::UpdateConfig(UpdateConfigMsg { + let update_config_msg = ExecuteMsg::ConfigureGovernor { governor: Some("new_governor".to_string()), - is_native_allowed: None, - wrapping_limit: None, - fee_percentage: None, - fee_recipient: None, - }); + }; let res = execute(deps.as_mut(), mock_env(), info, update_config_msg).unwrap(); assert_eq!(res.attributes, vec![attr("method", "update_config"),]); diff --git a/contracts/treasury-handler/Cargo.toml b/contracts/treasury-handler/Cargo.toml index 8505ed1..7ba417c 100644 --- a/contracts/treasury-handler/Cargo.toml +++ b/contracts/treasury-handler/Cargo.toml @@ -23,12 +23,10 @@ library = [] [dependencies] cosmwasm-std = "1.0.0" -cosmwasm-storage = "1.0.0" cw-storage-plus = "0.13.2" cw2 = "0.13.2" schemars = "0.8.3" serde = { version = "1.0.127", default-features = false, features = ["derive"] } -thiserror = { version = "1.0.26" } getrandom = { version = "0.2", features = ["js"] } protocol-cosmwasm = { version = "0.1.0", path = "../../packages/protocol_cosmwasm"} diff --git a/contracts/treasury-handler/src/contract.rs b/contracts/treasury-handler/src/contract.rs index 7f29127..24e8852 100644 --- a/contracts/treasury-handler/src/contract.rs +++ b/contracts/treasury-handler/src/contract.rs @@ -132,7 +132,9 @@ fn execute_proposal( ) -> Result { // Parse the (proposal)`data`. let parsed_resource_id = element_encoder(&data[0..32]); - let base64_encoded_proposal = &data[32..]; + let _func_sig = &data[32..36]; + let _nonce = &data[36..40]; + let base64_encoded_proposal = &data[40..]; let bridge_addr = STATE.load(deps.storage)?.bridge_addr; diff --git a/contracts/treasury/Cargo.toml b/contracts/treasury/Cargo.toml index e7ef97e..c7043b5 100644 --- a/contracts/treasury/Cargo.toml +++ b/contracts/treasury/Cargo.toml @@ -23,16 +23,11 @@ library = [] [dependencies] cosmwasm-std = "1.0.0" -cosmwasm-storage = "1.0.0" cw-storage-plus = "0.13.4" -cw-utils = "0.13.4" cw2 = "0.13.4" cw20 = "0.13.4" -cw-controllers = "0.13.4" -cw20-base = "0.13.4" schemars = "0.8.3" serde = { version = "1.0.127", default-features = false, features = ["derive"] } -thiserror = { version = "1.0.26" } protocol-cosmwasm = { version = "0.1.0", path = "../../packages/protocol_cosmwasm"} diff --git a/contracts/treasury/src/contract.rs b/contracts/treasury/src/contract.rs index 1375f36..6eee833 100644 --- a/contracts/treasury/src/contract.rs +++ b/contracts/treasury/src/contract.rs @@ -6,11 +6,9 @@ use cosmwasm_std::{ }; use cw2::set_contract_version; -use cw20::{BalanceResponse, Cw20ExecuteMsg}; +use cw20::{BalanceResponse, Cw20ExecuteMsg, TokenInfoResponse}; use protocol_cosmwasm::error::ContractError; -use protocol_cosmwasm::treasury::{ - ConfigResponse, ExecuteMsg, InstantiateMsg, QueryMsg, TokenInfo, -}; +use protocol_cosmwasm::treasury::{ConfigResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; use crate::state::{Config, CONFIG}; @@ -49,11 +47,11 @@ pub fn execute( ) -> Result { match msg { ExecuteMsg::RescueTokens { - token_info, + token_address, to, amount_to_rescue, nonce, - } => rescue_tokens(deps, env, info, token_info, to, amount_to_rescue, nonce), + } => rescue_tokens(deps, env, info, token_address, to, amount_to_rescue, nonce), ExecuteMsg::SetHandler { handler, nonce } => set_handler(deps, env, info, handler, nonce), } } @@ -62,7 +60,7 @@ fn rescue_tokens( deps: DepsMut, env: Env, info: MessageInfo, - token_info: TokenInfo, + token_address: String, to: String, amount_to_rescue: Uint128, nonce: u32, @@ -86,48 +84,58 @@ fn rescue_tokens( } let mut msgs: Vec = vec![]; - match token_info { - TokenInfo::Native(denom) => { - let mut coin = deps - .querier - .query_balance(env.contract.address, &denom) - .unwrap_or(Coin { - denom, - amount: Uint128::zero(), - }); - if !coin.amount.is_zero() { - if coin.amount > amount_to_rescue { - coin.amount = amount_to_rescue; - } - msgs.push(CosmosMsg::Bank(BankMsg::Send { - to_address: to.to_string(), - amount: vec![coin], - })); - }; - } - TokenInfo::Cw20(token_addr) => { - let token_balance: BalanceResponse = deps.querier.query_wasm_smart( - token_addr.clone(), - &cw20::Cw20QueryMsg::Balance { - address: env.contract.address.to_string(), - }, - )?; - if !token_balance.balance.is_zero() { - let amount = if token_balance.balance > amount_to_rescue { - amount_to_rescue - } else { - token_balance.balance - }; - msgs.push(CosmosMsg::Wasm(WasmMsg::Execute { - contract_addr: token_addr, - msg: to_binary(&Cw20ExecuteMsg::Transfer { - recipient: to.to_string(), - amount, - }) - .unwrap(), - funds: vec![], - })); + + // Validate the `token_address`. + // Here, there are 2 possibilities for `token_address` - native token denom or CW20 token address. + // For the validation, we try to query the `TokenInfo` from `token_address`. + // If the `token_address` represents the native token denom, then the query returns error. + // Otherwise, the query returns the result, which means it is CW20 token. + let cw20_token_info_query: Result = deps + .querier + .query_wasm_smart(token_address.to_string(), &cw20::Cw20QueryMsg::TokenInfo {}); + + if cw20_token_info_query.is_err() { + // Handle the case of the `token_address` is the native token denomination. + let denom = token_address; + let mut coin = deps + .querier + .query_balance(env.contract.address.to_string(), &denom) + .unwrap_or(Coin { + denom, + amount: Uint128::zero(), + }); + if !coin.amount.is_zero() { + if coin.amount > amount_to_rescue { + coin.amount = amount_to_rescue; } + msgs.push(CosmosMsg::Bank(BankMsg::Send { + to_address: to.to_string(), + amount: vec![coin], + })); + } + } else { + // Handle the case of the `token_address` is the CW20 token address. + let token_balance: BalanceResponse = deps.querier.query_wasm_smart( + token_address.clone(), + &cw20::Cw20QueryMsg::Balance { + address: env.contract.address.to_string(), + }, + )?; + if !token_balance.balance.is_zero() { + let amount = if token_balance.balance > amount_to_rescue { + amount_to_rescue + } else { + token_balance.balance + }; + msgs.push(CosmosMsg::Wasm(WasmMsg::Execute { + contract_addr: token_address, + msg: to_binary(&Cw20ExecuteMsg::Transfer { + recipient: to.to_string(), + amount, + }) + .unwrap(), + funds: vec![], + })); } } diff --git a/contracts/treasury/src/tests.rs b/contracts/treasury/src/tests.rs index 50b5284..3fb7fbb 100644 --- a/contracts/treasury/src/tests.rs +++ b/contracts/treasury/src/tests.rs @@ -5,9 +5,7 @@ use cosmwasm_std::testing::{ use cosmwasm_std::{attr, coin, from_binary, Coin, OwnedDeps, Uint128}; use protocol_cosmwasm::error::ContractError; -use protocol_cosmwasm::treasury::{ - ConfigResponse, ExecuteMsg, InstantiateMsg, QueryMsg, TokenInfo, -}; +use protocol_cosmwasm::treasury::{ConfigResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; use crate::contract::{execute, instantiate, query}; @@ -109,7 +107,7 @@ fn test_treasury_rescue_tokens() { // Fails to "rescue tokens" since the caller is not "handler" let info = mock_info("anyone", &[]); let rescue_tokens_msg = ExecuteMsg::RescueTokens { - token_info: TokenInfo::Native("earth".to_string()), + token_address: "earth".to_string(), amount_to_rescue: Uint128::from(200_u128), to: to.clone(), nonce, @@ -120,7 +118,7 @@ fn test_treasury_rescue_tokens() { // Fails to "rescue tokens" if "nonce" is too big or small let info = mock_info(TREASURY_HANDLER, &[]); let rescue_tokens_msg = ExecuteMsg::RescueTokens { - token_info: TokenInfo::Native("earth".to_string()), + token_address: "earth".to_string(), amount_to_rescue: Uint128::from(200_u128), to: to.clone(), nonce: nonce + 2000, @@ -131,7 +129,7 @@ fn test_treasury_rescue_tokens() { // Fails to "rescue tokens" since "amount_to_rescue" is 0 let info = mock_info(TREASURY_HANDLER, &[]); let rescue_tokens_msg = ExecuteMsg::RescueTokens { - token_info: TokenInfo::Native("earth".to_string()), + token_address: "earth".to_string(), amount_to_rescue: Uint128::zero(), to: to.clone(), nonce, @@ -146,7 +144,7 @@ fn test_treasury_rescue_tokens() { // But, it does not send any tokens let info = mock_info(TREASURY_HANDLER, &[]); let rescue_tokens_msg = ExecuteMsg::RescueTokens { - token_info: TokenInfo::Native("sun".to_string()), + token_address: "sun".to_string(), amount_to_rescue: Uint128::from(100_u128), to: to.clone(), nonce, @@ -157,7 +155,7 @@ fn test_treasury_rescue_tokens() { // Succeed to "rescue tokens" let info = mock_info(TREASURY_HANDLER, &[]); let rescue_token_msg = ExecuteMsg::RescueTokens { - token_info: TokenInfo::Native("earth".to_string()), + token_address: "earth".to_string(), amount_to_rescue: Uint128::from(200_u128), nonce: nonce + 1, to, diff --git a/contracts/vanchor/Cargo.toml b/contracts/vanchor/Cargo.toml index 7f7bcd9..56d6bcd 100644 --- a/contracts/vanchor/Cargo.toml +++ b/contracts/vanchor/Cargo.toml @@ -24,13 +24,11 @@ library = [] [dependencies] cosmwasm-std = "1.0.0" -cosmwasm-storage = "1.0.0" cw-storage-plus = "0.13.2" cw2 = "0.13.2" cw20 = "0.13.2" schemars = "0.8.3" serde = { version = "1.0.127", default-features = false, features = ["derive"] } -thiserror = { version = "1.0.26" } hex = "0.4" ark-ff = { version = "^0.3.0", default-features = false } diff --git a/packages/protocol_cosmwasm/Cargo.toml b/packages/protocol_cosmwasm/Cargo.toml index f551afd..755fd8d 100644 --- a/packages/protocol_cosmwasm/Cargo.toml +++ b/packages/protocol_cosmwasm/Cargo.toml @@ -14,7 +14,6 @@ backtraces = ["cosmwasm-std/backtraces"] cw20 = "0.13.4" cw20-base = { version = "0.13.4", features = ["library"] } cosmwasm-std = "1.0.0" -cosmwasm-storage = "1.0.0" schemars = "0.8.1" serde = { version = "1.0.103", default-features = false, features = ["derive"] } thiserror = { version = "1.0.26" } diff --git a/packages/protocol_cosmwasm/src/signature_bridge.rs b/packages/protocol_cosmwasm/src/signature_bridge.rs index cb5d29b..29d49a5 100644 --- a/packages/protocol_cosmwasm/src/signature_bridge.rs +++ b/packages/protocol_cosmwasm/src/signature_bridge.rs @@ -19,13 +19,19 @@ pub enum ExecuteMsg { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] pub struct SetResourceWithSigMsg { + pub data: Vec, // base64-encoded `ResourceIdUpdateData` + pub sig: Vec, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub struct ResourceIdUpdateData { pub resource_id: [u8; 32], pub function_sig: [u8; 4], pub nonce: u32, pub new_resource_id: [u8; 32], pub handler_addr: String, pub execution_context_addr: String, - pub sig: Vec, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] diff --git a/packages/protocol_cosmwasm/src/token_wrapper.rs b/packages/protocol_cosmwasm/src/token_wrapper.rs index 7c54909..0db1a79 100644 --- a/packages/protocol_cosmwasm/src/token_wrapper.rs +++ b/packages/protocol_cosmwasm/src/token_wrapper.rs @@ -52,8 +52,20 @@ pub enum ExecuteMsg { /* ----------------------------------- */ /* --- Governance functionality --- */ - /// Reset the config - UpdateConfig(UpdateConfigMsg), + /// Update the `governor` + ConfigureGovernor { governor: Option }, + + /// Update the `is_native_allowed` + ConfigureNativeAllowed { is_native_allowed: Option }, + + /// Update the `wrapping_limit` + ConfigureWrappingLimit { wrapping_limit: Option }, + + /// Update the `fee_recipient` + ConfigureFeeRecipient { fee_recipient: Option }, + + /// Update the `fee_percentage` + ConfigureFeePercentage { fee_percentage: Option }, /// Add cw20 token address to wrapping list AddCw20TokenAddr { token: String, nonce: u64 }, @@ -140,16 +152,6 @@ pub enum Cw20HookMsg { }, } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] -#[serde(rename_all = "snake_case")] -pub struct UpdateConfigMsg { - pub governor: Option, - pub is_native_allowed: Option, - pub wrapping_limit: Option, - pub fee_percentage: Option, - pub fee_recipient: Option, -} - #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] pub struct ConfigResponse { diff --git a/packages/protocol_cosmwasm/src/treasury.rs b/packages/protocol_cosmwasm/src/treasury.rs index bbb89ab..cb2a375 100644 --- a/packages/protocol_cosmwasm/src/treasury.rs +++ b/packages/protocol_cosmwasm/src/treasury.rs @@ -14,7 +14,7 @@ pub struct InstantiateMsg { pub enum ExecuteMsg { /// Send the (native or cw20) tokens to destination address RescueTokens { - token_info: TokenInfo, + token_address: String, to: String, amount_to_rescue: Uint128, nonce: u32, @@ -37,10 +37,3 @@ pub struct ConfigResponse { pub treasury_handler: String, pub proposal_nonce: String, } - -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] -#[serde(rename_all = "snake_case")] -pub enum TokenInfo { - Native(String), - Cw20(String), -} diff --git a/test-scripts/src/processes/tests/signatureBridge.ts b/test-scripts/src/processes/tests/signatureBridge.ts index fa204ad..b3b4302 100644 --- a/test-scripts/src/processes/tests/signatureBridge.ts +++ b/test-scripts/src/processes/tests/signatureBridge.ts @@ -67,27 +67,24 @@ export async function testSignatureBridgeAdminSetResWithSignature( const handler_addr = localjuno.contracts.anchorHandler; const execution_context_addr = localjuno.contracts.anchor; - const unsignedData = Buffer.concat([ - resource_id, - function_sig, - nonce_buf, - new_resource_id, - Buffer.from(handler_addr), - Buffer.from(execution_context_addr), - ]); + const data = Buffer.from(toEncodedBinary( + { + resource_id: Array.from(resource_id), + function_sig: Array.from(function_sig), + nonce: nonce, + new_resource_id: Array.from(new_resource_id), + handler_addr: handler_addr, + execution_context_addr: execution_context_addr, + } + ), 'base64'); const privkey = localjuno.contractsConsts.testPrivKey; - const sig = signMessage(privkey, Array.from(unsignedData)); + const sig = signMessage(privkey, Array.from(data)); const sigLen = sig.length; const result = await junod.execute(localjuno.addresses.wallet1, signatureBridge, { admin_set_resource_with_sig: { - resource_id: Array.from(resource_id), - function_sig: Array.from(function_sig), - nonce: nonce, - new_resource_id: Array.from(new_resource_id), - handler_addr: handler_addr, - execution_context_addr: execution_context_addr, + data: Array.from(data), sig: Array.from(Buffer.from(sig.substring(2, sigLen - 2), 'hex')), }, }, diff --git a/test-scripts/src/processes/tests/testnet.ts b/test-scripts/src/processes/tests/testnet.ts index 666fc85..579cc91 100644 --- a/test-scripts/src/processes/tests/testnet.ts +++ b/test-scripts/src/processes/tests/testnet.ts @@ -65,7 +65,7 @@ export async function testExecute( console.log(chalk.yellow("\nStep 3. Running Tests")); // // SignatureBridge // await testSignatureBridgeInitialize(junod, signatureBridge); - // await testSignatureBridgeAdminSetResWithSignature(junod, wallet1, signatureBridge); + await testSignatureBridgeAdminSetResWithSignature(junod, wallet1, signatureBridge); // await testSignatureBridgeExecProposalWithSignature(junod, wallet1, signatureBridge); // // TokenWrapper @@ -97,7 +97,7 @@ export async function testExecute( // await testMixerWithdrawNativeToken(junod, mixer, wallet1, wallet2, wallet3, "1000000"); // Treasury - await testTreasuryInitialize(junod, treasury); + // await testTreasuryInitialize(junod, treasury); process.exit(); }