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 recipient constructor argument for premint #435

Merged
merged 5 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- **Potentially breaking changes**:
- Add constructor argument `recipient` when using `premint` in `erc20`, `stablecoin`, and `realWorldAsset`.

## 0.5.0 (2025-01-23)

- Update to use TypeScript v5. ([#231](https://github.com/OpenZeppelin/contracts-wizard/pull/231))
Expand Down
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
21 changes: 13 additions & 8 deletions packages/core/src/erc20.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,11 @@ Generated by [AVA](https://avajs.dev).
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";␊
contract MyToken is ERC20, ERC20Permit {␊
constructor() ERC20("MyToken", "MTK") ERC20Permit("MyToken") {␊
_mint(msg.sender, 1000 * 10 ** decimals());␊
constructor(address recipient)␊
ERC20("MyToken", "MTK")␊
ERC20Permit("MyToken")␊
{␊
_mint(recipient, 1000 * 10 ** decimals());␊
}␊
}␊
`
Expand Down Expand Up @@ -463,7 +466,7 @@ Generated by [AVA](https://avajs.dev).
_disableInitializers();␊
}␊
function initialize(address defaultAdmin, address pauser, address minter)␊
function initialize(address defaultAdmin, address pauser, address recipient, address minter)␊
initializer public␊
{␊
__ERC20_init("MyToken", "MTK");␊
Expand All @@ -476,7 +479,7 @@ Generated by [AVA](https://avajs.dev).
_grantRole(DEFAULT_ADMIN_ROLE, defaultAdmin);␊
_grantRole(PAUSER_ROLE, pauser);␊
_mint(msg.sender, 2000 * 10 ** decimals());␊
_mint(recipient, 2000 * 10 ** decimals());␊
_grantRole(MINTER_ROLE, minter);␊
}␊
Expand Down Expand Up @@ -541,7 +544,7 @@ Generated by [AVA](https://avajs.dev).
_disableInitializers();␊
}␊
function initialize(address defaultAdmin, address pauser, address minter, address upgrader)␊
function initialize(address defaultAdmin, address pauser, address recipient, address minter, address upgrader)␊
initializer public␊
{␊
__ERC20_init("MyToken", "MTK");␊
Expand All @@ -555,7 +558,7 @@ Generated by [AVA](https://avajs.dev).
_grantRole(DEFAULT_ADMIN_ROLE, defaultAdmin);␊
_grantRole(PAUSER_ROLE, pauser);␊
_mint(msg.sender, 2000 * 10 ** decimals());␊
_mint(recipient, 2000 * 10 ** decimals());␊
_grantRole(MINTER_ROLE, minter);␊
_grantRole(UPGRADER_ROLE, upgrader);␊
}␊
Expand Down Expand Up @@ -623,7 +626,9 @@ Generated by [AVA](https://avajs.dev).
_disableInitializers();␊
}␊
function initialize(address initialAuthority) initializer public {␊
function initialize(address initialAuthority, address recipient)␊
initializer public␊
{␊
__ERC20_init("MyToken", "MTK");␊
__ERC20Burnable_init();␊
__ERC20Pausable_init();␊
Expand All @@ -633,7 +638,7 @@ Generated by [AVA](https://avajs.dev).
__ERC20FlashMint_init();␊
__UUPSUpgradeable_init();␊
_mint(msg.sender, 2000 * 10 ** decimals());␊
_mint(recipient, 2000 * 10 ** decimals());␊
}␊
function pause() public restricted {␊
Expand Down
Binary file modified packages/core/src/erc20.test.ts.snap
Binary file not shown.
20 changes: 16 additions & 4 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 @@ -152,7 +153,8 @@ function addPremint(c: ContractBuilder, amount: string) {
const zeroes = new Array(Math.max(0, -decimalPlace)).fill('0').join('');
const units = integer + decimals + zeroes;
const exp = decimalPlace <= 0 ? 'decimals()' : `(decimals() - ${decimalPlace})`;
c.addConstructorCode(`_mint(msg.sender, ${units} * 10 ** ${exp});`);
c.addConstructorArgument({type: 'address', name: 'recipient'});
c.addConstructorCode(`_mint(recipient, ${units} * 10 ** ${exp});`);
}
}
}
Expand Down Expand Up @@ -202,7 +204,7 @@ function addFlashMint(c: ContractBuilder) {
});
}

const functions = defineFunctions({
export const functions = defineFunctions({
_update: {
kind: 'internal' as const,
args: [
Expand All @@ -212,6 +214,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
19 changes: 11 additions & 8 deletions packages/core/src/stablecoin.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,11 @@ Generated by [AVA](https://avajs.dev).
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";␊
contract MyStablecoin is ERC20, ERC20Permit {␊
constructor() ERC20("MyStablecoin", "MST") ERC20Permit("MyStablecoin") {␊
_mint(msg.sender, 1000 * 10 ** decimals());␊
constructor(address recipient)␊
ERC20("MyStablecoin", "MST")␊
ERC20Permit("MyStablecoin")␊
{␊
_mint(recipient, 1000 * 10 ** decimals());␊
}␊
}␊
`
Expand Down Expand Up @@ -315,11 +318,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 +353,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 +399,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