Skip to content

Commit

Permalink
revert approve removal
Browse files Browse the repository at this point in the history
  • Loading branch information
coreggon11 committed Oct 3, 2023
1 parent 49b09b3 commit ddf3638
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions contracts/src/token/psp22/psp22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ pub trait PSP22Impl: Storage<Data> + Internal {
Ok(())
}

fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error> {
let owner = Self::env().caller();
self._approve_from_to(owner, spender, value)?;
Ok(())
}

fn increase_allowance(&mut self, spender: AccountId, delta_value: Balance) -> Result<(), PSP22Error> {
let owner = Self::env().caller();
self._approve_from_to(owner, spender, self._allowance(&owner, &spender) + delta_value)
Expand Down
15 changes: 15 additions & 0 deletions contracts/src/traits/psp22/psp22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ pub trait PSP22 {
data: Vec<u8>,
) -> Result<(), PSP22Error>;

/// Allows `spender` to withdraw from the caller's account multiple times, up to
/// the `value` amount.
///
/// If this function is called again it overwrites the current allowance with `value`.
///
/// An `Approval` event is emitted.
///
/// # Important
///
/// This function is vulnerable to the double spending attack.
/// Based on the OpenZeppelin security review of OpenBrush, we recommend using the
/// `increase_allowance` and `decrease_allowance` functions.
#[ink(message)]
fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error>;

/// Atomically increases the allowance granted to `spender` by the caller.
///
/// An `Approval` event is emitted.
Expand Down
10 changes: 10 additions & 0 deletions lang/codegen/src/implementations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ pub(crate) fn impl_psp22(impl_args: &mut ImplArgs, capped: bool) {
PSP22Impl::transfer_from(self, from, to, value, data)
}

#[ink(message)]
fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error> {
PSP22Impl::approve(self, spender, value)
}

#[ink(message)]
fn increase_allowance(&mut self, spender: AccountId, delta_value: Balance) -> Result<(), PSP22Error> {
PSP22Impl::increase_allowance(self, spender, delta_value)
Expand Down Expand Up @@ -891,6 +896,11 @@ pub(crate) fn impl_psp22_pallet(impl_args: &mut ImplArgs) {
PSP22PalletImpl::transfer_from(self, from, to, value, data)
}

#[ink(message)]
fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error> {
PSP22PalletImpl::approve(self, spender, value)
}

#[ink(message)]
fn increase_allowance(&mut self, spender: AccountId, delta_value: Balance) -> Result<(), PSP22Error> {
PSP22PalletImpl::increase_allowance(self, spender, delta_value)
Expand Down

0 comments on commit ddf3638

Please sign in to comment.