Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Erc20FlashMint #451

Merged
merged 13 commits into from
Feb 27, 2025
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
Copy link
Contributor Author

@0xNeshi 0xNeshi Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The packageManager fields gets auto-added on each yarn command, and I'm getting tired of removing it.

Will temporarily ignore package.json and revert the change before merging stylus to master

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
Loading