-
Notifications
You must be signed in to change notification settings - Fork 842
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
Move _setOwnersExplicit to separate extension #34
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-License-Identifier: MIT | ||
// Creator: Chiru Labs | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import '../ERC721A.sol'; | ||
|
||
abstract contract ERC721AOwnersExplicit is ERC721A { | ||
uint256 public nextOwnerToExplicitlySet = 0; | ||
|
||
/** | ||
* @dev Explicitly set `owners` to eliminate loops in future calls of ownerOf(). | ||
*/ | ||
function _setOwnersExplicit(uint256 quantity) internal { | ||
require(quantity > 0, 'quantity must be nonzero'); | ||
require(currentIndex > 0, 'no tokens minted yet'); | ||
require(nextOwnerToExplicitlySet < currentIndex, 'all ownerships have been set'); | ||
|
||
uint256 oldNextOwnerToSet = nextOwnerToExplicitlySet; | ||
uint256 endIndex = oldNextOwnerToSet + quantity - 1; | ||
// Set the end index to be the last token index | ||
if (endIndex > currentIndex - 1) { | ||
endIndex = currentIndex - 1; | ||
} | ||
|
||
for (uint256 i = oldNextOwnerToSet; i <= endIndex; i++) { | ||
if (_ownerships[i].addr == address(0)) { | ||
TokenOwnership memory ownership = ownershipOf(i); | ||
_ownerships[i] = TokenOwnership(ownership.addr, ownership.startTimestamp); | ||
} | ||
} | ||
nextOwnerToExplicitlySet = endIndex + 1; | ||
} | ||
} |
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,26 @@ | ||
// SPDX-License-Identifier: MIT | ||
// Creators: Chiru Labs | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import '../extensions/ERC721AOwnersExplicit.sol'; | ||
|
||
contract ERC721AOwnersExplicitMock is ERC721AOwnersExplicit { | ||
constructor( | ||
string memory name_, | ||
string memory symbol_, | ||
uint256 maxBatchSize_ | ||
) ERC721A(name_, symbol_, maxBatchSize_) {} | ||
|
||
function safeMint(address to, uint256 quantity) public { | ||
_safeMint(to, quantity); | ||
} | ||
|
||
function setOwnersExplicit(uint256 quantity) public { | ||
_setOwnersExplicit(quantity); | ||
} | ||
|
||
function getOwnershipAt(uint256 index) public view returns (TokenOwnership memory) { | ||
return _ownerships[index]; | ||
} | ||
} |
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,92 @@ | ||
const { expect } = require('chai'); | ||
const { constants } = require('@openzeppelin/test-helpers'); | ||
const { ZERO_ADDRESS } = constants; | ||
|
||
|
||
describe('ERC721AOwnersExplicit', function () { | ||
beforeEach(async function () { | ||
this.ERC721AOwnersExplicit = await ethers.getContractFactory('ERC721AOwnersExplicitMock'); | ||
this.token = await this.ERC721AOwnersExplicit.deploy('Azuki', 'AZUKI', 5); | ||
await this.token.deployed(); | ||
}); | ||
|
||
context('with no minted tokens', async function () { | ||
it('does not have enough tokens minted', async function () { | ||
await expect( | ||
this.token.setOwnersExplicit(1) | ||
).to.be.revertedWith('no tokens minted yet'); | ||
}); | ||
}); | ||
|
||
context('with minted tokens', async function () { | ||
beforeEach(async function () { | ||
const [owner, addr1, addr2, addr3] = await ethers.getSigners(); | ||
this.owner = owner; | ||
this.addr1 = addr1; | ||
this.addr2 = addr2; | ||
this.addr3 = addr3; | ||
// After the following mints, our ownership array will look like this: | ||
// | 1 | 2 | Empty | 3 | Empty | Empty | | ||
await this.token['safeMint(address,uint256)'](addr1.address, 1); | ||
await this.token['safeMint(address,uint256)'](addr2.address, 2); | ||
await this.token['safeMint(address,uint256)'](addr3.address, 3); | ||
}); | ||
|
||
describe('setOwnersExplicit', async function () { | ||
it('rejects 0 quantity', async function () { | ||
await expect( | ||
this.token.setOwnersExplicit(0) | ||
).to.be.revertedWith('quantity must be nonzero'); | ||
}); | ||
|
||
it('handles single increment properly', async function () { | ||
await this.token.setOwnersExplicit(1); | ||
expect(await this.token.nextOwnerToExplicitlySet()).to.equal('1'); | ||
}); | ||
|
||
it('properly sets the ownership of index 2', async function () { | ||
let ownerAtTwo = await this.token.getOwnershipAt(2); | ||
expect(ownerAtTwo[0]).to.equal(ZERO_ADDRESS); | ||
await this.token.setOwnersExplicit(3); | ||
ownerAtTwo = await this.token.getOwnershipAt(2); | ||
expect(ownerAtTwo[0]).to.equal(this.addr2.address); | ||
expect(await this.token.nextOwnerToExplicitlySet()).to.equal('3'); | ||
}); | ||
|
||
it('sets all ownerships in one go', async function () { | ||
await this.token.setOwnersExplicit(6); | ||
for (let tokenId = 0; tokenId < 6; tokenId++) { | ||
let owner = await this.token.getOwnershipAt(tokenId); | ||
expect(owner[0]).to.not.equal(ZERO_ADDRESS); | ||
} | ||
}); | ||
|
||
it('sets all ownerships with overflowing quantity', async function () { | ||
await this.token.setOwnersExplicit(15); | ||
for (let tokenId = 0; tokenId < 6; tokenId++) { | ||
let owner = await this.token.getOwnershipAt(tokenId); | ||
expect(owner[0]).to.not.equal(ZERO_ADDRESS); | ||
} | ||
}); | ||
|
||
it('sets all ownerships in multiple calls', async function () { | ||
await this.token.setOwnersExplicit(2); | ||
expect(await this.token.nextOwnerToExplicitlySet()).to.equal('2'); | ||
await this.token.setOwnersExplicit(1); | ||
expect(await this.token.nextOwnerToExplicitlySet()).to.equal('3'); | ||
await this.token.setOwnersExplicit(3); | ||
for (let tokenId = 0; tokenId < 6; tokenId++) { | ||
let owner = await this.token.getOwnershipAt(tokenId); | ||
expect(owner[0]).to.not.equal(ZERO_ADDRESS); | ||
} | ||
}); | ||
|
||
it('rejects after all ownerships have been set', async function () { | ||
await this.token.setOwnersExplicit(6); | ||
await expect( | ||
this.token.setOwnersExplicit(1) | ||
).to.be.revertedWith('all ownerships have been set'); | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to convert this to internal in order for ERC721AOwnersExplicit to read the variable. We can also redeclare ownerships in that file, I have no strong preference here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same with currentIndex above