-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into display_name
- Loading branch information
Showing
31 changed files
with
336 additions
and
130 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct Args { | ||
pub offer_id: u32, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub enum Response { | ||
Success, | ||
OfferAlreadyAccepted, | ||
OfferExpired, | ||
OfferNotFound, | ||
NotAuthorized, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod cancel_offer; | ||
pub mod create_offer; | ||
pub mod notify_deposit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
backend/canisters/escrow/impl/src/jobs/make_pending_payments.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use crate::model::pending_payments_queue::{PendingPayment, PendingPaymentReason}; | ||
use crate::{mutate_state, RuntimeState}; | ||
use candid::Principal; | ||
use escrow_canister::deposit_subaccount; | ||
use ic_cdk_timers::TimerId; | ||
use icrc_ledger_types::icrc1::account::Account; | ||
use icrc_ledger_types::icrc1::transfer::TransferArg; | ||
use std::cell::Cell; | ||
use std::time::Duration; | ||
use tracing::{error, trace}; | ||
use types::icrc1::CompletedCryptoTransaction; | ||
use types::CanisterId; | ||
use utils::time::NANOS_PER_MILLISECOND; | ||
|
||
thread_local! { | ||
static TIMER_ID: Cell<Option<TimerId>> = Cell::default(); | ||
} | ||
|
||
pub(crate) fn start_job_if_required(state: &RuntimeState) -> bool { | ||
if TIMER_ID.get().is_none() && !state.data.pending_payments_queue.is_empty() { | ||
let timer_id = ic_cdk_timers::set_timer_interval(Duration::ZERO, run); | ||
TIMER_ID.set(Some(timer_id)); | ||
trace!("'make_pending_payments' job started"); | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
pub fn run() { | ||
if let Some(pending_payment) = mutate_state(|state| state.data.pending_payments_queue.pop()) { | ||
ic_cdk::spawn(process_payment(pending_payment)); | ||
} else if let Some(timer_id) = TIMER_ID.take() { | ||
ic_cdk_timers::clear_timer(timer_id); | ||
trace!("'make_pending_payments' job stopped"); | ||
} | ||
} | ||
|
||
async fn process_payment(pending_payment: PendingPayment) { | ||
let from_user = match pending_payment.reason { | ||
PendingPaymentReason::Trade(other_user_id) => other_user_id, | ||
PendingPaymentReason::Refund => pending_payment.user_id, | ||
}; | ||
let created_at_time = pending_payment.timestamp * NANOS_PER_MILLISECOND; | ||
|
||
let args = TransferArg { | ||
from_subaccount: Some(deposit_subaccount(from_user, pending_payment.offer_id)), | ||
to: Principal::from(pending_payment.user_id).into(), | ||
fee: Some(pending_payment.token_info.fee.into()), | ||
created_at_time: Some(created_at_time), | ||
memo: None, | ||
amount: pending_payment.amount.into(), | ||
}; | ||
|
||
match make_payment(pending_payment.token_info.ledger, &args).await { | ||
Ok(block_index) => { | ||
mutate_state(|state| { | ||
if let Some(offer) = state.data.offers.get_mut(pending_payment.offer_id) { | ||
let transfer = CompletedCryptoTransaction { | ||
ledger: pending_payment.token_info.ledger, | ||
token: pending_payment.token_info.token, | ||
amount: pending_payment.amount, | ||
from: Account { | ||
owner: state.env.canister_id(), | ||
subaccount: args.from_subaccount, | ||
} | ||
.into(), | ||
to: Account::from(Principal::from(pending_payment.user_id)).into(), | ||
fee: pending_payment.token_info.fee, | ||
memo: None, | ||
created: created_at_time, | ||
block_index, | ||
}; | ||
offer.transfers_out.push(transfer); | ||
} | ||
}); | ||
} | ||
Err(retry) => { | ||
if retry { | ||
mutate_state(|state| { | ||
state.data.pending_payments_queue.push(pending_payment); | ||
start_job_if_required(state); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Error response contains a boolean stating if the transfer should be retried | ||
async fn make_payment(ledger_canister_id: CanisterId, args: &TransferArg) -> Result<u64, bool> { | ||
match icrc_ledger_canister_c2c_client::icrc1_transfer(ledger_canister_id, args).await { | ||
Ok(Ok(block_index)) => Ok(block_index.0.try_into().unwrap()), | ||
Ok(Err(transfer_error)) => { | ||
error!(?transfer_error, ?args, "Transfer failed"); | ||
Err(false) | ||
} | ||
Err(error) => { | ||
error!(?error, ?args, "Transfer failed"); | ||
Err(true) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
use crate::RuntimeState; | ||
|
||
pub(crate) fn start(_state: &RuntimeState) {} | ||
pub mod make_pending_payments; | ||
|
||
pub(crate) fn start(state: &RuntimeState) { | ||
make_pending_payments::start_job_if_required(state); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod offers; | ||
pub mod pending_payments_queue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
backend/canisters/escrow/impl/src/model/pending_payments_queue.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::VecDeque; | ||
use types::{TimestampMillis, TokenInfo, UserId}; | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
pub struct PendingPaymentsQueue { | ||
pending_payments: VecDeque<PendingPayment>, | ||
} | ||
|
||
impl PendingPaymentsQueue { | ||
pub fn push(&mut self, pending_payment: PendingPayment) { | ||
self.pending_payments.push_back(pending_payment); | ||
} | ||
|
||
pub fn pop(&mut self) -> Option<PendingPayment> { | ||
self.pending_payments.pop_front() | ||
} | ||
|
||
pub fn is_empty(&self) -> bool { | ||
self.pending_payments.is_empty() | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct PendingPayment { | ||
pub user_id: UserId, | ||
pub timestamp: TimestampMillis, | ||
pub token_info: TokenInfo, | ||
pub amount: u128, | ||
pub offer_id: u32, | ||
pub reason: PendingPaymentReason, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Copy)] | ||
pub enum PendingPaymentReason { | ||
Trade(UserId), | ||
Refund, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::{mutate_state, RuntimeState}; | ||
use canister_api_macros::update_msgpack; | ||
use canister_tracing_macros::trace; | ||
use escrow_canister::cancel_offer::{Response::*, *}; | ||
|
||
#[update_msgpack] | ||
#[trace] | ||
fn cancel_offer(args: Args) -> Response { | ||
mutate_state(|state| cancel_offer_impl(args, state)) | ||
} | ||
|
||
fn cancel_offer_impl(args: Args, state: &mut RuntimeState) -> Response { | ||
if let Some(offer) = state.data.offers.get_mut(args.offer_id) { | ||
let user_id = state.env.caller().into(); | ||
let now = state.env.now(); | ||
if offer.created_by != user_id { | ||
NotAuthorized | ||
} else if offer.accepted_by.is_some() { | ||
OfferAlreadyAccepted | ||
} else if offer.expires_at < now { | ||
OfferExpired | ||
} else { | ||
if offer.cancelled_at.is_none() { | ||
offer.cancelled_at = Some(now); | ||
} | ||
Success | ||
} | ||
} else { | ||
OfferNotFound | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod cancel_offer; | ||
pub mod create_offer; | ||
pub mod notify_deposit; | ||
pub mod wallet_receive; |
Oops, something went wrong.