]> git.ardis.lu - 4-byte-gen/commitdiff
Automatically map Solidity to ABI types
authorArdis Lu <ardisglu@gmail.com>
Sun, 20 Aug 2023 03:57:48 +0000 (20:57 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sun, 20 Aug 2023 03:57:48 +0000 (20:57 -0700)
index.html

index 36f40ee3871031ee74d85805a3ed2f7a074b2b80..813f82fd6d7147df4b6e2a17ac976d24f1992e0f 100644 (file)
       const { name, parameters } = functionRegex.exec(fn).groups;
 
       // Drop parameter names and extraneous whitespace, if there are any
-      // E.g. "uint256 abc, uint256, bool test" --> "uint256,uint256,bool"
-      const parametersTypes = parameters.split(',').map(param => param.trim().split(' ')[0]).join(',');
+      // E.g. "uint abc, uint256, bool test" --> ['uint', 'uint256', 'bool']
+      const parametersArray = parameters.split(',').map(param => param.trim().split(' ')[0]);
+
+      // Convert types from Solidity to ABI (e.g., 'uint' --> 'uint256')
+      // Reference: https://docs.soliditylang.org/en/latest/abi-spec.html#types
+      for (const [index, param] of parametersArray.entries()) {
+        switch (param) {
+          case 'int': parametersArray[index] = 'int256'; break;
+          case 'uint': parametersArray[index] = 'uint256'; break;
+          case 'fixed': parametersArray[index] = 'fixed128x18'; break;
+          case 'ufixed': parametersArray[index] = 'ufixed128x18'; break;
+          case 'contract': parametersArray[index] = 'address'; break;
+          case 'enum': parametersArray[index] = 'uint8'; break;
+          case 'struct': parametersArray[index] = 'tuple'; break;
+        }
+      }
+
+      const parametersTypes = parametersArray.join(',');
 
       return { name, parametersTypes };
     }
This page took 0.034149 seconds and 4 git commands to generate.