Skip to content

Commit

Permalink
Add Erc20FlashMint (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNeshi authored Feb 27, 2025
1 parent cc444b4 commit 452b4ba
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ dist
*.tsbuildinfo
node_modules


.env
.env.local

package.json
8 changes: 3 additions & 5 deletions packages/core/stylus/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface BaseImplementedTrait {
* Lower numbers are higher priority, undefined is lowest priority.
*/
priority?: number;
omit_inherit?: boolean;
}

export interface ImplementedTrait extends BaseImplementedTrait {
Expand Down Expand Up @@ -93,7 +94,7 @@ export class ContractBuilder implements Contract {
get errors(): Error[] {
return [...this.errorsMap.values()];
}

get constants(): Variable[] {
return [...this.constantsMap.values()];
}
Expand All @@ -116,11 +117,8 @@ export class ContractBuilder implements Contract {
return existingTrait;
} else {
const t: ImplementedTrait = {
name: baseTrait.name,
...baseTrait,
functions: [],
storage: baseTrait.storage,
section: baseTrait.section,
priority: baseTrait.priority,
};
this.implementedTraitsMap.set(key, t);
return t;
Expand Down
30 changes: 30 additions & 0 deletions packages/core/stylus/src/erc20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ testERC20('erc20 burnable', {
burnable: true,
});

testERC20('erc20 flash-mint', {
permit: false,
flashmint: true,
});

testERC20('erc20 burnable flash-mint', {
permit: false,
burnable: true,
flashmint: true,
});

// testERC20('erc20 pausable', {
// permit: false,
// pausable: true,
Expand All @@ -49,6 +60,12 @@ testERC20('erc20 burnable', {
// pausable: true,
// });

// testERC20('erc20 flash-mint pausable', {
// permit: false,
// flashmint: true,
// pausable: true,
// });

testERC20('erc20 permit', {
permit: true,
});
Expand All @@ -58,6 +75,11 @@ testERC20('erc20 permit burnable', {
burnable: true,
});

testERC20('erc20 permit flash-mint', {
permit: true,
flashmint: true,
});

// testERC20('erc20 permit pausable', {
// permit: true,
// pausable: true,
Expand All @@ -69,10 +91,17 @@ testERC20('erc20 permit burnable', {
// pausable: true,
// });

// testERC20('erc20 permit flash-mint pausable', {
// permit: true,
// flashmint: true,
// pausable: true,
// });

testERC20('erc20 full - complex name', {
name: 'Custom $ Token',
burnable: true,
permit: true,
flashmint: true,
// pausable: true,
});

Expand All @@ -87,6 +116,7 @@ testAPIEquivalence('erc20 API full', {
name: 'CustomToken',
burnable: true,
permit: true,
flashmint: true,
// pausable: true,
});

Expand Down
165 changes: 164 additions & 1 deletion packages/core/stylus/src/erc20.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,100 @@ Generated by [AVA](https://avajs.dev).
}␊
`

## erc20 flash-mint

> Snapshot 1
`// SPDX-License-Identifier: MIT␊
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3␊
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
extern crate alloc;␊
use openzeppelin_stylus::token::erc20::{Erc20, IErc20};␊
use openzeppelin_stylus::token::erc20::extensions::{Erc20FlashMint, IErc3156FlashLender};␊
use stylus_sdk::abi::Bytes;␊
use stylus_sdk::prelude::{entrypoint, public, storage};␊
#[entrypoint]␊
#[storage]␊
struct MyToken {␊
#[borrow]␊
erc20: Erc20,␊
#[borrow]␊
flash_mint: Erc20FlashMint,␊
}␊
#[public]␊
#[inherit(Erc20)]␊
impl MyToken {␊
fn max_flash_loan(&self, token: Address) -> U256 {␊
self.flash_mint.max_flash_loan(token, &self.erc20)␊
}␊
fn flash_fee(&self, token: Address, value: U256) -> Result<U256, Vec<u8>> {␊
self.flash_mint.flash_fee(token, value).map_err(|e| e.into())␊
}␊
fn flash_loan(&mut self, receiver: Address, token: Address, value: U256, data: Bytes) -> Result<bool, Vec<u8>> {␊
self.flash_mint.flash_loan(receiver, token, value, data, &mut self.erc20).map_err(|e| e.into())␊
}␊
}␊
`

## erc20 burnable flash-mint

> Snapshot 1
`// SPDX-License-Identifier: MIT␊
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3␊
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
extern crate alloc;␊
use alloc::vec::Vec;␊
use alloy_primitives::{Address, U256};␊
use openzeppelin_stylus::token::erc20::{Erc20, IErc20};␊
use openzeppelin_stylus::token::erc20::extensions::{␊
Erc20FlashMint, IErc20Burnable, IErc3156FlashLender␊
};␊
use stylus_sdk::abi::Bytes;␊
use stylus_sdk::prelude::{entrypoint, public, storage};␊
#[entrypoint]␊
#[storage]␊
struct MyToken {␊
#[borrow]␊
erc20: Erc20,␊
#[borrow]␊
flash_mint: Erc20FlashMint,␊
}␊
#[public]␊
#[inherit(Erc20)]␊
impl MyToken {␊
fn burn(&mut self, value: U256) -> Result<(), Vec<u8>> {␊
self.erc20.burn(value).map_err(|e| e.into())␊
}␊
fn burn_from(&mut self, account: Address, value: U256) -> Result<(), Vec<u8>> {␊
self.erc20.burn_from(account, value).map_err(|e| e.into())␊
}␊
fn max_flash_loan(&self, token: Address) -> U256 {␊
self.flash_mint.max_flash_loan(token, &self.erc20)␊
}␊
fn flash_fee(&self, token: Address, value: U256) -> Result<U256, Vec<u8>> {␊
self.flash_mint.flash_fee(token, value).map_err(|e| e.into())␊
}␊
fn flash_loan(&mut self, receiver: Address, token: Address, value: U256, data: Bytes) -> Result<bool, Vec<u8>> {␊
self.flash_mint.flash_loan(receiver, token, value, data, &mut self.erc20).map_err(|e| e.into())␊
}␊
}␊
`

## erc20 permit

> Snapshot 1
Expand Down Expand Up @@ -180,6 +274,58 @@ Generated by [AVA](https://avajs.dev).
}␊
`

## erc20 permit flash-mint

> Snapshot 1
`// SPDX-License-Identifier: MIT␊
// Compatible with OpenZeppelin Contracts for Stylus ^0.2.0-alpha.3␊
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]␊
extern crate alloc;␊
use openzeppelin_stylus::token::erc20::extensions::{␊
Erc20FlashMint, Erc20Permit, IErc3156FlashLender␊
};␊
use openzeppelin_stylus::token::erc20::IErc20;␊
use openzeppelin_stylus::utils::cryptography::eip712::IEip712;␊
use stylus_sdk::abi::Bytes;␊
use stylus_sdk::prelude::{entrypoint, public, storage};␊
#[entrypoint]␊
#[storage]␊
struct MyToken {␊
#[borrow]␊
flash_mint: Erc20FlashMint,␊
#[borrow]␊
erc20_permit: Erc20Permit<Eip712>,␊
}␊
#[storage]␊
struct Eip712 {}␊
impl IEip712 for Eip712 {␊
const NAME: &'static str = "ERC-20 Permit Example";␊
const VERSION: &'static str = "1";␊
}␊
#[public]␊
#[inherit(Erc20Permit<Eip712>)]␊
impl MyToken {␊
fn max_flash_loan(&self, token: Address) -> U256 {␊
self.flash_mint.max_flash_loan(token, &self.erc20_permit)␊
}␊
fn flash_fee(&self, token: Address, value: U256) -> Result<U256, Vec<u8>> {␊
self.flash_mint.flash_fee(token, value).map_err(|e| e.into())␊
}␊
fn flash_loan(&mut self, receiver: Address, token: Address, value: U256, data: Bytes) -> Result<bool, Vec<u8>> {␊
self.flash_mint.flash_loan(receiver, token, value, data, &mut self.erc20_permit).map_err(|e| e.into())␊
}␊
}␊
`

## erc20 full - complex name

> Snapshot 1
Expand All @@ -192,14 +338,19 @@ Generated by [AVA](https://avajs.dev).
use alloc::vec::Vec;␊
use alloy_primitives::{Address, U256};␊
use openzeppelin_stylus::token::erc20::extensions::{Erc20Permit, IErc20Burnable};␊
use openzeppelin_stylus::token::erc20::extensions::{␊
Erc20FlashMint, Erc20Permit, IErc20Burnable, IErc3156FlashLender␊
};␊
use openzeppelin_stylus::token::erc20::IErc20;␊
use openzeppelin_stylus::utils::cryptography::eip712::IEip712;␊
use stylus_sdk::abi::Bytes;␊
use stylus_sdk::prelude::{entrypoint, public, storage};␊
#[entrypoint]␊
#[storage]␊
struct CustomToken {␊
#[borrow]␊
flash_mint: Erc20FlashMint,␊
#[borrow]␊
erc20_permit: Erc20Permit<Eip712>,␊
}␊
Expand All @@ -215,6 +366,18 @@ Generated by [AVA](https://avajs.dev).
#[public]␊
#[inherit(Erc20Permit<Eip712>)]␊
impl CustomToken {␊
fn max_flash_loan(&self, token: Address) -> U256 {␊
self.flash_mint.max_flash_loan(token, &self.erc20_permit)␊
}␊
fn flash_fee(&self, token: Address, value: U256) -> Result<U256, Vec<u8>> {␊
self.flash_mint.flash_fee(token, value).map_err(|e| e.into())␊
}␊
fn flash_loan(&mut self, receiver: Address, token: Address, value: U256, data: Bytes) -> Result<bool, Vec<u8>> {␊
self.flash_mint.flash_loan(receiver, token, value, data, &mut self.erc20_permit).map_err(|e| e.into())␊
}␊
fn burn(&mut self, value: U256) -> Result<(), Vec<u8>> {␊
self.erc20_permit.burn(value).map_err(|e| e.into())␊
}␊
Expand Down
Binary file modified packages/core/stylus/src/erc20.test.ts.snap
Binary file not shown.
Loading

0 comments on commit 452b4ba

Please sign in to comment.