forked from chiru-labs/ERC721A
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ERC721APausable as equivalent to ERC721Pausable (chiru-labs#205)
* Add ERC721APausable equivalent to ERC721Pausable * CR: Docs and ContractPaused() error throw * Unit tests * Include unit test for burnable composability * run linter
- Loading branch information
1 parent
67ddb34
commit 3f47769
Showing
8 changed files
with
148 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// SPDX-License-Identifier: MIT | ||
// Creator: Chiru Labs | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
import '../ERC721A.sol'; | ||
import '@openzeppelin/contracts/security/Pausable.sol'; | ||
|
||
error ContractPaused(); | ||
|
||
/** | ||
* @dev ERC721A token with pausable token transfers, minting and burning. | ||
* | ||
* Based off of OpenZeppelin's ERC721Pausable extension. | ||
* | ||
* Useful for scenarios such as preventing trades until the end of an evaluation | ||
* period, or having an emergency switch for freezing all token transfers in the | ||
* event of a large bug. | ||
*/ | ||
abstract contract ERC721APausable is ERC721A, Pausable { | ||
/** | ||
* @dev See {ERC721A-_beforeTokenTransfers}. | ||
* | ||
* Requirements: | ||
* | ||
* - the contract must not be paused. | ||
*/ | ||
function _beforeTokenTransfers( | ||
address from, | ||
address to, | ||
uint256 startTokenId, | ||
uint256 quantity | ||
) internal virtual override { | ||
super._beforeTokenTransfers(from, to, startTokenId, quantity); | ||
if (paused()) revert ContractPaused(); | ||
} | ||
} |
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,32 @@ | ||
// SPDX-License-Identifier: MIT | ||
// Creators: Chiru Labs | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
import '../extensions/ERC721APausable.sol'; | ||
import '../extensions/ERC721ABurnable.sol'; | ||
|
||
contract ERC721APausableMock is ERC721A, ERC721APausable, ERC721ABurnable { | ||
constructor(string memory name_, string memory symbol_) ERC721A(name_, symbol_) {} | ||
|
||
function safeMint(address to, uint256 quantity) public { | ||
_safeMint(to, quantity); | ||
} | ||
|
||
function _beforeTokenTransfers( | ||
address from, | ||
address to, | ||
uint256 startTokenId, | ||
uint256 quantity | ||
) internal virtual override(ERC721A, ERC721APausable) { | ||
super._beforeTokenTransfers(from, to, startTokenId, quantity); | ||
} | ||
|
||
function pause() external { | ||
_pause(); | ||
} | ||
|
||
function unpause() external { | ||
_unpause(); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -14,4 +14,4 @@ contract StartTokenIdHelper { | |
constructor(uint256 startTokenId_) { | ||
startTokenId = startTokenId_; | ||
} | ||
} | ||
} |
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,73 @@ | ||
const { deployContract } = require('../helpers.js'); | ||
const { expect } = require('chai'); | ||
|
||
const createTestSuite = ({ contract, constructorArgs }) => | ||
function () { | ||
context(`${contract}`, function () { | ||
beforeEach(async function () { | ||
this.erc721aPausable = await deployContract(contract, constructorArgs); | ||
|
||
this.startTokenId = this.erc721aPausable.startTokenId | ||
? (await this.erc721aPausable.startTokenId()).toNumber() | ||
: 0; | ||
}); | ||
|
||
beforeEach(async function () { | ||
const [owner, addr1, addr2] = await ethers.getSigners(); | ||
this.owner = owner; | ||
this.addr1 = addr1; | ||
this.addr2 = addr2; | ||
this.existingTokenID = 0; | ||
this.numTestTokens = 1; | ||
await this.erc721aPausable['safeMint(address,uint256)'](this.addr1.address, this.numTestTokens); | ||
}); | ||
|
||
it('cannot burn a valid token id while paused', async function () { | ||
await this.erc721aPausable.connect(this.owner).pause() | ||
const query = this.erc721aPausable | ||
.connect(this.addr1) | ||
.burn(this.existingTokenID); | ||
|
||
await expect(query).to.be.revertedWith('ContractPaused'); | ||
expect(await this.erc721aPausable.ownerOf(this.existingTokenID)).to.be.equal(this.addr1.address); | ||
}); | ||
|
||
it('cannot transfer a valid token id while paused', async function () { | ||
await this.erc721aPausable.connect(this.owner).pause() | ||
const query = this.erc721aPausable | ||
.connect(this.addr1) | ||
.transferFrom(this.addr1.address, this.addr2.address, this.existingTokenID); | ||
|
||
await expect(query).to.be.revertedWith('ContractPaused'); | ||
expect(await this.erc721aPausable.ownerOf(this.existingTokenID)).to.be.equal(this.addr1.address); | ||
}); | ||
|
||
it('can transfer a valid token id while unpaused', async function () { | ||
await this.erc721aPausable.connect(this.owner).pause() | ||
await this.erc721aPausable.connect(this.owner).unpause() | ||
const query = this.erc721aPausable | ||
.connect(this.addr1) | ||
.transferFrom(this.addr1.address, this.addr2.address, this.existingTokenID); | ||
|
||
await expect(query).to.not.be.revertedWith('ContractPaused'); | ||
expect(await this.erc721aPausable.ownerOf(this.existingTokenID)).to.be.equal(this.addr2.address); | ||
}); | ||
|
||
it('can burn a valid token id while unpaused', async function () { | ||
await this.erc721aPausable.connect(this.owner).pause() | ||
await this.erc721aPausable.connect(this.owner).unpause() | ||
const query = this.erc721aPausable | ||
.connect(this.addr1) | ||
.burn(this.existingTokenID); | ||
|
||
await expect(query).to.not.be.revertedWith('ContractPaused'); | ||
await expect(this.erc721aPausable.ownerOf(this.existingTokenID)) | ||
.to.be.revertedWith('OwnerQueryForNonexistentToken'); | ||
}); | ||
}); | ||
}; | ||
|
||
describe( | ||
'ERC721APausable', | ||
createTestSuite({ contract: 'ERC721APausableMock', constructorArgs: ['Azuki', 'AZUKI'] }) | ||
); |