Skip to content

Commit

Permalink
Refactor stablecoin to build based on erc20 (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau authored Feb 5, 2025
1 parent f17b18f commit ac93b6c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 276 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/build-generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ERC721Options, buildERC721 } from './erc721';
import { ERC1155Options, buildERC1155 } from './erc1155';
import { StablecoinOptions, buildStablecoin } from './stablecoin';
import { GovernorOptions, buildGovernor } from './governor';
import { Contract } from './contract';

export interface KindedOptions {
ERC20: { kind: 'ERC20' } & ERC20Options;
Expand All @@ -17,7 +18,7 @@ export interface KindedOptions {

export type GenericOptions = KindedOptions[keyof KindedOptions];

export function buildGeneric(opts: GenericOptions) {
export function buildGeneric(opts: GenericOptions): Contract {
switch (opts.kind) {
case 'ERC20':
return buildERC20(opts);
Expand Down
17 changes: 14 additions & 3 deletions packages/core/src/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const defaults: Required<ERC20Options> = {
info: commonDefaults.info,
} as const;

function withDefaults(opts: ERC20Options): Required<ERC20Options> {
export function withDefaults(opts: ERC20Options): Required<ERC20Options> {
return {
...opts,
...withCommonDefaults(opts),
Expand All @@ -61,7 +61,7 @@ export function isAccessControlRequired(opts: Partial<ERC20Options>): boolean {
return opts.mintable || opts.pausable || opts.upgradeable === 'uups';
}

export function buildERC20(opts: ERC20Options): Contract {
export function buildERC20(opts: ERC20Options): ContractBuilder {
const allOpts = withDefaults(opts);

const c = new ContractBuilder(allOpts.name);
Expand Down Expand Up @@ -118,6 +118,7 @@ function addBase(c: ContractBuilder, name: string, symbol: string) {
);

c.addOverride(ERC20, functions._update);
c.addOverride(ERC20, functions._approve); // allows override from stablecoin
}

function addPausableExtension(c: ContractBuilder, access: Access) {
Expand Down Expand Up @@ -202,7 +203,7 @@ function addFlashMint(c: ContractBuilder) {
});
}

const functions = defineFunctions({
export const functions = defineFunctions({
_update: {
kind: 'internal' as const,
args: [
Expand All @@ -212,6 +213,16 @@ const functions = defineFunctions({
],
},

_approve: {
kind: 'internal' as const,
args: [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'emitEvent', type: 'bool' },
],
},

mint: {
kind: 'public' as const,
args: [
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/stablecoin.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,11 @@ Generated by [AVA](https://avajs.dev).
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";␊
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";␊
contract MyStablecoin is ERC20, ERC20Custodian, Ownable, ERC20Permit {␊
contract MyStablecoin is ERC20, ERC20Permit, ERC20Custodian, Ownable {␊
constructor(address initialOwner)␊
ERC20("MyStablecoin", "MST")␊
Ownable(initialOwner)␊
ERC20Permit("MyStablecoin")␊
Ownable(initialOwner)␊
{}␊
function _isCustodian(address user) internal view override returns (bool) {␊
Expand Down Expand Up @@ -350,11 +350,11 @@ Generated by [AVA](https://avajs.dev).
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";␊
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";␊
contract MyStablecoin is ERC20, ERC20Allowlist, Ownable, ERC20Permit {␊
contract MyStablecoin is ERC20, ERC20Permit, ERC20Allowlist, Ownable {␊
constructor(address initialOwner)␊
ERC20("MyStablecoin", "MST")␊
Ownable(initialOwner)␊
ERC20Permit("MyStablecoin")␊
Ownable(initialOwner)␊
{}␊
function allowUser(address user) public onlyOwner {␊
Expand Down Expand Up @@ -396,11 +396,11 @@ Generated by [AVA](https://avajs.dev).
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";␊
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";␊
contract MyStablecoin is ERC20, ERC20Blocklist, Ownable, ERC20Permit {␊
contract MyStablecoin is ERC20, ERC20Permit, ERC20Blocklist, Ownable {␊
constructor(address initialOwner)␊
ERC20("MyStablecoin", "MST")␊
Ownable(initialOwner)␊
ERC20Permit("MyStablecoin")␊
Ownable(initialOwner)␊
{}␊
function blockUser(address user) public onlyOwner {␊
Expand Down
Binary file modified packages/core/src/stablecoin.test.ts.snap
Binary file not shown.
Loading

0 comments on commit ac93b6c

Please sign in to comment.