Skip to content

Commit

Permalink
expires_at to expires_in for p2p trade initial (#5147)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Jan 8, 2024
1 parent dd1cc5e commit a5e1d1f
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 24 deletions.
1 change: 1 addition & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Don't mark messages as edited if only link removed ([#5119](https://github.com/open-chat-labs/open-chat/pull/5119))
- Increase max message length to 10k characters ([#5140](https://github.com/open-chat-labs/open-chat/pull/5140))
- Return success from `deleted_message` even if message not deleted ([#5145](https://github.com/open-chat-labs/open-chat/pull/5145))
- Change `expires_at` to `expires_in` for p2p trade initial ([#5147](https://github.com/open-chat-labs/open-chat/pull/5147))

### Fixed

Expand Down
1 change: 1 addition & 0 deletions backend/canisters/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Don't mark messages as edited if only link removed ([#5119](https://github.com/open-chat-labs/open-chat/pull/5119))
- Increase max message length to 10k characters ([#5140](https://github.com/open-chat-labs/open-chat/pull/5140))
- Return success from `deleted_message` even if message not deleted ([#5145](https://github.com/open-chat-labs/open-chat/pull/5145))
- Change `expires_at` to `expires_in` for p2p trade initial ([#5147](https://github.com/open-chat-labs/open-chat/pull/5147))

### Fixed

Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Don't mark messages as edited if only link removed ([#5119](https://github.com/open-chat-labs/open-chat/pull/5119))
- Increase max message length to 10k characters ([#5140](https://github.com/open-chat-labs/open-chat/pull/5140))
- Return success from `deleted_message` even if message not deleted ([#5145](https://github.com/open-chat-labs/open-chat/pull/5145))
- Change `expires_at` to `expires_in` for p2p trade initial ([#5147](https://github.com/open-chat-labs/open-chat/pull/5147))

### Removed

Expand Down
2 changes: 1 addition & 1 deletion backend/canisters/user/impl/src/updates/send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn send_message_impl(
let my_user_id = state.env.canister_id().into();
let recipient = args.recipient;
let content = if let Some(transfer) = completed_transfer.clone() {
MessageContentInternal::new_with_transfer(args.content.clone(), transfer, None)
MessageContentInternal::new_with_transfer(args.content.clone(), transfer, None, now)
} else {
args.content.clone().try_into().unwrap()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use escrow_canister::deposit_subaccount;
use ic_cdk_macros::update;
use icrc_ledger_types::icrc1::account::Account;
use types::{
icrc1, CanisterId, CompletedCryptoTransaction, CryptoTransaction, MessageContentInitial, PendingCryptoTransaction, UserId,
MAX_TEXT_LENGTH, MAX_TEXT_LENGTH_USIZE,
icrc1, CanisterId, CompletedCryptoTransaction, CryptoTransaction, MessageContentInitial, PendingCryptoTransaction,
TimestampMillis, UserId, MAX_TEXT_LENGTH, MAX_TEXT_LENGTH_USIZE,
};
use user_canister::send_message_with_transfer_to_channel;
use user_canister::send_message_with_transfer_to_group;
Expand All @@ -26,12 +26,13 @@ async fn send_message_with_transfer_to_channel(
run_regular_jobs();

// Check that the user is a member of the community
if read_state(|state| !state.data.communities.exists(&args.community_id)) {
let (exists, now) = read_state(|state| (state.data.communities.exists(&args.community_id), state.env.now()));
if !exists {
return UserNotInCommunity(None);
}

// Validate the content and extract the PendingCryptoTransaction
let (pending_transaction, p2p_offer_id) = match mutate_state(|state| prepare(&args.content, state)) {
let (pending_transaction, p2p_offer_id) = match mutate_state(|state| prepare(&args.content, now, state)) {
PrepareResult::Success(t) => (t, None),
PrepareResult::P2PTrade(escrow_canister_id, args) => match set_up_p2p_trade(escrow_canister_id, args).await {
Ok((id, t)) => (t, Some(id)),
Expand All @@ -46,7 +47,8 @@ async fn send_message_with_transfer_to_channel(
};

// Make the crypto transfer
let (content, completed_transaction) = match process_transaction(args.content, pending_transaction, p2p_offer_id).await {
let (content, completed_transaction) = match process_transaction(args.content, pending_transaction, p2p_offer_id, now).await
{
Ok((c, t)) => (c, t),
Err(error) => return TransferFailed(error),
};
Expand Down Expand Up @@ -126,12 +128,13 @@ async fn send_message_with_transfer_to_group(
run_regular_jobs();

// Check that the user is a member of the group
if read_state(|state| !state.data.group_chats.exists(&args.group_id)) {
let (exists, now) = read_state(|state| (state.data.group_chats.exists(&args.group_id), state.env.now()));
if !exists {
return CallerNotInGroup(None);
}

// Validate the content and extract the PendingCryptoTransaction
let (pending_transaction, p2p_offer_id) = match mutate_state(|state| prepare(&args.content, state)) {
let (pending_transaction, p2p_offer_id) = match mutate_state(|state| prepare(&args.content, now, state)) {
PrepareResult::Success(t) => (t, None),
PrepareResult::P2PTrade(escrow_canister_id, args) => match set_up_p2p_trade(escrow_canister_id, args).await {
Ok((id, t)) => (t, Some(id)),
Expand All @@ -146,7 +149,8 @@ async fn send_message_with_transfer_to_group(
};

// Make the crypto transfer
let (content, completed_transaction) = match process_transaction(args.content, pending_transaction, p2p_offer_id).await {
let (content, completed_transaction) = match process_transaction(args.content, pending_transaction, p2p_offer_id, now).await
{
Ok((c, t)) => (c, t),
Err(error) => return TransferFailed(error),
};
Expand Down Expand Up @@ -223,11 +227,9 @@ enum PrepareResult {
TransferCannotBeToSelf,
}

fn prepare(content: &MessageContentInitial, state: &mut RuntimeState) -> PrepareResult {
fn prepare(content: &MessageContentInitial, now: TimestampMillis, state: &mut RuntimeState) -> PrepareResult {
use PrepareResult::*;

let now = state.env.now();

if state.data.suspended.value {
return UserSuspended;
} else if content.text_length() > MAX_TEXT_LENGTH_USIZE {
Expand Down Expand Up @@ -273,7 +275,7 @@ fn prepare(content: &MessageContentInitial, state: &mut RuntimeState) -> Prepare
input_amount: p.input_amount,
output_token: p.output_token.clone(),
output_amount: p.output_amount,
expires_at: p.expires_at,
expires_at: now + p.expires_in,
};
return P2PTrade(state.data.escrow_canister_id, create_offer_args);
}
Expand All @@ -291,6 +293,7 @@ async fn process_transaction(
content: MessageContentInitial,
pending_transaction: PendingCryptoTransaction,
p2p_offer_id: Option<u32>,
now: TimestampMillis,
) -> Result<(MessageContentInternal, CompletedCryptoTransaction), String> {
match crate::crypto::process_transaction(pending_transaction).await {
Ok(completed) => {
Expand All @@ -305,7 +308,7 @@ async fn process_transaction(
NotifyEscrowCanisterOfDepositJob::run(id);
}
Ok((
MessageContentInternal::new_with_transfer(content, completed.clone(), p2p_offer_id),
MessageContentInternal::new_with_transfer(content, completed.clone(), p2p_offer_id, now),
completed,
))
}
Expand Down
6 changes: 2 additions & 4 deletions backend/integration_tests/src/p2p_trade_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::env::ENV;
use crate::rng::{random_message_id, random_string};
use crate::utils::{now_millis, tick_many};
use crate::utils::tick_many;
use crate::{client, CanisterIds, TestEnv, User};
use candid::Principal;
use pocket_ic::PocketIc;
Expand All @@ -20,8 +20,6 @@ fn p2p_trade_succeeds() {

let TestData { user1, user2, group_id } = init_test_data(env, canister_ids, *controller, true);

let now = now_millis(env);

client::icrc1::happy_path::transfer(
env,
*controller,
Expand Down Expand Up @@ -50,7 +48,7 @@ fn p2p_trade_succeeds() {
input_amount: 1_000_000_000,
output_token: Cryptocurrency::CHAT.try_into().unwrap(),
output_amount: 10_000_000_000,
expires_at: now + DAY_IN_MS,
expires_in: DAY_IN_MS,
caption: None,
}),
sender_name: user1.username(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl MessageContentInternal {
content: MessageContentInitial,
transfer: CompletedCryptoTransaction,
p2p_trade_offer_id: Option<u32>,
now: TimestampMillis,
) -> MessageContentInternal {
match content {
MessageContentInitial::Crypto(c) => MessageContentInternal::Crypto(CryptoContentInternal {
Expand All @@ -65,7 +66,7 @@ impl MessageContentInternal {
}),
MessageContentInitial::Prize(c) => MessageContentInternal::Prize(PrizeContentInternal::new(c, transfer)),
MessageContentInitial::P2PTrade(c) => {
MessageContentInternal::P2PTrade(P2PTradeContent::new(p2p_trade_offer_id.unwrap(), c, transfer))
MessageContentInternal::P2PTrade(P2PTradeContent::new(p2p_trade_offer_id.unwrap(), c, transfer, now))
}
_ => unreachable!("Message must include a crypto transfer"),
}
Expand Down
2 changes: 1 addition & 1 deletion backend/libraries/types/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ type P2PTradeContentInitial = record {
input_amount : nat;
output_token : TokenInfo;
output_amount : nat;
expires_at : TimestampMillis;
expires_in : Milliseconds;
caption : opt text;
};

Expand Down
13 changes: 9 additions & 4 deletions backend/libraries/types/src/message_content.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::polls::{InvalidPollReason, PollConfig, PollVotes};
use crate::{
CanisterId, CompletedCryptoTransaction, CryptoTransaction, CryptoTransferDetails, Cryptocurrency, MessageIndex,
ProposalContent, TimestampMillis, TokenInfo, TotalVotes, User, UserId,
Milliseconds, ProposalContent, TimestampMillis, TokenInfo, TotalVotes, User, UserId,
};
use candid::CandidType;
use ic_ledger_types::Tokens;
Expand Down Expand Up @@ -529,7 +529,7 @@ pub struct P2PTradeContentInitial {
pub input_amount: u128,
pub output_token: TokenInfo,
pub output_amount: u128,
pub expires_at: TimestampMillis,
pub expires_in: Milliseconds,
pub caption: Option<String>,
}

Expand All @@ -547,15 +547,20 @@ pub struct P2PTradeContent {
}

impl P2PTradeContent {
pub fn new(offer_id: u32, content: P2PTradeContentInitial, transfer: CompletedCryptoTransaction) -> P2PTradeContent {
pub fn new(
offer_id: u32,
content: P2PTradeContentInitial,
transfer: CompletedCryptoTransaction,
now: TimestampMillis,
) -> P2PTradeContent {
P2PTradeContent {
offer_id,
input_token: content.input_token,
input_amount: transfer.units(),
input_transaction_index: transfer.index(),
output_token: content.output_token,
output_amount: content.output_amount,
expires_at: content.expires_at,
expires_at: now + content.expires_in,
status: P2PTradeStatus::Open,
caption: content.caption,
}
Expand Down

0 comments on commit a5e1d1f

Please sign in to comment.