Skip to content
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 function to directly set extra data #336

Merged
merged 8 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions contracts/ERC721A.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ contract ERC721A is IERC721A {
// The bit position of `extraData` in packed ownership.
uint256 private constant BITPOS_EXTRA_DATA = 232;

// Mask of all 256 bits in a packed ownership except the 24 bits for `extraData`.
uint256 private constant BITMASK_EXTRA_DATA_COMPLEMENT = (1 << 232) - 1;

// The mask of the lower 160 bits for addresses.
uint256 private constant BITMASK_ADDRESS = (1 << 160) - 1;

// The maximum `quantity` that can be minted with `_mintERC2309`.
// This limit is to prevent overflows on the address data entries.
// For a limit of 5000, a total of 3.689e15 calls to `_mintERC2309`
Expand Down Expand Up @@ -208,8 +211,8 @@ contract ERC721A is IERC721A {
function _setAux(address owner, uint64 aux) internal {
uint256 packed = _packedAddressData[owner];
uint256 auxCasted;
// Cast `aux` with assembly to avoid redundant masking.
assembly {
// Cast aux without masking.
auxCasted := aux
}
packed = (packed & BITMASK_AUX_COMPLEMENT) | (auxCasted << BITPOS_AUX);
Expand Down Expand Up @@ -763,6 +766,21 @@ contract ERC721A is IERC721A {
}
}

/**
* @dev Directly sets the extra data for the ownership data `index`.
*/
function _setExtraDataAt(uint256 index, uint24 extraData) internal {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily specific to this PR, but we use index and tokenId to access _packedOwnerships. We should probably make these consistent in a a future PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index refers to something that may not be initialized, and will expose the user to the underlying logic.

tokenId refers to something that is initialized.

uint256 packed = _packedOwnerships[index];
if (packed == 0) revert OwnershipNotInitializedForExtraData();
uint256 extraDataCasted;
// Cast `extraData` with assembly to avoid redundant masking.
assembly {
extraDataCasted := extraData
}
packed = (packed & BITMASK_EXTRA_DATA_COMPLEMENT) | (extraDataCasted << BITPOS_EXTRA_DATA);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, is this cheaper to do in assembly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. That's why I removed it.

_packedOwnerships[index] = packed;
}

/**
* @dev Returns the next extra data for the packed ownership data.
* The returned result is shifted into position.
Expand All @@ -771,12 +789,9 @@ contract ERC721A is IERC721A {
address from,
address to,
uint256 prevOwnershipPacked
) internal view virtual returns (uint256) {
uint24 previousExtraData;
assembly {
previousExtraData := shr(BITPOS_EXTRA_DATA, prevOwnershipPacked)
}
return uint256(_extraData(from, to, previousExtraData)) << BITPOS_EXTRA_DATA;
) private view returns (uint256) {
uint24 extraData = uint24(prevOwnershipPacked >> BITPOS_EXTRA_DATA);
return uint256(_extraData(from, to, extraData)) << BITPOS_EXTRA_DATA;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions contracts/IERC721A.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ interface IERC721A {
*/
error MintERC2309QuantityExceedsLimit();

/**
* The `extraData` cannot be set on an unintialized ownership slot.
*/
error OwnershipNotInitializedForExtraData();

struct TokenOwnership {
// The address of the owner.
address addr;
Expand Down
4 changes: 4 additions & 0 deletions contracts/mocks/ERC721ATransferCounterMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ contract ERC721ATransferCounterMock is ERC721AMock {
}
return previousExtraData + 1;
}

function setExtraDataAt(uint256 index, uint24 extraData) public {
_setExtraDataAt(index, extraData);
}
}
5 changes: 0 additions & 5 deletions test/ERC721A.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,3 @@ describe('ERC721A with ERC2309', async function () {
await expect(deployContract('ERC721AWithERC2309Mock', args)).to.be.revertedWith('MintZeroQuantity');
});
});

describe(
'ERC721A override _extraData()',
createTestSuite({ contract: 'ERC721ATransferCounterMock', constructorArgs: ['Azuki', 'AZUKI'] })
);
33 changes: 22 additions & 11 deletions test/extensions/ERC721ATransferCounter.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
const { deployContract, offsettedIndex } = require('../helpers.js');
const { deployContract } = 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 () {
Expand All @@ -24,12 +16,12 @@ const createTestSuite = ({ contract, constructorArgs }) =>

this.addr1.expected = {
balance: 1,
tokens: [offsetted(0)],
tokens: [0],
};

this.owner.expected = {
balance: 2,
tokens: offsetted(1, 2),
tokens: [1, 2],
};

this.mintOrder = [this.addr1, this.owner];
Expand Down Expand Up @@ -84,6 +76,25 @@ const createTestSuite = ({ contract, constructorArgs }) =>
}
});
});

describe('setExtraData', function () {
it('can set and get the extraData directly', async function () {
const extraData = 12345;
await this.erc721aCounter.setExtraDataAt(0, extraData);
const ownership = await this.erc721aCounter.getOwnershipAt(0);
expect(ownership.extraData).to.equal(extraData);
});

it('setting the extraData for uninitialized slot reverts', async function () {
const extraData = 12345;
await expect(this.erc721aCounter.setExtraDataAt(2, extraData))
.to.be.revertedWith('OwnershipNotInitializedForExtraData');
await this.erc721aCounter.transferFrom(this.owner.address, this.addr1.address, 2);
await this.erc721aCounter.setExtraDataAt(2, extraData);
const ownership = await this.erc721aCounter.getOwnershipAt(2);
expect(ownership.extraData).to.equal(extraData);
});
});
});
});
};
Expand Down