Skip to content

Commit ac93b6c

Browse files
authored
Refactor stablecoin to build based on erc20 (#434)
1 parent f17b18f commit ac93b6c

File tree

5 files changed

+75
-276
lines changed

5 files changed

+75
-276
lines changed

packages/core/src/build-generic.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ERC721Options, buildERC721 } from './erc721';
44
import { ERC1155Options, buildERC1155 } from './erc1155';
55
import { StablecoinOptions, buildStablecoin } from './stablecoin';
66
import { GovernorOptions, buildGovernor } from './governor';
7+
import { Contract } from './contract';
78

89
export interface KindedOptions {
910
ERC20: { kind: 'ERC20' } & ERC20Options;
@@ -17,7 +18,7 @@ export interface KindedOptions {
1718

1819
export type GenericOptions = KindedOptions[keyof KindedOptions];
1920

20-
export function buildGeneric(opts: GenericOptions) {
21+
export function buildGeneric(opts: GenericOptions): Contract {
2122
switch (opts.kind) {
2223
case 'ERC20':
2324
return buildERC20(opts);

packages/core/src/erc20.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const defaults: Required<ERC20Options> = {
3939
info: commonDefaults.info,
4040
} as const;
4141

42-
function withDefaults(opts: ERC20Options): Required<ERC20Options> {
42+
export function withDefaults(opts: ERC20Options): Required<ERC20Options> {
4343
return {
4444
...opts,
4545
...withCommonDefaults(opts),
@@ -61,7 +61,7 @@ export function isAccessControlRequired(opts: Partial<ERC20Options>): boolean {
6161
return opts.mintable || opts.pausable || opts.upgradeable === 'uups';
6262
}
6363

64-
export function buildERC20(opts: ERC20Options): Contract {
64+
export function buildERC20(opts: ERC20Options): ContractBuilder {
6565
const allOpts = withDefaults(opts);
6666

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

120120
c.addOverride(ERC20, functions._update);
121+
c.addOverride(ERC20, functions._approve); // allows override from stablecoin
121122
}
122123

123124
function addPausableExtension(c: ContractBuilder, access: Access) {
@@ -202,7 +203,7 @@ function addFlashMint(c: ContractBuilder) {
202203
});
203204
}
204205

205-
const functions = defineFunctions({
206+
export const functions = defineFunctions({
206207
_update: {
207208
kind: 'internal' as const,
208209
args: [
@@ -212,6 +213,16 @@ const functions = defineFunctions({
212213
],
213214
},
214215

216+
_approve: {
217+
kind: 'internal' as const,
218+
args: [
219+
{ name: 'owner', type: 'address' },
220+
{ name: 'spender', type: 'address' },
221+
{ name: 'value', type: 'uint256' },
222+
{ name: 'emitEvent', type: 'bool' },
223+
],
224+
},
225+
215226
mint: {
216227
kind: 'public' as const,
217228
args: [

packages/core/src/stablecoin.test.ts.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,11 @@ Generated by [AVA](https://avajs.dev).
315315
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";␊
316316
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";␊
317317
318-
contract MyStablecoin is ERC20, ERC20Custodian, Ownable, ERC20Permit {␊
318+
contract MyStablecoin is ERC20, ERC20Permit, ERC20Custodian, Ownable {␊
319319
constructor(address initialOwner)␊
320320
ERC20("MyStablecoin", "MST")␊
321-
Ownable(initialOwner)␊
322321
ERC20Permit("MyStablecoin")␊
322+
Ownable(initialOwner)␊
323323
{}␊
324324
325325
function _isCustodian(address user) internal view override returns (bool) {␊
@@ -350,11 +350,11 @@ Generated by [AVA](https://avajs.dev).
350350
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";␊
351351
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";␊
352352
353-
contract MyStablecoin is ERC20, ERC20Allowlist, Ownable, ERC20Permit {␊
353+
contract MyStablecoin is ERC20, ERC20Permit, ERC20Allowlist, Ownable {␊
354354
constructor(address initialOwner)␊
355355
ERC20("MyStablecoin", "MST")␊
356-
Ownable(initialOwner)␊
357356
ERC20Permit("MyStablecoin")␊
357+
Ownable(initialOwner)␊
358358
{}␊
359359
360360
function allowUser(address user) public onlyOwner {␊
@@ -396,11 +396,11 @@ Generated by [AVA](https://avajs.dev).
396396
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";␊
397397
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";␊
398398
399-
contract MyStablecoin is ERC20, ERC20Blocklist, Ownable, ERC20Permit {␊
399+
contract MyStablecoin is ERC20, ERC20Permit, ERC20Blocklist, Ownable {␊
400400
constructor(address initialOwner)␊
401401
ERC20("MyStablecoin", "MST")␊
402-
Ownable(initialOwner)␊
403402
ERC20Permit("MyStablecoin")␊
403+
Ownable(initialOwner)␊
404404
{}␊
405405
406406
function blockUser(address user) public onlyOwner {␊
13 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)