* Reads contracts from the ABIs. * @returns array of contracts
()
| 48 | * @returns array of contracts |
| 49 | */ |
| 50 | private async getPrograms(): Promise<Program[]> { |
| 51 | const allFiles = getAllFiles(this.workspaceRoot, []); |
| 52 | const swayFilePaths = allFiles.filter(file => file.endsWith('.sw')); |
| 53 | const forcTomlFilePaths = allFiles.filter(file => |
| 54 | file.endsWith('Forc.toml') |
| 55 | ); |
| 56 | const abiFilePaths = allFiles.filter(file => |
| 57 | file.endsWith(ABI_FILE_SUFFIX) |
| 58 | ); |
| 59 | const programs = abiFilePaths.map(filepath => { |
| 60 | const contractName = path |
| 61 | .parse(filepath) |
| 62 | .base.replace(ABI_FILE_SUFFIX, ''); |
| 63 | |
| 64 | // This is assuming that there is only one sway contract in the same directory as a Forc.toml |
| 65 | const forcFilePath = forcTomlFilePaths.find(forcFilePath => |
| 66 | fs.readFileSync(forcFilePath).toString().includes(contractName) |
| 67 | ); |
| 68 | |
| 69 | // Find out the program type of the sway file |
| 70 | const swayFilePath = swayFilePaths.find(swayFilePath => { |
| 71 | return swayFilePath.startsWith(path.parse(forcFilePath).dir); |
| 72 | }); |
| 73 | const swayFileMatchesType = fs |
| 74 | .readFileSync(swayFilePath) |
| 75 | .toString() |
| 76 | .startsWith(this.type); |
| 77 | if (!swayFileMatchesType) return undefined; |
| 78 | |
| 79 | // Attach the source file path to each function node in addition to the contract node |
| 80 | const buffer = fs.readFileSync(filepath); |
| 81 | const abi: Object[] = JSON.parse(buffer.toString()); |
| 82 | const functions = abi |
| 83 | .filter(obj => obj['type'] === 'function') |
| 84 | .map(func => new Function(func['name'], swayFilePath)); |
| 85 | |
| 86 | return new Program(contractName, swayFilePath, functions, this.type); |
| 87 | }); |
| 88 | |
| 89 | // Filter out programs that do not match the given type |
| 90 | return programs.filter(program => !!program); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
no test coverage detected