From fb18bddb57db6b6e44acc171d5e47c8aeee4b3c4 Mon Sep 17 00:00:00 2001 From: Amit Yadav Date: Wed, 23 Aug 2023 12:32:09 +0530 Subject: [PATCH 1/3] Add checks, update timestamps --- contracts/ics100/src/.DS_Store | Bin 0 -> 6148 bytes contracts/ics100/src/atomic_swap_handler.rs | 9 +++- contracts/ics100/src/contract.rs | 50 +++++++++++++++----- contracts/ics100/src/state.rs | 2 + 4 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 contracts/ics100/src/.DS_Store diff --git a/contracts/ics100/src/.DS_Store b/contracts/ics100/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a50e356a02bf9fdf2a6134be417452e86f80b31e GIT binary patch literal 6148 zcmeHK!A=4(5Pj8+*~o>H@wi{$${%dVdNW>#UUdZ$O+W~{#CY3J_sw)Ll7#~oBxZ(8 zUpv!jr*G4i0tmzPWB~L5q*TG=lEn@Yzi3C!%yK~tI>tN3ct(v+RGr>#a0Z-#Q)58( z?iF`yg9YaJy?>Y7$#3+0V1gN@SmB0RFPqoIa^O0V}>>BQ(AMB zI&Q_pwD!VIk`4?Yl*X)~|J3eOXavBWq&K5~~5xsNzyG&`yyXly1OJMFkd&ix$R*j`+F6|3wHft=DkA;bh#JDe hu4AppReViVp)ZpPu`p~S(n9e+0!o7y&cIO__y%p(hRy&0 literal 0 HcmV?d00001 diff --git a/contracts/ics100/src/atomic_swap_handler.rs b/contracts/ics100/src/atomic_swap_handler.rs index aade890..ce03c63 100644 --- a/contracts/ics100/src/atomic_swap_handler.rs +++ b/contracts/ics100/src/atomic_swap_handler.rs @@ -65,7 +65,7 @@ pub(crate) fn do_ibc_packet_receive( pub(crate) fn on_received_make( deps: DepsMut, - _env: Env, + env: Env, packet: &IbcPacket, msg: MakeSwapMsg, ) -> Result { @@ -86,6 +86,7 @@ pub(crate) fn on_received_make( cancel_timestamp: None, complete_timestamp: None, path: path.clone(), + create_timestamp: env.block.time.seconds() }; let count_check = ORDER_TO_COUNT.may_load(deps.storage, &order_id)?; @@ -97,6 +98,7 @@ pub(crate) fn on_received_make( let res = IbcReceiveResponse::new() .set_ack(ack_success()) + .add_attribute("order_id", order_id) .add_attribute("action", "receive") .add_attribute("success", "true") .add_attribute("action", "make_swap_received"); @@ -142,6 +144,7 @@ pub(crate) fn on_received_take( let res = IbcReceiveResponse::new() .set_ack(ack_success()) .add_submessages(submsg) + .add_attribute("order_id", order_id) .add_attribute("action", "receive") .add_attribute("success", "true"); @@ -175,6 +178,7 @@ pub(crate) fn on_received_cancel( let res = IbcReceiveResponse::new() .set_ack(ack_success()) + .add_attribute("order_id", order_id) .add_attribute("action", "receive") .add_attribute("success", "true"); @@ -185,7 +189,7 @@ pub(crate) fn on_received_cancel( pub(crate) fn on_packet_success( deps: DepsMut, packet: IbcPacket, - env: Env + env: Env, ) -> Result { let packet_data: AtomicSwapPacketData = from_binary(&packet.data)?; @@ -217,6 +221,7 @@ pub(crate) fn on_packet_success( taker: None, cancel_timestamp: None, complete_timestamp: None, + create_timestamp: env.block.time.seconds() }; append_atomic_order(deps.storage, &order_id, &new_order)?; diff --git a/contracts/ics100/src/contract.rs b/contracts/ics100/src/contract.rs index 59fdb72..87fea43 100644 --- a/contracts/ics100/src/contract.rs +++ b/contracts/ics100/src/contract.rs @@ -2,7 +2,7 @@ use cosmwasm_std::entry_point; use cosmwasm_std::{ to_binary, Binary, Deps, DepsMut, Env, IbcMsg, IbcTimeout, MessageInfo, Order, Response, - StdResult, Timestamp, StdError, + StdResult, StdError, }; use cw2::set_contract_version; @@ -16,7 +16,7 @@ use crate::state::{ AtomicSwapOrder, Status, // CHANNEL_INFO, - SWAP_ORDERS, set_atomic_order, get_atomic_order, COUNT, + SWAP_ORDERS, set_atomic_order, get_atomic_order, COUNT, move_order_to_bottom, }; use crate::utils::extract_source_channel_for_taker_msg; use cw_storage_plus::Bound; @@ -24,6 +24,7 @@ use cw_storage_plus::Bound; // Version info, for migration info const CONTRACT_NAME: &str = "ics100-swap"; const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); +const DEFAULT_TIMEOUT_TIMESTAMP_OFFSET: u64 = 600; #[cfg_attr(not(feature = "library"), entry_point)] pub fn instantiate( @@ -48,6 +49,16 @@ pub fn execute( ExecuteMsg::MakeSwap(msg) => execute_make_swap(deps, env, info, msg), ExecuteMsg::TakeSwap(msg) => execute_take_swap(deps, env, info, msg), ExecuteMsg::CancelSwap(msg) => execute_cancel_swap(deps, env, info, msg), + // MakeBid, + // - User will bid on make orders + // - Once make is close, delete user bid data structure + // TakeBid + // - User can choose to TakeBid + // - Once make is close delete user bid data structure by returning amounts of all bids + // or maybe let user reclaim their amounts + // - Reclaim amounts ? OR Integrate in make done pool ? + // - integrating can result in more state updates and refund amounts. + } } @@ -55,7 +66,7 @@ pub fn execute( // This is the step 1 (Create order & Lock Token) of the atomic swap: https://github.com/cosmos/ibc/tree/main/spec/app/ics-100-atomic-swap pub fn execute_make_swap( _deps: DepsMut, - _env: Env, + env: Env, info: MessageInfo, msg: MakeSwapMsg, ) -> Result { @@ -84,8 +95,11 @@ pub fn execute_make_swap( let ibc_msg = IbcMsg::SendPacket { channel_id: msg.source_channel.clone(), data: to_binary(&ibc_packet)?, - // timeout: msg.timeout_timestamp.into(), - timeout: IbcTimeout::from(Timestamp::from_nanos(msg.timeout_timestamp)), + timeout: IbcTimeout::from( + env.block + .time + .plus_seconds(DEFAULT_TIMEOUT_TIMESTAMP_OFFSET), + ), }; let res = Response::new() @@ -98,7 +112,7 @@ pub fn execute_make_swap( // This method lock the order (set a value to the field "Taker") and lock Token pub fn execute_take_swap( deps: DepsMut, - _env: Env, + env: Env, info: MessageInfo, msg: TakeSwapMsg, ) -> Result { @@ -129,8 +143,6 @@ pub fn execute_take_swap( // Checks if the order has already been taken if let Some(_taker) = order.taker { - // Do Nothing - } else { return Err(ContractError::OrderTaken); } @@ -139,6 +151,11 @@ pub fn execute_take_swap( return Err(ContractError::InvalidTakerAddress); } + if env.block.time.seconds() > order.maker.expiration_timestamp { + move_order_to_bottom(deps.storage, &msg.order_id)?; + return Err(ContractError::Expired); + } + order.taker = Some(msg.clone()); let ibc_packet = AtomicSwapPacketData { @@ -152,15 +169,19 @@ pub fn execute_take_swap( let ibc_msg = IbcMsg::SendPacket { channel_id: extract_source_channel_for_taker_msg(&order.path)?, data: to_binary(&ibc_packet)?, - timeout: IbcTimeout::from(Timestamp::from_nanos(msg.timeout_timestamp)), + timeout: IbcTimeout::from( + env.block + .time + .plus_seconds(DEFAULT_TIMEOUT_TIMESTAMP_OFFSET), + ), }; // Save order set_atomic_order(deps.storage, &order.id, &order)?; - //SWAP_ORDERS.save(deps.storage, &order.id, &order)?; let res = Response::new() .add_message(ibc_msg) + .add_attribute("order_id", msg.order_id) .add_attribute("action", "take_swap") .add_attribute("order_id", order.id.clone()); return Ok(res); @@ -170,7 +191,7 @@ pub fn execute_take_swap( // It is executed on the Maker chain. Only the maker of the order can cancel the order. pub fn execute_cancel_swap( deps: DepsMut, - _env: Env, + env: Env, info: MessageInfo, msg: CancelSwapMsg, ) -> Result { @@ -202,11 +223,16 @@ pub fn execute_cancel_swap( let ibc_msg = IbcMsg::SendPacket { channel_id: order.maker.source_channel, data: to_binary(&packet)?, - timeout: Timestamp::from_nanos(msg.timeout_timestamp.parse().unwrap()).into(), + timeout: IbcTimeout::from( + env.block + .time + .plus_seconds(DEFAULT_TIMEOUT_TIMESTAMP_OFFSET), + ), }; let res = Response::new() .add_message(ibc_msg) + .add_attribute("order_id", msg.order_id) .add_attribute("action", "cancel_swap") .add_attribute("order_id", order.id.clone()); return Ok(res); diff --git a/contracts/ics100/src/state.rs b/contracts/ics100/src/state.rs index 11363fa..e7087f7 100644 --- a/contracts/ics100/src/state.rs +++ b/contracts/ics100/src/state.rs @@ -42,6 +42,8 @@ pub struct AtomicSwapOrder { // an IBC path, define channel and port on both Maker Chain and Taker Chain pub path: String, pub taker: Option, + // In seconds + pub create_timestamp: u64, pub cancel_timestamp: Option, pub complete_timestamp: Option, } From 9e21cafad7109d08206be6ffc0d74e4586a2957b Mon Sep 17 00:00:00 2001 From: Amit Yadav Date: Wed, 23 Aug 2023 12:32:44 +0530 Subject: [PATCH 2/3] delete file --- contracts/ics100/src/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 contracts/ics100/src/.DS_Store diff --git a/contracts/ics100/src/.DS_Store b/contracts/ics100/src/.DS_Store deleted file mode 100644 index a50e356a02bf9fdf2a6134be417452e86f80b31e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK!A=4(5Pj8+*~o>H@wi{$${%dVdNW>#UUdZ$O+W~{#CY3J_sw)Ll7#~oBxZ(8 zUpv!jr*G4i0tmzPWB~L5q*TG=lEn@Yzi3C!%yK~tI>tN3ct(v+RGr>#a0Z-#Q)58( z?iF`yg9YaJy?>Y7$#3+0V1gN@SmB0RFPqoIa^O0V}>>BQ(AMB zI&Q_pwD!VIk`4?Yl*X)~|J3eOXavBWq&K5~~5xsNzyG&`yyXly1OJMFkd&ix$R*j`+F6|3wHft=DkA;bh#JDe hu4AppReViVp)ZpPu`p~S(n9e+0!o7y&cIO__y%p(hRy&0 From 55561cad9bfc0761483e649fdb65e0da727d5214 Mon Sep 17 00:00:00 2001 From: Amit Yadav Date: Wed, 23 Aug 2023 12:33:46 +0530 Subject: [PATCH 3/3] add migration function --- contracts/ics100/src/contract.rs | 20 +++++++++++++++++++- contracts/ics100/src/msg.rs | 4 ++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/contracts/ics100/src/contract.rs b/contracts/ics100/src/contract.rs index 87fea43..aa6fc17 100644 --- a/contracts/ics100/src/contract.rs +++ b/contracts/ics100/src/contract.rs @@ -10,7 +10,7 @@ use cw2::set_contract_version; use crate::error::ContractError; use crate::msg::{ AtomicSwapPacketData, CancelSwapMsg, DetailsResponse, ExecuteMsg, InstantiateMsg, ListResponse, - MakeSwapMsg, QueryMsg, SwapMessageType, TakeSwapMsg, + MakeSwapMsg, QueryMsg, SwapMessageType, TakeSwapMsg, MigrateMsg, }; use crate::state::{ AtomicSwapOrder, @@ -238,6 +238,24 @@ pub fn execute_cancel_swap( return Ok(res); } +#[entry_point] +pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result { + let ver = cw2::get_contract_version(deps.storage)?; + // ensure we are migrating from an allowed contract + if ver.contract != CONTRACT_NAME { + return Err(StdError::generic_err("Can only upgrade from same type").into()); + } + // note: better to do proper semver compare, but string compare *usually* works + if ver.version >= CONTRACT_VERSION.to_string() { + return Err(StdError::generic_err("Cannot upgrade from a newer version").into()); + } + + // set the new version + cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; + + Ok(Response::default()) +} + #[cfg_attr(not(feature = "library"), entry_point)] pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { match msg { diff --git a/contracts/ics100/src/msg.rs b/contracts/ics100/src/msg.rs index 0e25b18..942aa28 100644 --- a/contracts/ics100/src/msg.rs +++ b/contracts/ics100/src/msg.rs @@ -36,6 +36,10 @@ pub fn is_valid_name(name: &str) -> bool { true } +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] +pub struct MigrateMsg { +} + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub enum SwapMessageType { #[serde(rename = "TYPE_UNSPECIFIED")]