-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
248 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import { Contract, ContractBuilder, Impl } from './contract'; | ||
import { Access, requireAccessControl, setAccessControl } from './set-access-control'; | ||
import { addPausable, setPausable } from './add-pausable'; | ||
import { defineFunctions } from './utils/define-functions'; | ||
import { CommonOptions, withCommonDefaults, getSelfArg } from './common-options'; | ||
import { setUpgradeable } from './set-upgradeable'; | ||
import { setInfo } from './set-info'; | ||
import { OptionsError } from './error'; | ||
import { defineComponents } from './utils/define-components'; | ||
import { defaults as commonDefaults } from './common-options'; | ||
import { printContract } from './print'; | ||
// import { importGetCallerAddress } from './common-functions'; | ||
|
||
export const defaults: Required<ERC721Options> = { | ||
name: 'MyToken', | ||
symbol: 'MTK', | ||
burnable: false, | ||
pausable: false, | ||
mintable: false, | ||
access: commonDefaults.access, | ||
upgradeable: commonDefaults.upgradeable, | ||
info: commonDefaults.info | ||
} as const; | ||
|
||
export function printERC721(opts: ERC721Options = defaults): string { | ||
return printContract(buildERC721(opts)); | ||
} | ||
|
||
export interface ERC721Options extends CommonOptions { | ||
name: string; | ||
symbol: string; | ||
burnable?: boolean; | ||
pausable?: boolean; | ||
mintable?: boolean; | ||
} | ||
|
||
function withDefaults(opts: ERC721Options): Required<ERC721Options> { | ||
return { | ||
...opts, | ||
...withCommonDefaults(opts), | ||
burnable: opts.burnable ?? defaults.burnable, | ||
pausable: opts.pausable ?? defaults.pausable, | ||
mintable: opts.mintable ?? defaults.mintable, | ||
}; | ||
} | ||
|
||
export function isAccessControlRequired(opts: Partial<ERC721Options>): boolean { | ||
return opts.mintable === true || opts.pausable === true || opts.upgradeable === true; | ||
} | ||
|
||
export function buildERC721(opts: ERC721Options): Contract { | ||
const c = new ContractBuilder(opts.name); | ||
|
||
const allOpts = withDefaults(opts); | ||
|
||
addBase(c, allOpts.name, allOpts.symbol); | ||
|
||
if (allOpts.burnable) { | ||
addBurnable(c); | ||
} | ||
|
||
if (allOpts.pausable) { | ||
addPausable(c, allOpts.access); | ||
if (allOpts.burnable) { | ||
setPausable(c, functions.burn); | ||
} | ||
} | ||
|
||
if (allOpts.mintable) { | ||
addMintable(c, allOpts.access); | ||
} | ||
|
||
setAccessControl(c, allOpts.access); | ||
setUpgradeable(c, allOpts.upgradeable, allOpts.access); | ||
setInfo(c, allOpts.info); | ||
|
||
return c; | ||
} | ||
|
||
function addBase(c: ContractBuilder, name: string, symbol: string) { | ||
c.addComponent( | ||
components.ERC721Component, | ||
[ | ||
name, symbol | ||
], | ||
true, | ||
); | ||
} | ||
|
||
function addBurnable(c: ContractBuilder) { | ||
c.addStandaloneImport('starknet::get_caller_address'); | ||
c.addFunction(functions.burn); | ||
} | ||
|
||
function addMintable(c: ContractBuilder, access: Access) { | ||
c.addStandaloneImport('starknet::ContractAddress'); | ||
c.addFunction(functions.safeMint); | ||
requireAccessControl(c, functions.safeMint, access, 'MINTER'); | ||
} | ||
|
||
const components = defineComponents( { | ||
ERC721Component: { | ||
path: 'openzeppelin::token::erc721', | ||
substorage: { | ||
name: 'erc721', | ||
type: 'ERC721Component::Storage', | ||
}, | ||
event: { | ||
name: 'ERC721Event', | ||
type: 'ERC721Component::Event', | ||
}, | ||
impls: [ | ||
{ | ||
name: 'ERC721Impl', | ||
value: 'ERC721Component::ERC721Impl<ContractState>', | ||
}, | ||
{ | ||
name: 'ERC721MetadataImpl', | ||
value: 'ERC721Component::ERC721MetadataImpl<ContractState>', | ||
}, | ||
{ | ||
name: 'ERC721CamelOnly', | ||
value: 'ERC721Component::ERC721CamelOnlyImpl<ContractState>', | ||
}, | ||
{ | ||
name: 'ERC721MetadataCamelOnly', | ||
value: 'ERC721Component::ERC721MetadataCamelOnlyImpl<ContractState>', | ||
} | ||
], | ||
internalImpl: { | ||
name: 'ERC721InternalImpl', | ||
value: 'ERC721Component::InternalImpl<ContractState>', | ||
}, | ||
}, | ||
}); | ||
|
||
const functions = defineFunctions({ | ||
burn: { | ||
kind: 'external(v0)', | ||
args: [ | ||
getSelfArg(), | ||
{ name: 'tokenId', type: 'u256' } | ||
], | ||
code: [ | ||
'// TODO check if caller is the token owner', | ||
'self.erc721._burn(tokenId);' | ||
] | ||
}, | ||
safeMint: { | ||
kind: 'external(v0)', | ||
args: [ | ||
getSelfArg(), | ||
{ name: 'recipient', type: 'ContractAddress' }, | ||
{ name: 'tokenId', type: 'u256' }, | ||
{ name: 'tokenURI', type: 'felt252' }, | ||
], | ||
code: [ | ||
'self.erc721._mint(recipient, tokenId);', | ||
'self.erc721._set_token_uri(tokenId, tokenURI);', | ||
] | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<script lang="ts"> | ||
import HelpTooltip from '../HelpTooltip.svelte'; | ||
import type { KindedOptions } from '@openzeppelin/wizard-cairo'; | ||
import { erc721, infoDefaults } from '@openzeppelin/wizard-cairo'; | ||
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, | ||
info: { ...infoDefaults }, // create new object since Info is nested | ||
}; | ||
$: requireAccessControl = erc721.isAccessControlRequired(opts); | ||
</script> | ||
|
||
<section class="controls-section"> | ||
<h1>Settings</h1> | ||
|
||
<div class="grid grid-cols-[2fr,1fr] gap-2"> | ||
<label class="labeled-input"> | ||
<span>Name</span> | ||
<input bind:value={opts.name}> | ||
</label> | ||
<label class="labeled-input"> | ||
<span>Symbol</span> | ||
<input bind:value={opts.symbol}> | ||
</label> | ||
</div> | ||
</section> | ||
|
||
<section class="controls-section"> | ||
<h1>Features</h1> | ||
|
||
<div class="checkbox-group"> | ||
<label class:checked={opts.mintable}> | ||
<input type="checkbox" bind:checked={opts.mintable}> | ||
Mintable | ||
<HelpTooltip link="https://docs.openzeppelin.com/contracts-cairo/erc721#presets"> | ||
Privileged accounts will be able to emit new tokens. | ||
</HelpTooltip> | ||
</label> | ||
<label class:checked={opts.burnable}> | ||
<input type="checkbox" bind:checked={opts.burnable}> | ||
Burnable | ||
<HelpTooltip link="https://docs.openzeppelin.com/contracts-cairo/erc721#presets"> | ||
Token holders will be able to destroy their tokens. | ||
</HelpTooltip> | ||
</label> | ||
<label class:checked={opts.pausable}> | ||
<input type="checkbox" bind:checked={opts.pausable}> | ||
Pausable | ||
<HelpTooltip link="https://docs.openzeppelin.com/contracts-cairo/security#pausable"> | ||
Privileged accounts will be able to pause the functionality marked with <code>assert_not_paused</code>. | ||
Useful for emergency response. | ||
</HelpTooltip> | ||
</label> | ||
</div> | ||
</section> | ||
|
||
<AccessControlSection bind:access={opts.access} required={requireAccessControl} /> | ||
|
||
<UpgradeabilitySection bind:upgradeable={opts.upgradeable} /> | ||
|
||
<InfoSection bind:info={opts.info} /> |