Skip to content

Commit dc27299

Browse files
authored
Add ERC165 to the SBT contract (#69)
1 parent abb78ee commit dc27299

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed

contracts/interfaces/tokens/ISBT.sol

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ pragma solidity ^0.8.4;
55
* @notice The SBT module
66
*/
77
interface ISBT {
8-
event Minted(address to, uint256 tokenId);
9-
event Burned(address from, uint256 tokenId);
8+
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
109

1110
function name() external view returns (string memory);
1211

contracts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solarity/solidity-lib",
3-
"version": "2.6.1",
3+
"version": "2.6.2",
44
"license": "MIT",
55
"author": "Distributed Lab",
66
"readme": "README.md",

contracts/tokens/SBT.sol

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.4;
33

4-
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
4+
import {ERC165Upgradeable} from "@openzeppelin/contracts-upgradeable/utils/introspection/ERC165Upgradeable.sol";
5+
import {IERC721Metadata} from "@openzeppelin/contracts/interfaces/IERC721Metadata.sol";
56
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
67
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
78

@@ -15,7 +16,7 @@ import {ISBT} from "../interfaces/tokens/ISBT.sol";
1516
*
1617
* Has to be inherited in order to be useful in the project
1718
*/
18-
abstract contract SBT is ISBT, Initializable {
19+
abstract contract SBT is ISBT, ERC165Upgradeable {
1920
using Strings for uint256;
2021
using EnumerableSet for EnumerableSet.UintSet;
2122

@@ -134,6 +135,18 @@ abstract contract SBT is ISBT, Initializable {
134135
return "";
135136
}
136137

138+
/**
139+
* @notice Returns true if this contract implements the interface defined by `interfaceId`
140+
* @param interfaceId_ the interface ID to check
141+
* @return true if the passed interface ID is supported, otherwise false
142+
*/
143+
function supportsInterface(bytes4 interfaceId_) public view virtual override returns (bool) {
144+
return
145+
interfaceId_ == type(IERC721Metadata).interfaceId ||
146+
interfaceId_ == type(ISBT).interfaceId ||
147+
super.supportsInterface(interfaceId_);
148+
}
149+
137150
/**
138151
* @notice The function to mint the token
139152
* @param to_ the receiver of the token
@@ -148,7 +161,7 @@ abstract contract SBT is ISBT, Initializable {
148161
_balances[to_].add(tokenId_);
149162
_tokenOwners[tokenId_] = to_;
150163

151-
emit Minted(to_, tokenId_);
164+
emit Transfer(address(0), to_, tokenId_);
152165
}
153166

154167
/**
@@ -166,7 +179,7 @@ abstract contract SBT is ISBT, Initializable {
166179

167180
delete _tokenURIs[tokenId_];
168181

169-
emit Burned(owner_, tokenId_);
182+
emit Transfer(owner_, address(0), tokenId_);
170183
}
171184

172185
/**

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solarity/solidity-lib",
3-
"version": "2.6.1",
3+
"version": "2.6.2",
44
"license": "MIT",
55
"author": "Distributed Lab",
66
"description": "Solidity Library by Distributed Lab",

test/token/SBT.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe("SBT", () => {
4747

4848
describe("mint()", () => {
4949
it("should correctly mint", async () => {
50-
await sbt.mint(FIRST.address, 1337);
50+
const tx = await sbt.mint(FIRST.address, 1337);
5151

5252
expect(await sbt.tokenExists(1337)).to.be.true;
5353

@@ -58,6 +58,8 @@ describe("SBT", () => {
5858
expect(await sbt.tokensOf(FIRST.address)).to.deep.equal([1337n]);
5959

6060
expect(await sbt.tokenURI(1337)).to.equal("");
61+
62+
expect(tx).to.emit(sbt, "Transfer").withArgs(ZERO_ADDR, FIRST.address, 1337);
6163
});
6264

6365
it("should not mint to null address", async () => {
@@ -73,7 +75,7 @@ describe("SBT", () => {
7375

7476
describe("burn()", () => {
7577
it("should correctly burn", async () => {
76-
await sbt.mint(FIRST.address, 1337);
78+
const tx = await sbt.mint(FIRST.address, 1337);
7779

7880
await sbt.burn(1337);
7981

@@ -83,6 +85,8 @@ describe("SBT", () => {
8385
expect(await sbt.ownerOf(0)).to.equal(ZERO_ADDR);
8486

8587
expect(await sbt.tokensOf(FIRST.address)).to.deep.equal([]);
88+
89+
expect(tx).to.emit(sbt, "Transfer").withArgs(FIRST.address, ZERO_ADDR, 1337);
8690
});
8791

8892
it("should not burn SBT that doesn't exist", async () => {
@@ -129,4 +133,20 @@ describe("SBT", () => {
129133
expect(await sbt.tokenURI(1337)).to.equal("test");
130134
});
131135
});
136+
137+
describe("supportsInterface()", () => {
138+
it("should return correct values", async () => {
139+
const IERC721MetadaInterfaceID = "0x5b5e139f";
140+
const ISBTInterfaceID = "0xddd872b5";
141+
const IERC165InterfaceID = "0x01ffc9a7";
142+
143+
expect(await sbt.supportsInterface(IERC721MetadaInterfaceID)).to.be.eq(true);
144+
expect(await sbt.supportsInterface(ISBTInterfaceID)).to.be.eq(true);
145+
expect(await sbt.supportsInterface(IERC165InterfaceID)).to.be.eq(true);
146+
147+
const randomInterfaceID = "0xaaa1234d";
148+
149+
expect(await sbt.supportsInterface(randomInterfaceID)).to.be.eq(false);
150+
});
151+
});
132152
});

0 commit comments

Comments
 (0)