Skip to content

Commit

Permalink
Update some contracts for cosmwasm-proposal (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
duguorong009 authored Jul 18, 2022
1 parent 3e7d0ac commit 924d828
Show file tree
Hide file tree
Showing 25 changed files with 169 additions and 212 deletions.
47 changes: 0 additions & 47 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions contracts/anchor-handler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
4 changes: 3 additions & 1 deletion contracts/anchor-handler/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ fn execute_proposal(
) -> Result<Response, ContractError> {
// 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;

Expand Down
2 changes: 0 additions & 2 deletions contracts/anchor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion contracts/mixer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions contracts/signature-bridge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
41 changes: 21 additions & 20 deletions contracts/signature-bridge/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#[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;

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,
Expand Down Expand Up @@ -85,42 +85,43 @@ fn admin_set_resource_with_signature(
) -> Result<Response, ContractError> {
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<u8> = 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<CosmosMsg> = 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(),
})];
Expand Down
2 changes: 0 additions & 2 deletions contracts/tokenwrapper-handler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
4 changes: 3 additions & 1 deletion contracts/tokenwrapper-handler/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ fn execute_proposal(
) -> Result<Response, ContractError> {
// 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;

Expand Down
12 changes: 3 additions & 9 deletions contracts/tokenwrapper-handler/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions contracts/tokenwrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down
Loading

0 comments on commit 924d828

Please sign in to comment.