|
| 1 | +import { |
| 2 | + BigInt, |
| 3 | + Bytes, |
| 4 | + dataSource, |
| 5 | + DataSourceContext, |
| 6 | + DataSourceTemplate, |
| 7 | + json, |
| 8 | + JSONValueKind, |
| 9 | +} from '@graphprotocol/graph-ts'; |
| 10 | +import { Bulk, BulkSlice } from '../../generated/schema'; |
| 11 | +import { CONTEXT_BULK, CONTEXT_INDEX, CONTEXT_REQUESTHASH, createBulkSliceID } from '../utils'; |
| 12 | + |
| 13 | +function isIntegerString(str: string): boolean { |
| 14 | + // empty string is not valid |
| 15 | + if (str.length == 0) { |
| 16 | + return false; |
| 17 | + } |
| 18 | + // 0 prefixed string is not valid |
| 19 | + if (str[0] === '0' && str.length > 1) { |
| 20 | + return false; |
| 21 | + } |
| 22 | + // non numeric character is not valid |
| 23 | + for (let i = 0; i < str.length; i++) { |
| 24 | + if (!['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(str[i])) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + } |
| 28 | + return true; |
| 29 | +} |
| 30 | + |
| 31 | +export function handleBulk(content: Bytes): void { |
| 32 | + const hash = dataSource.stringParam(); |
| 33 | + const context = dataSource.context(); |
| 34 | + const requestorder = context.getString(CONTEXT_REQUESTHASH); |
| 35 | + |
| 36 | + // multiple deals using same requestorder use the same bulk, we use requestorderHash as bulk ID |
| 37 | + const bulkid = requestorder; |
| 38 | + |
| 39 | + let bulk = new Bulk(bulkid); |
| 40 | + bulk.hash = hash; |
| 41 | + bulk.content = content.toString(); |
| 42 | + |
| 43 | + const jsonContent = json.try_fromBytes(content); |
| 44 | + if (jsonContent.isOk) { |
| 45 | + const contentObject = jsonContent.value.toObject(); |
| 46 | + |
| 47 | + for (let i = 0; i < contentObject.entries.length; i++) { |
| 48 | + const entry = contentObject.entries[i]; |
| 49 | + |
| 50 | + if (isIntegerString(entry.key)) { |
| 51 | + const index = BigInt.fromString(entry.key); |
| 52 | + if (entry.value.kind == JSONValueKind.OBJECT) { |
| 53 | + let sliceCid = entry.value.toObject().getEntry('datasets'); |
| 54 | + if (sliceCid != null && sliceCid.value.kind == JSONValueKind.STRING) { |
| 55 | + let sliceContext = new DataSourceContext(); |
| 56 | + sliceContext.setString(CONTEXT_BULK, bulkid); |
| 57 | + sliceContext.setBigInt(CONTEXT_INDEX, index); |
| 58 | + DataSourceTemplate.createWithContext( |
| 59 | + 'BulkSlice', |
| 60 | + [sliceCid.value.toString()], |
| 61 | + sliceContext, |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + bulk.save(); |
| 70 | +} |
| 71 | + |
| 72 | +export function handleBulkSlice(content: Bytes): void { |
| 73 | + const hash = dataSource.stringParam(); |
| 74 | + const context = dataSource.context(); |
| 75 | + const bulk = context.getString(CONTEXT_BULK); |
| 76 | + const index = context.getBigInt(CONTEXT_INDEX); |
| 77 | + |
| 78 | + let bulkSlice = new BulkSlice(createBulkSliceID(bulk, index)); |
| 79 | + bulkSlice.hash = hash; |
| 80 | + bulkSlice.bulk = bulk; |
| 81 | + bulkSlice.index = index; |
| 82 | + bulkSlice.content = content.toString(); |
| 83 | + |
| 84 | + const jsonContent = json.try_fromBytes(content); |
| 85 | + if (jsonContent.isOk && jsonContent.value.kind == JSONValueKind.ARRAY) { |
| 86 | + const datasetArray = jsonContent.value.toArray(); |
| 87 | + for (let i = 0; i < datasetArray.length; i++) { |
| 88 | + const dataset = datasetArray[i]; |
| 89 | + if (dataset.kind == JSONValueKind.STRING) { |
| 90 | + let datasetAddress = dataset.toString().toLowerCase(); |
| 91 | + let datasets = bulkSlice.datasets; |
| 92 | + if (datasets == null) { |
| 93 | + datasets = new Array<string>(); |
| 94 | + } |
| 95 | + // dataset address may be invalid, this is not an issue, the model will prune it |
| 96 | + datasets.push(datasetAddress); |
| 97 | + bulkSlice.datasets = datasets; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + bulkSlice.save(); |
| 103 | +} |
0 commit comments