From cfa050719971b50a0d2d8214348ee5e86d33e9e6 Mon Sep 17 00:00:00 2001 From: malik672 Date: Tue, 14 Nov 2023 21:30:07 +0100 Subject: [PATCH] fix dictionary issue and generated ast using ethers_solc --- Cargo.toml | 4 +- artifacts/contract.sol/TypeHashHelper.json | 1259 ++++++++++++++++++++ cache/solidity-files-cache.json | 46 + report.json | 12 +- src/contract.sol | 5 +- src/main.rs | 18 +- src/optimizor/ast.json | 1 + src/optimizor/ast.rs | 5 + src/optimizor/gas_tricks.rs | 304 ++++- 9 files changed, 1586 insertions(+), 68 deletions(-) create mode 100644 artifacts/contract.sol/TypeHashHelper.json create mode 100644 cache/solidity-files-cache.json create mode 100644 src/optimizor/ast.json create mode 100644 src/optimizor/ast.rs diff --git a/Cargo.toml b/Cargo.toml index c5b6c8c..902e7e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,8 @@ serde_json = "1.0.107" snap = "1.1.0" tokio = {version = "1", features = ["full"]} + + [[bin]] name = "analyzer" -path = "src/main.rs" \ No newline at end of file +path = "src/main.rs" diff --git a/artifacts/contract.sol/TypeHashHelper.json b/artifacts/contract.sol/TypeHashHelper.json new file mode 100644 index 0000000..b399ad0 --- /dev/null +++ b/artifacts/contract.sol/TypeHashHelper.json @@ -0,0 +1,1259 @@ +{ + "abi": [ + { + "inputs": [], + "name": "TRANSACTION_PARAMS_TYPEHASH", + "outputs": [ + { + "internalType": "bytes32", + "name": "", + "type": "bytes32" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "VALIDATION_PARAMS_TYPEHASH", + "outputs": [ + { + "internalType": "bytes32", + "name": "", + "type": "bytes32" + } + ], + "stateMutability": "view", + "type": "function" + } + ], + "bytecode": { + "object": "0x61012a610053600b82828239805160001a607314610046577f4e487b7100000000000000000000000000000000000000000000000000000000600052600060045260246000fd5b30600052607381538281f3fe7300000000000000000000000000000000000000003014608060405260043610603d5760003560e01c806306e01350146042578063aaa5e62d14605c575b600080fd5b60486076565b6040516053919060db565b60405180910390f35b6062609d565b604051606d919060db565b60405180910390f35b7f0c7f653e0f641e41fbb4ed1440c7d0b08b8d2a19e1c35cfc98de2d47519e15b160001b81565b7ffd4628b53a91b366f1977138e2eda53b93c8f5cc74bda8440f108d7da1e9929060001b81565b6000819050919050565b60d58160c4565b82525050565b600060208201905060ee600083018460ce565b9291505056fea2646970667358221220ef78c693e50f13b582cea66888d7d32879a7347ad0f99069f4fc8380a20e566b64736f6c634300080c0033", + "sourceMap": "240:2924:0:-:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;", + "linkReferences": {} + }, + "deployedBytecode": { + "object": "0x7300000000000000000000000000000000000000003014608060405260043610603d5760003560e01c806306e01350146042578063aaa5e62d14605c575b600080fd5b60486076565b6040516053919060db565b60405180910390f35b6062609d565b604051606d919060db565b60405180910390f35b7f0c7f653e0f641e41fbb4ed1440c7d0b08b8d2a19e1c35cfc98de2d47519e15b160001b81565b7ffd4628b53a91b366f1977138e2eda53b93c8f5cc74bda8440f108d7da1e9929060001b81565b6000819050919050565b60d58160c4565b82525050565b600060208201905060ee600083018460ce565b9291505056fea2646970667358221220ef78c693e50f13b582cea66888d7d32879a7347ad0f99069f4fc8380a20e566b64736f6c634300080c0033", + "sourceMap": "240:2924:0:-:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1942:127;;;:::i;:::-;;;;;;;:::i;:::-;;;;;;;;1512:128;;;:::i;:::-;;;;;;;:::i;:::-;;;;;;;;1942:127;2003:66;1942:127;;;:::o;1512:128::-;1574:66;1512:128;;;:::o;7:77:1:-;44:7;73:5;62:16;;7:77;;;:::o;90:126::-;185:24;203:5;185:24;:::i;:::-;180:3;173:37;90:126;;:::o;222:238::-;323:4;361:2;350:9;346:18;338:26;;374:79;450:1;439:9;435:17;426:6;374:79;:::i;:::-;222:238;;;;:::o", + "linkReferences": {} + }, + "methodIdentifiers": { + "TRANSACTION_PARAMS_TYPEHASH()": "aaa5e62d", + "VALIDATION_PARAMS_TYPEHASH()": "06e01350" + }, + "ast": { + "absolutePath": "src/contract.sol", + "id": 90, + "exportedSymbols": { + "TypeHashHelper": [ + 89 + ] + }, + "nodeType": "SourceUnit", + "src": "73:3091:0", + "nodes": [ + { + "id": 1, + "nodeType": "PragmaDirective", + "src": "73:23:0", + "nodes": [], + "literals": [ + "solidity", + "0.8", + ".12" + ] + }, + { + "id": 89, + "nodeType": "ContractDefinition", + "src": "240:2924:0", + "nodes": [ + { + "id": 17, + "nodeType": "StructDefinition", + "src": "679:188:0", + "nodes": [], + "canonicalName": "TypeHashHelper.Transaction", + "members": [ + { + "constant": false, + "id": 4, + "mutability": "mutable", + "name": "operation", + "nameLocation": "714:9:0", + "nodeType": "VariableDeclaration", + "scope": 17, + "src": "708:15:0", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_uint8", + "typeString": "uint8" + }, + "typeName": { + "id": 3, + "name": "uint8", + "nodeType": "ElementaryTypeName", + "src": "708:5:0", + "typeDescriptions": { + "typeIdentifier": "t_uint8", + "typeString": "uint8" + } + }, + "visibility": "internal" + }, + { + "constant": false, + "id": 6, + "mutability": "mutable", + "name": "to", + "nameLocation": "741:2:0", + "nodeType": "VariableDeclaration", + "scope": 17, + "src": "733:10:0", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_address", + "typeString": "address" + }, + "typeName": { + "id": 5, + "name": "address", + "nodeType": "ElementaryTypeName", + "src": "733:7:0", + "stateMutability": "nonpayable", + "typeDescriptions": { + "typeIdentifier": "t_address", + "typeString": "address" + } + }, + "visibility": "internal" + }, + { + "constant": false, + "id": 8, + "mutability": "mutable", + "name": "account", + "nameLocation": "761:7:0", + "nodeType": "VariableDeclaration", + "scope": 17, + "src": "753:15:0", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_address", + "typeString": "address" + }, + "typeName": { + "id": 7, + "name": "address", + "nodeType": "ElementaryTypeName", + "src": "753:7:0", + "stateMutability": "nonpayable", + "typeDescriptions": { + "typeIdentifier": "t_address", + "typeString": "address" + } + }, + "visibility": "internal" + }, + { + "constant": false, + "id": 10, + "mutability": "mutable", + "name": "executor", + "nameLocation": "786:8:0", + "nodeType": "VariableDeclaration", + "scope": 17, + "src": "778:16:0", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_address", + "typeString": "address" + }, + "typeName": { + "id": 9, + "name": "address", + "nodeType": "ElementaryTypeName", + "src": "778:7:0", + "stateMutability": "nonpayable", + "typeDescriptions": { + "typeIdentifier": "t_address", + "typeString": "address" + } + }, + "visibility": "internal" + }, + { + "constant": false, + "id": 12, + "mutability": "mutable", + "name": "value", + "nameLocation": "812:5:0", + "nodeType": "VariableDeclaration", + "scope": 17, + "src": "804:13:0", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_uint256", + "typeString": "uint256" + }, + "typeName": { + "id": 11, + "name": "uint256", + "nodeType": "ElementaryTypeName", + "src": "804:7:0", + "typeDescriptions": { + "typeIdentifier": "t_uint256", + "typeString": "uint256" + } + }, + "visibility": "internal" + }, + { + "constant": false, + "id": 14, + "mutability": "mutable", + "name": "nonce", + "nameLocation": "835:5:0", + "nodeType": "VariableDeclaration", + "scope": 17, + "src": "827:13:0", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_uint256", + "typeString": "uint256" + }, + "typeName": { + "id": 13, + "name": "uint256", + "nodeType": "ElementaryTypeName", + "src": "827:7:0", + "typeDescriptions": { + "typeIdentifier": "t_uint256", + "typeString": "uint256" + } + }, + "visibility": "internal" + }, + { + "constant": false, + "id": 16, + "mutability": "mutable", + "name": "data", + "nameLocation": "856:4:0", + "nodeType": "VariableDeclaration", + "scope": 17, + "src": "850:10:0", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_bytes_storage_ptr", + "typeString": "bytes" + }, + "typeName": { + "id": 15, + "name": "bytes", + "nodeType": "ElementaryTypeName", + "src": "850:5:0", + "typeDescriptions": { + "typeIdentifier": "t_bytes_storage_ptr", + "typeString": "bytes" + } + }, + "visibility": "internal" + } + ], + "name": "Transaction", + "nameLocation": "686:11:0", + "scope": 89, + "visibility": "public" + }, + { + "id": 24, + "nodeType": "StructDefinition", + "src": "1176:120:0", + "nodes": [], + "canonicalName": "TypeHashHelper.Validation", + "members": [ + { + "constant": false, + "id": 19, + "mutability": "mutable", + "name": "expiryEpoch", + "nameLocation": "1211:11:0", + "nodeType": "VariableDeclaration", + "scope": 24, + "src": "1204:18:0", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_uint32", + "typeString": "uint32" + }, + "typeName": { + "id": 18, + "name": "uint32", + "nodeType": "ElementaryTypeName", + "src": "1204:6:0", + "typeDescriptions": { + "typeIdentifier": "t_uint32", + "typeString": "uint32" + } + }, + "visibility": "internal" + }, + { + "constant": false, + "id": 21, + "mutability": "mutable", + "name": "transactionStructHash", + "nameLocation": "1240:21:0", + "nodeType": "VariableDeclaration", + "scope": 24, + "src": "1232:29:0", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + }, + "typeName": { + "id": 20, + "name": "bytes32", + "nodeType": "ElementaryTypeName", + "src": "1232:7:0", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + "visibility": "internal" + }, + { + "constant": false, + "id": 23, + "mutability": "mutable", + "name": "policyHash", + "nameLocation": "1279:10:0", + "nodeType": "VariableDeclaration", + "scope": 24, + "src": "1271:18:0", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + }, + "typeName": { + "id": 22, + "name": "bytes32", + "nodeType": "ElementaryTypeName", + "src": "1271:7:0", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + "visibility": "internal" + } + ], + "name": "Validation", + "nameLocation": "1183:10:0", + "scope": 89, + "visibility": "public" + }, + { + "id": 28, + "nodeType": "VariableDeclaration", + "src": "1512:128:0", + "nodes": [], + "constant": true, + "documentation": { + "id": 25, + "nodeType": "StructuredDocumentation", + "src": "1302:205:0", + "text": " @notice EIP712 typehash for transaction data\n @dev keccak256(\"ExecutionParams(address to,uint256 value,bytes data,uint8 operation,address account,address executor,uint256 nonce)\");" + }, + "functionSelector": "aaa5e62d", + "mutability": "constant", + "name": "TRANSACTION_PARAMS_TYPEHASH", + "nameLocation": "1536:27:0", + "scope": 89, + "stateVariable": true, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + }, + "typeName": { + "id": 26, + "name": "bytes32", + "nodeType": "ElementaryTypeName", + "src": "1512:7:0", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + "value": { + "hexValue": "307866643436323862353361393162333636663139373731333865326564613533623933633866356363373462646138343430663130386437646131653939323930", + "id": 27, + "isConstant": false, + "isLValue": false, + "isPure": true, + "kind": "number", + "lValueRequested": false, + "nodeType": "Literal", + "src": "1574:66:0", + "typeDescriptions": { + "typeIdentifier": "t_rational_114559110941873152283416461588046472969218532235332707338872480181029899440784_by_1", + "typeString": "int_const 1145...(70 digits omitted)...0784" + }, + "value": "0xfd4628b53a91b366f1977138e2eda53b93c8f5cc74bda8440f108d7da1e99290" + }, + "visibility": "public" + }, + { + "id": 32, + "nodeType": "VariableDeclaration", + "src": "1942:127:0", + "nodes": [], + "constant": true, + "documentation": { + "id": 29, + "nodeType": "StructuredDocumentation", + "src": "1647:290:0", + "text": " @notice EIP712 typehash for validation data\n @dev keccak256(\"ValidationParams(ExecutionParams executionParams,bytes32 policyHash,uint32 expiryEpoch)ExecutionParams(address to,uint256 value,bytes data,uint8 operation,address account,address executor,uint256 nonce)\")" + }, + "functionSelector": "06e01350", + "mutability": "constant", + "name": "VALIDATION_PARAMS_TYPEHASH", + "nameLocation": "1966:26:0", + "scope": 89, + "stateVariable": true, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + }, + "typeName": { + "id": 30, + "name": "bytes32", + "nodeType": "ElementaryTypeName", + "src": "1942:7:0", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + "value": { + "hexValue": "307830633766363533653066363431653431666262346564313434306337643062303862386432613139653163333563666339386465326434373531396531356231", + "id": 31, + "isConstant": false, + "isLValue": false, + "isPure": true, + "kind": "number", + "lValueRequested": false, + "nodeType": "Literal", + "src": "2003:66:0", + "typeDescriptions": { + "typeIdentifier": "t_rational_5652842509744641291905326774718137488969457354485079193916485264203405202865_by_1", + "typeString": "int_const 5652...(68 digits omitted)...2865" + }, + "value": "0x0c7f653e0f641e41fbb4ed1440c7d0b08b8d2a19e1c35cfc98de2d47519e15b1" + }, + "visibility": "public" + }, + { + "id": 65, + "nodeType": "FunctionDefinition", + "src": "2226:424:0", + "nodes": [], + "body": { + "id": 64, + "nodeType": "Block", + "src": "2319:331:0", + "nodes": [], + "statements": [ + { + "expression": { + "arguments": [ + { + "arguments": [ + { + "id": 44, + "name": "TRANSACTION_PARAMS_TYPEHASH", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 28, + "src": "2387:27:0", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + { + "expression": { + "id": 45, + "name": "txn", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 36, + "src": "2432:3:0", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Transaction_$17_memory_ptr", + "typeString": "struct TypeHashHelper.Transaction memory" + } + }, + "id": 46, + "isConstant": false, + "isLValue": true, + "isPure": false, + "lValueRequested": false, + "memberName": "to", + "nodeType": "MemberAccess", + "referencedDeclaration": 6, + "src": "2432:6:0", + "typeDescriptions": { + "typeIdentifier": "t_address", + "typeString": "address" + } + }, + { + "expression": { + "id": 47, + "name": "txn", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 36, + "src": "2456:3:0", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Transaction_$17_memory_ptr", + "typeString": "struct TypeHashHelper.Transaction memory" + } + }, + "id": 48, + "isConstant": false, + "isLValue": true, + "isPure": false, + "lValueRequested": false, + "memberName": "value", + "nodeType": "MemberAccess", + "referencedDeclaration": 12, + "src": "2456:9:0", + "typeDescriptions": { + "typeIdentifier": "t_uint256", + "typeString": "uint256" + } + }, + { + "arguments": [ + { + "expression": { + "id": 50, + "name": "txn", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 36, + "src": "2493:3:0", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Transaction_$17_memory_ptr", + "typeString": "struct TypeHashHelper.Transaction memory" + } + }, + "id": 51, + "isConstant": false, + "isLValue": true, + "isPure": false, + "lValueRequested": false, + "memberName": "data", + "nodeType": "MemberAccess", + "referencedDeclaration": 16, + "src": "2493:8:0", + "typeDescriptions": { + "typeIdentifier": "t_bytes_memory_ptr", + "typeString": "bytes memory" + } + } + ], + "expression": { + "argumentTypes": [ + { + "typeIdentifier": "t_bytes_memory_ptr", + "typeString": "bytes memory" + } + ], + "id": 49, + "name": "keccak256", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": -8, + "src": "2483:9:0", + "typeDescriptions": { + "typeIdentifier": "t_function_keccak256_pure$_t_bytes_memory_ptr_$returns$_t_bytes32_$", + "typeString": "function (bytes memory) pure returns (bytes32)" + } + }, + "id": 52, + "isConstant": false, + "isLValue": false, + "isPure": false, + "kind": "functionCall", + "lValueRequested": false, + "names": [], + "nodeType": "FunctionCall", + "src": "2483:19:0", + "tryCall": false, + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + { + "expression": { + "id": 53, + "name": "txn", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 36, + "src": "2520:3:0", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Transaction_$17_memory_ptr", + "typeString": "struct TypeHashHelper.Transaction memory" + } + }, + "id": 54, + "isConstant": false, + "isLValue": true, + "isPure": false, + "lValueRequested": false, + "memberName": "operation", + "nodeType": "MemberAccess", + "referencedDeclaration": 4, + "src": "2520:13:0", + "typeDescriptions": { + "typeIdentifier": "t_uint8", + "typeString": "uint8" + } + }, + { + "expression": { + "id": 55, + "name": "txn", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 36, + "src": "2551:3:0", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Transaction_$17_memory_ptr", + "typeString": "struct TypeHashHelper.Transaction memory" + } + }, + "id": 56, + "isConstant": false, + "isLValue": true, + "isPure": false, + "lValueRequested": false, + "memberName": "account", + "nodeType": "MemberAccess", + "referencedDeclaration": 8, + "src": "2551:11:0", + "typeDescriptions": { + "typeIdentifier": "t_address", + "typeString": "address" + } + }, + { + "expression": { + "id": 57, + "name": "txn", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 36, + "src": "2580:3:0", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Transaction_$17_memory_ptr", + "typeString": "struct TypeHashHelper.Transaction memory" + } + }, + "id": 58, + "isConstant": false, + "isLValue": true, + "isPure": false, + "lValueRequested": false, + "memberName": "executor", + "nodeType": "MemberAccess", + "referencedDeclaration": 10, + "src": "2580:12:0", + "typeDescriptions": { + "typeIdentifier": "t_address", + "typeString": "address" + } + }, + { + "expression": { + "id": 59, + "name": "txn", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 36, + "src": "2610:3:0", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Transaction_$17_memory_ptr", + "typeString": "struct TypeHashHelper.Transaction memory" + } + }, + "id": 60, + "isConstant": false, + "isLValue": true, + "isPure": false, + "lValueRequested": false, + "memberName": "nonce", + "nodeType": "MemberAccess", + "referencedDeclaration": 14, + "src": "2610:9:0", + "typeDescriptions": { + "typeIdentifier": "t_uint256", + "typeString": "uint256" + } + } + ], + "expression": { + "argumentTypes": [ + { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + }, + { + "typeIdentifier": "t_address", + "typeString": "address" + }, + { + "typeIdentifier": "t_uint256", + "typeString": "uint256" + }, + { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + }, + { + "typeIdentifier": "t_uint8", + "typeString": "uint8" + }, + { + "typeIdentifier": "t_address", + "typeString": "address" + }, + { + "typeIdentifier": "t_address", + "typeString": "address" + }, + { + "typeIdentifier": "t_uint256", + "typeString": "uint256" + } + ], + "expression": { + "id": 42, + "name": "abi", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": -1, + "src": "2359:3:0", + "typeDescriptions": { + "typeIdentifier": "t_magic_abi", + "typeString": "abi" + } + }, + "id": 43, + "isConstant": false, + "isLValue": false, + "isPure": true, + "lValueRequested": false, + "memberName": "encode", + "nodeType": "MemberAccess", + "src": "2359:10:0", + "typeDescriptions": { + "typeIdentifier": "t_function_abiencode_pure$__$returns$_t_bytes_memory_ptr_$", + "typeString": "function () pure returns (bytes memory)" + } + }, + "id": 61, + "isConstant": false, + "isLValue": false, + "isPure": false, + "kind": "functionCall", + "lValueRequested": false, + "names": [], + "nodeType": "FunctionCall", + "src": "2359:274:0", + "tryCall": false, + "typeDescriptions": { + "typeIdentifier": "t_bytes_memory_ptr", + "typeString": "bytes memory" + } + } + ], + "expression": { + "argumentTypes": [ + { + "typeIdentifier": "t_bytes_memory_ptr", + "typeString": "bytes memory" + } + ], + "id": 41, + "name": "keccak256", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": -8, + "src": "2336:9:0", + "typeDescriptions": { + "typeIdentifier": "t_function_keccak256_pure$_t_bytes_memory_ptr_$returns$_t_bytes32_$", + "typeString": "function (bytes memory) pure returns (bytes32)" + } + }, + "id": 62, + "isConstant": false, + "isLValue": false, + "isPure": false, + "kind": "functionCall", + "lValueRequested": false, + "names": [], + "nodeType": "FunctionCall", + "src": "2336:307:0", + "tryCall": false, + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + "functionReturnParameters": 40, + "id": 63, + "nodeType": "Return", + "src": "2329:314:0" + } + ] + }, + "documentation": { + "id": 33, + "nodeType": "StructuredDocumentation", + "src": "2076:145:0", + "text": " @notice Builds EIP712 transaction struct hash\n @param txn transaction params struct\n @return transactionStructHash" + }, + "implemented": true, + "kind": "function", + "modifiers": [], + "name": "_buildTransactionStructHash", + "nameLocation": "2235:27:0", + "parameters": { + "id": 37, + "nodeType": "ParameterList", + "parameters": [ + { + "constant": false, + "id": 36, + "mutability": "mutable", + "name": "txn", + "nameLocation": "2282:3:0", + "nodeType": "VariableDeclaration", + "scope": 65, + "src": "2263:22:0", + "stateVariable": false, + "storageLocation": "memory", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Transaction_$17_memory_ptr", + "typeString": "struct TypeHashHelper.Transaction" + }, + "typeName": { + "id": 35, + "nodeType": "UserDefinedTypeName", + "pathNode": { + "id": 34, + "name": "Transaction", + "nodeType": "IdentifierPath", + "referencedDeclaration": 17, + "src": "2263:11:0" + }, + "referencedDeclaration": 17, + "src": "2263:11:0", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Transaction_$17_storage_ptr", + "typeString": "struct TypeHashHelper.Transaction" + } + }, + "visibility": "internal" + } + ], + "src": "2262:24:0" + }, + "returnParameters": { + "id": 40, + "nodeType": "ParameterList", + "parameters": [ + { + "constant": false, + "id": 39, + "mutability": "mutable", + "name": "", + "nameLocation": "-1:-1:-1", + "nodeType": "VariableDeclaration", + "scope": 65, + "src": "2310:7:0", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + }, + "typeName": { + "id": 38, + "name": "bytes32", + "nodeType": "ElementaryTypeName", + "src": "2310:7:0", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + "visibility": "internal" + } + ], + "src": "2309:9:0" + }, + "scope": 89, + "stateMutability": "pure", + "virtual": false, + "visibility": "internal" + }, + { + "id": 88, + "nodeType": "FunctionDefinition", + "src": "2810:352:0", + "nodes": [], + "body": { + "id": 87, + "nodeType": "Block", + "src": "2908:254:0", + "nodes": [], + "statements": [ + { + "expression": { + "arguments": [ + { + "arguments": [ + { + "id": 77, + "name": "VALIDATION_PARAMS_TYPEHASH", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 32, + "src": "2976:26:0", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + { + "expression": { + "id": 78, + "name": "validation", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 69, + "src": "3020:10:0", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Validation_$24_memory_ptr", + "typeString": "struct TypeHashHelper.Validation memory" + } + }, + "id": 79, + "isConstant": false, + "isLValue": true, + "isPure": false, + "lValueRequested": false, + "memberName": "transactionStructHash", + "nodeType": "MemberAccess", + "referencedDeclaration": 21, + "src": "3020:32:0", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + { + "expression": { + "id": 80, + "name": "validation", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 69, + "src": "3070:10:0", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Validation_$24_memory_ptr", + "typeString": "struct TypeHashHelper.Validation memory" + } + }, + "id": 81, + "isConstant": false, + "isLValue": true, + "isPure": false, + "lValueRequested": false, + "memberName": "policyHash", + "nodeType": "MemberAccess", + "referencedDeclaration": 23, + "src": "3070:21:0", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + { + "expression": { + "id": 82, + "name": "validation", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 69, + "src": "3109:10:0", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Validation_$24_memory_ptr", + "typeString": "struct TypeHashHelper.Validation memory" + } + }, + "id": 83, + "isConstant": false, + "isLValue": true, + "isPure": false, + "lValueRequested": false, + "memberName": "expiryEpoch", + "nodeType": "MemberAccess", + "referencedDeclaration": 19, + "src": "3109:22:0", + "typeDescriptions": { + "typeIdentifier": "t_uint32", + "typeString": "uint32" + } + } + ], + "expression": { + "argumentTypes": [ + { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + }, + { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + }, + { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + }, + { + "typeIdentifier": "t_uint32", + "typeString": "uint32" + } + ], + "expression": { + "id": 75, + "name": "abi", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": -1, + "src": "2948:3:0", + "typeDescriptions": { + "typeIdentifier": "t_magic_abi", + "typeString": "abi" + } + }, + "id": 76, + "isConstant": false, + "isLValue": false, + "isPure": true, + "lValueRequested": false, + "memberName": "encode", + "nodeType": "MemberAccess", + "src": "2948:10:0", + "typeDescriptions": { + "typeIdentifier": "t_function_abiencode_pure$__$returns$_t_bytes_memory_ptr_$", + "typeString": "function () pure returns (bytes memory)" + } + }, + "id": 84, + "isConstant": false, + "isLValue": false, + "isPure": false, + "kind": "functionCall", + "lValueRequested": false, + "names": [], + "nodeType": "FunctionCall", + "src": "2948:197:0", + "tryCall": false, + "typeDescriptions": { + "typeIdentifier": "t_bytes_memory_ptr", + "typeString": "bytes memory" + } + } + ], + "expression": { + "argumentTypes": [ + { + "typeIdentifier": "t_bytes_memory_ptr", + "typeString": "bytes memory" + } + ], + "id": 74, + "name": "keccak256", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": -8, + "src": "2925:9:0", + "typeDescriptions": { + "typeIdentifier": "t_function_keccak256_pure$_t_bytes_memory_ptr_$returns$_t_bytes32_$", + "typeString": "function (bytes memory) pure returns (bytes32)" + } + }, + "id": 85, + "isConstant": false, + "isLValue": false, + "isPure": false, + "kind": "functionCall", + "lValueRequested": false, + "names": [], + "nodeType": "FunctionCall", + "src": "2925:230:0", + "tryCall": false, + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + "functionReturnParameters": 73, + "id": 86, + "nodeType": "Return", + "src": "2918:237:0" + } + ] + }, + "documentation": { + "id": 66, + "nodeType": "StructuredDocumentation", + "src": "2656:149:0", + "text": " @notice Builds EIP712 validation struct hash\n @param validation validation params struct\n @return validationStructHash" + }, + "implemented": true, + "kind": "function", + "modifiers": [], + "name": "_buildValidationStructHash", + "nameLocation": "2819:26:0", + "parameters": { + "id": 70, + "nodeType": "ParameterList", + "parameters": [ + { + "constant": false, + "id": 69, + "mutability": "mutable", + "name": "validation", + "nameLocation": "2864:10:0", + "nodeType": "VariableDeclaration", + "scope": 88, + "src": "2846:28:0", + "stateVariable": false, + "storageLocation": "memory", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Validation_$24_memory_ptr", + "typeString": "struct TypeHashHelper.Validation" + }, + "typeName": { + "id": 68, + "nodeType": "UserDefinedTypeName", + "pathNode": { + "id": 67, + "name": "Validation", + "nodeType": "IdentifierPath", + "referencedDeclaration": 24, + "src": "2846:10:0" + }, + "referencedDeclaration": 24, + "src": "2846:10:0", + "typeDescriptions": { + "typeIdentifier": "t_struct$_Validation_$24_storage_ptr", + "typeString": "struct TypeHashHelper.Validation" + } + }, + "visibility": "internal" + } + ], + "src": "2845:30:0" + }, + "returnParameters": { + "id": 73, + "nodeType": "ParameterList", + "parameters": [ + { + "constant": false, + "id": 72, + "mutability": "mutable", + "name": "", + "nameLocation": "-1:-1:-1", + "nodeType": "VariableDeclaration", + "scope": 88, + "src": "2899:7:0", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + }, + "typeName": { + "id": 71, + "name": "bytes32", + "nodeType": "ElementaryTypeName", + "src": "2899:7:0", + "typeDescriptions": { + "typeIdentifier": "t_bytes32", + "typeString": "bytes32" + } + }, + "visibility": "internal" + } + ], + "src": "2898:9:0" + }, + "scope": 89, + "stateMutability": "pure", + "virtual": false, + "visibility": "internal" + } + ], + "abstract": false, + "baseContracts": [], + "canonicalName": "TypeHashHelper", + "contractDependencies": [], + "contractKind": "library", + "documentation": { + "id": 2, + "nodeType": "StructuredDocumentation", + "src": "98:139:0", + "text": " @title TypeHashHelper\n @author Brahma.fi\n @notice Helper library containing functions to build EIP712 struct and type hashes" + }, + "fullyImplemented": true, + "linearizedBaseContracts": [ + 89 + ], + "name": "TypeHashHelper", + "nameLocation": "248:14:0", + "scope": 90, + "usedErrors": [] + } + ], + "license": "BUSL-1.1" + }, + "id": 0 +} \ No newline at end of file diff --git a/cache/solidity-files-cache.json b/cache/solidity-files-cache.json new file mode 100644 index 0000000..ba23a92 --- /dev/null +++ b/cache/solidity-files-cache.json @@ -0,0 +1,46 @@ +{ + "_format": "ethers-rs-sol-cache-3", + "paths": { + "artifacts": "artifacts", + "build_infos": "artifacts/build-info", + "sources": "contracts", + "tests": "test", + "scripts": "script", + "libraries": [ + "node_modules" + ] + }, + "files": { + "src/contract.sol": { + "lastModificationDate": 1699959572526, + "contentHash": "f3613cf746d20faaf0808270629634c2", + "sourceName": "src/contract.sol", + "solcConfig": { + "settings": { + "optimizer": { + "enabled": false, + "runs": 200 + }, + "outputSelection": { + "*": { + "": [ + "ast" + ], + "*": [ + "abi", + "evm.bytecode", + "evm.deployedBytecode", + "evm.methodIdentifiers" + ] + } + }, + "evmVersion": "shanghai", + "libraries": {} + } + }, + "imports": [], + "versionRequirement": "=0.8.12", + "artifacts": {} + } + } +} \ No newline at end of file diff --git a/report.json b/report.json index c1be55f..9fc135f 100644 --- a/report.json +++ b/report.json @@ -1,6 +1,10 @@ { - "line_12": "instead of using openzeppelin we can use solady which is way cheaper and way efficient [https://github.com/Vectorized/solady", - "line_13": "SafeMath is no longer needed since solidity version 0.8.0, use of safeMath can be considered unnessary", - "line_51": "variables that are constant should have a visibility of private", - "line_58": "variables that are constant should have a visibility of private" + "line_50": [ + "Use Uint256 instead of bytes32 to store constant", + "variables that are constant should have a visibility of private" + ], + "line_57": [ + "Use Uint256 instead of bytes32 to store constant", + "variables that are constant should have a visibility of private" + ] } \ No newline at end of file diff --git a/src/contract.sol b/src/contract.sol index 9e7b22c..4fade73 100644 --- a/src/contract.sol +++ b/src/contract.sol @@ -2,15 +2,14 @@ /// Copyright (C) 2023 Brahma.fi -pragma solidity 0.8.19; +pragma solidity 0.8.12; /** * @title TypeHashHelper * @author Brahma.fi * @notice Helper library containing functions to build EIP712 struct and type hashes */ - import "@openzeppelin/contracts/utils/math/SafeCast.sol"; - import "@openzeppelin/contracts/utils/math/SafeMath.sol"; + library TypeHashHelper { /** * @notice Structural representation of transaction details diff --git a/src/main.rs b/src/main.rs index 18ca28d..b2f0514 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,25 @@ +use std::fs; use tokio; mod config; mod loader; mod optimizor; +use ethers_solc::{Project}; #[tokio::main] async fn main() { - loader::loader().await; + // create a project from a configuration file + let project = Project::builder().build().unwrap(); + let output = project + .compile_files(vec!["src/contract.sol"]) + .unwrap(); + let artifacts = output.output().sources.0; + let mut s_ast = artifacts.values(); + let mut binding = s_ast.next(); + let mut binding = binding.iter_mut().next(); + let s_asts = binding.as_mut().unwrap(); + let ast = s_asts.get(0).unwrap().source_file.ast.clone().unwrap(); + let ast_json = serde_json::to_string(&ast).unwrap(); + let write = fs::write("src/optimizor/ast.json", ast_json); + // println!("{:?}", ast); + loader::loader().await; } diff --git a/src/optimizor/ast.json b/src/optimizor/ast.json new file mode 100644 index 0000000..ba09c34 --- /dev/null +++ b/src/optimizor/ast.json @@ -0,0 +1 @@ +{"absolutePath":"src/contract.sol","id":90,"exportedSymbols":{"TypeHashHelper":[89]},"nodeType":"SourceUnit","src":"73:3091:0","nodes":[{"id":1,"nodeType":"PragmaDirective","src":"73:23:0","nodes":[],"literals":["solidity","0.8",".12"]},{"id":89,"nodeType":"ContractDefinition","src":"240:2924:0","nodes":[{"id":17,"nodeType":"StructDefinition","src":"679:188:0","nodes":[],"canonicalName":"TypeHashHelper.Transaction","members":[{"constant":false,"id":4,"mutability":"mutable","name":"operation","nameLocation":"714:9:0","nodeType":"VariableDeclaration","scope":17,"src":"708:15:0","stateVariable":false,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_uint8","typeString":"uint8"},"typeName":{"id":3,"name":"uint8","nodeType":"ElementaryTypeName","src":"708:5:0","typeDescriptions":{"typeIdentifier":"t_uint8","typeString":"uint8"}},"visibility":"internal"},{"constant":false,"id":6,"mutability":"mutable","name":"to","nameLocation":"741:2:0","nodeType":"VariableDeclaration","scope":17,"src":"733:10:0","stateVariable":false,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_address","typeString":"address"},"typeName":{"id":5,"name":"address","nodeType":"ElementaryTypeName","src":"733:7:0","stateMutability":"nonpayable","typeDescriptions":{"typeIdentifier":"t_address","typeString":"address"}},"visibility":"internal"},{"constant":false,"id":8,"mutability":"mutable","name":"account","nameLocation":"761:7:0","nodeType":"VariableDeclaration","scope":17,"src":"753:15:0","stateVariable":false,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_address","typeString":"address"},"typeName":{"id":7,"name":"address","nodeType":"ElementaryTypeName","src":"753:7:0","stateMutability":"nonpayable","typeDescriptions":{"typeIdentifier":"t_address","typeString":"address"}},"visibility":"internal"},{"constant":false,"id":10,"mutability":"mutable","name":"executor","nameLocation":"786:8:0","nodeType":"VariableDeclaration","scope":17,"src":"778:16:0","stateVariable":false,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_address","typeString":"address"},"typeName":{"id":9,"name":"address","nodeType":"ElementaryTypeName","src":"778:7:0","stateMutability":"nonpayable","typeDescriptions":{"typeIdentifier":"t_address","typeString":"address"}},"visibility":"internal"},{"constant":false,"id":12,"mutability":"mutable","name":"value","nameLocation":"812:5:0","nodeType":"VariableDeclaration","scope":17,"src":"804:13:0","stateVariable":false,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_uint256","typeString":"uint256"},"typeName":{"id":11,"name":"uint256","nodeType":"ElementaryTypeName","src":"804:7:0","typeDescriptions":{"typeIdentifier":"t_uint256","typeString":"uint256"}},"visibility":"internal"},{"constant":false,"id":14,"mutability":"mutable","name":"nonce","nameLocation":"835:5:0","nodeType":"VariableDeclaration","scope":17,"src":"827:13:0","stateVariable":false,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_uint256","typeString":"uint256"},"typeName":{"id":13,"name":"uint256","nodeType":"ElementaryTypeName","src":"827:7:0","typeDescriptions":{"typeIdentifier":"t_uint256","typeString":"uint256"}},"visibility":"internal"},{"constant":false,"id":16,"mutability":"mutable","name":"data","nameLocation":"856:4:0","nodeType":"VariableDeclaration","scope":17,"src":"850:10:0","stateVariable":false,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_bytes_storage_ptr","typeString":"bytes"},"typeName":{"id":15,"name":"bytes","nodeType":"ElementaryTypeName","src":"850:5:0","typeDescriptions":{"typeIdentifier":"t_bytes_storage_ptr","typeString":"bytes"}},"visibility":"internal"}],"name":"Transaction","nameLocation":"686:11:0","scope":89,"visibility":"public"},{"id":24,"nodeType":"StructDefinition","src":"1176:120:0","nodes":[],"canonicalName":"TypeHashHelper.Validation","members":[{"constant":false,"id":19,"mutability":"mutable","name":"expiryEpoch","nameLocation":"1211:11:0","nodeType":"VariableDeclaration","scope":24,"src":"1204:18:0","stateVariable":false,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_uint32","typeString":"uint32"},"typeName":{"id":18,"name":"uint32","nodeType":"ElementaryTypeName","src":"1204:6:0","typeDescriptions":{"typeIdentifier":"t_uint32","typeString":"uint32"}},"visibility":"internal"},{"constant":false,"id":21,"mutability":"mutable","name":"transactionStructHash","nameLocation":"1240:21:0","nodeType":"VariableDeclaration","scope":24,"src":"1232:29:0","stateVariable":false,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"},"typeName":{"id":20,"name":"bytes32","nodeType":"ElementaryTypeName","src":"1232:7:0","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},"visibility":"internal"},{"constant":false,"id":23,"mutability":"mutable","name":"policyHash","nameLocation":"1279:10:0","nodeType":"VariableDeclaration","scope":24,"src":"1271:18:0","stateVariable":false,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"},"typeName":{"id":22,"name":"bytes32","nodeType":"ElementaryTypeName","src":"1271:7:0","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},"visibility":"internal"}],"name":"Validation","nameLocation":"1183:10:0","scope":89,"visibility":"public"},{"id":28,"nodeType":"VariableDeclaration","src":"1512:128:0","nodes":[],"constant":true,"documentation":{"id":25,"nodeType":"StructuredDocumentation","src":"1302:205:0","text":" @notice EIP712 typehash for transaction data\n @dev keccak256(\"ExecutionParams(address to,uint256 value,bytes data,uint8 operation,address account,address executor,uint256 nonce)\");"},"functionSelector":"aaa5e62d","mutability":"constant","name":"TRANSACTION_PARAMS_TYPEHASH","nameLocation":"1536:27:0","scope":89,"stateVariable":true,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"},"typeName":{"id":26,"name":"bytes32","nodeType":"ElementaryTypeName","src":"1512:7:0","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},"value":{"hexValue":"307866643436323862353361393162333636663139373731333865326564613533623933633866356363373462646138343430663130386437646131653939323930","id":27,"isConstant":false,"isLValue":false,"isPure":true,"kind":"number","lValueRequested":false,"nodeType":"Literal","src":"1574:66:0","typeDescriptions":{"typeIdentifier":"t_rational_114559110941873152283416461588046472969218532235332707338872480181029899440784_by_1","typeString":"int_const 1145...(70 digits omitted)...0784"},"value":"0xfd4628b53a91b366f1977138e2eda53b93c8f5cc74bda8440f108d7da1e99290"},"visibility":"public"},{"id":32,"nodeType":"VariableDeclaration","src":"1942:127:0","nodes":[],"constant":true,"documentation":{"id":29,"nodeType":"StructuredDocumentation","src":"1647:290:0","text":" @notice EIP712 typehash for validation data\n @dev keccak256(\"ValidationParams(ExecutionParams executionParams,bytes32 policyHash,uint32 expiryEpoch)ExecutionParams(address to,uint256 value,bytes data,uint8 operation,address account,address executor,uint256 nonce)\")"},"functionSelector":"06e01350","mutability":"constant","name":"VALIDATION_PARAMS_TYPEHASH","nameLocation":"1966:26:0","scope":89,"stateVariable":true,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"},"typeName":{"id":30,"name":"bytes32","nodeType":"ElementaryTypeName","src":"1942:7:0","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},"value":{"hexValue":"307830633766363533653066363431653431666262346564313434306337643062303862386432613139653163333563666339386465326434373531396531356231","id":31,"isConstant":false,"isLValue":false,"isPure":true,"kind":"number","lValueRequested":false,"nodeType":"Literal","src":"2003:66:0","typeDescriptions":{"typeIdentifier":"t_rational_5652842509744641291905326774718137488969457354485079193916485264203405202865_by_1","typeString":"int_const 5652...(68 digits omitted)...2865"},"value":"0x0c7f653e0f641e41fbb4ed1440c7d0b08b8d2a19e1c35cfc98de2d47519e15b1"},"visibility":"public"},{"id":65,"nodeType":"FunctionDefinition","src":"2226:424:0","nodes":[],"body":{"id":64,"nodeType":"Block","src":"2319:331:0","nodes":[],"statements":[{"expression":{"arguments":[{"arguments":[{"id":44,"name":"TRANSACTION_PARAMS_TYPEHASH","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":28,"src":"2387:27:0","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},{"expression":{"id":45,"name":"txn","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":36,"src":"2432:3:0","typeDescriptions":{"typeIdentifier":"t_struct$_Transaction_$17_memory_ptr","typeString":"struct TypeHashHelper.Transaction memory"}},"id":46,"isConstant":false,"isLValue":true,"isPure":false,"lValueRequested":false,"memberName":"to","nodeType":"MemberAccess","referencedDeclaration":6,"src":"2432:6:0","typeDescriptions":{"typeIdentifier":"t_address","typeString":"address"}},{"expression":{"id":47,"name":"txn","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":36,"src":"2456:3:0","typeDescriptions":{"typeIdentifier":"t_struct$_Transaction_$17_memory_ptr","typeString":"struct TypeHashHelper.Transaction memory"}},"id":48,"isConstant":false,"isLValue":true,"isPure":false,"lValueRequested":false,"memberName":"value","nodeType":"MemberAccess","referencedDeclaration":12,"src":"2456:9:0","typeDescriptions":{"typeIdentifier":"t_uint256","typeString":"uint256"}},{"arguments":[{"expression":{"id":50,"name":"txn","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":36,"src":"2493:3:0","typeDescriptions":{"typeIdentifier":"t_struct$_Transaction_$17_memory_ptr","typeString":"struct TypeHashHelper.Transaction memory"}},"id":51,"isConstant":false,"isLValue":true,"isPure":false,"lValueRequested":false,"memberName":"data","nodeType":"MemberAccess","referencedDeclaration":16,"src":"2493:8:0","typeDescriptions":{"typeIdentifier":"t_bytes_memory_ptr","typeString":"bytes memory"}}],"expression":{"argumentTypes":[{"typeIdentifier":"t_bytes_memory_ptr","typeString":"bytes memory"}],"id":49,"name":"keccak256","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":-8,"src":"2483:9:0","typeDescriptions":{"typeIdentifier":"t_function_keccak256_pure$_t_bytes_memory_ptr_$returns$_t_bytes32_$","typeString":"function (bytes memory) pure returns (bytes32)"}},"id":52,"isConstant":false,"isLValue":false,"isPure":false,"kind":"functionCall","lValueRequested":false,"names":[],"nodeType":"FunctionCall","src":"2483:19:0","tryCall":false,"typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},{"expression":{"id":53,"name":"txn","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":36,"src":"2520:3:0","typeDescriptions":{"typeIdentifier":"t_struct$_Transaction_$17_memory_ptr","typeString":"struct TypeHashHelper.Transaction memory"}},"id":54,"isConstant":false,"isLValue":true,"isPure":false,"lValueRequested":false,"memberName":"operation","nodeType":"MemberAccess","referencedDeclaration":4,"src":"2520:13:0","typeDescriptions":{"typeIdentifier":"t_uint8","typeString":"uint8"}},{"expression":{"id":55,"name":"txn","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":36,"src":"2551:3:0","typeDescriptions":{"typeIdentifier":"t_struct$_Transaction_$17_memory_ptr","typeString":"struct TypeHashHelper.Transaction memory"}},"id":56,"isConstant":false,"isLValue":true,"isPure":false,"lValueRequested":false,"memberName":"account","nodeType":"MemberAccess","referencedDeclaration":8,"src":"2551:11:0","typeDescriptions":{"typeIdentifier":"t_address","typeString":"address"}},{"expression":{"id":57,"name":"txn","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":36,"src":"2580:3:0","typeDescriptions":{"typeIdentifier":"t_struct$_Transaction_$17_memory_ptr","typeString":"struct TypeHashHelper.Transaction memory"}},"id":58,"isConstant":false,"isLValue":true,"isPure":false,"lValueRequested":false,"memberName":"executor","nodeType":"MemberAccess","referencedDeclaration":10,"src":"2580:12:0","typeDescriptions":{"typeIdentifier":"t_address","typeString":"address"}},{"expression":{"id":59,"name":"txn","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":36,"src":"2610:3:0","typeDescriptions":{"typeIdentifier":"t_struct$_Transaction_$17_memory_ptr","typeString":"struct TypeHashHelper.Transaction memory"}},"id":60,"isConstant":false,"isLValue":true,"isPure":false,"lValueRequested":false,"memberName":"nonce","nodeType":"MemberAccess","referencedDeclaration":14,"src":"2610:9:0","typeDescriptions":{"typeIdentifier":"t_uint256","typeString":"uint256"}}],"expression":{"argumentTypes":[{"typeIdentifier":"t_bytes32","typeString":"bytes32"},{"typeIdentifier":"t_address","typeString":"address"},{"typeIdentifier":"t_uint256","typeString":"uint256"},{"typeIdentifier":"t_bytes32","typeString":"bytes32"},{"typeIdentifier":"t_uint8","typeString":"uint8"},{"typeIdentifier":"t_address","typeString":"address"},{"typeIdentifier":"t_address","typeString":"address"},{"typeIdentifier":"t_uint256","typeString":"uint256"}],"expression":{"id":42,"name":"abi","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":-1,"src":"2359:3:0","typeDescriptions":{"typeIdentifier":"t_magic_abi","typeString":"abi"}},"id":43,"isConstant":false,"isLValue":false,"isPure":true,"lValueRequested":false,"memberName":"encode","nodeType":"MemberAccess","src":"2359:10:0","typeDescriptions":{"typeIdentifier":"t_function_abiencode_pure$__$returns$_t_bytes_memory_ptr_$","typeString":"function () pure returns (bytes memory)"}},"id":61,"isConstant":false,"isLValue":false,"isPure":false,"kind":"functionCall","lValueRequested":false,"names":[],"nodeType":"FunctionCall","src":"2359:274:0","tryCall":false,"typeDescriptions":{"typeIdentifier":"t_bytes_memory_ptr","typeString":"bytes memory"}}],"expression":{"argumentTypes":[{"typeIdentifier":"t_bytes_memory_ptr","typeString":"bytes memory"}],"id":41,"name":"keccak256","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":-8,"src":"2336:9:0","typeDescriptions":{"typeIdentifier":"t_function_keccak256_pure$_t_bytes_memory_ptr_$returns$_t_bytes32_$","typeString":"function (bytes memory) pure returns (bytes32)"}},"id":62,"isConstant":false,"isLValue":false,"isPure":false,"kind":"functionCall","lValueRequested":false,"names":[],"nodeType":"FunctionCall","src":"2336:307:0","tryCall":false,"typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},"functionReturnParameters":40,"id":63,"nodeType":"Return","src":"2329:314:0"}]},"documentation":{"id":33,"nodeType":"StructuredDocumentation","src":"2076:145:0","text":" @notice Builds EIP712 transaction struct hash\n @param txn transaction params struct\n @return transactionStructHash"},"implemented":true,"kind":"function","modifiers":[],"name":"_buildTransactionStructHash","nameLocation":"2235:27:0","parameters":{"id":37,"nodeType":"ParameterList","parameters":[{"constant":false,"id":36,"mutability":"mutable","name":"txn","nameLocation":"2282:3:0","nodeType":"VariableDeclaration","scope":65,"src":"2263:22:0","stateVariable":false,"storageLocation":"memory","typeDescriptions":{"typeIdentifier":"t_struct$_Transaction_$17_memory_ptr","typeString":"struct TypeHashHelper.Transaction"},"typeName":{"id":35,"nodeType":"UserDefinedTypeName","pathNode":{"id":34,"name":"Transaction","nodeType":"IdentifierPath","referencedDeclaration":17,"src":"2263:11:0"},"referencedDeclaration":17,"src":"2263:11:0","typeDescriptions":{"typeIdentifier":"t_struct$_Transaction_$17_storage_ptr","typeString":"struct TypeHashHelper.Transaction"}},"visibility":"internal"}],"src":"2262:24:0"},"returnParameters":{"id":40,"nodeType":"ParameterList","parameters":[{"constant":false,"id":39,"mutability":"mutable","name":"","nameLocation":"-1:-1:-1","nodeType":"VariableDeclaration","scope":65,"src":"2310:7:0","stateVariable":false,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"},"typeName":{"id":38,"name":"bytes32","nodeType":"ElementaryTypeName","src":"2310:7:0","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},"visibility":"internal"}],"src":"2309:9:0"},"scope":89,"stateMutability":"pure","virtual":false,"visibility":"internal"},{"id":88,"nodeType":"FunctionDefinition","src":"2810:352:0","nodes":[],"body":{"id":87,"nodeType":"Block","src":"2908:254:0","nodes":[],"statements":[{"expression":{"arguments":[{"arguments":[{"id":77,"name":"VALIDATION_PARAMS_TYPEHASH","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":32,"src":"2976:26:0","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},{"expression":{"id":78,"name":"validation","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":69,"src":"3020:10:0","typeDescriptions":{"typeIdentifier":"t_struct$_Validation_$24_memory_ptr","typeString":"struct TypeHashHelper.Validation memory"}},"id":79,"isConstant":false,"isLValue":true,"isPure":false,"lValueRequested":false,"memberName":"transactionStructHash","nodeType":"MemberAccess","referencedDeclaration":21,"src":"3020:32:0","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},{"expression":{"id":80,"name":"validation","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":69,"src":"3070:10:0","typeDescriptions":{"typeIdentifier":"t_struct$_Validation_$24_memory_ptr","typeString":"struct TypeHashHelper.Validation memory"}},"id":81,"isConstant":false,"isLValue":true,"isPure":false,"lValueRequested":false,"memberName":"policyHash","nodeType":"MemberAccess","referencedDeclaration":23,"src":"3070:21:0","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},{"expression":{"id":82,"name":"validation","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":69,"src":"3109:10:0","typeDescriptions":{"typeIdentifier":"t_struct$_Validation_$24_memory_ptr","typeString":"struct TypeHashHelper.Validation memory"}},"id":83,"isConstant":false,"isLValue":true,"isPure":false,"lValueRequested":false,"memberName":"expiryEpoch","nodeType":"MemberAccess","referencedDeclaration":19,"src":"3109:22:0","typeDescriptions":{"typeIdentifier":"t_uint32","typeString":"uint32"}}],"expression":{"argumentTypes":[{"typeIdentifier":"t_bytes32","typeString":"bytes32"},{"typeIdentifier":"t_bytes32","typeString":"bytes32"},{"typeIdentifier":"t_bytes32","typeString":"bytes32"},{"typeIdentifier":"t_uint32","typeString":"uint32"}],"expression":{"id":75,"name":"abi","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":-1,"src":"2948:3:0","typeDescriptions":{"typeIdentifier":"t_magic_abi","typeString":"abi"}},"id":76,"isConstant":false,"isLValue":false,"isPure":true,"lValueRequested":false,"memberName":"encode","nodeType":"MemberAccess","src":"2948:10:0","typeDescriptions":{"typeIdentifier":"t_function_abiencode_pure$__$returns$_t_bytes_memory_ptr_$","typeString":"function () pure returns (bytes memory)"}},"id":84,"isConstant":false,"isLValue":false,"isPure":false,"kind":"functionCall","lValueRequested":false,"names":[],"nodeType":"FunctionCall","src":"2948:197:0","tryCall":false,"typeDescriptions":{"typeIdentifier":"t_bytes_memory_ptr","typeString":"bytes memory"}}],"expression":{"argumentTypes":[{"typeIdentifier":"t_bytes_memory_ptr","typeString":"bytes memory"}],"id":74,"name":"keccak256","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":-8,"src":"2925:9:0","typeDescriptions":{"typeIdentifier":"t_function_keccak256_pure$_t_bytes_memory_ptr_$returns$_t_bytes32_$","typeString":"function (bytes memory) pure returns (bytes32)"}},"id":85,"isConstant":false,"isLValue":false,"isPure":false,"kind":"functionCall","lValueRequested":false,"names":[],"nodeType":"FunctionCall","src":"2925:230:0","tryCall":false,"typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},"functionReturnParameters":73,"id":86,"nodeType":"Return","src":"2918:237:0"}]},"documentation":{"id":66,"nodeType":"StructuredDocumentation","src":"2656:149:0","text":" @notice Builds EIP712 validation struct hash\n @param validation validation params struct\n @return validationStructHash"},"implemented":true,"kind":"function","modifiers":[],"name":"_buildValidationStructHash","nameLocation":"2819:26:0","parameters":{"id":70,"nodeType":"ParameterList","parameters":[{"constant":false,"id":69,"mutability":"mutable","name":"validation","nameLocation":"2864:10:0","nodeType":"VariableDeclaration","scope":88,"src":"2846:28:0","stateVariable":false,"storageLocation":"memory","typeDescriptions":{"typeIdentifier":"t_struct$_Validation_$24_memory_ptr","typeString":"struct TypeHashHelper.Validation"},"typeName":{"id":68,"nodeType":"UserDefinedTypeName","pathNode":{"id":67,"name":"Validation","nodeType":"IdentifierPath","referencedDeclaration":24,"src":"2846:10:0"},"referencedDeclaration":24,"src":"2846:10:0","typeDescriptions":{"typeIdentifier":"t_struct$_Validation_$24_storage_ptr","typeString":"struct TypeHashHelper.Validation"}},"visibility":"internal"}],"src":"2845:30:0"},"returnParameters":{"id":73,"nodeType":"ParameterList","parameters":[{"constant":false,"id":72,"mutability":"mutable","name":"","nameLocation":"-1:-1:-1","nodeType":"VariableDeclaration","scope":88,"src":"2899:7:0","stateVariable":false,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"},"typeName":{"id":71,"name":"bytes32","nodeType":"ElementaryTypeName","src":"2899:7:0","typeDescriptions":{"typeIdentifier":"t_bytes32","typeString":"bytes32"}},"visibility":"internal"}],"src":"2898:9:0"},"scope":89,"stateMutability":"pure","virtual":false,"visibility":"internal"}],"abstract":false,"baseContracts":[],"canonicalName":"TypeHashHelper","contractDependencies":[],"contractKind":"library","documentation":{"id":2,"nodeType":"StructuredDocumentation","src":"98:139:0","text":" @title TypeHashHelper\n @author Brahma.fi\n @notice Helper library containing functions to build EIP712 struct and type hashes"},"fullyImplemented":true,"linearizedBaseContracts":[89],"name":"TypeHashHelper","nameLocation":"248:14:0","scope":90,"usedErrors":[]}],"license":"BUSL-1.1"} \ No newline at end of file diff --git a/src/optimizor/ast.rs b/src/optimizor/ast.rs new file mode 100644 index 0000000..6ef435a --- /dev/null +++ b/src/optimizor/ast.rs @@ -0,0 +1,5 @@ +use ethers_solc; + +fn ast() { + +} \ No newline at end of file diff --git a/src/optimizor/gas_tricks.rs b/src/optimizor/gas_tricks.rs index 8f9cdb6..c4555a4 100644 --- a/src/optimizor/gas_tricks.rs +++ b/src/optimizor/gas_tricks.rs @@ -1,4 +1,5 @@ use regex::Regex; +use serde_json::json; use serde_json::Map; pub fn bytes32(contract: &str, gas_inefficiencies: &mut Map) { @@ -16,10 +17,22 @@ pub fn bytes32(contract: &str, gas_inefficiencies: &mut Map = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr.push("Use Uint256 instead of bytes32 to store constant".to_string()); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = vec!["Use Uint256 instead of bytes32 to store constant"]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } + println!( "Use Uint256 instead of bytes32 to store constant: Modifier: {}, variable_name: {}, Line: {}", modifier, @@ -39,13 +52,28 @@ pub fn openzepplin(contract: &str, gas_inefficiencies: &mut Map = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr.push("instead of using openzeppelin we can use solady which is way cheaper and way efficient [https://github.com/Vectorized/solady".to_string()); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = vec!["instead of using openzeppelin we can use solady which is way cheaper and way efficient [https://github.com/Vectorized/solady"]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } + let modifier = capture.get(0).map_or("default", |m| m.as_str()); - gas_inefficiencies.insert(inefficiency_id, "instead of using openzeppelin we can use solady which is way cheaper and way efficient [https://github.com/Vectorized/solady".into()); println!( "instead of using openzeppelin we can use solady which is way cheaper and way efficient [https://github.com/Vectorized/solady: {}", - modifier - ); + modifier + ); } } } @@ -57,12 +85,27 @@ pub fn safemath(contract: &str, gas_inefficiencies: &mut Map = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr.push("SafeMath is no longer needed since solidity version 0.8.0, use of safeMath can be considered unnessary".to_string()); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = vec!["SafeMath is no longer needed since solidity version 0.8.0, use of safeMath can be considered unnessary"]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } + let modifier = capture.get(0).map_or("default", |m| m.as_str()); println!( "SafeMath is no longer needed since solidity version 0.8.0, use of safeMath can be considered unnessary: {}", modifier ); - gas_inefficiencies.insert(inefficiency_id, "SafeMath is no longer needed since solidity version 0.8.0, use of safeMath can be considered unnessary".into()); } } } @@ -75,12 +118,27 @@ pub fn token(contract: &str, gas_inefficiencies: &mut Map = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr.push( + "For string of length less than 33, its better to use uint256 to store them" + .to_string(), + ); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = vec![ + "For string of length less than 33, its better to use uint256 to store them", + ]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } println!("For string of length less than 33, its better to use uint256 to store them"); - - gas_inefficiencies.insert( - inefficiency_id, - "For string of length less than 33, its better to use uint256 to store them".into(), - ); } } } @@ -97,12 +155,29 @@ pub fn uint_incur_overhead( for (line_number, line) in lines.iter().enumerate() { if variable_declaration_regex.captures(line).is_some() { let inefficiency_id = format!("line_{}", line_number + 1); + + if let Some(existing_value) = gas_inefficiencies.get_mut(&inefficiency_id) { + // Slot exists, append the new issue to the existing array + let mut existing_arr: Vec = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr.push( + "instead of a uint24, uint16 or any uint and int type apart from uint256 or int256, it's way better to use uint256 or int256" + .to_string(), + ); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = vec![ + "instead of a uint24, uint16 or any uint and int type apart from uint256 or int256, it's way better to use uint256 or int256", + ]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } + println!( "instead of a uint24, uint16 or any uint and int type apart from uint256 or int256, it's way better to use uint256 or int256:" - ); - gas_inefficiencies.insert( - inefficiency_id, - "instead of a uint24, uint16 or any uint and int type apart from uint256 or int256, it's way better to use uint256 or int256".into(), ); } } @@ -137,12 +212,28 @@ pub fn mapping_instead_array( for (line_number, line) in lines.iter().enumerate() { if regexe.captures(line).is_some() { let inefficiency_id = format!("line_{}", line_number + 1); - println!("Use mapping instead of array: Modifier:"); - gas_inefficiencies.insert( - inefficiency_id, - "instead of a uint24, uint16 or any uint and int type apart from uint256 or int256, it's way better to use uint256 or int256".into(), - ); + if let Some(existing_value) = gas_inefficiencies.get_mut(&inefficiency_id) { + // Slot exists, append the new issue to the existing array + let mut existing_arr: Vec = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr.push( + "Use mapping instead of array: Modifier:" + .to_string(), + ); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = vec![ + "Use mapping instead of array: Modifier:", + ]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } + + println!("Use mapping instead of array: Modifier:"); } } } @@ -160,12 +251,28 @@ pub fn uint256_instead_bool( for (line_number, line) in lines.iter().enumerate() { if regexe.captures(line).is_some() { let inefficiency_id = format!("line_{}", line_number + 1); - println!("Use uint256 type to store boolean value instead of bool"); - gas_inefficiencies.insert( - inefficiency_id, - "instead of a uint24, uint16 or any uint and int type apart from uint256 or int256, it's way better to use uint256 or int256".into(), - ); + if let Some(existing_value) = gas_inefficiencies.get_mut(&inefficiency_id) { + // Slot exists, append the new issue to the existing array + let mut existing_arr: Vec = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr.push( + "Use uint256 type to store boolean value instead of bool" + .to_string(), + ); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = vec![ + "Use uint256 type to store boolean value instead of bool", + ]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } + + println!("Use uint256 type to store boolean value instead of bool"); } } } @@ -180,12 +287,25 @@ pub fn use_named_retunrs(contract: &str, gas_inefficiencies: &mut Map = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr.push( + "Use named returns".to_string(), + ); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = + vec!["Use named returns"]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } + println!("Use named returns"); } } } @@ -199,12 +319,27 @@ pub fn require_double_logic( for (line_number, line) in lines.iter().enumerate() { if regexe.captures(line).is_some() { let inefficiency_id = format!("line_{}", line_number + 1); - println!("split require statements "); - gas_inefficiencies.insert( - inefficiency_id, - "split require statements that use && into two seperate parts to save gas ".into(), - ); + if let Some(existing_value) = gas_inefficiencies.get_mut(&inefficiency_id) { + // Slot exists, append the new issue to the existing array + let mut existing_arr: Vec = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr.push( + "split require statements that use && into two seperate parts to save gas " + .to_string(), + ); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = vec![ + "split require statements that use && into two seperate parts to save gas ", + ]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } + println!("split require statements "); } } } @@ -215,12 +350,25 @@ pub fn revert_32(contract: &str, gas_inefficiencies: &mut Map = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr.push( + "revert statement that has it's string longer than 32 length is always more expensive ".to_string(), + ); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = + vec!["revert statement that has it's string longer than 32 length is always more expensive "]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } + println!("string length more thaan 32 "); } } } @@ -231,45 +379,83 @@ pub fn do_while(contract: &str, gas_inefficiencies: &mut Map = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr + .push("do while loops are cheaper than loops and consume less gas".to_string()); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = vec!["do while loops are cheaper than loops and consume less gas"]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } + println!("use do while loops instead of for loops "); } } } -pub fn priv_constants_immut(contract: &str, gas_inefficiencies: &mut Map) { +pub fn priv_constants_immut( + contract: &str, + gas_inefficiencies: &mut Map, +) { let regexe = Regex::new(r"\bpublic\b.*(constant | immutable)").unwrap(); let lines: Vec<&str> = contract.lines().collect(); for (line_number, line) in lines.iter().enumerate() { if regexe.captures(line).is_some() { let inefficiency_id = format!("line_{}", line_number + 1); + if let Some(existing_value) = gas_inefficiencies.get_mut(&inefficiency_id) { + // Slot exists, append the new issue to the existing array + let mut existing_arr: Vec = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr.push( + "variables that are constant should have a visibility of private".to_string(), + ); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = + vec!["variables that are constant should have a visibility of private"]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } println!("variables that are constant should have a visibility of private"); - - gas_inefficiencies.insert( - inefficiency_id, - "variables that are constant should have a visibility of private".into(), - ); } } } - pub fn emit_loops(contract: &str, gas_inefficiencies: &mut Map) { let regexe = Regex::new(r"\bfor\s*\(\s*.*emit").unwrap(); let lines: Vec<&str> = contract.lines().collect(); for (line_number, line) in lines.iter().enumerate() { if regexe.captures(line).is_some() { let inefficiency_id = format!("line_{}", line_number + 1); + if let Some(existing_value) = gas_inefficiencies.get_mut(&inefficiency_id) { + // Slot exists, append the new issue to the existing array + let mut existing_arr: Vec = + serde_json::from_value(existing_value.clone()).unwrap_or_default(); + + existing_arr.push( + "emiting events in loops cost more, and should be done using other means" + .to_string(), + ); + + // Update the value in the map + gas_inefficiencies.insert(inefficiency_id, json!(existing_arr)); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = + vec!["emiting events in loops cost more, and should be done using other means"]; + gas_inefficiencies.insert(inefficiency_id, json!(new_arr)); + } println!("emiting events in loops cost more, and should be done using other means"); - - gas_inefficiencies.insert( - inefficiency_id, - "emiting events in loops cost more, and should be done using other means".into(), - ); } } }