-
Notifications
You must be signed in to change notification settings - Fork 138
/
addressZero.ts
38 lines (34 loc) · 1.16 KB
/
addressZero.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { InputType, IssueTypes, Instance, ASTIssue } from '../../types';
import { findAll } from 'solidity-ast/utils';
import { instanceFromSRC } from '../../utils';
import { Expression, SourceUnit } from 'solidity-ast';
const issue: ASTIssue = {
regexOrAST: 'AST',
type: IssueTypes.GAS,
title: 'Use assembly to check for `address(0)`',
description: '*Saves 6 gas per instance*',
detector: (files: InputType): Instance[] => {
let instances: Instance[] = [];
const addressZero = (node: Expression): boolean => {
return (
node.nodeType === 'FunctionCall' &&
node.kind === 'typeConversion' &&
node.arguments?.[0].nodeType === 'Literal' &&
node.arguments?.[0].value === '0'
);
};
for (const file of files) {
if (!!file.ast) {
for (const node of findAll('BinaryOperation', file.ast)) {
if (node.operator === '!=' || node.operator === '==') {
if (addressZero(node.rightExpression) || addressZero(node.leftExpression)) {
instances.push(instanceFromSRC(file, node.src));
}
}
}
}
}
return instances;
},
};
export default issue;