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 6591470 commit 623aff9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
6 changes: 3 additions & 3 deletions lib/db/models/Referral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ class Referral extends Model implements ReferralType {
public premium = (
pair: string,
type: SwapType,
orderSide?: OrderSide,
orderSide: OrderSide,
): number | undefined => {
const premium =
this.config?.pairs?.[pair]?.premiums?.[type] ||
this.config?.premiums?.[type];

if (type === SwapType.Chain && orderSide !== undefined) {
return premium ? premium[orderSide] : undefined;
if (type === SwapType.Chain) {
return premium !== undefined ? premium[orderSide] : undefined;
}

return premium as number | undefined;
Expand Down
50 changes: 31 additions & 19 deletions lib/db/repositories/ReferralRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class ReferralRepository {
ORDER BY year, month;
`;

private static readonly maxPremiumPercentage = 100;
private static readonly minPremiumPercentage = -100;
private static readonly minExpiration = 120;
private static readonly maxExpiration = 60 * 60 * 24;
private static readonly minRoutingFee = 0;
private static readonly maxRoutingFee = 0.005;

public static addReferral = async (
referral: ReferralType,
): Promise<Referral> => {
Expand Down Expand Up @@ -117,16 +124,11 @@ class ReferralRepository {
private static sanityCheckConfig = (
config: ReferralConfig | null | undefined,
) => {
const MAX_PREMIUM_PERCENTAGE = 100;
const MIN_PREMIUM_PERCENTAGE = -100;
const MIN_EXPIRATION = 120;
const MAX_EXPIRATION = 60 * 60 * 24;
const MAX_ROUTING_FEE = 0.005;

const sanityCheckPairConfig = (cfg: ReferralPairConfig) => {
if (
cfg.maxRoutingFee &&
(cfg.maxRoutingFee < 0 || cfg.maxRoutingFee > MAX_ROUTING_FEE)
cfg.maxRoutingFee !== undefined &&
(cfg.maxRoutingFee < ReferralRepository.minRoutingFee ||
cfg.maxRoutingFee > ReferralRepository.maxRoutingFee)
) {
throw 'maxRoutingFee out of range';
}
Expand All @@ -146,27 +148,37 @@ class ReferralRepository {
}

if (
directionalPremium[OrderSide.BUY] < MIN_PREMIUM_PERCENTAGE ||
directionalPremium[OrderSide.BUY] > MAX_PREMIUM_PERCENTAGE ||
directionalPremium[OrderSide.SELL] < MIN_PREMIUM_PERCENTAGE ||
directionalPremium[OrderSide.SELL] > MAX_PREMIUM_PERCENTAGE
[
directionalPremium[OrderSide.BUY],
directionalPremium[OrderSide.SELL],
].some(
(p) =>
p < ReferralRepository.minPremiumPercentage ||
p > ReferralRepository.maxPremiumPercentage,
)
) {
throw 'premium out of range';
}
} else {
if (typeof premium !== 'number') {
throw 'premium must be a number';
}
if (
premium < ReferralRepository.minPremiumPercentage ||
premium > ReferralRepository.maxPremiumPercentage
) {
throw 'premium out of range';
}
} else if (
typeof premium === 'number' &&
(premium < MIN_PREMIUM_PERCENTAGE ||
premium > MAX_PREMIUM_PERCENTAGE)
) {
throw 'premium out of range';
}
}
}

if (cfg.expirations) {
if (
Object.values(cfg.expirations).some(
(e) => e < MIN_EXPIRATION || e > MAX_EXPIRATION,
(e) =>
e < ReferralRepository.minExpiration ||
e > ReferralRepository.maxExpiration,
)
) {
throw 'expiration out of range';
Expand Down
4 changes: 1 addition & 3 deletions lib/rates/FeeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ class FeeProvider {
typeof percentageType === 'number'
? percentageType
: percentageType[orderSide],
type === SwapType.Chain
? referral?.premium(pair, type, orderSide)
: referral?.premium(pair, type),
referral?.premium(pair, type, orderSide),
);

return feeType === PercentageFeeType.Calculation
Expand Down
13 changes: 5 additions & 8 deletions lib/rates/providers/RateProviderTaproot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,14 +522,11 @@ class RateProviderTaproot extends RateProviderBase<SwapTypes> {

const { quote } = splitPairId(getConfigPairId(pairIds));

const premium =
type === SwapType.Chain
? referral?.premiumForPairs(
pairIds,
type,
to === quote ? OrderSide.SELL : OrderSide.BUY,
)
: referral?.premiumForPairs(pairIds, type);
const premium = referral?.premiumForPairs(
pairIds,
type,
to === quote ? OrderSide.SELL : OrderSide.BUY,
);
const limits = referral?.limitsForPairs(pairIds, type);

const result = {
Expand Down
17 changes: 5 additions & 12 deletions test/unit/rates/providers/RateProviderTaproot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,11 @@ describe('RateProviderTaproot', () => {
getPairs.call(provider, referral).get(from)!.get(to)!.fees.percentage,
).toEqual(expectedFee);

if (type === SwapType.Chain) {
expect(referral.premiumForPairs).toHaveBeenCalledWith(
expect.arrayContaining(['L-BTC/BTC', 'BTC/L-BTC']),
type,
expect.any(Number),
);
} else {
expect(referral.premiumForPairs).toHaveBeenCalledWith(
expect.arrayContaining(['L-BTC/BTC', 'BTC/L-BTC']),
type,
);
}
expect(referral.premiumForPairs).toHaveBeenCalledWith(
expect.arrayContaining(['L-BTC/BTC', 'BTC/L-BTC']),
type,
expect.any(Number),
);

expect(
getPairs.call(provider).get(from)!.get(to)!.fees.percentage,
Expand Down

0 comments on commit 623aff9

Please sign in to comment.