From ec101a41e53addbe66be01df5143c49ac69f72d6 Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Thu, 4 May 2023 14:18:57 +0200 Subject: [PATCH 1/8] add oracle math via local dep and add test --- package.json | 1 + src/oracle/OracleMath.ts | 14 ++++++++++++++ src/oracle/index.ts | 1 + test/ema/emaMath.spec.ts | 21 +++++++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 src/oracle/OracleMath.ts create mode 100644 src/oracle/index.ts create mode 100644 test/ema/emaMath.spec.ts diff --git a/package.json b/package.json index 3d44f3ee..d7e804f1 100755 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "@galacticcouncil/math-lbp": "^0.0.7", "@galacticcouncil/math-omnipool": "^0.0.7", "@galacticcouncil/math-xyk": "^0.0.7", + "@galacticcouncil/math-ema": "file:../hydra-wasm/packages/ema", "bignumber.js": "^9.1.0", "lodash.clonedeep": "^4.5.0" }, diff --git a/src/oracle/OracleMath.ts b/src/oracle/OracleMath.ts new file mode 100644 index 00000000..23bf0aa4 --- /dev/null +++ b/src/oracle/OracleMath.ts @@ -0,0 +1,14 @@ +import { + iterated_price_ema, + iterated_balance_ema, +} from '@galacticcouncil/math-ema'; + +export class EmaLowPrecisionMath { + static iteratedPriceEma(iterations: string, prev_n: string, prev_d: string, incoming_n: string, incoming_d: string, smoothing: string): string { + return iterated_price_ema(iterations, prev_n, prev_d, incoming_n, incoming_d, smoothing); + } + + static iteratedBalanceEma(iterations: string, prev: string, incoming: string, smoothing: string): string { + return iterated_balance_ema(iterations, prev, incoming, smoothing); + } +} \ No newline at end of file diff --git a/src/oracle/index.ts b/src/oracle/index.ts new file mode 100644 index 00000000..c576952f --- /dev/null +++ b/src/oracle/index.ts @@ -0,0 +1 @@ +export { EmaLowPrecisionMath } from './OracleMath'; \ No newline at end of file diff --git a/test/ema/emaMath.spec.ts b/test/ema/emaMath.spec.ts new file mode 100644 index 00000000..ae2c9b44 --- /dev/null +++ b/test/ema/emaMath.spec.ts @@ -0,0 +1,21 @@ +import { EmaLowPrecisionMath } from '../../src/oracle'; +import { BigNumber, bnum } from '../../src/utils/bignumber'; + +function smoothingFromPeriod(period: number): BigNumber { + return bnum(2).pow(127).multipliedBy(2).dividedBy(period + 1) +} + +describe('Ema Math', () => { + + it('Should return correct EMA for given params', async () => { + let smoothing = smoothingFromPeriod(7); + let startPriceN = bnum(4); + let startPriceD = bnum(1); + let incomingPriceN = bnum(8); + let incomingPriceD = bnum(1); + let iterations = bnum(1); + let nextPrice = EmaLowPrecisionMath.iteratedPriceEma(iterations.toString(), startPriceN.toString(), startPriceD.toString(), incomingPriceN.toString(), incomingPriceD.toString(), smoothing.toString()); + let expected = bnum(5).multipliedBy(bnum("1000000000000000000")); + expect(bnum(nextPrice)).toStrictEqual(expected); + }); +}); From 108e24e937136c911030b64e04f34b05f8b5eade Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Thu, 4 May 2023 16:58:14 +0200 Subject: [PATCH 2/8] add period constants and more tests --- src/oracle/OracleMath.ts | 12 ++++++++++-- test/ema/emaMath.spec.ts | 26 ++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/oracle/OracleMath.ts b/src/oracle/OracleMath.ts index 23bf0aa4..6d517057 100644 --- a/src/oracle/OracleMath.ts +++ b/src/oracle/OracleMath.ts @@ -1,11 +1,19 @@ import { - iterated_price_ema, + low_precision_iterated_price_ema, iterated_balance_ema, } from '@galacticcouncil/math-ema'; export class EmaLowPrecisionMath { + // Smoothing factors for the currently supported oracle periods. + static readonly LastBlock: string = "170141183460469231731687303715884105728"; + static readonly Short: string = "34028236692093846346337460743176821146"; + static readonly TenMinutes: string = "3369132345751865974884897103284833777"; + static readonly Hour: string = "566193622164623067326746434994622648"; + static readonly Day: string = "23629079016800115510268356880200556"; + static readonly Week: string = "3375783642235081630771268215908257"; + static iteratedPriceEma(iterations: string, prev_n: string, prev_d: string, incoming_n: string, incoming_d: string, smoothing: string): string { - return iterated_price_ema(iterations, prev_n, prev_d, incoming_n, incoming_d, smoothing); + return low_precision_iterated_price_ema(iterations, prev_n, prev_d, incoming_n, incoming_d, smoothing); } static iteratedBalanceEma(iterations: string, prev: string, incoming: string, smoothing: string): string { diff --git a/test/ema/emaMath.spec.ts b/test/ema/emaMath.spec.ts index ae2c9b44..db08dff4 100644 --- a/test/ema/emaMath.spec.ts +++ b/test/ema/emaMath.spec.ts @@ -5,9 +5,9 @@ function smoothingFromPeriod(period: number): BigNumber { return bnum(2).pow(127).multipliedBy(2).dividedBy(period + 1) } -describe('Ema Math', () => { +describe('EMA Math', () => { - it('Should return correct EMA for given params', async () => { + it('Should return correct price EMA for simple params', async () => { let smoothing = smoothingFromPeriod(7); let startPriceN = bnum(4); let startPriceD = bnum(1); @@ -18,4 +18,26 @@ describe('Ema Math', () => { let expected = bnum(5).multipliedBy(bnum("1000000000000000000")); expect(bnum(nextPrice)).toStrictEqual(expected); }); + + it('Should return correct price EMA for hard-coded period', async () => { + let smoothing = EmaLowPrecisionMath.Short; + let startPriceN = bnum("100"); + let startPriceD = bnum(1); + let incomingPriceN = bnum("1100"); + let incomingPriceD = bnum(1); + let iterations = bnum(1); + let nextPrice = EmaLowPrecisionMath.iteratedPriceEma(iterations.toString(), startPriceN.toString(), startPriceD.toString(), incomingPriceN.toString(), incomingPriceD.toString(), smoothing.toString()); + let expected = bnum("300").multipliedBy(bnum("1000000000000000000")); + expect(bnum(nextPrice)).toStrictEqual(expected); + }); + + it('Should return correct balance EMA for hard-coded period', async () => { + let smoothing = EmaLowPrecisionMath.Short; + let startBalance = bnum("100000000"); + let incomingBalance = bnum("1100000000"); + let iterations = bnum(1); + let nextPrice = EmaLowPrecisionMath.iteratedBalanceEma(iterations.toString(), startBalance.toString(), incomingBalance.toString(), smoothing.toString()); + let expected = bnum("300000000"); + expect(bnum(nextPrice)).toStrictEqual(expected); + }); }); From 228eaafef770979348efc721a44d72ec81f9aede Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Thu, 4 May 2023 17:00:54 +0200 Subject: [PATCH 3/8] formatting --- src/oracle/OracleMath.ts | 28 ++++++++++++++----------- test/ema/emaMath.spec.ts | 45 +++++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/oracle/OracleMath.ts b/src/oracle/OracleMath.ts index 6d517057..e23d626d 100644 --- a/src/oracle/OracleMath.ts +++ b/src/oracle/OracleMath.ts @@ -1,22 +1,26 @@ -import { - low_precision_iterated_price_ema, - iterated_balance_ema, -} from '@galacticcouncil/math-ema'; +import { low_precision_iterated_price_ema, iterated_balance_ema } from '@galacticcouncil/math-ema'; export class EmaLowPrecisionMath { // Smoothing factors for the currently supported oracle periods. - static readonly LastBlock: string = "170141183460469231731687303715884105728"; - static readonly Short: string = "34028236692093846346337460743176821146"; - static readonly TenMinutes: string = "3369132345751865974884897103284833777"; - static readonly Hour: string = "566193622164623067326746434994622648"; - static readonly Day: string = "23629079016800115510268356880200556"; - static readonly Week: string = "3375783642235081630771268215908257"; + static readonly LastBlock: string = '170141183460469231731687303715884105728'; + static readonly Short: string = '34028236692093846346337460743176821146'; + static readonly TenMinutes: string = '3369132345751865974884897103284833777'; + static readonly Hour: string = '566193622164623067326746434994622648'; + static readonly Day: string = '23629079016800115510268356880200556'; + static readonly Week: string = '3375783642235081630771268215908257'; - static iteratedPriceEma(iterations: string, prev_n: string, prev_d: string, incoming_n: string, incoming_d: string, smoothing: string): string { + static iteratedPriceEma( + iterations: string, + prev_n: string, + prev_d: string, + incoming_n: string, + incoming_d: string, + smoothing: string + ): string { return low_precision_iterated_price_ema(iterations, prev_n, prev_d, incoming_n, incoming_d, smoothing); } static iteratedBalanceEma(iterations: string, prev: string, incoming: string, smoothing: string): string { return iterated_balance_ema(iterations, prev, incoming, smoothing); } -} \ No newline at end of file +} diff --git a/test/ema/emaMath.spec.ts b/test/ema/emaMath.spec.ts index db08dff4..43c13814 100644 --- a/test/ema/emaMath.spec.ts +++ b/test/ema/emaMath.spec.ts @@ -2,11 +2,13 @@ import { EmaLowPrecisionMath } from '../../src/oracle'; import { BigNumber, bnum } from '../../src/utils/bignumber'; function smoothingFromPeriod(period: number): BigNumber { - return bnum(2).pow(127).multipliedBy(2).dividedBy(period + 1) + return bnum(2) + .pow(127) + .multipliedBy(2) + .dividedBy(period + 1); } describe('EMA Math', () => { - it('Should return correct price EMA for simple params', async () => { let smoothing = smoothingFromPeriod(7); let startPriceN = bnum(4); @@ -14,30 +16,49 @@ describe('EMA Math', () => { let incomingPriceN = bnum(8); let incomingPriceD = bnum(1); let iterations = bnum(1); - let nextPrice = EmaLowPrecisionMath.iteratedPriceEma(iterations.toString(), startPriceN.toString(), startPriceD.toString(), incomingPriceN.toString(), incomingPriceD.toString(), smoothing.toString()); - let expected = bnum(5).multipliedBy(bnum("1000000000000000000")); + let nextPrice = EmaLowPrecisionMath.iteratedPriceEma( + iterations.toString(), + startPriceN.toString(), + startPriceD.toString(), + incomingPriceN.toString(), + incomingPriceD.toString(), + smoothing.toString() + ); + let expected = bnum(5).multipliedBy(bnum('1000000000000000000')); expect(bnum(nextPrice)).toStrictEqual(expected); }); it('Should return correct price EMA for hard-coded period', async () => { let smoothing = EmaLowPrecisionMath.Short; - let startPriceN = bnum("100"); + let startPriceN = bnum('100'); let startPriceD = bnum(1); - let incomingPriceN = bnum("1100"); + let incomingPriceN = bnum('1100'); let incomingPriceD = bnum(1); let iterations = bnum(1); - let nextPrice = EmaLowPrecisionMath.iteratedPriceEma(iterations.toString(), startPriceN.toString(), startPriceD.toString(), incomingPriceN.toString(), incomingPriceD.toString(), smoothing.toString()); - let expected = bnum("300").multipliedBy(bnum("1000000000000000000")); + let nextPrice = EmaLowPrecisionMath.iteratedPriceEma( + iterations.toString(), + startPriceN.toString(), + startPriceD.toString(), + incomingPriceN.toString(), + incomingPriceD.toString(), + smoothing.toString() + ); + let expected = bnum('300').multipliedBy(bnum('1000000000000000000')); expect(bnum(nextPrice)).toStrictEqual(expected); }); it('Should return correct balance EMA for hard-coded period', async () => { let smoothing = EmaLowPrecisionMath.Short; - let startBalance = bnum("100000000"); - let incomingBalance = bnum("1100000000"); + let startBalance = bnum('100000000'); + let incomingBalance = bnum('1100000000'); let iterations = bnum(1); - let nextPrice = EmaLowPrecisionMath.iteratedBalanceEma(iterations.toString(), startBalance.toString(), incomingBalance.toString(), smoothing.toString()); - let expected = bnum("300000000"); + let nextPrice = EmaLowPrecisionMath.iteratedBalanceEma( + iterations.toString(), + startBalance.toString(), + incomingBalance.toString(), + smoothing.toString() + ); + let expected = bnum('300000000'); expect(bnum(nextPrice)).toStrictEqual(expected); }); }); From 24bca1ab5c8622bbbef8507b24de5efefa938b08 Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Thu, 4 May 2023 17:47:51 +0200 Subject: [PATCH 4/8] add convenience abstraction over low level oracle math --- src/oracle/OracleMath.ts | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/oracle/OracleMath.ts b/src/oracle/OracleMath.ts index e23d626d..99da181b 100644 --- a/src/oracle/OracleMath.ts +++ b/src/oracle/OracleMath.ts @@ -1,4 +1,22 @@ import { low_precision_iterated_price_ema, iterated_balance_ema } from '@galacticcouncil/math-ema'; +import BigNumber from 'bignumber.js'; +import { ZERO, bnum } from '../utils/bignumber'; + +interface OracleEntry { + price: [BigNumber, BigNumber]; + volume: BigNumber[]; + liquidity: [BigNumber, BigNumber]; + timestamp: BigNumber; +} + +interface LowPrecisionOracleEntry { + price: BigNumber; + volume: BigNumber[]; + liquidity: [BigNumber, BigNumber]; + timestamp: BigNumber; +} + +type OraclePeriod = 'LastBlock' | 'Short' | 'TenMinutes' | 'Hour' | 'Day' | 'Week'; export class EmaLowPrecisionMath { // Smoothing factors for the currently supported oracle periods. @@ -24,3 +42,51 @@ export class EmaLowPrecisionMath { return iterated_balance_ema(iterations, prev, incoming, smoothing); } } + +export class OracleMath { + static readonly SmoothingForPeriod: Map = new Map([ + ['LastBlock', EmaLowPrecisionMath.LastBlock], + ['Short', EmaLowPrecisionMath.Short], + ['TenMinutes', EmaLowPrecisionMath.TenMinutes], + ['Hour', EmaLowPrecisionMath.Hour], + ['Day', EmaLowPrecisionMath.Day], + ['Week', EmaLowPrecisionMath.Week], + ]); + + /// Calculate the current oracle values from the `outdated` and `update_with` values using the `smoothing` factor with the old values being `iterations` out of date. + /// + /// Note: The volume is always updated with zero values so it is not a parameter. + static updateOutdatedToCurrent( + outdated: OracleEntry, + updateWith: OracleEntry, + period: OraclePeriod + ): LowPrecisionOracleEntry { + if (outdated.timestamp > updateWith.timestamp) { + throw new Error('invalid timestamp'); + } + let iterations = BigNumber.max(updateWith.timestamp.minus(outdated.timestamp), 0).toString(); + let smoothing = OracleMath.SmoothingForPeriod[period]; + let [prevN, prevD] = outdated.price; + let [incomingN, incomingD] = updateWith.price; + let price = bnum( + EmaLowPrecisionMath.iteratedPriceEma( + iterations, + prevN.toString(), + prevD.toString(), + incomingN.toString(), + incomingD.toString(), + smoothing + ) + ); + let volume = outdated.volume.map((v) => + bnum(EmaLowPrecisionMath.iteratedBalanceEma(iterations, v.toString(), ZERO.toString(), smoothing)) + ); + let [prevLiq1, prevLiq2] = outdated.liquidity; + let [incomingLiq1, incomingLiq2] = updateWith.liquidity; + let liquidity: [BigNumber, BigNumber] = [ + bnum(EmaLowPrecisionMath.iteratedBalanceEma(iterations, prevLiq1.toString(), incomingLiq1.toString(), smoothing)), + bnum(EmaLowPrecisionMath.iteratedBalanceEma(iterations, prevLiq2.toString(), incomingLiq2.toString(), smoothing)), + ]; + return { price, volume, liquidity, timestamp: updateWith.timestamp }; + } +} From c25355d9bfa4735089e3ffb56ea883ff63fc92e3 Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Fri, 5 May 2023 15:32:35 +0200 Subject: [PATCH 5/8] update comments --- src/oracle/OracleMath.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/oracle/OracleMath.ts b/src/oracle/OracleMath.ts index 99da181b..4b04666c 100644 --- a/src/oracle/OracleMath.ts +++ b/src/oracle/OracleMath.ts @@ -20,6 +20,7 @@ type OraclePeriod = 'LastBlock' | 'Short' | 'TenMinutes' | 'Hour' | 'Day' | 'Wee export class EmaLowPrecisionMath { // Smoothing factors for the currently supported oracle periods. + // Taken from https://github.com/galacticcouncil/warehouse/blob/0047e9ceff47b2a058ae9ecc25da96d1e827a26a/ema-oracle/src/types.rs#L198-L207 static readonly LastBlock: string = '170141183460469231731687303715884105728'; static readonly Short: string = '34028236692093846346337460743176821146'; static readonly TenMinutes: string = '3369132345751865974884897103284833777'; @@ -61,8 +62,8 @@ export class OracleMath { updateWith: OracleEntry, period: OraclePeriod ): LowPrecisionOracleEntry { - if (outdated.timestamp > updateWith.timestamp) { - throw new Error('invalid timestamp'); + if (outdated.timestamp >= updateWith.timestamp) { + throw new Error('invalid timestamp (outdated should be older)'); } let iterations = BigNumber.max(updateWith.timestamp.minus(outdated.timestamp), 0).toString(); let smoothing = OracleMath.SmoothingForPeriod[period]; From 0e13bcaf411e2f80297c301fe56d4a6a9f68448f Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Fri, 5 May 2023 15:36:37 +0200 Subject: [PATCH 6/8] formatting --- src/oracle/OracleMath.ts | 10 +++++----- src/oracle/index.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/oracle/OracleMath.ts b/src/oracle/OracleMath.ts index 4b04666c..e83b8047 100644 --- a/src/oracle/OracleMath.ts +++ b/src/oracle/OracleMath.ts @@ -30,13 +30,13 @@ export class EmaLowPrecisionMath { static iteratedPriceEma( iterations: string, - prev_n: string, - prev_d: string, - incoming_n: string, - incoming_d: string, + prevN: string, + prevD: string, + incomingN: string, + incomingD: string, smoothing: string ): string { - return low_precision_iterated_price_ema(iterations, prev_n, prev_d, incoming_n, incoming_d, smoothing); + return low_precision_iterated_price_ema(iterations, prevN, prevD, incomingN, incomingD, smoothing); } static iteratedBalanceEma(iterations: string, prev: string, incoming: string, smoothing: string): string { diff --git a/src/oracle/index.ts b/src/oracle/index.ts index c576952f..27f368a4 100644 --- a/src/oracle/index.ts +++ b/src/oracle/index.ts @@ -1 +1 @@ -export { EmaLowPrecisionMath } from './OracleMath'; \ No newline at end of file +export { OracleMath, EmaLowPrecisionMath } from './OracleMath'; From fc11ca465673b4e357b815693aef1ccecaae2e7e Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Fri, 5 May 2023 17:38:28 +0200 Subject: [PATCH 7/8] define OraclePeriod enum and refactor --- src/oracle/OracleMath.ts | 42 ++++++++++++++++++++-------------------- src/oracle/index.ts | 2 +- test/ema/emaMath.spec.ts | 6 +++--- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/oracle/OracleMath.ts b/src/oracle/OracleMath.ts index e83b8047..7e254ff9 100644 --- a/src/oracle/OracleMath.ts +++ b/src/oracle/OracleMath.ts @@ -2,32 +2,30 @@ import { low_precision_iterated_price_ema, iterated_balance_ema } from '@galacti import BigNumber from 'bignumber.js'; import { ZERO, bnum } from '../utils/bignumber'; -interface OracleEntry { +export interface OracleEntry { price: [BigNumber, BigNumber]; volume: BigNumber[]; liquidity: [BigNumber, BigNumber]; timestamp: BigNumber; } -interface LowPrecisionOracleEntry { +export interface LowPrecisionOracleEntry { price: BigNumber; volume: BigNumber[]; liquidity: [BigNumber, BigNumber]; timestamp: BigNumber; } -type OraclePeriod = 'LastBlock' | 'Short' | 'TenMinutes' | 'Hour' | 'Day' | 'Week'; +export enum OraclePeriod { + LastBlock = 'LastBlock', + Short = 'Short', + TenMinutes = 'TenMinutes', + Hour = 'Hour', + Day = 'Day', + Week = 'Week', +} export class EmaLowPrecisionMath { - // Smoothing factors for the currently supported oracle periods. - // Taken from https://github.com/galacticcouncil/warehouse/blob/0047e9ceff47b2a058ae9ecc25da96d1e827a26a/ema-oracle/src/types.rs#L198-L207 - static readonly LastBlock: string = '170141183460469231731687303715884105728'; - static readonly Short: string = '34028236692093846346337460743176821146'; - static readonly TenMinutes: string = '3369132345751865974884897103284833777'; - static readonly Hour: string = '566193622164623067326746434994622648'; - static readonly Day: string = '23629079016800115510268356880200556'; - static readonly Week: string = '3375783642235081630771268215908257'; - static iteratedPriceEma( iterations: string, prevN: string, @@ -45,16 +43,18 @@ export class EmaLowPrecisionMath { } export class OracleMath { - static readonly SmoothingForPeriod: Map = new Map([ - ['LastBlock', EmaLowPrecisionMath.LastBlock], - ['Short', EmaLowPrecisionMath.Short], - ['TenMinutes', EmaLowPrecisionMath.TenMinutes], - ['Hour', EmaLowPrecisionMath.Hour], - ['Day', EmaLowPrecisionMath.Day], - ['Week', EmaLowPrecisionMath.Week], + // Smoothing factors for the currently supported oracle periods. + // Taken from https://github.com/galacticcouncil/warehouse/blob/0047e9ceff47b2a058ae9ecc25da96d1e827a26a/ema-oracle/src/types.rs#L198-L207 + static readonly SmoothingForPeriod: Map = new Map([ + [OraclePeriod.LastBlock, '170141183460469231731687303715884105728'], + [OraclePeriod.Short, '34028236692093846346337460743176821146'], + [OraclePeriod.TenMinutes, '3369132345751865974884897103284833777'], + [OraclePeriod.Hour, '566193622164623067326746434994622648'], + [OraclePeriod.Day, '23629079016800115510268356880200556'], + [OraclePeriod.Week, '3375783642235081630771268215908257'], ]); - /// Calculate the current oracle values from the `outdated` and `update_with` values using the `smoothing` factor with the old values being `iterations` out of date. + /// Calculate the current oracle values from the `outdated` and `updateWith` values using the `smoothing` factor with the old values being `iterations` out of date. /// /// Note: The volume is always updated with zero values so it is not a parameter. static updateOutdatedToCurrent( @@ -66,7 +66,7 @@ export class OracleMath { throw new Error('invalid timestamp (outdated should be older)'); } let iterations = BigNumber.max(updateWith.timestamp.minus(outdated.timestamp), 0).toString(); - let smoothing = OracleMath.SmoothingForPeriod[period]; + let smoothing = OracleMath.SmoothingForPeriod.get(period); let [prevN, prevD] = outdated.price; let [incomingN, incomingD] = updateWith.price; let price = bnum( diff --git a/src/oracle/index.ts b/src/oracle/index.ts index 27f368a4..9fa9579c 100644 --- a/src/oracle/index.ts +++ b/src/oracle/index.ts @@ -1 +1 @@ -export { OracleMath, EmaLowPrecisionMath } from './OracleMath'; +export { EmaLowPrecisionMath, LowPrecisionOracleEntry, OracleEntry, OracleMath, OraclePeriod } from './OracleMath'; diff --git a/test/ema/emaMath.spec.ts b/test/ema/emaMath.spec.ts index 43c13814..c3654cf3 100644 --- a/test/ema/emaMath.spec.ts +++ b/test/ema/emaMath.spec.ts @@ -1,4 +1,4 @@ -import { EmaLowPrecisionMath } from '../../src/oracle'; +import { EmaLowPrecisionMath, OracleMath, OraclePeriod } from '../../src/oracle'; import { BigNumber, bnum } from '../../src/utils/bignumber'; function smoothingFromPeriod(period: number): BigNumber { @@ -29,7 +29,7 @@ describe('EMA Math', () => { }); it('Should return correct price EMA for hard-coded period', async () => { - let smoothing = EmaLowPrecisionMath.Short; + let smoothing = OracleMath.SmoothingForPeriod.get(OraclePeriod.Short); let startPriceN = bnum('100'); let startPriceD = bnum(1); let incomingPriceN = bnum('1100'); @@ -48,7 +48,7 @@ describe('EMA Math', () => { }); it('Should return correct balance EMA for hard-coded period', async () => { - let smoothing = EmaLowPrecisionMath.Short; + let smoothing = OracleMath.SmoothingForPeriod.get(OraclePeriod.Short); let startBalance = bnum('100000000'); let incomingBalance = bnum('1100000000'); let iterations = bnum(1); From 7f34cbd3cc47fcbdf935631a948e1bc5f558b69a Mon Sep 17 00:00:00 2001 From: Alexander Popiak Date: Fri, 5 May 2023 17:41:40 +0200 Subject: [PATCH 8/8] check smoothing for undefined --- src/oracle/OracleMath.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/oracle/OracleMath.ts b/src/oracle/OracleMath.ts index 7e254ff9..1549b575 100644 --- a/src/oracle/OracleMath.ts +++ b/src/oracle/OracleMath.ts @@ -67,6 +67,9 @@ export class OracleMath { } let iterations = BigNumber.max(updateWith.timestamp.minus(outdated.timestamp), 0).toString(); let smoothing = OracleMath.SmoothingForPeriod.get(period); + if (!smoothing) { + throw new Error('unknown period'); + } let [prevN, prevD] = outdated.price; let [incomingN, incomingD] = updateWith.price; let price = bnum(