Skip to content

Commit 0d6bd40

Browse files
refactor(build): Adapt ckERC20 build script to more curated data
1 parent af017a8 commit 0d6bd40

File tree

1 file changed

+45
-31
lines changed

1 file changed

+45
-31
lines changed

scripts/build.tokens.ckerc20.ts

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,13 @@ const saveTokenLogo = ({ name, logoData }: { name: EnvTokenSymbol; logoData: str
167167
type EnvTokenTags = EnvCkErc20TokensWithMetadata[string]['tags'];
168168
type EnvTagsRecord = Record<string, Record<string, EnvTokenTags>>;
169169

170-
const readExistingCkErc20Tags = (): EnvTagsRecord => {
170+
interface EnvCuratedData {
171+
tags: EnvTagsRecord;
172+
}
173+
174+
const readExistingCkErc20CuratedData = (): EnvCuratedData => {
171175
if (!existsSync(CK_ERC20_JSON_FILE)) {
172-
return {};
176+
return { tags: {} };
173177
}
174178

175179
try {
@@ -178,50 +182,54 @@ const readExistingCkErc20Tags = (): EnvTagsRecord => {
178182
Record<string, { tags?: EnvTokenTags }>
179183
>;
180184

181-
return Object.entries(existing).reduce<EnvTagsRecord>((envAcc, [env, tokens]) => {
182-
const envTags = Object.entries(tokens).reduce<Record<string, EnvTokenTags>>(
183-
(acc, [symbol, data]) => {
184-
if (nonNullish(data?.tags)) {
185-
acc[symbol] = data.tags;
186-
}
187-
return acc;
188-
},
189-
{}
190-
);
191-
192-
if (Object.keys(envTags).length > 0) {
193-
envAcc[env] = envTags;
194-
}
185+
return Object.entries(existing).reduce<EnvCuratedData>(
186+
(envAcc, [env, tokens]) => {
187+
const envTags = Object.entries(tokens).reduce<Record<string, EnvTokenTags>>(
188+
(acc, [symbol, data]) => {
189+
if (nonNullish(data?.tags)) {
190+
acc[symbol] = data.tags;
191+
}
192+
return acc;
193+
},
194+
{}
195+
);
196+
197+
if (Object.keys(envTags).length > 0) {
198+
envAcc.tags[env] = envTags;
199+
}
195200

196-
return envAcc;
197-
}, {});
201+
return envAcc;
202+
},
203+
{ tags: {} }
204+
);
198205
} catch (err: unknown) {
199206
console.error(
200-
`Failed to parse existing CK ERC20 tags from ${CK_ERC20_JSON_FILE}. Aborting to avoid losing curated tags.`,
207+
`Failed to parse existing CK ERC20 curated data from ${CK_ERC20_JSON_FILE}. Aborting to avoid losing curated data.`,
201208
err
202209
);
203210
throw err;
204211
}
205212
};
206213

207-
const mergeTags = ({
214+
const mergeCuratedData = ({
208215
tokens,
209216
envTags
210217
}: {
211218
tokens: EnvCkErc20TokensWithMetadata;
212219
envTags: Record<string, EnvTokenTags> | undefined;
213220
}): EnvCkErc20TokensWithMetadata =>
214-
isNullish(envTags)
215-
? tokens
216-
: Object.fromEntries(
217-
Object.entries(tokens).map(([symbol, data]) => [
218-
symbol,
219-
nonNullish(envTags[symbol]) ? { ...data, tags: envTags[symbol] } : data
220-
])
221-
);
221+
Object.fromEntries(
222+
Object.entries(tokens).map(([symbol, data]) => [
223+
symbol,
224+
{
225+
...data,
226+
...(nonNullish(envTags?.[symbol]) && { tags: envTags[symbol] })
227+
}
228+
])
229+
);
222230

223231
const findCkErc20 = async () => {
224-
const existingTags = readExistingCkErc20Tags();
232+
const { tags: existingTags } = readExistingCkErc20CuratedData();
225233

226234
const [
227235
{ tokens: staging, icons: stagingIcons },
@@ -231,8 +239,14 @@ const findCkErc20 = async () => {
231239
);
232240

233241
const tokens: EnvTokensCkErc20 = {
234-
production: mergeTags({ tokens: production, envTags: existingTags['production'] }),
235-
staging: mergeTags({ tokens: staging, envTags: existingTags['staging'] })
242+
production: mergeCuratedData({
243+
tokens: production,
244+
envTags: existingTags['production']
245+
}),
246+
staging: mergeCuratedData({
247+
tokens: staging,
248+
envTags: existingTags['staging']
249+
})
236250
};
237251

238252
writeFileSync(CK_ERC20_JSON_FILE, JSON.stringify(tokens, jsonReplacer, 8));

0 commit comments

Comments
 (0)