Skip to content

Commit

Permalink
Add integration tests(1) (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
duguorong009 authored May 27, 2022
1 parent cfc3b0f commit 197c16e
Show file tree
Hide file tree
Showing 46 changed files with 3,877 additions and 958 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ Cargo.lock

/artifacts

node_modules
node_modules

test-scripts/src/config/localjunoConstants.ts
test-scripts/src/config/wasmPaths.ts
test-scripts/src/config/wasms/*.wasm

.DS_Store
4 changes: 2 additions & 2 deletions contracts/anchor-handler/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use cosmwasm_std::{Addr, StdResult, Storage};
use cw_storage_plus::{Item, Map};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct UpdateRecord {
pub token_addr: Addr,
pub src_chain_id: u64,
Expand All @@ -13,7 +13,7 @@ pub struct UpdateRecord {
pub leaf_id: u64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct State {
pub bridge_addr: Addr,
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/anchor/src/mock_querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use protocol_cosmwasm::token_wrapper::{
ConfigResponse as TokenWrapperConfigResponse, GetAmountToWrapResponse,
};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
Config {},
Expand Down
6 changes: 3 additions & 3 deletions contracts/anchor/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub const NULLIFIERS: Map<Vec<u8>, bool> = Map::new("used_nullifers");
// NOTE: The `chain_id` field is just for temporary development purpose.
// In the future, it should be removed & the contract should use the
// `chain_id`(blockchain-unique ID) obtained inside the contract.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct Anchor {
pub chain_id: u64, // ChainID of underlying blockchain(Temporary field)
pub handler: Addr, // Address of "anchor-handler", which add/updte the `edge` info
Expand Down Expand Up @@ -78,7 +78,7 @@ pub fn save_neighbor_roots(
}

// LinkableMerkleTree
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct LinkableMerkleTree {
pub max_edges: u32,
pub chain_id_list: Vec<ChainId>,
Expand Down Expand Up @@ -199,7 +199,7 @@ impl LinkableMerkleTree {
}

// MerkleTree
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct MerkleTree {
pub levels: u32,
pub current_root_index: u32,
Expand Down
4 changes: 2 additions & 2 deletions contracts/mixer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use protocol_cosmwasm::structs::ROOT_HISTORY_SIZE;
use protocol_cosmwasm::zeroes;

// Mixer
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct Mixer {
pub deposit_size: Uint128,
pub cw20_address: Option<Addr>,
Expand All @@ -20,7 +20,7 @@ pub struct Mixer {
}

// MerkleTree
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct MerkleTree {
pub levels: u32,
pub current_root_index: u32,
Expand Down
2 changes: 1 addition & 1 deletion contracts/signature-bridge/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use cosmwasm_std::Addr;
use cw_storage_plus::{Item, Map};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct State {
pub governor: Addr,
pub proposal_nonce: u32,
Expand Down
4 changes: 2 additions & 2 deletions contracts/tokenwrapper-handler/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use cosmwasm_std::{Addr, StdResult, Storage};
use cw_storage_plus::{Item, Map};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct UpdateRecord {
pub tokenwrapper_addr: Addr,
pub exec_chain_id: u64,
Expand All @@ -13,7 +13,7 @@ pub struct UpdateRecord {
pub update_value: [u8; 32],
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct State {
pub bridge_addr: Addr,
}
Expand Down
37 changes: 7 additions & 30 deletions contracts/tokenwrapper/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use protocol_cosmwasm::token_wrapper::{
InstantiateMsg, QueryMsg, UpdateConfigMsg,
};

use crate::state::{Config, Supply, CONFIG, HISTORICAL_TOKENS, TOKENS, TOTAL_SUPPLY};
use crate::state::{Config, CONFIG, HISTORICAL_TOKENS, TOKENS};
use crate::utils::{
calc_fee_perc_from_string, get_amount_to_wrap, get_fee_from_amount, is_valid_address,
is_valid_unwrap_amount, is_valid_wrap_amount, parse_string_to_uint128,
Expand Down Expand Up @@ -56,10 +56,6 @@ pub fn instantiate(
};
TOKEN_INFO.save(deps.storage, &data)?;

// set supply to 0
let supply = Supply::default();
TOTAL_SUPPLY.save(deps.storage, &supply)?;

// set config
let governor = match msg.governor {
Some(v) => deps.api.addr_validate(v.as_str())?,
Expand Down Expand Up @@ -203,11 +199,6 @@ fn wrap_native(
get_fee_from_amount(wrapping_amount, config.fee_percentage.numerator().u128());
let left_over = wrapping_amount - cost_to_wrap;

// Save the wrapped token amount.
let mut supply = TOTAL_SUPPLY.load(deps.storage)?;
supply.issued += left_over;
TOTAL_SUPPLY.save(deps.storage, &supply)?;

// call into cw20-base to mint the token, call as self as no one else is allowed
let sub_info = MessageInfo {
sender: env.contract.address.clone(),
Expand Down Expand Up @@ -257,6 +248,7 @@ fn unwrap_native(
amount: Uint128,
recipient: Option<String>,
) -> Result<Response, ContractError> {
let sender = sender.unwrap_or_else(|| info.sender.to_string());
let config = CONFIG.load(deps.storage)?;
// Validate the "is_native_allowed"
if !config.is_native_allowed {
Expand All @@ -266,19 +258,13 @@ fn unwrap_native(
}

// Validate the "amount"
if !is_valid_unwrap_amount(deps.branch(), info.clone(), amount) {
if !is_valid_unwrap_amount(deps.branch(), &sender, amount) {
return Err(ContractError::Std(StdError::GenericErr {
msg: "Insufficient native token balance".to_string(),
msg: format!("Insufficient native token balance for sender({})", &sender),
}));
}

// Calculate the remainder
let total_supply = TOTAL_SUPPLY.load(deps.storage)?;
let remainder = total_supply.issued - amount;
TOTAL_SUPPLY.save(deps.storage, &Supply { issued: remainder })?;

// burn from the "sender"
let sender = sender.unwrap_or_else(|| info.sender.to_string());
let sub_info = MessageInfo {
sender: deps.api.addr_validate(sender.as_str())?,
funds: vec![],
Expand Down Expand Up @@ -311,6 +297,8 @@ fn unwrap_cw20(
amount: Uint128,
recipient: Option<String>,
) -> Result<Response, ContractError> {
let sender = sender.unwrap_or_else(|| info.sender.to_string());

// Validate the "token" address
let is_valid_unwrap_address = HISTORICAL_TOKENS.has(deps.storage, token.clone());
if !is_valid_unwrap_address {
Expand All @@ -320,19 +308,13 @@ fn unwrap_cw20(
}

// Validate the "token" amount
if !is_valid_unwrap_amount(deps.branch(), info.clone(), amount) {
if !is_valid_unwrap_amount(deps.branch(), &sender, amount) {
return Err(ContractError::Std(StdError::GenericErr {
msg: "Insufficient cw20 token amount".to_string(),
}));
}

// Calculate the remainder
let total_supply = TOTAL_SUPPLY.load(deps.storage)?;
let remainder = total_supply.issued - amount;
TOTAL_SUPPLY.save(deps.storage, &Supply { issued: remainder })?;

// burn from the "sender"
let sender = sender.unwrap_or_else(|| info.sender.to_string());
let sub_info = MessageInfo {
sender: deps.api.addr_validate(sender.as_str())?,
funds: vec![],
Expand Down Expand Up @@ -391,11 +373,6 @@ fn wrap_cw20(

match from_binary(&cw20_msg.msg) {
Ok(Cw20HookMsg::Wrap { sender, recipient }) => {
// Save the wrapping number
let mut supply = TOTAL_SUPPLY.load(deps.storage)?;
supply.issued += left_over;
TOTAL_SUPPLY.save(deps.storage, &supply)?;

// call into cw20-base to mint the token, call as self as no one else is allowed
let sub_info = MessageInfo {
sender: env.contract.address.clone(),
Expand Down
12 changes: 1 addition & 11 deletions contracts/tokenwrapper/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@ use serde::{Deserialize, Serialize};
use cosmwasm_std::{Addr, Decimal, Uint128};
use cw_storage_plus::{Item, Map};

/// Supply is dynamic and tracks the current supply of staked and ERC20 tokens.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema, Default)]
pub struct Supply {
/// issued is how many wrapped tokens this contract has issued
pub issued: Uint128,
}

/// Config
/// Governance - related params
#[derive(Serialize, Deserialize, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, JsonSchema)]
pub struct Config {
pub governor: Addr,
pub native_token_denom: String,
Expand All @@ -24,9 +17,6 @@ pub struct Config {
pub proposal_nonce: u64,
}

/// Normal Cw20 - related
pub const TOTAL_SUPPLY: Item<Supply> = Item::new("total_supply");

/// Governance - related
pub const CONFIG: Item<Config> = Item::new("config");

Expand Down
12 changes: 6 additions & 6 deletions contracts/tokenwrapper/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use cosmwasm_std::{Addr, Decimal, DepsMut, Fraction, MessageInfo, StdError, Uint128};
use cosmwasm_std::{Addr, Decimal, DepsMut, Fraction, StdError, Uint128};
use cw20::BalanceResponse;
use cw20_base::contract::query_balance;
use cw20_base::contract::{query_balance, query_token_info};
use protocol_cosmwasm::error::ContractError;

use crate::state::{CONFIG, TOKENS, TOTAL_SUPPLY};
use crate::state::{CONFIG, TOKENS};

// Check if the cw20 token address is valid in "TOKENS".
pub fn is_valid_address(deps: DepsMut, token_address: Addr) -> bool {
Expand All @@ -12,16 +12,16 @@ pub fn is_valid_address(deps: DepsMut, token_address: Addr) -> bool {

// Check if the "wrap_amount" is valid.
pub fn is_valid_wrap_amount(deps: DepsMut, amount: Uint128) -> bool {
let total_supply = TOTAL_SUPPLY.load(deps.storage).unwrap().issued;
let total_supply = query_token_info(deps.as_ref()).unwrap().total_supply;
let config = CONFIG.load(deps.storage).unwrap();
amount
.saturating_add(total_supply)
.le(&config.wrapping_limit)
}

// Check if the "unwrap_amount" is valid.
pub fn is_valid_unwrap_amount(deps: DepsMut, info: MessageInfo, amount: Uint128) -> bool {
let sender_token_balance = query_balance(deps.as_ref(), info.sender.to_string())
pub fn is_valid_unwrap_amount(deps: DepsMut, sender: &str, amount: Uint128) -> bool {
let sender_token_balance = query_balance(deps.as_ref(), sender.to_string())
.unwrap_or(BalanceResponse {
balance: Uint128::zero(),
})
Expand Down
Binary file modified contracts/tokenwrapper/tests/cosmwasm_tokenwrapper.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion contracts/vanchor/src/mock_querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use protocol_cosmwasm::token_wrapper::{
ConfigResponse as TokenWrapperConfigResponse, GetAmountToWrapResponse,
};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
Config {},
Expand Down
6 changes: 3 additions & 3 deletions contracts/vanchor/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn save_neighbor_roots(
}

// LinkableMerkleTree
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct LinkableMerkleTree {
pub max_edges: u32,
pub chain_id_list: Vec<ChainId>,
Expand Down Expand Up @@ -177,7 +177,7 @@ impl LinkableMerkleTree {
}

// VAnchor: (TODO: Add the description)
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct VAnchor {
pub chain_id: u64,
pub creator: Addr,
Expand Down Expand Up @@ -207,7 +207,7 @@ pub const VERIFIER_2_2: Item<VAnchorVerifier> = Item::new("vanchor_verifier_2_2"
pub const VERIFIER_16_2: Item<VAnchorVerifier> = Item::new("vanchor_verifier_16_2");

// MerkleTree
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct MerkleTree {
pub levels: u32,
pub current_root_index: u32,
Expand Down
12 changes: 6 additions & 6 deletions packages/protocol_cosmwasm/src/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cw20::Cw20ReceiveMsg;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct InstantiateMsg {
pub max_edges: u32,
pub chain_id: u64,
Expand Down Expand Up @@ -54,7 +54,7 @@ pub enum ExecuteMsg {
},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum Cw20HookMsg {
/// Depcosit Cw20 tokens
Expand All @@ -72,7 +72,7 @@ pub enum Cw20HookMsg {
},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct WithdrawMsg {
pub proof_bytes: Vec<u8>,
pub roots: Vec<[u8; 32]>,
Expand All @@ -85,7 +85,7 @@ pub struct WithdrawMsg {
pub cw20_address: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
Config {},
Expand All @@ -95,7 +95,7 @@ pub enum QueryMsg {
MerkleRootInfo { id: u32 },
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct ConfigResponse {
pub handler: String,
pub proposal_nonce: u32,
Expand All @@ -104,5 +104,5 @@ pub struct ConfigResponse {
pub deposit_size: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct MigrateMsg {}
Loading

0 comments on commit 197c16e

Please sign in to comment.