|
| 1 | +const immutable = require('immutable') |
| 2 | +const ABI = require('./abi') |
| 3 | +const Subgraph = require('../../subgraph') |
| 4 | + |
| 5 | +module.exports = class EthereumSubgraph { |
| 6 | + constructor(options = {}) { |
| 7 | + this.manifest = options.manifest |
| 8 | + this.resolveFile = options.resolveFile |
| 9 | + } |
| 10 | + |
| 11 | + validateManifest() { |
| 12 | + return this.validateAbis() |
| 13 | + .concat(this.validateContractAddresses()) |
| 14 | + .concat(this.validateEvents()) |
| 15 | + .concat(this.validateCallFunctions()) |
| 16 | + } |
| 17 | + |
| 18 | + validateAbis() { |
| 19 | + let dataSources = Subgraph.collectDataSources(this.manifest, 'ethereum/contract') |
| 20 | + let dataSourceTemplates = Subgraph.collectDataSourceTemplates(this.manifest, 'ethereum/contract') |
| 21 | + |
| 22 | + return dataSources.concat(dataSourceTemplates).reduce( |
| 23 | + (errors, dataSourceOrTemplate) => |
| 24 | + errors.concat( |
| 25 | + this.validateDataSourceAbis( |
| 26 | + dataSourceOrTemplate.get('dataSource'), |
| 27 | + dataSourceOrTemplate.get('path'), |
| 28 | + ), |
| 29 | + ), |
| 30 | + immutable.List(), |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + validateDataSourceAbis(dataSource, path) { |
| 35 | + // Validate that the the "source > abi" reference of all data sources |
| 36 | + // points to an existing ABI in the data source ABIs |
| 37 | + let abiName = dataSource.getIn(['source', 'abi']) |
| 38 | + let abiNames = dataSource.getIn(['mapping', 'abis']).map(abi => abi.get('name')) |
| 39 | + let nameErrors = abiNames.includes(abiName) |
| 40 | + ? immutable.List() |
| 41 | + : immutable.fromJS([ |
| 42 | + { |
| 43 | + path: [...path, 'source', 'abi'], |
| 44 | + message: `\ |
| 45 | +ABI name '${abiName}' not found in mapping > abis. |
| 46 | +Available ABIs: |
| 47 | +${abiNames |
| 48 | + .sort() |
| 49 | + .map(name => `- ${name}`) |
| 50 | + .join('\n')}`, |
| 51 | + }, |
| 52 | + ]) |
| 53 | + |
| 54 | + // Validate that all ABI files are valid |
| 55 | + let fileErrors = dataSource |
| 56 | + .getIn(['mapping', 'abis']) |
| 57 | + .reduce((errors, abi, abiIndex) => { |
| 58 | + try { |
| 59 | + ABI.load(abi.get('name'), this.resolveFile(abi.get('file'))) |
| 60 | + return errors |
| 61 | + } catch (e) { |
| 62 | + return errors.push( |
| 63 | + immutable.fromJS({ |
| 64 | + path: [...path, 'mapping', 'abis', abiIndex, 'file'], |
| 65 | + message: e.message, |
| 66 | + }), |
| 67 | + ) |
| 68 | + } |
| 69 | + }, immutable.List()) |
| 70 | + |
| 71 | + return nameErrors.concat(fileErrors) |
| 72 | + } |
| 73 | + |
| 74 | + validateContractAddresses() { |
| 75 | + const ethereumAddressPattern = /^(0x)?[0-9a-fA-F]{40}$/ |
| 76 | + |
| 77 | + return Subgraph.validateContractAddresses( |
| 78 | + this.manifest, |
| 79 | + 'ethereum/contract', |
| 80 | + address => ethereumAddressPattern.test(address), |
| 81 | + "Must be 40 hexadecimal characters, with an optional '0x' prefix.", |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + validateEvents() { |
| 86 | + let dataSources = Subgraph.collectDataSources(this.manifest, 'ethereum/contract') |
| 87 | + let dataSourceTemplates = Subgraph.collectDataSourceTemplates(this.manifest, 'ethereum/contract') |
| 88 | + |
| 89 | + return dataSources |
| 90 | + .concat(dataSourceTemplates) |
| 91 | + .reduce((errors, dataSourceOrTemplate) => { |
| 92 | + return errors.concat( |
| 93 | + this.validateDataSourceEvents( |
| 94 | + dataSourceOrTemplate.get('dataSource'), |
| 95 | + dataSourceOrTemplate.get('path'), |
| 96 | + ), |
| 97 | + ) |
| 98 | + }, immutable.List()) |
| 99 | + } |
| 100 | + |
| 101 | + validateDataSourceEvents(dataSource, path) { |
| 102 | + let abi |
| 103 | + try { |
| 104 | + // Resolve the source ABI name into a real ABI object |
| 105 | + let abiName = dataSource.getIn(['source', 'abi']) |
| 106 | + let abiEntry = dataSource |
| 107 | + .getIn(['mapping', 'abis']) |
| 108 | + .find(abi => abi.get('name') === abiName) |
| 109 | + abi = ABI.load(abiEntry.get('name'), this.resolveFile(abiEntry.get('file'))) |
| 110 | + } catch (_) { |
| 111 | + // Ignore errors silently; we can't really say anything about |
| 112 | + // the events if the ABI can't even be loaded |
| 113 | + return immutable.List() |
| 114 | + } |
| 115 | + |
| 116 | + // Obtain event signatures from the mapping |
| 117 | + let manifestEvents = dataSource |
| 118 | + .getIn(['mapping', 'eventHandlers'], immutable.List()) |
| 119 | + .map(handler => handler.get('event')) |
| 120 | + |
| 121 | + // Obtain event signatures from the ABI |
| 122 | + let abiEvents = abi.eventSignatures() |
| 123 | + |
| 124 | + // Add errors for every manifest event signature that is not |
| 125 | + // present in the ABI |
| 126 | + return manifestEvents.reduce( |
| 127 | + (errors, manifestEvent, index) => |
| 128 | + abiEvents.includes(manifestEvent) |
| 129 | + ? errors |
| 130 | + : errors.push( |
| 131 | + immutable.fromJS({ |
| 132 | + path: [...path, 'eventHandlers', index], |
| 133 | + message: `\ |
| 134 | +Event with signature '${manifestEvent}' not present in ABI '${abi.name}'. |
| 135 | +Available events: |
| 136 | +${abiEvents |
| 137 | + .sort() |
| 138 | + .map(event => `- ${event}`) |
| 139 | + .join('\n')}`, |
| 140 | + }), |
| 141 | + ), |
| 142 | + immutable.List(), |
| 143 | + ) |
| 144 | + } |
| 145 | + |
| 146 | + validateCallFunctions() { |
| 147 | + return this.manifest |
| 148 | + .get('dataSources') |
| 149 | + .filter(dataSource => dataSource.get('kind') === 'ethereum/contract') |
| 150 | + .reduce((errors, dataSource, dataSourceIndex) => { |
| 151 | + let path = ['dataSources', dataSourceIndex, 'callHandlers'] |
| 152 | + |
| 153 | + let abi |
| 154 | + try { |
| 155 | + // Resolve the source ABI name into a real ABI object |
| 156 | + let abiName = dataSource.getIn(['source', 'abi']) |
| 157 | + let abiEntry = dataSource |
| 158 | + .getIn(['mapping', 'abis']) |
| 159 | + .find(abi => abi.get('name') === abiName) |
| 160 | + abi = ABI.load(abiEntry.get('name'), this.resolveFile(abiEntry.get('file'))) |
| 161 | + } catch (e) { |
| 162 | + // Ignore errors silently; we can't really say anything about |
| 163 | + // the call functions if the ABI can't even be loaded |
| 164 | + return errors |
| 165 | + } |
| 166 | + |
| 167 | + // Obtain event signatures from the mapping |
| 168 | + let manifestFunctions = dataSource |
| 169 | + .getIn(['mapping', 'callHandlers'], immutable.List()) |
| 170 | + .map(handler => handler.get('function')) |
| 171 | + |
| 172 | + // Obtain event signatures from the ABI |
| 173 | + let abiFunctions = abi.callFunctionSignatures() |
| 174 | + |
| 175 | + // Add errors for every manifest event signature that is not |
| 176 | + // present in the ABI |
| 177 | + return manifestFunctions.reduce( |
| 178 | + (errors, manifestFunction, index) => |
| 179 | + abiFunctions.includes(manifestFunction) |
| 180 | + ? errors |
| 181 | + : errors.push( |
| 182 | + immutable.fromJS({ |
| 183 | + path: [...path, index], |
| 184 | + message: `\ |
| 185 | +Call function with signature '${manifestFunction}' not present in ABI '${abi.name}'. |
| 186 | +Available call functions: |
| 187 | +${abiFunctions |
| 188 | + .sort() |
| 189 | + .map(tx => `- ${tx}`) |
| 190 | + .join('\n')}`, |
| 191 | + }), |
| 192 | + ), |
| 193 | + errors, |
| 194 | + ) |
| 195 | + }, immutable.List()) |
| 196 | + } |
| 197 | + |
| 198 | + handlerTypes() { |
| 199 | + return immutable.List([ |
| 200 | + 'blockHandlers', |
| 201 | + 'callHandlers', |
| 202 | + 'eventHandlers', |
| 203 | + ]) |
| 204 | + } |
| 205 | +} |
0 commit comments