@@ -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 {
0 commit comments