-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use deep copy instead of shallow copy for the undercut strategy
- Loading branch information
1 parent
9a3e410
commit cbeab79
Showing
1 changed file
with
42 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,54 @@ | ||
import type { Strategy } from 'libs/queries'; | ||
import { SafeDecimal } from 'libs/safedecimal'; | ||
|
||
const replaceDecimal = (key: string, value: any) => { | ||
if (value instanceof SafeDecimal) | ||
return { __type__: 'SafeDecimal', value: value.toString() }; | ||
return value; | ||
}; | ||
const reviveDecimal = (key: string, value: any) => { | ||
if ( | ||
typeof value === 'object' && | ||
value !== null && | ||
value['__type__'] === 'SafeDecimal' | ||
) | ||
return new SafeDecimal(value.value); | ||
return value; | ||
}; | ||
|
||
export const deepCopy = (obj: any): any => | ||
JSON.parse(JSON.stringify(obj, replaceDecimal), reviveDecimal); | ||
|
||
export const getUndercutStrategy = ( | ||
strategy: Strategy, | ||
undercutDifference: number | ||
): Strategy => { | ||
const multiplyRate = (rate: string, factor: number) => | ||
const multiplyByRate = (rate: string, factor: number) => | ||
new SafeDecimal(rate).times(factor).toString(); | ||
|
||
const undercutStrategy = { | ||
...strategy, | ||
order0: { | ||
...strategy.order0, | ||
startRate: multiplyRate( | ||
strategy.order0.startRate, | ||
1 + undercutDifference | ||
), | ||
endRate: multiplyRate(strategy.order0.endRate, 1 + undercutDifference), | ||
}, | ||
order1: { | ||
...strategy.order1, | ||
startRate: multiplyRate( | ||
strategy.order1.startRate, | ||
1 - undercutDifference | ||
), | ||
endRate: multiplyRate(strategy.order1.endRate, 1 - undercutDifference), | ||
}, | ||
}; | ||
if (undercutDifference < 0 || undercutDifference > 1) | ||
throw new Error( | ||
'undercutDifference must be less than or equal to 1, and higher than or equal to 0' | ||
); | ||
|
||
const undercutStrategy = deepCopy(strategy); | ||
|
||
undercutStrategy.order0.startRate = multiplyByRate( | ||
strategy.order0.startRate, | ||
1 + undercutDifference | ||
); | ||
undercutStrategy.order0.endRate = multiplyByRate( | ||
strategy.order0.endRate, | ||
1 + undercutDifference | ||
); | ||
undercutStrategy.order1.startRate = multiplyByRate( | ||
strategy.order1.startRate, | ||
1 - undercutDifference | ||
); | ||
undercutStrategy.order1.endRate = multiplyByRate( | ||
strategy.order1.endRate, | ||
1 - undercutDifference | ||
); | ||
|
||
return undercutStrategy; | ||
}; |