[? ]
那么您有一组智能合约,并且想要查找所有具有名为withdrawEth
的public
方法的合约,但词法 grep 会产生大量误报?这就是 solgrep 可以提供帮助的地方! ?
? npm install -g solgrep
Solgrep 递归地在目标目录中查找智能合约,解析源单元以理解语言语义,并使这些信息可供强大的基于 javascript 的过滤功能使用。这样,您可以:
AST_EXACT
和AST_FUZZY
匹配)使用此工具的最常见方法可能是使用--find=<js-filter-statement>
选项运行它,其中js-filter-statement
是一个 javascript 单行代码,它告诉引擎您感兴趣的内容。您可以提供返回boolean
的语句(“查找模式”)或返回要提取的信息(“提取模式”)。
--find=<js-filter-statement>
的可信输入,因为此参数被评估为 javascript!
--find
、 --rule
时检查参数的正确顺序。请参阅用法。
运行默认规则并显示一些统计数据?
⇒ solgrep < folder >
运行特定或多个内置规则( solgrep -l
列出可用规则)?
⇒ solgrep < folder > -- rule = IsInitializable -- rule = Stats
您想查找具有名为withdrawEth
的函数的合约的所有源单元吗?
⇒ solgrep < folder > -- find = "function.name=='withdrawEth'"
做同样的事情但不区分大小写?
⇒ solgrep < folder > -- find = "function.name.toLowerCase()=='withdraweth'"
查找所有调用selfdestruct
的函数?
⇒ solgrep < folder > -- find = "function.callsTo('selfdestruct')"
从所有合约中提取所有函数名称?
⇒ solgrep < folder > -- find = "function.name"
获取所有external
函数的列表?
⇒ solgrep < folder > -- find = "function.visibility.includes('external')"
找到ERC777
合约?
⇒ solgrep < folder > -- find = "contract.name=='ERC777'"
提取所有合同名称?
⇒ solgrep < folder > -- find = "contract.name"
提取所有接口名称?
⇒ solgrep < folder > -- find = "contract.name && contract.kind=='interface'"
与AST
中的某些内容匹配?
⇒ solgrep < folder > -- find = "Object.keys(function.modifiers).includes('nonReentrant')"
词法匹配函数源代码?
⇒ solgrep < folder > -- find = "function.getSource().includes('hi')"
找到重复的合同? (AST精确匹配和模糊匹配)
⇒ solgrep < folder > -- rule DupeFinder
使用选项--output=<output.json>
将所有结果写入文件。
--find
的内置关键字sourceUnit
对象与引擎当前正在处理的源文件相匹配。contract
对象与引擎当前正在处理的合约相匹配。function
对象与引擎当前正在处理的函数相匹配。 可以使用以下方法:
<sourceUnit|contract|function>.getSource()
- 提供对单位源代码的访问<sourceUnit|contract|function>.ast
- 提供对 Solidity-parser AST 的直接访问function.callsTo('withdrawEth')
- 查找所有合约中调用名为withdrawEth
的方法的所有函数特殊合约函数可以参考:
function.name == '__fallback__'
function.name == '__receiveEther__'
function.name == '__constructor__'
Stats
- 收集有关已处理的文件、合同、唯一名称等数量的统计信息GenericGrep
- 是--find
功能背后的引擎分享就是关怀- 提交您的规则!
⇒ solgrep -- help
Usage: solgrep < folder | ... > [ options ]
Options:
- r , -- rule Enable rules [ array ] [ default : [ ] ]
- l , -- list - rules List available rules [ boolean ] [ default : false ]
- f , -- find Find / Extract information using custom pattern
[ array ] [ default : [ ] ]
- o , -- output Write "results" as JSON to output file path . [ string ]
- h , -- help Show help [ boolean ]
- v , -- version Show version number [ boolean ]
--find
、 --rule
)时,请确保使用以下格式:
⇒ solgrep <folder|...> [options]
或者
⇒ solgrep [options] -- <folder|...>
否则附加选项可能会被解释为附加--find
选项!
const solgrep = require ( 'solgrep' ) ;
let sg = new SolGrep ( '::memory::' , rules , callbacks ) ;
sg . analyzeDir ( "/path/to/smart/contracts" ) . then ( ( ) => {
console . log ( " ──────────────────────────── Results" )
console . log ( sg . results )
console . log ( " ────────────────────────────" )
sgrep . close ( ) ;
} )
下面的示例说明了如何从所有sourceUnits
和contracts
中提取所有函数名称。
⇒ solgrep . . / d0 -- find = 'function.name'
? SolGrep v0 .0 .3 ready !
Enabled Modules :
✔️ GenericGrep _function . name
[ ████████████████████████████████████████ ] 100 % | ? ETA : 0 s | 5 / 5 | ? 389 | 0 | . . / d0
────────────────────────────
{
'../d0/D0788Af7a613b81F437a51b96594A6387c7329b1_PendleLiquidityMiningBaseV2Multi.sol' : [
{
rule : 'GenericGrep' ,
tag : 'match-function: PendleLiquidityMiningBaseV2Multi.__constructor__' ,
info : '__constructor__' ,
loc : [ Object ]
} ,
{
rule : 'GenericGrep' ,
tag : 'match-function: PendleLiquidityMiningBaseV2Multi.setUpEmergencyMode' ,
info : 'setUpEmergencyMode' ,
loc : [ Object ]
} ,
...
如果您没有提供配置选项,它将采用默认的内置 grep 规则并使用stats
模块来处理数字。
⇒ solgrep . . / d6
? SolGrep v0 .0 .1 starting . . .
? . . / d6
████████████████████████████████████████ 100 % | ETA : 0 s | 5 / 5
──────────────────────────── Results
{
'../d6/d60a598998ed27a7C54315F2675908B628E434B1_LiquidityPool.sol' : [
{
rule : 'IsInitializable' ,
tag : 'INITIALIZEABLE' ,
info : 'initialize - public initialize function; likely proxy'
} ,
{
rule : 'IsInitializable' ,
tag : 'INITIALIZEABLE' ,
info : 'initialize - public initialize function; likely proxy'
}
] ,
'../d6/d64E77C7C6A1dcC7e302F8fe31A22745e223c39c_MyStrategy.sol' : [
{
rule : 'IsInitializable' ,
tag : 'INITIALIZEABLE' ,
info : 'initialize - public initialize function; likely proxy'
}
] ,
undefined : [ { rule : 'Stats' , tag : 'STATS' , info : [ Object ] } ]
}
────────────────────────────
{
sourceUnits : 4 ,
contracts : {
total : 13 ,
names : {
ERC20 : 1 ,
StrategySushiEthMimLp : 1 ,
LiquidityPool : 1 ,
Getter : 1 ,
Governance : 1 ,
LibraryEvents : 1 ,
Perpetual : 1 ,
Storage : 1 ,
ArbiBoneman : 1 ,
ERC721 : 1 ,
MyStrategy : 1 ,
PausableUpgradeable : 1 ,
SettAccessControl : 1
}
} ,
interfaces : {
total : 39 ,
names : {
IERC20 : 1 ,
IJar : 1 ,
IStakingRewards : 1 ,
IStakingRewardsFactory : 1 ,
IMasterchef : 1 ,
UniswapRouterV2 : 1 ,
IUniswapV2Pair : 1 ,
IUniswapV2Factory : 1 ,
IRewarder : 1 ,
IMiniChefV2 : 1 ,
ILiquidityPool : 1 ,
IOracle : 1 ,
IAccessControl : 1 ,
IGovernor : 1 ,
IPoolCreatorFull : 1 ,
ISymbolService : 1 ,
IPoolCreator : 1 ,
ITracer : 1 ,
IVersionControl : 1 ,
IVariables : 1 ,
IKeeperWhitelist : 1 ,
IProxyAdmin : 1 ,
IDecimals : 1 ,
ILiquidityPoolGetter : 1 ,
ILiquidityPoolGovernance : 1 ,
IPerpetual : 1 ,
IERC721Enumerable : 1 ,
IERC721 : 1 ,
IERC721Receiver : 1 ,
IERC721Metadata : 1 ,
IERC165 : 1 ,
ICurveGauge : 1 ,
ICurveStableSwapREN : 1 ,
IUniswapRouterV2 : 1 ,
IStrategy : 1 ,
IController : 2 ,
IERC20Upgradeable : 2
}
} ,
libraries : {
total : 31 ,
names : {
SafeMath : 1 ,
SafeERC20 : 1 ,
BoringERC20 : 1 ,
EnumerableSetUpgradeable : 1 ,
SafeCastUpgradeable : 1 ,
AMMModule : 1 ,
LiquidityPoolModule : 1 ,
PerpetualModule : 1 ,
SignedSafeMathUpgradeable : 1 ,
Constant : 1 ,
Math : 1 ,
SafeMathExt : 1 ,
Utils : 1 ,
MarginAccountModule : 1 ,
EnumerableSet : 1 ,
OrderData : 1 ,
CollateralModule : 1 ,
TradeModule : 1 ,
OrderModule : 1 ,
Signature : 1 ,
ECDSAUpgradeable : 1 ,
Strings : 1 ,
MathUpgradeable : 1 ,
Address : 2 ,
SafeMathUpgradeable : 2 ,
AddressUpgradeable : 2 ,
SafeERC20Upgradeable : 2
}
} ,
abstract : {
total : 13 ,
names : {
StrategyBase : 1 ,
StrategySushiFarmBase : 1 ,
ReentrancyGuardUpgradeable : 1 ,
ERC721Enumerable : 1 ,
Ownable : 1 ,
ERC165 : 1 ,
BaseStrategy : 1 ,
Context : 2 ,
ContextUpgradeable : 2 ,
Initializable : 2
}
}
}
TOTAL FILES : 5
ERRORS: 1
────────────────────────────
cheers ?
@ tintinweb
ConsenSys Diligence @ https : //consensys.net/diligence/
https : //github.com/tintinweb/solgrep/