Skip to content

Commit

Permalink
feat: sanity check referral config values (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Feb 10, 2025
1 parent b7034db commit 5771b32
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 154 deletions.
2 changes: 1 addition & 1 deletion lib/db/models/Referral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,4 @@ class Referral extends Model implements ReferralType {
}

export default Referral;
export { ReferralType, ReferralConfig };
export { ReferralType, ReferralConfig, ReferralPairConfig };
62 changes: 57 additions & 5 deletions lib/db/repositories/ReferralRepository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { QueryTypes } from 'sequelize';
import { SuccessSwapUpdateEvents } from '../../consts/Enums';
import Database from '../Database';
import Referral, { ReferralConfig, ReferralType } from '../models/Referral';
import Referral, {
ReferralConfig,
ReferralPairConfig,
ReferralType,
} from '../models/Referral';
import { StatsDate } from './StatsRepository';

type ReferralSumRow = StatsDate & {
Expand Down Expand Up @@ -34,8 +38,12 @@ class ReferralRepository {
ORDER BY year, month;
`;

public static addReferral = (referral: ReferralType): Promise<Referral> => {
return Referral.create(referral);
public static addReferral = async (
referral: ReferralType,
): Promise<Referral> => {
ReferralRepository.sanityCheckConfig(referral.config);

return await Referral.create(referral);
};

public static getReferrals = (): Promise<Referral[]> => {
Expand Down Expand Up @@ -90,10 +98,54 @@ class ReferralRepository {
return res;
};

public static setConfig = (ref: Referral, config?: ReferralConfig | null) =>
ref.update({
public static setConfig = async (
ref: Referral,
config?: ReferralConfig | null,
) => {
ReferralRepository.sanityCheckConfig(config);

return await ref.update({
config: config === undefined ? null : config,
});
};

private static sanityCheckConfig = (
config: ReferralConfig | null | undefined,
) => {
const sanityCheckPairConfig = (cfg: ReferralPairConfig) => {
if (cfg.maxRoutingFee) {
if (cfg.maxRoutingFee < 0 || cfg.maxRoutingFee > 0.005) {
throw 'maxRoutingFee out of range';
}
}

if (cfg.premiums) {
if (Object.values(cfg.premiums).some((p) => p < -100 || p > 100)) {
throw 'premium out of range';
}
}

if (cfg.expirations) {
if (
Object.values(cfg.expirations).some(
(e) => e < 120 || e > 60 * 60 * 24,
)
) {
throw 'expiration out of range';
}
}
};

if (config) {
sanityCheckPairConfig(config);

if (config.pairs) {
for (const pair of Object.values(config.pairs)) {
sanityCheckPairConfig(pair);
}
}
}
};
}

export default ReferralRepository;
Expand Down
5 changes: 4 additions & 1 deletion lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { parseTransaction } from '../Core';
import { dumpHeap } from '../HeapDump';
import Logger, { LogLevel as BackendLevel } from '../Logger';
import { wait } from '../PromiseUtils';
import { getHexString, getUnixTime } from '../Utils';
import { getHexString, getUnixTime, stringify } from '../Utils';
import { CurrencyType, swapTypeToPrettyString } from '../consts/Enums';
import Referral, { ReferralConfig } from '../db/models/Referral';
import ReferralRepository from '../db/repositories/ReferralRepository';
Expand Down Expand Up @@ -518,6 +518,9 @@ class GrpcService {
}
}

this.logger.debug(
`Setting referral config for ${referral.id}: ${stringify(parsedConfig)}`,
);
await ReferralRepository.setConfig(referral, parsedConfig);

return new boltzrpc.SetReferralResponse();
Expand Down
Loading

0 comments on commit 5771b32

Please sign in to comment.