Skip to content

Commit 029d6fe

Browse files
committed
flashmint implementation #2
1 parent cd53be8 commit 029d6fe

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

contracts/token/psp22/src/extensions/flashmint.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,68 @@
1-
use crate::traits::PSP22;
1+
use crate::{
2+
extensions::PSP3156FlashBorrowerStub,
3+
traits::{
4+
PSP22Error,
5+
PSP22,
6+
},
7+
};
28
use brush::traits::{
39
AccountId,
410
Balance,
511
};
12+
use ink_lang::ToAccountId;
613
use ink_prelude::vec::Vec;
714

815
#[brush::trait_definition]
9-
pub trait PSP22FlashMint: PSP22 + PSP3156FlashBorrower {}
16+
pub trait PSP22FlashMint: PSP22 + PSP3156FlashBorrower {
17+
const RETURN_VALUE: [u8; 32] = brush::blake2b_256!("PSP3156FlashBorrower.onFlashLoan");
18+
19+
#[ink(message)]
20+
fn max_flashloan(&mut self, token: AccountId) -> Balance {
21+
if token == Self::env().account_id() {
22+
Balance::MAX - self.total_supply()
23+
} else {
24+
0
25+
}
26+
}
27+
28+
#[ink(message)]
29+
fn flash_fee(&mut self, token: AccountId, _amount: Balance) -> Balance {
30+
assert_eq!(
31+
token,
32+
Self::env().account_id(),
33+
"{}",
34+
PSP22Error::Custom(String::from("Wrong token")).as_ref()
35+
);
36+
0
37+
}
38+
39+
#[ink(message)]
40+
fn flashloan(
41+
&mut self,
42+
receiver: &mut PSP3156FlashBorrowerStub,
43+
token: AccountId,
44+
amount: Balance,
45+
data: Vec<u8>,
46+
) -> Result<(), PSP22Error> {
47+
let receiver_account = receiver.to_account_id();
48+
let fee = self.flash_fee(token, amount);
49+
self._mint(receiver_account, amount);
50+
if receiver.on_flash_loan(Self::env().caller(), token, amount, fee, data) != Self::RETURN_VALUE {
51+
return Err(PSP22Error::Custom(String::from("Invalid return value")))
52+
}
53+
let current_allowance = self.allowance(receiver_account, Self::env().account_id());
54+
if current_allowance < amount + fee {
55+
return Err(PSP22Error::Custom(String::from("Allowance does not allow refund")))
56+
}
57+
self._approve_from_to(
58+
receiver_account,
59+
Self::env().account_id(),
60+
current_allowance - amount - fee,
61+
)?;
62+
self._burn(receiver_account, amount + fee);
63+
Ok(())
64+
}
65+
}
1066

1167
#[brush::trait_definition]
1268
pub trait PSP3156FlashBorrower {

contracts/token/psp22/src/extensions/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub mod stub;
33
pub mod wrapper;
44

55
pub use self::stub::{
6-
psp22flashmint::PSP22FlashMint,
7-
psp3156_flash_borrower::PSP3156FlashBorrower,
8-
wrapper::PSP22Wrapper,
6+
psp22flashmint::PSP22FlashMint as PSP22FlashMintStub,
7+
psp3156_flash_borrower::PSP3156FlashBorrower as PSP3156FlashBorrowerStub,
8+
wrapper::PSP22Wrapper as PSP22WrapperStub,
99
};

contracts/token/psp22/src/extensions/stub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub mod psp22flashmint {
5757
#[ink(message)]
5858
pub fn flashloan(
5959
&mut self,
60-
reciever: PSP3156FlashBorrower,
60+
receiver: &mut PSP3156FlashBorrower,
6161
token: AccountId,
6262
amount: Balance,
6363
data: Vec<u8>,

0 commit comments

Comments
 (0)