Skip to content

Commit

Permalink
Add royalty option to ERC721 and ERC1155
Browse files Browse the repository at this point in the history
  • Loading branch information
gmertk committed Jan 3, 2022
1 parent 8a5565c commit 29d903e
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/erc1155.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ testERC1155('basic + roles', {
access: 'roles',
});

testERC1155('royalty', {
royaltyRecipient: '0',
royaltyFraction: '2000',
});

testERC1155('burnable', {
burnable: true,
});
Expand Down
24 changes: 24 additions & 0 deletions packages/core/src/erc1155.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,27 @@ Generated by [AVA](https://avajs.dev).
}␊
}␊
`

## royalty

> Snapshot 1
`// SPDX-License-Identifier: MIT␊
pragma solidity ^0.8.2;␊
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";␊
import "@openzeppelin/contracts/access/Ownable.sol";␊
import "@openzeppelin/contracts/token/ERC721/extensions/ERC1155Royalty.sol";␊
contract MyToken is ERC1155, Ownable, ERC1155Royalty {␊
constructor()␊
ERC1155("https://gateway.pinata.cloud/ipfs/QmcP9hxrnC1T5ATPmq2saFeAM1ypFX9BnAswCdHB9JCjLA/")␊
{␊
_setDefaultRoyalty(address(0), 2000);␊
}␊
function setURI(string memory newuri) public onlyOwner {␊
_setURI(newuri);␊
}␊
}␊
`
Binary file modified packages/core/src/erc1155.test.ts.snap
Binary file not shown.
15 changes: 15 additions & 0 deletions packages/core/src/erc1155.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { defineFunctions } from './utils/define-functions';
import { CommonOptions, withCommonDefaults } from './common-options';
import { setUpgradeable } from './set-upgradeable';
import { setInfo } from './set-info';
import { setRoyalty } from './set-royalty';

export interface ERC1155Options extends CommonOptions {
name: string;
uri: string;
royaltyRecipient?: string;
royaltyFraction?: string;
burnable?: boolean;
pausable?: boolean;
mintable?: boolean;
Expand All @@ -24,6 +27,18 @@ export function buildERC1155(opts: ERC1155Options): Contract {
addBase(c, opts.uri);
addSetUri(c, access);

if (opts.royaltyRecipient && opts.royaltyFraction) {
setRoyalty(
c,
{
name: 'ERC1155Royalty',
path: '@openzeppelin/contracts/token/ERC721/extensions/ERC1155Royalty.sol',
},
opts.royaltyRecipient,
opts.royaltyFraction
);
}

if (opts.pausable) {
addPausable(c, access, [functions._beforeTokenTransfer]);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/erc721.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ testERC721('base uri', {
baseUri: 'https://gateway.pinata.cloud/ipfs/QmcP9hxrnC1T5ATPmq2saFeAM1ypFX9BnAswCdHB9JCjLA/',
});

testERC721('royalty', {
royaltyRecipient: '0',
royaltyFraction: '2000',
});

testERC721('enumerable', {
enumerable: true,
});
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/erc721.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,20 @@ Generated by [AVA](https://avajs.dev).
}␊
}␊
`

## royalty

> Snapshot 1
`// SPDX-License-Identifier: MIT␊
pragma solidity ^0.8.2;␊
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";␊
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Royalty.sol";␊
contract MyToken is ERC721, ERC721Royalty {␊
constructor() ERC721("MyToken", "MTK") {␊
_setDefaultRoyalty(address(0), 2000);␊
}␊
}␊
`
Binary file modified packages/core/src/erc721.test.ts.snap
Binary file not shown.
15 changes: 15 additions & 0 deletions packages/core/src/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import { defineFunctions } from './utils/define-functions';
import { CommonOptions, withCommonDefaults } from './common-options';
import { setUpgradeable } from './set-upgradeable';
import { setInfo } from './set-info';
import { setRoyalty } from './set-royalty';

