Skip to content

Commit

Permalink
emit NonceChanged event when the nonce changed
Browse files Browse the repository at this point in the history
  • Loading branch information
0xanders committed Jul 13, 2023
1 parent dd90917 commit 70c6463
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
10 changes: 8 additions & 2 deletions EIPS/eip-5008.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ title: ERC-721 Nonce Extension
description: Add a `nonce` function to ERC-721.
author: Anders (@0xanders), Lance (@LanceSnow), Shrug <[email protected]>
discussions-to: https://ethereum-magicians.org/t/eip5008-eip-721-nonce-and-metadata-update-extension/8925
status: Final
status: Last Call
last-call-deadline: 2023-08-15
type: Standards Track
category: ERC
created: 2022-04-10
Expand All @@ -26,7 +27,12 @@ This EIP proposes adding a `nonce` property to ERC-721 tokens, and the `nonce` w
The keywords "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY" and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

```solidity
/// @dev the ERC-165 identifier for this interface is 0xce03fdab.
interface IERC5008 /* is IERC165 */ {
/// @notice Emitted when the `nonce` of an NFT is changed
event NonceChanged(uint256 tokenId, uint256 nonce);
/// @notice Get the nonce of an NFT
/// Throws if `tokenId` is not a valid NFT
/// @param tokenId The id of the NFT
Expand Down Expand Up @@ -56,7 +62,7 @@ Run:
```sh
cd ../assets/eip-5008
npm install
npx hardhat test ./test/test.ts
npm run test
```

## Reference Implementation
Expand Down
1 change: 1 addition & 0 deletions assets/eip-5008/contracts/ERC5008.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ contract ERC5008 is ERC721, IERC5008 {
) internal virtual override{
super._beforeTokenTransfer(from, to, tokenId);
_tokenNonce[tokenId]++;
emit NonceChanged(tokenId, _tokenNonce[tokenId]);
}

/// @dev See {IERC165-supportsInterface}.
Expand Down
4 changes: 4 additions & 0 deletions assets/eip-5008/contracts/ERC5008Demo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ contract ERC5008Demo is ERC5008{
function mint(address to, uint256 tokenId) public {
_mint(to, tokenId);
}

function getInterfaceId() public pure returns (bytes4) {
return type(IERC5008).interfaceId;
}
}
3 changes: 3 additions & 0 deletions assets/eip-5008/contracts/IERC5008.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
pragma solidity ^0.8.0;

interface IERC5008 /* is IERC165 */ {
/// @notice Emitted when the `nonce` of an NFT is changed
event NonceChanged(uint256 tokenId, uint256 nonce);

/// @notice Get the nonce of an NFT
/// Throws if `tokenId` is not a valid NFT
/// @param tokenId The id of the NFT
Expand Down
3 changes: 3 additions & 0 deletions assets/eip-5008/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "EIP-5008",
"scripts": {
"test": "npx hardhat test"
},
"dependencies": {
"@openzeppelin/contracts": "^4.7.3",
"@nomiclabs/hardhat-ethers": "^2.0.5",
Expand Down
11 changes: 9 additions & 2 deletions assets/eip-5008/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ describe("Test ERC5008 ", function () {
let contract = await ERC5008Demo.deploy("ERC5008Demo","ERC5008Demo");

let tokenId = 1;
await contract.mint(alice.address, tokenId);
await expect(contract.mint(alice.address, tokenId)).to.emit(contract, "NonceChanged").withArgs(tokenId, 1);

expect(await contract.nonce(tokenId)).equals(1);

await contract.transferFrom(alice.address, bob.address, tokenId);

await expect(contract.transferFrom(alice.address, bob.address, tokenId)).to.emit(contract, "NonceChanged").withArgs(tokenId, 2);


expect(await contract.nonce(tokenId)).equals(2);

console.log("IERC5008 InterfaceId:", await contract.getInterfaceId())
let isSupport = await contract.supportsInterface('0xce03fdab');
expect(isSupport).equals(true , "supportsInterface error");

});
});

0 comments on commit 70c6463

Please sign in to comment.