Skip to content

Commit

Permalink
Prevent the patron from being the vendor
Browse files Browse the repository at this point in the history
  • Loading branch information
bitdivine committed Oct 1, 2024
1 parent 12684b2 commit f52bda4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ pub enum PaymentError {
needed: TokenAmount,
available: TokenAmount,
},
InvalidPatron,
}
6 changes: 6 additions & 0 deletions src/guard/src/guards/icrc2_cycles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ impl Default for Icrc2CyclesPaymentGuard {

impl PaymentGuard for Icrc2CyclesPaymentGuard {
async fn deduct(&self, fee: TokenAmount) -> Result<(), PaymentError> {
// The patron must not be the vendor itself (this canister).
if self.payer_account.owner == self.own_canister_id {
return Err(PaymentError::InvalidPatron);
}
// The cycles ledger has a special `withdraw_from` method, similar to `transfer_from`,
// but that adds the cycles to the canister rather than putting it into a ledger account.
cycles_ledger_client::Service(cycles_ledger_canister_id())
.withdraw_from(&WithdrawFromArgs {
to: self.own_canister_id,
Expand Down
4 changes: 4 additions & 0 deletions src/guard/src/guards/patron_pays_icrc2_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ impl PatronPaysIcrc2TokensPaymentGuard {

impl PaymentGuard for PatronPaysIcrc2TokensPaymentGuard {
async fn deduct(&self, cost: TokenAmount) -> Result<(), PaymentError> {
// The patron must not be the vendor itself (this canister).
if self.payer_account.owner == self.own_canister_id {
return Err(PaymentError::InvalidPatron);
}
// Note: The cycles ledger client is ICRC-2 compatible so can be used here.
cycles_ledger_client::Service(self.ledger)
.icrc_2_transfer_from(&TransferFromArgs {
Expand Down

0 comments on commit f52bda4

Please sign in to comment.