1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- const { makeInterfaceId } = require('openzeppelin-test-helpers');
- const INTERFACES = {
- ERC165: [
- 'supportsInterface(bytes4)',
- ],
- ERC721: [
- 'balanceOf(address)',
- 'ownerOf(uint256)',
- 'approve(address,uint256)',
- 'getApproved(uint256)',
- 'setApprovalForAll(address,bool)',
- 'isApprovedForAll(address,address)',
- 'transferFrom(address,address,uint256)',
- 'safeTransferFrom(address,address,uint256)',
- 'safeTransferFrom(address,address,uint256,bytes)',
- ],
- ERC721Enumerable: [
- 'totalSupply()',
- 'tokenOfOwnerByIndex(address,uint256)',
- 'tokenByIndex(uint256)',
- ],
- ERC721Metadata: [
- 'name()',
- 'symbol()',
- 'tokenURI(uint256)',
- ],
- };
- const INTERFACE_IDS = {};
- const FN_SIGNATURES = {};
- for (const k of Object.getOwnPropertyNames(INTERFACES)) {
- INTERFACE_IDS[k] = makeInterfaceId.ERC165(INTERFACES[k]);
- for (const fnName of INTERFACES[k]) {
- // the interface id of a single function is equivalent to its function signature
- FN_SIGNATURES[fnName] = makeInterfaceId.ERC165([fnName]);
- }
- }
- function shouldSupportInterfaces (interfaces = []) {
- describe('Contract interface', function () {
- beforeEach(function () {
- this.contractUnderTest = this.mock || this.token;
- });
- for (const k of interfaces) {
- const interfaceId = INTERFACE_IDS[k];
- describe(k, function () {
- describe('ERC165\'s supportsInterface(bytes4)', function () {
- it('should use less than 30k gas', async function () {
- (await this.contractUnderTest.supportsInterface.estimateGas(interfaceId)).should.be.lte(30000);
- });
- it('should claim support', async function () {
- (await this.contractUnderTest.supportsInterface(interfaceId)).should.equal(true);
- });
- });
- for (const fnName of INTERFACES[k]) {
- const fnSig = FN_SIGNATURES[fnName];
- describe(fnName, function () {
- it('should be implemented', function () {
- this.contractUnderTest.abi.filter(fn => fn.signature === fnSig).length.should.equal(1);
- });
- });
- }
- });
- }
- });
- }
- module.exports = {
- shouldSupportInterfaces,
- };
|