Skip to content

Commit

Permalink
fixup! feat: directional premiums for referrals
Browse files Browse the repository at this point in the history
  • Loading branch information
maybeast committed Mar 5, 2025
1 parent 5b36106 commit 2b7415a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
27 changes: 24 additions & 3 deletions lib/db/repositories/ReferralRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { QueryTypes } from 'sequelize';
import { SuccessSwapUpdateEvents, SwapType } from '../../consts/Enums';
import {
OrderSide,
SuccessSwapUpdateEvents,
SwapType,
} from '../../consts/Enums';
import Database from '../Database';
import Referral, {
DirectionalPremium,
Expand Down Expand Up @@ -120,6 +124,24 @@ class ReferralRepository {
private static sanityCheckConfig = (
config: ReferralConfig | null | undefined,
) => {
const validateDirectionalPremium = (
premium: unknown,
): DirectionalPremium => {
if (typeof premium === 'object') {
const premiumObj = premium as Record<string, unknown>;
if (
(OrderSide.BUY in premiumObj &&
typeof premiumObj[OrderSide.BUY] !== 'number') ||
(OrderSide.SELL in premiumObj &&
typeof premiumObj[OrderSide.SELL] !== 'number')
) {
throw 'premium values must be numbers';
}
}

return premium as DirectionalPremium;
};

const sanityCheckPairConfig = (cfg: ReferralPairConfig) => {
if (
cfg.maxRoutingFee !== undefined &&
Expand All @@ -134,8 +156,7 @@ class ReferralRepository {
const type = Number(typeStr) as SwapType;

if (type === SwapType.Chain) {
const directionalPremium = premium as DirectionalPremium;

const directionalPremium = validateDirectionalPremium(premium);
Object.values(directionalPremium).forEach((p) => {
if (
p < ReferralRepository.minPremiumPercentage ||
Expand Down
69 changes: 28 additions & 41 deletions test/integration/db/repositories/ReferralRepository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,62 +149,49 @@ describe('ReferralRepository', () => {
},
);

test('should throw if chain swap premium is not a DirectionalPremium object', async () => {
await expect(
ReferralRepository.addReferral({
...fixture,
config: {
premiums: {
[SwapType.Chain]: 10 as any,
test.each`
premium | description
${{ [OrderSide.BUY]: 'not-a-number' }} | ${'invalid BUY value type'}
${{ [OrderSide.SELL]: 'not-a-number' }} | ${'invalid SELL value type'}
${{ [OrderSide.BUY]: true }} | ${'invalid BUY value type'}
${{ [OrderSide.SELL]: {} }} | ${'invalid SELL value type'}
${{ [OrderSide.BUY]: () => {} }} | ${'invalid BUY value type'}
${{ [OrderSide.SELL]: [1, 2] }} | ${'invalid SELL value type'}
`(
'should throw if chain swap premium has $description',
async ({ premium }) => {
await expect(
ReferralRepository.addReferral({
...fixture,
config: {
premiums: {
[SwapType.Chain]: premium,
},
},
},
}),
).rejects.toEqual('premium must be a number');
});
}),
).rejects.toEqual('premium values must be numbers');
},
);

test.each`
premiumValue | description
premium | description
${{}} | ${'empty object'}
${{ [OrderSide.BUY]: 10 }} | ${'only BUY'}
${{ [OrderSide.SELL]: 10 }} | ${'only SELL'}
${{ [OrderSide.BUY]: 10, [OrderSide.SELL]: 10 }} | ${'both BUY and SELL'}
`(
'should accept valid partial chain swap premiums ($description)',
async ({ premiumValue }) => {
'should accept valid chain swap premium ($description)',
async ({ premium }) => {
const ref = await ReferralRepository.addReferral({
...fixture,
config: {
premiums: {
[SwapType.Chain]: premiumValue,
[SwapType.Chain]: premium,
},
},
});

expect(ref.config?.premiums?.[SwapType.Chain]).toEqual(premiumValue);
},
);

test.each`
buy | sell
${101} | ${10}
${10} | ${-101}
${-101} | ${10}
${10} | ${101}
`(
'should throw if chain swap premium values are out of range (buy: $buy, sell: $sell)',
async ({ buy, sell }) => {
await expect(
ReferralRepository.addReferral({
...fixture,
config: {
premiums: {
[SwapType.Chain]: {
[OrderSide.BUY]: buy,
[OrderSide.SELL]: sell,
},
},
},
}),
).rejects.toEqual('premium out of range');
expect(ref.config?.premiums?.[SwapType.Chain]).toEqual(premium);
},
);

Expand Down

0 comments on commit 2b7415a

Please sign in to comment.