Skip to content

Commit eac6715

Browse files
committed
Merge branch 'develop'
2 parents c0a55e0 + 6ec76eb commit eac6715

File tree

6 files changed

+373
-61
lines changed

6 files changed

+373
-61
lines changed

src/helpers/index.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -295,24 +295,37 @@ export function loadPolicy(policyPath: string): PolicyConfig {
295295
);
296296
}
297297

298-
const normalizedPolicy = value as PolicyConfig;
299-
normalizedPolicy.withdraw.rule = normalizedPolicy.withdraw.rule.map(
300-
(rule) => ({
301-
...rule,
302-
exchange: rule.exchange.trim().toUpperCase(),
303-
network: rule.network.trim().toUpperCase(),
304-
whitelist: rule.whitelist.map((a) => a.trim().toLowerCase()),
305-
}),
306-
);
307-
normalizedPolicy.order.rule.limits =
308-
normalizedPolicy.order.rule.limits ?? [];
309-
return normalizedPolicy;
298+
return normalizePolicyConfig(value as PolicyConfig);
310299
} catch (error) {
311300
console.error("Failed to load policy:", error);
312301
throw new Error("Policy configuration could not be loaded");
313302
}
314303
}
315304

305+
export function normalizePolicyConfig(policy: PolicyConfig): PolicyConfig {
306+
return {
307+
...policy,
308+
withdraw: {
309+
...policy.withdraw,
310+
rule: policy.withdraw.rule.map((rule) => ({
311+
...rule,
312+
exchange: rule.exchange.trim().toUpperCase(),
313+
network: rule.network.trim().toUpperCase(),
314+
whitelist: rule.whitelist.map((address) =>
315+
address.trim().toLowerCase(),
316+
),
317+
})),
318+
},
319+
order: {
320+
...policy.order,
321+
rule: {
322+
...policy.order.rule,
323+
limits: policy.order.rule.limits ?? [],
324+
},
325+
},
326+
};
327+
}
328+
316329
/**
317330
* Validates withdraw request against policy rules
318331
*/
@@ -346,9 +359,10 @@ export function validateWithdraw(
346359
_amount: number,
347360
_ticker: string,
348361
): { valid: boolean; error?: string } {
362+
const normalizedPolicy = normalizePolicyConfig(policy);
349363
const exchangeNorm = exchange.trim().toUpperCase();
350364
const networkNorm = network.trim().toUpperCase();
351-
const matchingRules = policy.withdraw.rule
365+
const matchingRules = normalizedPolicy.withdraw.rule
352366
.map((rule) => ({
353367
rule,
354368
priority: getWithdrawRulePriority(rule, exchangeNorm, networkNorm),
@@ -358,7 +372,7 @@ export function validateWithdraw(
358372
const withdrawRule = matchingRules[0]?.rule;
359373

360374
if (!withdrawRule) {
361-
const allowedPairs = policy.withdraw.rule.map(
375+
const allowedPairs = normalizedPolicy.withdraw.rule.map(
362376
(r) => `${r.exchange}:${r.network}`,
363377
);
364378
return {

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as grpc from "@grpc/grpc-js";
22
import ccxt, { type Exchange } from "@usherlabs/ccxt";
33
import { unwatchFile, watchFile } from "fs";
44
import Joi from "joi";
5-
import { createBrokerPool, loadPolicy } from "./helpers";
5+
import { createBrokerPool, loadPolicy, normalizePolicyConfig } from "./helpers";
66
import { log } from "./helpers/logger";
77
import {
88
createOtelLogsFromEnv,
@@ -164,7 +164,7 @@ export default class CEXBroker {
164164
this.policy = loadPolicy(policies);
165165
this.port = config?.port ?? 8086;
166166
} else {
167-
this.policy = policies;
167+
this.policy = normalizePolicyConfig(policies);
168168
}
169169

170170
// If monitoring a file, start watcher

0 commit comments

Comments
 (0)