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 support for ERC2981 royalty #151

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
34 changes: 30 additions & 4 deletions packages/core/src/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface ERC721Options extends CommonOptions {
baseUri?: string;
enumerable?: boolean;
uriStorage?: boolean;
royalty?: boolean;
burnable?: boolean;
pausable?: boolean;
mintable?: boolean;
Expand All @@ -27,6 +28,7 @@ export const defaults: Required<ERC721Options> = {
baseUri: '',
enumerable: false,
uriStorage: false,
royalty: false,
burnable: false,
pausable: false,
mintable: false,
Expand All @@ -44,6 +46,7 @@ function withDefaults(opts: ERC721Options): Required<ERC721Options> {
baseUri: opts.baseUri ?? defaults.baseUri,
enumerable: opts.enumerable ?? defaults.enumerable,
uriStorage: opts.uriStorage ?? defaults.uriStorage,
royalty: opts.royalty ?? defaults.royalty,
burnable: opts.burnable ?? defaults.burnable,
pausable: opts.pausable ?? defaults.pausable,
mintable: opts.mintable ?? defaults.mintable,
Expand Down Expand Up @@ -81,6 +84,10 @@ export function buildERC721(opts: ERC721Options): Contract {
addURIStorage(c);
}

if (allOpts.royalty) {
addRoyalty(c, access);
}

if (allOpts.pausable) {
addPausable(c, access, [functions._beforeTokenTransfer]);
}
Expand All @@ -90,7 +97,7 @@ export function buildERC721(opts: ERC721Options): Contract {
}

if (allOpts.mintable) {
addMintable(c, access, allOpts.incremental, allOpts.uriStorage);
addMintable(c, access, allOpts.incremental, allOpts.uriStorage, allOpts.royalty);
}

if (allOpts.votes) {
Expand Down Expand Up @@ -145,15 +152,25 @@ function addURIStorage(c: ContractBuilder) {
c.addOverride('ERC721URIStorage', functions.tokenURI);
}

function addRoyalty(c: ContractBuilder, access: Access) {
c.addParent({
name: 'ERC721Royalty',
path: '@openzeppelin/contracts/token/ERC721/extensions/ERC721Royalty.sol',
});

c.addOverride('ERC721Royalty', supportsInterface);
c.addOverride('ERC721Royalty', functions._burn);
}

function addBurnable(c: ContractBuilder) {
c.addParent({
name: 'ERC721Burnable',
path: '@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol',
});
}

function addMintable(c: ContractBuilder, access: Access, incremental = false, uriStorage = false) {
const fn = getMintFunction(incremental, uriStorage);
function addMintable(c: ContractBuilder, access: Access, incremental = false, uriStorage = false, royalty = false) {
const fn = getMintFunction(incremental, uriStorage, royalty);
requireAccessControl(c, fn, access, 'MINTER');

if (incremental) {
Expand All @@ -172,6 +189,10 @@ function addMintable(c: ContractBuilder, access: Access, incremental = false, ur
if (uriStorage) {
c.addFunctionCode('_setTokenURI(tokenId, uri);', fn);
}

if (royalty) {
c.addFunctionCode('_setTokenRoyalty(tokenId, receiver, fee);', fn);
}
}

function addVotes(c: ContractBuilder, name: string) {
Expand Down Expand Up @@ -233,7 +254,7 @@ const functions = defineFunctions({
},
});

function getMintFunction(incremental: boolean, uriStorage: boolean) {
function getMintFunction(incremental: boolean, uriStorage: boolean, royalty: boolean) {
const fn = {
name: 'safeMint',
kind: 'public' as const,
Expand All @@ -250,5 +271,10 @@ function getMintFunction(incremental: boolean, uriStorage: boolean) {
fn.args.push({ name: 'uri', type: 'string memory' });
}

if (royalty) {
fn.args.push({ name: 'receiver', type: 'address' });
fn.args.push({ name: 'fee', type: 'uint96' });
}

return fn;
}
1 change: 1 addition & 0 deletions packages/core/src/generate/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const blueprint = {
baseUri: ['https://example.com/'],
enumerable: booleans,
uriStorage: booleans,
royalty: booleans,
burnable: booleans,
pausable: booleans,
mintable: booleans,
Expand Down
11 changes: 9 additions & 2 deletions packages/ui/src/ERC721Controls.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import type { KindedOptions } from '@openzeppelin/wizard';
import { erc721, infoDefaults } from '@openzeppelin/wizard';

import AccessControlSection from './AccessControlSection.svelte';
import UpgradeabilitySection from './UpgradeabilitySection.svelte';
import InfoSection from './InfoSection.svelte';

export const opts: Required<KindedOptions['ERC721']> = {
kind: 'ERC721',
...erc721.defaults,
Expand Down Expand Up @@ -109,6 +109,13 @@
Allows updating token URIs for individual token IDs.
</HelpTooltip>
</label>
<label class:checked={opts.royalty}>
<input type="checkbox" bind:checked={opts.royalty}>
Royalties
<HelpTooltip link="https://docs.openzeppelin.com/contracts/4.x/api/token/erc721#ERC721Royalty">
Allows settings royalites on token transfers.
</HelpTooltip>
</label>
</div>
</section>

Expand Down