forked from smartcontractkit/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect-data.sh
More file actions
executable file
·290 lines (247 loc) · 8.32 KB
/
detect-data.sh
File metadata and controls
executable file
·290 lines (247 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env bash
set -e # Exit immediately on error
# This script orchestrates the detection of new data using the TS script src/scripts/data/detect-new-data.ts
# 1) "init-baseline": creates a baseline with all currently visible feedIDs (no changelog updates)
# 2) "check-data": calls the TS script, checks for new items, updates baseline/changelog if found
BASELINE_FILE=".github/scripts/data/baseline.json"
CHANGELOG_FILE="public/changelog.json"
TEMP_DIR="temp"
NEW_DATA_FILE="${TEMP_DIR}/NEW_DATA_FOUND.json"
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $*"
}
init_baseline() {
log "Initializing baseline..."
# If baseline exists, back it up or remove it
if [ -f "$BASELINE_FILE" ]; then
mv "$BASELINE_FILE" "${BASELINE_FILE}.bak"
fi
# Run TS script once, ignoring any exit code
npx tsx src/scripts/data/detect-new-data.ts || true
# If NEW_DATA_FOUND.json doesn't exist => no new data
if [ ! -f "$NEW_DATA_FILE" ]; then
cat <<EOF > "$BASELINE_FILE"
{
"timestamp": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
"knownIds": []
}
EOF
log "No items found, baseline created as empty."
return
fi
# read newly found IDs as a JSON array
# e.g. [ "arbitrum-1inch-usd", "arbitrum-aave-usd", ... ]
ids=$(jq '[.newlyFoundItems[].feedID] | unique' "$NEW_DATA_FILE")
# Write baseline as a single array
cat <<EOF > "$BASELINE_FILE"
{
"timestamp": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
"knownIds": $ids
}
EOF
log "Baseline file created with current known IDs."
rm -f "$NEW_DATA_FILE"
}
check_data() {
log "Checking for new data..."
if [ ! -f "$BASELINE_FILE" ]; then
log "Baseline file not found. Please run: $0 init-baseline"
exit 1
fi
# 1) Run the TS script
set +e
npx tsx src/scripts/data/detect-new-data.ts
exit_code=$?
set -e
if [ "$exit_code" -ne 0 ]; then
log "TypeScript script encountered an error (exit=$exit_code)."
exit 1
fi
# 2) If NEW_DATA_FOUND.json doesn't exist => no new items
if [ ! -f "$NEW_DATA_FILE" ]; then
log "No new data found. Exiting."
exit 0
fi
# 3) We do have new items, so read them
count=$(jq '.newlyFoundItems | length' "$NEW_DATA_FILE")
log "Found $count new items."
# 4) Merge new IDs into baseline
# Step A: read existing knownIds (as JSON array) from the baseline
existingArray=$(jq '.knownIds' "$BASELINE_FILE")
# Step B: read newly found feedIDs (as JSON array)
newArray=$(jq '[.newlyFoundItems[].feedID] | unique' "$NEW_DATA_FILE")
# Step C: combine them in pure JSON
combinedArray=$(jq -n --argjson old "$existingArray" --argjson new "$newArray" '
($old + $new) | unique
')
# Step D: write updated baseline
cat <<EOF > "$BASELINE_FILE"
{
"timestamp": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
"knownIds": $combinedArray
}
EOF
log "Baseline updated with new IDs."
# 5) Now update the changelog
node <<EOF
const fs = require('fs');
const path = require('path');
const newlyFound = JSON.parse(fs.readFileSync('${NEW_DATA_FILE}', 'utf8'));
const items = newlyFound.newlyFoundItems || [];
const CHANGELOG_PATH = path.resolve('${CHANGELOG_FILE}');
let changelog;
if (fs.existsSync(CHANGELOG_PATH)) {
changelog = JSON.parse(fs.readFileSync(CHANGELOG_PATH, 'utf8'));
} else {
changelog = { networks: {}, data: [] };
}
if (!changelog.data) {
changelog.data = [];
}
// === GROUPING: data-streams, smartData, data-feeds ===
const dataStreams = [];
const smartData = [];
const dataFeeds = [];
for (const item of items) {
const code = (item.productTypeCode || '').toUpperCase().trim();
if (item.deliveryChannelCode === 'DS') {
dataStreams.push(item);
} else if (['POR','NAV','AUM'].includes(code)) {
smartData.push(item);
} else {
dataFeeds.push(item);
}
}
// === HELPER to build a single Changelog Entry
function createChangelogEntry(topic, title, description, relatedNetworks, tokens) {
return {
category: "integration",
date: new Date().toISOString().split('T')[0],
description,
...(relatedNetworks ? { relatedNetworks } : {}),
relatedTokens: tokens,
title,
topic,
};
}
// === data-streams networks
const STREAMS_NETWORKS = [
"0g", "apechain", "aptos", "arbitrum", "avalanche", "base", "berachain", "bitlayer", "blast",
"bnb-chain", "bob", "botanix", "celo", "ethereum", "gnosis-chain", "gravity", "hashkey", "hedera", "hyperliquid", "injective",
"ink", "jovay", "katana", "lens", "linea", "mantle", "metis", "monad", "opbnb", "optimism", "polygon", "pharos", "plasma", "ronin",
"scroll", "shibarium", "sei", "soneium", "sonic",
"solana", "taiko", "unichain", "worldchain", "zksync"
];
// === Build relatedTokens for FEEDS
function buildDataFeedTokens(feedItems) {
return feedItems.map(i => {
const baseLower = i.baseAsset.toLowerCase();
return {
assetName: i.assetName,
baseAsset: i.baseAsset,
quoteAsset: i.quoteAsset || "",
network: i.network,
url: i.url,
iconUrl: \`https://d2f70xi62kby8n.cloudfront.net/tokens/\${baseLower}.webp\`
};
}).sort((a, b) => a.assetName.localeCompare(b.assetName));
}
// === Build relatedTokens for STREAMS
function buildDataStreamTokens(streamItems) {
return streamItems.map(i => {
const baseLower = i.baseAsset.toLowerCase();
return {
assetName: i.assetName,
baseAsset: i.baseAsset,
quoteAsset: i.quoteAsset || "",
url: i.url,
iconUrl: \`https://d2f70xi62kby8n.cloudfront.net/tokens/\${baseLower}.webp\`
};
}).sort((a, b) => a.assetName.localeCompare(b.assetName));
}
// === Build relatedTokens for SMARTDATA
function buildSmartDataTokens(smartItems) {
return smartItems.map(i => {
const baseLower = i.baseAsset.toLowerCase();
return {
assetName: i.assetName,
baseAsset: i.baseAsset,
network: i.network,
productTypeCode: i.productTypeCode,
url: i.url,
iconUrl: \`https://d2f70xi62kby8n.cloudfront.net/tokens/\${baseLower}.webp\`
};
}).sort((a, b) => a.assetName.localeCompare(b.assetName));
}
// === Now build each group
const dataFeedsTokens = buildDataFeedTokens(dataFeeds);
const dataStreamsTokens = buildDataStreamTokens(dataStreams);
const smartDataTokens = buildSmartDataTokens(smartData);
// === Create new changelog entries
const newEntries = [];
// If we have streams
if (dataStreamsTokens.length > 0) {
newEntries.push(
createChangelogEntry(
"Data Streams",
"Added support to Data Streams",
"New Data Streams available on all [supported networks](https://docs.chain.link/data-streams/crypto-streams):",
STREAMS_NETWORKS,
dataStreamsTokens
)
);
}
// If we have smartData
if (smartDataTokens.length > 0) {
const networksSet = new Set(smartDataTokens.map(t => t.network));
const networksList = [...networksSet];
newEntries.push(
createChangelogEntry(
"SmartData",
"Added support to SmartData",
"New SmartData Feeds available:",
networksList,
smartDataTokens
)
);
}
// If we have normal data feeds
if (dataFeedsTokens.length > 0) {
const networksSet = new Set(dataFeedsTokens.map(t => t.network));
const networksList = [...networksSet];
newEntries.push(
createChangelogEntry(
"Data Feeds",
"Added support to Data Feeds",
"New Data Feeds available:",
networksList,
dataFeedsTokens
)
);
}
// Insert them at the start of `changelog.data`
for (const entry of newEntries.reverse()) {
changelog.data.unshift(entry);
}
fs.writeFileSync(CHANGELOG_PATH, JSON.stringify(changelog, null, 2), 'utf8');
console.log(\`changelog.json updated with \${newEntries.length} new entry(ies).\`);
EOF
log "changelog.json updated."
log "Done."
}
main() {
local cmd="${1:-help}"
case "$cmd" in
init-baseline)
init_baseline
;;
check-data)
check_data
;;
*)
echo "Usage: $0 {init-baseline|check-data}"
exit 1
;;
esac
}
main "$@"