Skip to content

Commit

Permalink
fix: add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrizsabin committed Oct 4, 2024
1 parent 5859966 commit 7821c76
Show file tree
Hide file tree
Showing 10 changed files with 619 additions and 21 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion contracts/cosmwasm-vm/cw-intents-v1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ common ={ git = "https://github.com/icon-project/IBC-Integration.git",branch="ma
serde-json-wasm = {workspace=true}
hex="*"
cw20="*"
cw20-base="*"

[dev-dependencies]
cosmwasm = "0.7.2"
cw-multi-test = "0.16.2"
cw-multi-test = "*"
getrandom = {version = "0.2", default-features = false, features = ["custom"]}

35 changes: 24 additions & 11 deletions contracts/cosmwasm-vm/cw-intents-v1/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use common::{
};
use cosmwasm_std::{to_binary, Addr, BankMsg, Coin};
use cw20::Cw20ExecuteMsg;
use events::create_send_message_event;
use events::{create_order_fill_event, create_send_message_event};

use super::*;

Expand All @@ -27,6 +27,8 @@ impl<'a> CwIntentV1Service<'a> {
self.set_nid(deps.storage, msg.nid)?;
self.set_fee_handler(deps.storage, deps.api.addr_validate(&msg.fee_handler)?)?;
self.set_deposit_id(deps.storage, 0)?;
let relayer = deps.api.addr_validate(&msg.relayer)?;
self.set_relayer(deps.storage, relayer)?;

Ok(Response::new())
}
Expand Down Expand Up @@ -54,15 +56,13 @@ impl<'a> CwIntentV1Service<'a> {
env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
let stored = self.get_order(deps.storage, order.id)?;
let self_nid = self.get_nid(deps.storage)?;

if order.dst_nid != self_nid {
return Err(ContractError::InvalidDstNId);
}
let order_bytes = order.rlp_bytes();
let order_hash = keccak256(&order_bytes);
if keccak256(&stored.rlp_bytes()) != order_hash {
return Err(ContractError::DecodeError {
error: "Hash Mismatch".to_string(),
});
}

if self.is_order_finished(deps.storage, &order_hash) {
return Err(ContractError::OrderAlreadyComplete);
}
Expand Down Expand Up @@ -117,6 +117,9 @@ impl<'a> CwIntentV1Service<'a> {
};
let mut response = Response::new();

let order_fill_event =
create_order_fill_event(&order_fill, remaining_amount, fee, fill_amount);

if order.src_nid == order.dst_nid {
response = self.resolve_fill(deps, env, order.src_nid, order_fill)?;
} else {
Expand All @@ -128,7 +131,12 @@ impl<'a> CwIntentV1Service<'a> {
let event = create_send_message_event(order.src_nid, sn, msg.rlp_bytes().to_vec());
response = response.add_event(event);
}
response = response.add_messages(vec![fee_transfer, receiver_transfer]);

response = response.add_message(receiver_transfer);
if fee > 0 {
response = response.add_message(fee_transfer);
}
response = response.add_event(order_fill_event);
Ok(response)
}

Expand All @@ -141,6 +149,11 @@ impl<'a> CwIntentV1Service<'a> {
conn_sn: u128,
order_msg: OrderMsg,
) -> Result<Response, ContractError> {
let relayer = self.get_relayer(deps.storage)?;
if info.sender != relayer {
return Err(ContractError::Unauthorized {});
}

if self.have_received(deps.storage, (src_network.clone(), conn_sn)) {
return Err(ContractError::MessageAlreadyReceived);
}
Expand Down Expand Up @@ -321,9 +334,9 @@ impl<'a> CwIntentV1Service<'a> {

pub fn is_native(&self, deps: Deps, denom: &String) -> bool {
if let Some(addr) = deps.api.addr_validate(denom).ok() {
return self.is_contract(deps, &addr);
return !self.is_contract(deps, &addr);
}
return false;
return true;
}

pub fn get_next_deposit_id(&self, storage: &mut dyn Storage) -> StdResult<u128> {
Expand Down
2 changes: 2 additions & 0 deletions contracts/cosmwasm-vm/cw-intents-v1/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ pub enum ContractError {
InvalidMessageType,
#[error("MessageAlreadyReceived")]
MessageAlreadyReceived,
#[error("InvalidDstNId")]
InvalidDstNId,
}
12 changes: 10 additions & 2 deletions contracts/cosmwasm-vm/cw-intents-v1/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ pub fn create_swap_order_event(order: &SwapOrder) -> Event {
.add_attribute("token", order.token.to_string())
}

pub fn create_order_fill_event(fill: &OrderFill) -> Event {
pub fn create_order_fill_event(
fill: &OrderFill,
remaining_amount: u128,
fee: u128,
fill_amount: u128,
) -> Event {
Event::new("OrderFill")
.add_attribute("id", fill.id.to_string())
.add_attribute("amount", fill.amount.to_string())
.add_attribute("payout", fill.amount.to_string())
.add_attribute("solver_address", fill.solver_address.to_string())
.add_attribute("order_bytes", hex::encode(&fill.order_bytes))
.add_attribute("closed", fill.closed.to_string())
.add_attribute("remaining_amount", remaining_amount.to_string())
.add_attribute("fee", fee.to_string())
.add_attribute("fill_amount", fill_amount.to_string())
}

pub fn create_send_message_event(nid: String, conn_sn: u128, msg: Vec<u8>) -> Event {
Expand Down
5 changes: 4 additions & 1 deletion contracts/cosmwasm-vm/cw-intents-v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use cosmwasm_std::{
use cw2::set_contract_version;
use cw_storage_plus::{Item, Map};
pub use errors::*;
use msg::{ExecuteMsg, QueryMsg};
use msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use state::CwIntentV1Service;
use thiserror::Error;
pub use types::*;
Expand Down Expand Up @@ -119,5 +119,8 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
to_json_binary(&call_service.get_order(deps.storage, id).unwrap())
}
QueryMsg::GetDepositId {} => to_json_binary(&call_service.get_deposit_id(deps.storage)),
QueryMsg::GetFeeHandler {} => {
to_json_binary(&call_service.get_fee_handler(deps.storage).unwrap())
}
}
}
10 changes: 10 additions & 0 deletions contracts/cosmwasm-vm/cw-intents-v1/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use cosmwasm_schema::{cw_serde, QueryResponses};

#[cw_serde]
pub struct InstantiateMsg {
pub fee_handler: String,
pub nid: String,
pub fee: u8,
pub relayer: String,
}

#[cw_serde]
pub enum ExecuteMsg {
Swap {
Expand Down Expand Up @@ -44,4 +52,6 @@ pub enum QueryMsg {

#[returns(u64)]
GetDepositId {},
#[returns(String)]
GetFeeHandler {},
}
10 changes: 10 additions & 0 deletions contracts/cosmwasm-vm/cw-intents-v1/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct CwIntentV1Service<'a> {
finished_orders: Map<'a, Vec<u8>, bool>,
conn_sn: Item<'a, u128>,
receipts: Map<'a, (String, u128), bool>,
relayer: Item<'a, Addr>,
}

impl<'a> Default for CwIntentV1Service<'a> {
Expand All @@ -34,13 +35,18 @@ impl<'a> CwIntentV1Service<'a> {
finished_orders: Map::new(StorageKey::FinishedOrders.as_str()),
conn_sn: Item::new(StorageKey::ConnectionSN.as_str()),
receipts: Map::new(StorageKey::Receipts.as_str()),
relayer: Item::new(StorageKey::Relayer.as_str()),
}
}

pub fn get_deposit_id(&self, storage: &dyn Storage) -> u128 {
self.deposit_id.load(storage).unwrap_or(0)
}

pub fn get_relayer(&self, storage: &dyn Storage) -> StdResult<Addr> {
self.relayer.load(storage)
}

pub fn get_nid(&self, storage: &dyn Storage) -> StdResult<String> {
self.nid.load(storage)
}
Expand Down Expand Up @@ -80,6 +86,10 @@ impl<'a> CwIntentV1Service<'a> {
self.deposit_id.save(storage, &value)
}

pub fn set_relayer(&self, storage: &mut dyn Storage, value: Addr) -> StdResult<()> {
self.relayer.save(storage, &value)
}

pub fn set_conn_sn(&self, storage: &mut dyn Storage, value: u128) -> StdResult<()> {
self.conn_sn.save(storage, &value)
}
Expand Down
8 changes: 2 additions & 6 deletions contracts/cosmwasm-vm/cw-intents-v1/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ use serde::Serialize;

pub const ORDER_FILL: u8 = 1;
pub const ORDER_CANCEL: u8 = 2;
#[cw_serde]
pub struct InstantiateMsg {
pub fee_handler: String,
pub nid: String,
pub fee: u8,
}

#[cw_serde]
pub enum StorageKey {
Expand All @@ -25,6 +19,7 @@ pub enum StorageKey {
FinishedOrders,
ConnectionSN,
Receipts,
Relayer,
}

impl StorageKey {
Expand All @@ -40,6 +35,7 @@ impl StorageKey {
StorageKey::FinishedOrders => "finished_orders",
StorageKey::ConnectionSN => "conn_sn",
StorageKey::Receipts => "receipts",
StorageKey::Relayer => "relayer",
}
}
}
Expand Down
Loading

0 comments on commit 7821c76

Please sign in to comment.