-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #541 from protofire/gc-mutlitoken1155
GC: mutlitoken1155 rule
- Loading branch information
Showing
9 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
warning: "This is a dynamically generated file. Do not edit manually." | ||
layout: "default" | ||
title: "gas-multitoken1155 | Solhint" | ||
--- | ||
|
||
# gas-multitoken1155 | ||
![Category Badge](https://img.shields.io/badge/-Gas%20Consumption%20Rules-informational) | ||
![Default Severity Badge warn](https://img.shields.io/badge/Default%20Severity-warn-yellow) | ||
|
||
## Description | ||
ERC1155 is a cheaper non-fungible token than ERC721 | ||
|
||
## Options | ||
This rule accepts a string option of rule severity. Must be one of "error", "warn", "off". Default to warn. | ||
|
||
### Example Config | ||
```json | ||
{ | ||
"rules": { | ||
"gas-multitoken1155": "warn" | ||
} | ||
} | ||
``` | ||
|
||
### Notes | ||
- [source](https://www.rareskills.io/post/gas-optimization?postId=c9db474a-ff97-4fa3-a51d-fe13ccb8fe3b&utm_campaign=42ccb5d8-c2cc-4416-b661-8eec8368f72b&utm_source=so&utm_medium=mail&utm_content=40a3d3be-d07d-479e-af1d-6b2ef1b950da&cid=9619984a-b43c-4002-ba71-820fd72bb83a#viewer-8v8t9) of the rule initiciative | ||
|
||
## Examples | ||
This rule does not have examples. | ||
|
||
## Version | ||
This rule is introduced in the latest version. | ||
|
||
## Resources | ||
- [Rule source](https://github.com/protofire/solhint/tree/master/lib/rules/gas-consumption/gas-multitoken1155.js) | ||
- [Document source](https://github.com/protofire/solhint/tree/master/docs/rules/gas-consumption/gas-multitoken1155.md) | ||
- [Test cases](https://github.com/protofire/solhint/tree/master/test/rules/gas-consumption/gas-multitoken1155.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const BaseChecker = require('../base-checker') | ||
|
||
const ruleId = 'gas-multitoken1155' | ||
const meta = { | ||
type: 'gas-consumption', | ||
|
||
docs: { | ||
description: 'ERC1155 is a cheaper non-fungible token than ERC721', | ||
category: 'Gas Consumption Rules', | ||
notes: [ | ||
{ | ||
note: '[source](https://www.rareskills.io/post/gas-optimization?postId=c9db474a-ff97-4fa3-a51d-fe13ccb8fe3b&utm_campaign=42ccb5d8-c2cc-4416-b661-8eec8368f72b&utm_source=so&utm_medium=mail&utm_content=40a3d3be-d07d-479e-af1d-6b2ef1b950da&cid=9619984a-b43c-4002-ba71-820fd72bb83a#viewer-8v8t9) of the rule initiciative', | ||
}, | ||
], | ||
}, | ||
|
||
isDefault: false, | ||
recommended: false, | ||
defaultSetup: 'warn', | ||
|
||
schema: null, | ||
} | ||
|
||
class GasMultitoken1155 extends BaseChecker { | ||
constructor(reporter) { | ||
super(reporter, ruleId, meta) | ||
} | ||
|
||
hasERC721String(path) { | ||
// Convert both the input string and the substring to lowercase for case-insensitive comparison | ||
const lowercaseInput = path.toLowerCase() | ||
const lowercaseSubstring = 'erc721' | ||
|
||
// Check if the lowercase input string contains the lowercase substring | ||
return lowercaseInput.includes(lowercaseSubstring) | ||
} | ||
|
||
ImportDirective(node) { | ||
if (this.hasERC721String(node.path)) { | ||
this.error(node, 'GC: ERC721 import found - Use of ERC1155 recommended') | ||
} | ||
} | ||
} | ||
|
||
module.exports = GasMultitoken1155 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const GasMultitoken1155 = require('./gas-multitoken1155') | ||
|
||
// module.exports = function checkers(reporter, config, tokens) { | ||
module.exports = function checkers(reporter, config) { | ||
return [new GasMultitoken1155(reporter, config)] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const assert = require('assert') | ||
const linter = require('../../../lib/index') | ||
|
||
const ERROR_MSG = 'GC: ERC721 import found - Use of ERC1155 recommended' | ||
|
||
describe('Linter - gas-multitoken1155', () => { | ||
it('should raise error', () => { | ||
const code = ` | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.4; | ||
import '@openzeppelin/contracts/token/ERC722/ERC722.sol'; | ||
import '@openzeppelin/contracts/token/erc721.sol'; | ||
import '@openzeppelin/contracts/token/ERC721.sol'; | ||
contract A {} | ||
` | ||
|
||
const report = linter.processStr(code, { | ||
rules: { 'gas-multitoken1155': 'error' }, | ||
}) | ||
|
||
assert.equal(report.errorCount, 2) | ||
assert.equal(report.messages[0].message, ERROR_MSG) | ||
assert.equal(report.messages[1].message, ERROR_MSG) | ||
}) | ||
|
||
it('should raise error', () => { | ||
const code = ` | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.4; | ||
import '@openzeppelin/contracts/token/ERC721/ERC722.sol'; | ||
contract A {} | ||
` | ||
|
||
const report = linter.processStr(code, { | ||
rules: { 'gas-multitoken1155': 'error' }, | ||
}) | ||
|
||
assert.equal(report.errorCount, 1) | ||
assert.equal(report.messages[0].message, ERROR_MSG) | ||
}) | ||
|
||
it('should NOT raise error', () => { | ||
const code = ` | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.4; | ||
import '@openzeppelin/contracts/token/ERC722/ERC722.sol'; | ||
import '@openzeppelin/contracts/token/erc20.sol'; | ||
contract A {} | ||
` | ||
|
||
const report = linter.processStr(code, { | ||
rules: { 'gas-multitoken1155': 'error' }, | ||
}) | ||
|
||
assert.equal(report.errorCount, 0) | ||
}) | ||
}) |