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

Optimize _approve and add test #436

Merged
merged 2 commits into from
Nov 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
18 changes: 11 additions & 7 deletions contracts/ERC721A.sol
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ contract ERC721A is IERC721A {
/**
* Returns the packed ownership data of `tokenId`.
*/
function _packedOwnershipOf(uint256 tokenId) private view returns (uint256 packed) {
function _packedOwnershipOf(uint256 tokenId) private view returns (uint256 packed) {
if (_startTokenId() <= tokenId) {
packed = _packedOwnerships[tokenId];
// If not burned.
Expand Down Expand Up @@ -888,7 +888,6 @@ contract ERC721A is IERC721A {
// APPROVAL OPERATIONS
// =============================================================


/**
* @dev Equivalent to `_approve(to, tokenId, false)`.
*/
Expand All @@ -909,13 +908,18 @@ contract ERC721A is IERC721A {
*
* Emits an {Approval} event.
*/
function _approve(address to, uint256 tokenId, bool approvalCheck) internal virtual {
function _approve(
address to,
uint256 tokenId,
bool approvalCheck
) internal virtual {
address owner = ownerOf(tokenId);

if (approvalCheck && _msgSenderERC721A() != owner)
if (!isApprovedForAll(owner, _msgSenderERC721A())) {
revert ApprovalCallerNotOwnerNorApproved();
}
if (approvalCheck)
if (_msgSenderERC721A() != owner)
if (!isApprovedForAll(owner, _msgSenderERC721A())) {
revert ApprovalCallerNotOwnerNorApproved();
}

_tokenApprovals[tokenId].value = to;
emit Approval(owner, to, tokenId);
Expand Down
4 changes: 4 additions & 0 deletions contracts/mocks/ERC721AMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ contract ERC721AMock is ERC721A {
_setAux(owner, aux);
}

function directApprove(address to, uint256 tokenId) public {
_approve(to, tokenId);
}

function baseURI() public view returns (string memory) {
return _baseURI();
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/ERC721A.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ const createTestSuite = ({ contract, constructorArgs }) =>
await this.erc721a.connect(this.addr1).transferFrom(this.addr1.address, this.addr2.address, this.tokenId);
expect(await this.erc721a.getApproved(this.tokenId)).to.not.equal(this.addr1.address);
});

it('direct approve works', async function () {
expect(await this.erc721a.getApproved(this.tokenId)).to.not.equal(this.addr1.address);
await this.erc721a.connect(this.addr2).directApprove(this.addr1.address, this.tokenId);
expect(await this.erc721a.getApproved(this.tokenId)).to.equal(this.addr1.address);
});
});

describe('setApprovalForAll', async function () {
Expand Down