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 a232f75 commit 5b36106
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 45 deletions.
4 changes: 2 additions & 2 deletions lib/db/models/Referral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ type Limits = {
maximal?: number;
};

type DirectionalPremium = {
type DirectionalPremium = Partial<{
[OrderSide.BUY]: number;
[OrderSide.SELL]: number;
};
}>;

type Premiums = Partial<{
[SwapType.Submarine]: number;
Expand Down
33 changes: 9 additions & 24 deletions lib/db/repositories/ReferralRepository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { QueryTypes } from 'sequelize';
import {
OrderSide,
SuccessSwapUpdateEvents,
SwapType,
} from '../../consts/Enums';
import { SuccessSwapUpdateEvents, SwapType } from '../../consts/Enums';
import Database from '../Database';
import Referral, {
DirectionalPremium,
Expand Down Expand Up @@ -140,25 +136,14 @@ class ReferralRepository {
if (type === SwapType.Chain) {
const directionalPremium = premium as DirectionalPremium;

if (
directionalPremium[OrderSide.BUY] === undefined ||
directionalPremium[OrderSide.SELL] === undefined
) {
throw 'Chain swap premiums must specify both BUY and SELL values';
}

if (
[
directionalPremium[OrderSide.BUY],
directionalPremium[OrderSide.SELL],
].some(
(p) =>
p < ReferralRepository.minPremiumPercentage ||
p > ReferralRepository.maxPremiumPercentage,
)
) {
throw 'premium out of range';
}
Object.values(directionalPremium).forEach((p) => {
if (
p < ReferralRepository.minPremiumPercentage ||
p > ReferralRepository.maxPremiumPercentage
) {
throw 'premium out of range';
}
});
} else {
if (typeof premium !== 'number') {
throw 'premium must be a number';
Expand Down
34 changes: 15 additions & 19 deletions test/integration/db/repositories/ReferralRepository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,31 +159,27 @@ describe('ReferralRepository', () => {
},
},
}),
).rejects.toEqual(
'Chain swap premiums must specify both BUY and SELL values',
);
).rejects.toEqual('premium must be a number');
});

test.each`
premiumValue
${10}
${{ [OrderSide.BUY]: 10 }}
${{ [OrderSide.SELL]: 10 }}
premiumValue | description
${{ [OrderSide.BUY]: 10 }} | ${'only BUY'}
${{ [OrderSide.SELL]: 10 }} | ${'only SELL'}
${{ [OrderSide.BUY]: 10, [OrderSide.SELL]: 10 }} | ${'both BUY and SELL'}
`(
'should throw if chain swap premium is invalid or incomplete ($premiumValue)',
'should accept valid partial chain swap premiums ($description)',
async ({ premiumValue }) => {
await expect(
ReferralRepository.addReferral({
...fixture,
config: {
premiums: {
[SwapType.Chain]: premiumValue,
},
const ref = await ReferralRepository.addReferral({
...fixture,
config: {
premiums: {
[SwapType.Chain]: premiumValue,
},
}),
).rejects.toEqual(
'Chain swap premiums must specify both BUY and SELL values',
);
},
});

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

Expand Down

0 comments on commit 5b36106

Please sign in to comment.