|
| 1 | +const fs = require('fs'); |
| 2 | +const exec = require('child_process').execSync; |
| 3 | +const chalk = require('chalk'); |
| 4 | + |
| 5 | +// These functions/events are allowed to differ. (These are present in STR/STRGetter but not used and hence not defined in ISTR) |
| 6 | +let strExceptions = [ |
| 7 | + "initialize", |
| 8 | + "getBytes32Value", |
| 9 | + "getBytesValue", |
| 10 | + "getAddressValue", |
| 11 | + "getArrayAddress", |
| 12 | + "getBoolValue", |
| 13 | + "getStringValue", |
| 14 | + "getArrayBytes32", |
| 15 | + "getUintValue", |
| 16 | + "getArrayUint", |
| 17 | + undefined, // constructor |
| 18 | +]; |
| 19 | + |
| 20 | +// These functions/events are allowed to differ. (These are present in ST/STGetter but not used and hence not defined in IST) |
| 21 | +let stExceptions = [ |
| 22 | + undefined, // constructor |
| 23 | +]; |
| 24 | + |
| 25 | +async function readFiles() { |
| 26 | + if (!fs.existsSync("./build/contracts/ISecurityTokenRegistry.json") || !fs.existsSync("./build/contracts/ISecurityToken.json")) { |
| 27 | + console.log(chalk.yellow('Compiling contracts. This may take a while, please wait.')); |
| 28 | + exec('./node_modules/.bin/truffle compile'); |
| 29 | + } |
| 30 | + let strABIs = [ |
| 31 | + JSON.parse(fs.readFileSync(`./build/contracts/ISecurityTokenRegistry.json`).toString()).abi, |
| 32 | + JSON.parse(fs.readFileSync(`./build/contracts/SecurityTokenRegistry.json`).toString()).abi, |
| 33 | + JSON.parse(fs.readFileSync(`./build/contracts/STRGetter.json`).toString()).abi |
| 34 | + ]; |
| 35 | + let stABIs = [ |
| 36 | + JSON.parse(fs.readFileSync(`./build/contracts/ISecurityToken.json`).toString()).abi, |
| 37 | + JSON.parse(fs.readFileSync(`./build/contracts/SecurityToken.json`).toString()).abi, |
| 38 | + JSON.parse(fs.readFileSync(`./build/contracts/STGetter.json`).toString()).abi |
| 39 | + ]; |
| 40 | + return [strABIs, stABIs]; |
| 41 | + } |
| 42 | + |
| 43 | +async function checkInterfaces() { |
| 44 | + // Reading ABIs from build files |
| 45 | + let [strABIs, stABIs] = await readFiles(); |
| 46 | + // Perform checks on the interface files |
| 47 | + let strMismatch = await interfaceCheck(strABIs, strExceptions, "ISecurityTokenRegistry"); |
| 48 | + let stMismatch = await interfaceCheck(stABIs, stExceptions, "ISecurityToken"); |
| 49 | + if (strMismatch || stMismatch) {process.exit(1)}; |
| 50 | + console.log(chalk.green("Interface checks finished. No mismatch was found between interfaces and contacts")); |
| 51 | +} |
| 52 | + |
| 53 | +async function interfaceCheck(ABIs, exceptions, interfaceName) { |
| 54 | + // Removing functions/events defined in the exceptions arrays. |
| 55 | + removeExceptions(ABIs, exceptions); |
| 56 | + // Removing parameter and return names from ABIs as they can differ. |
| 57 | + // Only cleaning ABIs of third file as other files can be cleaned in the following loops efficiently. |
| 58 | + for (let i = 0; i < ABIs[2].length; i++) { |
| 59 | + cleanABI(ABIs[2][i]); |
| 60 | + } |
| 61 | + // Removing one instance of duplicates common to main contract and getter contract |
| 62 | + removeDuplicates(ABIs); |
| 63 | + // Combine second and third ABI after duplicates were removed |
| 64 | + ABIs = [ABIs[0],[...ABIs[1],...ABIs[2]]]; |
| 65 | + |
| 66 | + // This function removes elements that match between the interface and contracts |
| 67 | + // i.e If the signature matches in Interface and the contract, it is removed from the ABI. |
| 68 | + // This means that the left over elements after this loop are mistakes. |
| 69 | + for (let i = 0; i < ABIs[0].length; i++) { |
| 70 | + let fna = cleanABI(ABIs[0][i]); |
| 71 | + for (let j = 0; j < ABIs[1].length; j++) { |
| 72 | + let fnb = ABIs[1][j]; |
| 73 | + if (fna.name == fnb.name && fna.type == fnb.type) { |
| 74 | + if (JSON.stringify(fna) === JSON.stringify(fnb)) { |
| 75 | + ABIs[0].splice(i, 1); |
| 76 | + ABIs[1].splice(j, 1); |
| 77 | + i--; |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // If there is any element remaining in either ABI, it is an error and they should be reported. |
| 85 | + if (ABIs[0].length >= 1 || ABIs[1].length >= 1) { |
| 86 | + for (let i = 0; i < ABIs[0].length; i++) { |
| 87 | + console.log(ABIs[0][i].name); |
| 88 | + } |
| 89 | + for (let i = 0; i < ABIs[1].length; i++) { |
| 90 | + console.log(ABIs[1][i].name); |
| 91 | + } |
| 92 | + console.log(chalk.red('The above Functions/events had a mismatch with ' + interfaceName + '. Please synchronize the Interface with the contract.\n')); |
| 93 | + return true; |
| 94 | + } |
| 95 | + return false; |
| 96 | +} |
| 97 | + |
| 98 | +// This function removes parameter and return names from ABIs as they can differ. |
| 99 | +function cleanABI(element) { |
| 100 | + if (element.type === 'event') { |
| 101 | + for(let i = 0; i < element.inputs.length; i++) { |
| 102 | + element.inputs[i].name = ""; |
| 103 | + } |
| 104 | + } else if (element.type === 'function') { |
| 105 | + for(let i = 0; i < element.inputs.length; i++) { |
| 106 | + element.inputs[i].name = ""; |
| 107 | + } |
| 108 | + for(let i = 0; i < element.outputs.length; i++) { |
| 109 | + element.outputs[i].name = ""; |
| 110 | + } |
| 111 | + } |
| 112 | + return element; |
| 113 | +} |
| 114 | + |
| 115 | +// This function removes one instance of duplicates common to main contract and getter |
| 116 | +function removeDuplicates(ABIs) { |
| 117 | + for (let i = 0; i < ABIs[1].length; i++) { |
| 118 | + let fna = cleanABI(ABIs[1][i]); |
| 119 | + for (let j = 0; j < ABIs[2].length; j++) { |
| 120 | + let fnb = ABIs[2][j]; |
| 121 | + if (fna.name == fnb.name && fna.type == fnb.type) { |
| 122 | + if (JSON.stringify(fna) === JSON.stringify(fnb)) { |
| 123 | + ABIs[1].splice(i, 1); |
| 124 | + i--; |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// This function removes functions/events defined in an exceptions arrays. |
| 133 | +function removeExceptions(ABIs, exceptions) { |
| 134 | + for (let i = 0; i < ABIs.length; i++) { |
| 135 | + for (let j = 0; j < ABIs[i].length; j++) { |
| 136 | + if (exceptions.includes(ABIs[i][j].name)) { |
| 137 | + ABIs[i].splice(j, 1); |
| 138 | + j--; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +checkInterfaces(); |
0 commit comments