-
Notifications
You must be signed in to change notification settings - Fork 841
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
Add a generic interface for ownership storage hitchhiking #323
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e62ddc5
Add a generic interface for ownership storage hitchhiking
cxkoda 75e342b
Merge branch 'main' into extra-data
cxkoda 155b46c
Add own 24bit extra data field
cxkoda 7dc8f42
Clean up test and remove a rogue only
cxkoda 6d616b9
Merge branch 'main' into extra-data
Vectorized 274469b
Update IERC721AQueryable.sol
Vectorized ac8c9ab
Update ERC721ATransferCounterMock.sol
Vectorized 77d93f3
Update IERC721A.sol
Vectorized a21900b
Update IERC721AQueryable.sol
Vectorized 672fdb4
Update ERC721A.sol
Vectorized 3db68a1
Update ERC721AQueryable.sol
Vectorized 513013c
Add requested changes
Vectorized 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
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,25 @@ | ||
// SPDX-License-Identifier: MIT | ||
// ERC721A Contracts v4.0.0 | ||
// Creators: Chiru Labs | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
import './ERC721AMock.sol'; | ||
|
||
contract ERC721ATransferCounterMock is ERC721AMock { | ||
constructor(string memory name_, string memory symbol_) ERC721AMock(name_, symbol_) {} | ||
|
||
function _extraData( | ||
address from, | ||
address to, | ||
uint24 previousExtraData | ||
) internal view virtual override returns (uint256) { | ||
if (from == address(0)) { | ||
return 42; | ||
} | ||
if (to == address(0)) { | ||
return 1337; | ||
} | ||
return previousExtraData + 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
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,97 @@ | ||
const { deployContract, offsettedIndex } = require('../helpers.js'); | ||
const { expect } = require('chai'); | ||
|
||
const createTestSuite = ({ contract, constructorArgs }) => | ||
function () { | ||
let offsetted; | ||
|
||
context(`${contract}`, function () { | ||
beforeEach(async function () { | ||
this.erc721aCounter = await deployContract(contract, constructorArgs); | ||
|
||
this.startTokenId = this.erc721aCounter.startTokenId | ||
? (await this.erc721aCounter.startTokenId()).toNumber() | ||
: 0; | ||
|
||
offsetted = (...arr) => offsettedIndex(this.startTokenId, arr); | ||
}); | ||
|
||
context('with minted tokens', async function () { | ||
beforeEach(async function () { | ||
const [owner, addr1] = await ethers.getSigners(); | ||
this.owner = owner; | ||
this.addr1 = addr1; | ||
|
||
this.addr1.expected = { | ||
balance: 1, | ||
tokens: [offsetted(0)], | ||
}; | ||
|
||
this.owner.expected = { | ||
balance: 2, | ||
tokens: offsetted(1, 2), | ||
}; | ||
|
||
this.mintOrder = [this.addr1, this.owner]; | ||
|
||
for (const minter of this.mintOrder) { | ||
const balance = minter.expected.balance; | ||
if (balance > 0) { | ||
await this.erc721aCounter['safeMint(address,uint256)'](minter.address, balance); | ||
} | ||
// sanity check | ||
expect(await this.erc721aCounter.balanceOf(minter.address)).to.equal(minter.expected.balance); | ||
} | ||
}); | ||
|
||
describe('_ownershipOf', function () { | ||
it('initial', async function () { | ||
for (const minter of this.mintOrder) { | ||
for (const tokenId in minter.expected.tokens) { | ||
const ownership = await this.erc721aCounter.getOwnershipOf(tokenId); | ||
expect(ownership.extraData).to.equal(42); | ||
} | ||
} | ||
}); | ||
|
||
it('after a transfer', async function () { | ||
await this.erc721aCounter.transferFrom(this.owner.address, this.addr1.address, 1); | ||
|
||
const tests = [ | ||
{ tokenId: 0, expectedData: 42 }, | ||
{ tokenId: 1, expectedData: 43 }, | ||
{ tokenId: 2, expectedData: 42 }, | ||
]; | ||
|
||
for (const test of tests) { | ||
const ownership = await this.erc721aCounter.getOwnershipOf(test.tokenId); | ||
expect(ownership.extraData).to.equal(test.expectedData); | ||
} | ||
}); | ||
|
||
it('after a burn', async function () { | ||
await this.erc721aCounter['burn(uint256)'](2); | ||
|
||
const tests = [ | ||
{ tokenId: 0, expectedData: 42 }, | ||
{ tokenId: 1, expectedData: 42 }, | ||
{ tokenId: 2, expectedData: 1337 }, | ||
]; | ||
|
||
for (const test of tests) { | ||
const ownership = await this.erc721aCounter.getOwnershipAt(test.tokenId); | ||
expect(ownership.extraData).to.equal(test.expectedData); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
describe( | ||
'ERC721A override _extraData()', | ||
createTestSuite({ | ||
contract: 'ERC721ATransferCounterMock', | ||
constructorArgs: ['Azuki', 'AZUKI'], | ||
}) | ||
); |
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.
One of my new tests for the behavior of
_extraData
failed for burns on my machine. I was able to track it back to the_addressToUint256(from)
part in the above code. Replacing it with(_addressToUint256(from) & ((1 << 160) - 1)) |
, so zeroing the leading 96bits solved the issue. I have no idea why_addressToUint256
seems to produce dirtyuint256
s though. @VectorizedThere 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.
It looks like the compiler didn't clean the bits.
I have added some changes to ensure that.