export interface ERC721Options extends CommonOptions {
name: string;
symbol: string;
baseUri?: string;
royaltyRecipient?: string;
royaltyFraction?: string;
enumerable?: boolean;
uriStorage?: boolean;
burnable?: boolean;
Expand All @@ -30,6 +33,18 @@ export function buildERC721(opts: ERC721Options): Contract {
addBaseURI(c, opts.baseUri);
}

if (opts.royaltyRecipient && opts.royaltyFraction) {
setRoyalty(
c,
{
name: "ERC721Royalty",
path: "@openzeppelin/contracts/token/ERC721/extensions/ERC721Royalty.sol",
},
opts.royaltyRecipient,
opts.royaltyFraction
);
}

if (opts.enumerable) {
addEnumerable(c);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/generate/erc1155.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const booleans = [true, false];
const blueprint = {
name: ['MyToken'],
uri: ['https://example.com/'],
royaltyRecipient: ['0'],
royaltyFraction: ['0'],
burnable: booleans,
pausable: booleans,
mintable: booleans,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/generate/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const blueprint = {
name: ['MyToken'],
symbol: ['MTK'],
baseUri: ['https://example.com/'],
royaltyRecipient: ['0'],
royaltyFraction: ['0'],
enumerable: booleans,
uriStorage: booleans,
burnable: booleans,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ export { OptionsError } from './error';

export type { Kind } from './kind';
export { sanitizeKind } from './kind';

export { royaltyFractionPattern } from './set-royalty'
12 changes: 12 additions & 0 deletions packages/core/src/set-royalty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { ContractBuilder, ParentContract } from './contract';

export const royaltyFractionPattern = /^\d+$/;

export function setRoyalty(c: ContractBuilder, parent: ParentContract, recipient: string, fraction: string) {
if(!royaltyFractionPattern.test(fraction)) {
return;
}

c.addParent(parent);
c.addConstructorCode(`_setDefaultRoyalty(address(${recipient}), ${fraction});`);
}
18 changes: 17 additions & 1 deletion packages/ui/src/ERC1155Controls.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import HelpTooltip from './HelpTooltip.svelte';
import type { KindedOptions } from '@openzeppelin/wizard';
import { infoDefaults } from '@openzeppelin/wizard';
import { royaltyFractionPattern, infoDefaults } from '@openzeppelin/wizard';
import AccessControlSection from './AccessControlSection.svelte';
import UpgradeabilitySection from './UpgradeabilitySection.svelte';
Expand All @@ -12,6 +12,8 @@
kind: 'ERC1155',
name: 'MyToken',
uri: '',
royaltyRecipient: '',
royaltyFraction: '',
burnable: false,
pausable: false,
mintable: false,
Expand All @@ -36,6 +38,20 @@
</span>
<input bind:value={opts.uri} placeholder="https://...">
</label>
<label class="labeled-input">
<span class="flex justify-between pr-2">
Royalty Recipient
<HelpTooltip>Sets the royalty recipient.</HelpTooltip>
</span>
<input bind:value={opts.royaltyRecipient} placeholder="0x...">
</label>
<label class="labeled-input">
<span class="flex justify-between pr-2">
Royalty Fraction
<HelpTooltip>Sets the royalty fraction.</HelpTooltip>
</span>
<input bind:value={opts.royaltyFraction} placeholder="1000" pattern={royaltyFractionPattern.source}>
</label>
</section>

<section class="controls-section">
Expand Down
18 changes: 17 additions & 1 deletion packages/ui/src/ERC721Controls.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import HelpTooltip from './HelpTooltip.svelte';
import type { KindedOptions } from '@openzeppelin/wizard';
import { infoDefaults } from '@openzeppelin/wizard';
import { royaltyFractionPattern, infoDefaults } from '@openzeppelin/wizard';
import AccessControlSection from './AccessControlSection.svelte';
import UpgradeabilitySection from './UpgradeabilitySection.svelte';
Expand All @@ -14,6 +14,8 @@
name: 'MyToken',
symbol: 'MTK',
baseUri: '',
royaltyRecipient: '',
royaltyFraction: '',
enumerable: false,
uriStorage: false,
burnable: false,
Expand Down Expand Up @@ -62,6 +64,20 @@
</span>
<input bind:value={opts.baseUri} placeholder="https://...">
</label>
<label class="labeled-input">
<span class="flex justify-between pr-2">
Royalty Recipient
<HelpTooltip>Sets the royalty recipient.</HelpTooltip>
</span>
<input bind:value={opts.royaltyRecipient} placeholder="0x...">
</label>
<label class="labeled-input">
<span class="flex justify-between pr-2">
Royalty Fraction
<HelpTooltip>Sets the royalty fraction.</HelpTooltip>
</span>
<input bind:value={opts.royaltyFraction} placeholder="1000" pattern={royaltyFractionPattern.source}>
</label>
</section>

<section class="controls-section">
Expand Down

0 comments on commit 29d903e

Please sign in to comment.