Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,8 @@
"packages/core/src/compliance/plugin.ts",
"packages/core/src/engagement/notifications/plugin.ts",
"packages/core/src/pam/tag/plugin.ts",
"packages/core/src/server/runtime/create-app.ts"
"packages/core/src/server/runtime/create-app.ts",
"packages/core/src/wallet/plugin.ts"
]
},
{
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/contracts/adapters/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export type DirectAuditAction =
| 'wallet.auto_withdrawal_rule.set'
| 'wallet.auto_withdrawal_rule.deleted'
| 'wallet.auto_withdrawal_config.set'
| 'wallet.manual_adjustment.created';
| 'wallet.manual_adjustment.created'
| 'wallet.custody.sweep_cycle';

/**
* Every value the audit `action` column legitimately holds: a cross-module domain
Expand Down
90 changes: 90 additions & 0 deletions packages/core/src/wallet/__tests__/custody-sweep-gate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { describe, it, expect } from 'vitest';
import { gateSweepBalance } from '../service/custody-sweep.service.js';

const base = {
amount: '100',
estimatedFee: '1',
minDeposit: '10',
feeMultiple: '5',
sweepFeeCeiling: null as string | null,
poolLiquidityFloor: null as string | null,
poolBalance: null as string | null,
};

describe('gateSweepBalance', () => {
it('sweeps a balance that clears every gate', () => {
expect(gateSweepBalance(base)).toBe('sweep');
});

describe('dust floor (amount >= minDeposit)', () => {
it('skips strictly below the floor', () => {
expect(gateSweepBalance({ ...base, amount: '9.999999999999999999' })).toBe('dust');
});

it('sweeps exactly at the floor', () => {
expect(gateSweepBalance({ ...base, amount: '10' })).toBe('sweep');
});
});

describe('fee-multiple floor (amount >= estimatedFee * feeMultiple)', () => {
it('skips strictly below the floor', () => {
// fee 1 * multiple 5 = 5; amount must be >= 5. Use 4.999... below it, but still
// above the dust floor (10) so dust never masks this gate.
expect(gateSweepBalance({ ...base, minDeposit: '1', amount: '4.999999999999999999' })).toBe(
'fee',
);
});

it('sweeps exactly at the floor', () => {
expect(gateSweepBalance({ ...base, minDeposit: '1', amount: '5' })).toBe('sweep');
});
});

describe('fee ceiling (skip when estimatedFee > sweepFeeCeiling)', () => {
it('sweeps when the fee is exactly at the ceiling', () => {
expect(gateSweepBalance({ ...base, estimatedFee: '2', sweepFeeCeiling: '2' })).toBe('sweep');
});

it('skips when the fee exceeds the ceiling and there is no liquidity floor', () => {
expect(gateSweepBalance({ ...base, estimatedFee: '3', sweepFeeCeiling: '2' })).toBe(
'ceiling',
);
});

it('skips when the fee exceeds the ceiling and the pool is at or above the floor', () => {
expect(
gateSweepBalance({
...base,
estimatedFee: '3',
sweepFeeCeiling: '2',
poolLiquidityFloor: '1000',
poolBalance: '1000',
}),
).toBe('ceiling');
});

it('overrides the ceiling only when the pool is strictly below the liquidity floor', () => {
expect(
gateSweepBalance({
...base,
estimatedFee: '3',
sweepFeeCeiling: '2',
poolLiquidityFloor: '1000',
poolBalance: '999.999999999999999999',
}),
).toBe('sweep');
});

it('never overrides on a null poolBalance even with a floor configured', () => {
expect(
gateSweepBalance({
...base,
estimatedFee: '3',
sweepFeeCeiling: '2',
poolLiquidityFloor: '1000',
poolBalance: null,
}),
).toBe('ceiling');
});
});
});
Loading
Loading