Skip to content

Commit 29d903e

Browse files
committed
Add royalty option to ERC721 and ERC1155
1 parent 8a5565c commit 29d903e

14 files changed

+133
-2
lines changed

packages/core/src/erc1155.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ testERC1155('basic + roles', {
2020
access: 'roles',
2121
});
2222

23+
testERC1155('royalty', {
24+
royaltyRecipient: '0',
25+
royaltyFraction: '2000',
26+
});
27+
2328
testERC1155('burnable', {
2429
burnable: true,
2530
});

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,27 @@ Generated by [AVA](https://avajs.dev).
441441
}␊
442442
}␊
443443
`
444+
445+
## royalty
446+
447+
> Snapshot 1
448+
449+
`// SPDX-License-Identifier: MIT␊
450+
pragma solidity ^0.8.2;␊
451+
452+
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";␊
453+
import "@openzeppelin/contracts/access/Ownable.sol";␊
454+
import "@openzeppelin/contracts/token/ERC721/extensions/ERC1155Royalty.sol";␊
455+
456+
contract MyToken is ERC1155, Ownable, ERC1155Royalty {␊
457+
constructor()␊
458+
ERC1155("https://gateway.pinata.cloud/ipfs/QmcP9hxrnC1T5ATPmq2saFeAM1ypFX9BnAswCdHB9JCjLA/")␊
459+
{␊
460+
_setDefaultRoyalty(address(0), 2000);␊
461+
}␊
462+
463+
function setURI(string memory newuri) public onlyOwner {␊
464+
_setURI(newuri);␊
465+
}␊
466+
}␊
467+
`
80 Bytes
Binary file not shown.

packages/core/src/erc1155.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import { defineFunctions } from './utils/define-functions';
66
import { CommonOptions, withCommonDefaults } from './common-options';
77
import { setUpgradeable } from './set-upgradeable';
88
import { setInfo } from './set-info';
9+
import { setRoyalty } from './set-royalty';
910

1011
export interface ERC1155Options extends CommonOptions {
1112
name: string;
1213
uri: string;
14+
royaltyRecipient?: string;
15+
royaltyFraction?: string;
1316
burnable?: boolean;
1417
pausable?: boolean;
1518
mintable?: boolean;
@@ -24,6 +27,18 @@ export function buildERC1155(opts: ERC1155Options): Contract {
2427
addBase(c, opts.uri);
2528
addSetUri(c, access);
2629

30+
if (opts.royaltyRecipient && opts.royaltyFraction) {
31+
setRoyalty(
32+
c,
33+
{
34+
name: 'ERC1155Royalty',
35+
path: '@openzeppelin/contracts/token/ERC721/extensions/ERC1155Royalty.sol',
36+
},
37+
opts.royaltyRecipient,
38+
opts.royaltyFraction
39+
);
40+
}
41+
2742
if (opts.pausable) {
2843
addPausable(c, access, [functions._beforeTokenTransfer]);
2944
}

packages/core/src/erc721.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ testERC721('base uri', {
2020
baseUri: 'https://gateway.pinata.cloud/ipfs/QmcP9hxrnC1T5ATPmq2saFeAM1ypFX9BnAswCdHB9JCjLA/',
2121
});
2222

23+
testERC721('royalty', {
24+
royaltyRecipient: '0',
25+
royaltyFraction: '2000',
26+
});
27+
2328
testERC721('enumerable', {
2429
enumerable: true,
2530
});

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,20 @@ Generated by [AVA](https://avajs.dev).
464464
}␊
465465
}␊
466466
`
467+
468+
## royalty
469+
470+
> Snapshot 1
471+
472+
`// SPDX-License-Identifier: MIT␊
473+
pragma solidity ^0.8.2;␊
474+
475+
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";␊
476+
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Royalty.sol";␊
477+
478+
contract MyToken is ERC721, ERC721Royalty {␊
479+
constructor() ERC721("MyToken", "MTK") {␊
480+
_setDefaultRoyalty(address(0), 2000);␊
481+
}␊
482+
}␊
483+
`

packages/core/src/erc721.test.ts.snap

73 Bytes
Binary file not shown.

packages/core/src/erc721.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import { defineFunctions } from './utils/define-functions';
66
import { CommonOptions, withCommonDefaults } from './common-options';
77
import { setUpgradeable } from './set-upgradeable';
88
import { setInfo } from './set-info';
9+
import { setRoyalty } from './set-royalty';
910

1011
export interface ERC721Options extends CommonOptions {
1112
name: string;
1213
symbol: string;
1314
baseUri?: string;
15+
royaltyRecipient?: string;
16+
royaltyFraction?: string;
1417
enumerable?: boolean;
1518
uriStorage?: boolean;
1619
burnable?: boolean;
@@ -30,6 +33,18 @@ export function buildERC721(opts: ERC721Options): Contract {
3033
addBaseURI(c, opts.baseUri);
3134
}
3235

36+
if (opts.royaltyRecipient && opts.royaltyFraction) {
37+
setRoyalty(
38+
c,
39+
{
40+
name: "ERC721Royalty",
41+
path: "@openzeppelin/contracts/token/ERC721/extensions/ERC721Royalty.sol",
42+
},
43+
opts.royaltyRecipient,
44+
opts.royaltyFraction
45+
);
46+
}
47+
3348
if (opts.enumerable) {
3449
addEnumerable(c);
3550
}

packages/core/src/generate/erc1155.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const booleans = [true, false];
99
const blueprint = {
1010
name: ['MyToken'],
1111
uri: ['https://example.com/'],
12+
royaltyRecipient: ['0'],
13+
royaltyFraction: ['0'],
1214
burnable: booleans,
1315
pausable: booleans,
1416
mintable: booleans,

packages/core/src/generate/erc721.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const blueprint = {
1010
name: ['MyToken'],
1111
symbol: ['MTK'],
1212
baseUri: ['https://example.com/'],
13+
royaltyRecipient: ['0'],
14+
royaltyFraction: ['0'],
1315
enumerable: booleans,
1416
uriStorage: booleans,
1517
burnable: booleans,

0 commit comments

Comments
 (0)