diff --git a/packages/backend/migrations/20241103204430_assets_peer_liquidity_threshold.js b/packages/backend/migrations/20241103204430_assets_peer_liquidity_threshold.js index 7716649f3c..13d728323d 100644 --- a/packages/backend/migrations/20241103204430_assets_peer_liquidity_threshold.js +++ b/packages/backend/migrations/20241103204430_assets_peer_liquidity_threshold.js @@ -1,25 +1,25 @@ -exports.up = function(knex) { - return Promise.all([ - knex.schema.table('assets', (table) => { - table.renameColumn('liquidityThreshold', 'liquidityThresholdLow') - table.bigInteger('liquidityThresholdHigh').nullable() - }), - knex.schema.table('peers', (table) => { - table.renameColumn('liquidityThreshold', 'liquidityThresholdLow') - table.bigInteger('liquidityThresholdHigh').nullable() - }) - ]) +exports.up = function (knex) { + return Promise.all([ + knex.schema.table('assets', (table) => { + table.renameColumn('liquidityThreshold', 'liquidityThresholdLow') + table.bigInteger('liquidityThresholdHigh').nullable() + }), + knex.schema.table('peers', (table) => { + table.renameColumn('liquidityThreshold', 'liquidityThresholdLow') + table.bigInteger('liquidityThresholdHigh').nullable() + }) + ]) } -exports.down = function(knex) { - return Promise.all([ - knex.schema.table('assets', (table) => { - table.renameColumn('liquidityThresholdLow', 'liquidityThreshold') - table.dropColumn('liquidityThresholdHigh') - }), - knex.schema.table('peers', (table) => { - table.renameColumn('liquidityThresholdLow', 'liquidityThreshold') - table.dropColumn('liquidityThresholdHigh') - }) - ]) +exports.down = function (knex) { + return Promise.all([ + knex.schema.table('assets', (table) => { + table.renameColumn('liquidityThresholdLow', 'liquidityThreshold') + table.dropColumn('liquidityThresholdHigh') + }), + knex.schema.table('peers', (table) => { + table.renameColumn('liquidityThresholdLow', 'liquidityThreshold') + table.dropColumn('liquidityThresholdHigh') + }) + ]) } diff --git a/packages/backend/src/asset/model.test.ts b/packages/backend/src/asset/model.test.ts index e86c59ffcd..fbaf4c85f9 100644 --- a/packages/backend/src/asset/model.test.ts +++ b/packages/backend/src/asset/model.test.ts @@ -56,7 +56,10 @@ describe('Models', (): void => { async ({ balance }): Promise => { await asset.onDebit({ balance }) const event = ( - await AssetEvent.query(knex).where('type', AssetEventType.LiquidityHigh) + await AssetEvent.query(knex).where( + 'type', + AssetEventType.LiquidityHigh + ) )[0] expect(event).toMatchObject({ @@ -112,7 +115,6 @@ describe('Models', (): void => { ).resolves.toEqual([]) }) - test.each` balance ${BigInt(200)} diff --git a/packages/backend/src/asset/model.ts b/packages/backend/src/asset/model.ts index 279163c11a..c2523d9669 100644 --- a/packages/backend/src/asset/model.ts +++ b/packages/backend/src/asset/model.ts @@ -30,12 +30,18 @@ export class Asset extends BaseModel implements LiquidityAccount { } } - private async checkAndInsertEvent(balance: bigint, threshold: bigint | null, eventType: AssetEventType) { + private async checkAndInsertEvent( + balance: bigint, + threshold: bigint | null, + eventType: AssetEventType + ) { if (threshold === null) { return } - const isThresholdCrossed = (eventType === AssetEventType.LiquidityLow && balance <= threshold) || (eventType === AssetEventType.LiquidityHigh && balance >= threshold) + const isThresholdCrossed = + (eventType === AssetEventType.LiquidityLow && balance <= threshold) || + (eventType === AssetEventType.LiquidityHigh && balance >= threshold) if (isThresholdCrossed) { await AssetEvent.query().insert({ @@ -57,8 +63,16 @@ export class Asset extends BaseModel implements LiquidityAccount { public async onDebit({ balance }: OnDebitOptions): Promise { await Promise.all([ - this.checkAndInsertEvent(balance, this.liquidityThresholdLow, AssetEventType.LiquidityLow), - this.checkAndInsertEvent(balance, this.liquidityThresholdHigh, AssetEventType.LiquidityHigh) + this.checkAndInsertEvent( + balance, + this.liquidityThresholdLow, + AssetEventType.LiquidityLow + ), + this.checkAndInsertEvent( + balance, + this.liquidityThresholdHigh, + AssetEventType.LiquidityHigh + ) ]) return this diff --git a/packages/backend/src/asset/service.test.ts b/packages/backend/src/asset/service.test.ts index aedfa7b2bc..560fbb7e24 100644 --- a/packages/backend/src/asset/service.test.ts +++ b/packages/backend/src/asset/service.test.ts @@ -57,12 +57,16 @@ describe('Asset Service', (): void => { ${BigInt(5)} | ${BigInt(5)} | ${BigInt(500)} `( 'Asset can be created and fetched', - async ({ withdrawalThreshold, liquidityThresholdLow, liquidityThresholdHigh }): Promise => { + async ({ + withdrawalThreshold, + liquidityThresholdLow, + liquidityThresholdHigh + }): Promise => { const options = { ...randomAsset(), withdrawalThreshold, liquidityThresholdLow, - liquidityThresholdHigh, + liquidityThresholdHigh } const asset = await assetService.create(options) assert.ok(!isAssetError(asset)) @@ -72,7 +76,7 @@ describe('Asset Service', (): void => { ledger: asset.ledger, withdrawalThreshold: withdrawalThreshold || null, liquidityThresholdLow: liquidityThresholdLow || null, - liquidityThresholdHigh: liquidityThresholdHigh || null, + liquidityThresholdHigh: liquidityThresholdHigh || null }) await expect(assetService.get(asset.id)).resolves.toEqual(asset) } @@ -150,7 +154,7 @@ describe('Asset Service', (): void => { describe('update', (): void => { describe.each` - withdrawalThreshold | liquidityThresholdLow | liquidityThresholdHigh + withdrawalThreshold | liquidityThresholdLow | liquidityThresholdHigh ${null} | ${null} | ${null} ${null} | ${null} | ${BigInt(500)} ${null} | ${BigInt(5)} | ${null} @@ -165,7 +169,11 @@ describe('Asset Service', (): void => { ${BigInt(5)} | ${BigInt(5)} | ${BigInt(500)} `( 'Asset threshold can be updated from withdrawalThreshold: $withdrawalThreshold, liquidityThresholdLow: $liquidityThresholdLow, liquidityThresholdHigh: $liquidityThresholdHigh', - ({ withdrawalThreshold, liquidityThresholdLow, liquidityThresholdHigh }): void => { + ({ + withdrawalThreshold, + liquidityThresholdLow, + liquidityThresholdHigh + }): void => { let assetId: string beforeEach(async (): Promise => { @@ -181,7 +189,7 @@ describe('Asset Service', (): void => { }) test.each` - withdrawalThreshold | liquidityThresholdLow | liquidityThresholdHigh + withdrawalThreshold | liquidityThresholdLow | liquidityThresholdHigh ${null} | ${null} | ${null} ${null} | ${null} | ${BigInt(500)} ${null} | ${BigInt(5)} | ${null} diff --git a/packages/backend/src/asset/service.ts b/packages/backend/src/asset/service.ts index 67ddde756a..bb34fd04c6 100644 --- a/packages/backend/src/asset/service.ts +++ b/packages/backend/src/asset/service.ts @@ -72,7 +72,13 @@ export async function createAssetService({ async function createAsset( deps: ServiceDependencies, - { code, scale, withdrawalThreshold, liquidityThresholdLow, liquidityThresholdHigh }: CreateOptions + { + code, + scale, + withdrawalThreshold, + liquidityThresholdLow, + liquidityThresholdHigh + }: CreateOptions ): Promise { try { // check if exists but deleted | by code-scale @@ -101,7 +107,7 @@ async function createAsset( scale, withdrawalThreshold, liquidityThresholdLow, - liquidityThresholdHigh, + liquidityThresholdHigh }) await deps.accountingService.createLiquidityAndLinkedSettlementAccount( asset, @@ -120,14 +126,23 @@ async function createAsset( async function updateAsset( deps: ServiceDependencies, - { id, withdrawalThreshold, liquidityThresholdLow, liquidityThresholdHigh }: UpdateOptions + { + id, + withdrawalThreshold, + liquidityThresholdLow, + liquidityThresholdHigh + }: UpdateOptions ): Promise { if (!deps.knex) { throw new Error('Knex undefined') } try { return await Asset.query(deps.knex) - .patchAndFetchById(id, { withdrawalThreshold, liquidityThresholdLow, liquidityThresholdHigh }) + .patchAndFetchById(id, { + withdrawalThreshold, + liquidityThresholdLow, + liquidityThresholdHigh + }) .throwIfNotFound() } catch (err) { if (err instanceof NotFoundError) { diff --git a/packages/backend/src/graphql/resolvers/asset.test.ts b/packages/backend/src/graphql/resolvers/asset.test.ts index 78e9021f59..71acce38cc 100644 --- a/packages/backend/src/graphql/resolvers/asset.test.ts +++ b/packages/backend/src/graphql/resolvers/asset.test.ts @@ -59,14 +59,14 @@ describe('Asset Resolvers', (): void => { describe('Create Asset', (): void => { test.each` - withdrawalThreshold | expectedWithdrawalThreshold | liquidityThresholdLow | expectedLiquidityThresholdLow | liquidityThresholdHigh | expectedLiquidityThresholdHigh - ${undefined} | ${null} | ${undefined} | ${null} | ${undefined} | ${null} - ${BigInt(0)} | ${'0'} | ${undefined} | ${null} | ${undefined} | ${null} - ${BigInt(5)} | ${'5'} | ${undefined} | ${null} | ${undefined} | ${null} - ${undefined} | ${null} | ${BigInt(0)} | ${'0'} | ${BigInt(100)} | ${'100'} - ${undefined} | ${null} | ${BigInt(5)} | ${'5'} | ${BigInt(5000)} | ${'5000'} - ${BigInt(0)} | ${'0'} | ${BigInt(0)} | ${'0'} | ${BigInt(100)} | ${'100'} - ${BigInt(5)} | ${'5'} | ${BigInt(5)} | ${'5'} | ${BigInt(5000)} | ${'5000'} + withdrawalThreshold | expectedWithdrawalThreshold | liquidityThresholdLow | expectedLiquidityThresholdLow | liquidityThresholdHigh | expectedLiquidityThresholdHigh + ${undefined} | ${null} | ${undefined} | ${null} | ${undefined} | ${null} + ${BigInt(0)} | ${'0'} | ${undefined} | ${null} | ${undefined} | ${null} + ${BigInt(5)} | ${'5'} | ${undefined} | ${null} | ${undefined} | ${null} + ${undefined} | ${null} | ${BigInt(0)} | ${'0'} | ${BigInt(100)} | ${'100'} + ${undefined} | ${null} | ${BigInt(5)} | ${'5'} | ${BigInt(5000)} | ${'5000'} + ${BigInt(0)} | ${'0'} | ${BigInt(0)} | ${'0'} | ${BigInt(100)} | ${'100'} + ${BigInt(5)} | ${'5'} | ${BigInt(5)} | ${'5'} | ${BigInt(5000)} | ${'5000'} `( 'Can create an asset (withdrawalThreshold: $withdrawalThreshold, liquidityThresholdLow: $liquidityThresholdLow, liquidityThresholdHigh: $liquidityThresholdHigh)', async ({ @@ -293,7 +293,7 @@ describe('Asset Resolvers', (): void => { const asset = await assetService.create({ ...randomAsset(), withdrawalThreshold: BigInt(10), - liquidityThreshold: BigInt(100) + liquidityThresholdLow: BigInt(100) }) assert.ok(!isAssetError(asset)) assert.ok(asset.withdrawalThreshold) @@ -331,7 +331,8 @@ describe('Asset Resolvers', (): void => { scale: asset.scale, liquidity: '0', withdrawalThreshold: asset.withdrawalThreshold.toString(), - liquidityThreshold: asset.liquidityThreshold?.toString(), + liquidityThresholdLow: asset.liquidityThresholdLow?.toString(), + liquidityThresholdHigh: asset.liquidityThresholdHigh?.toString(), createdAt: new Date(+asset.createdAt).toISOString() }) @@ -348,7 +349,8 @@ describe('Asset Resolvers', (): void => { scale: asset.scale, liquidity: '100', withdrawalThreshold: asset.withdrawalThreshold.toString(), - liquidityThreshold: asset.liquidityThreshold?.toString(), + liquidityThresholdLow: asset.liquidityThresholdLow?.toString(), + liquidityThresholdHigh: asset.liquidityThresholdHigh?.toString(), createdAt: new Date(+asset.createdAt).toISOString() }) }) @@ -628,7 +630,11 @@ describe('Asset Resolvers', (): void => { ${BigInt(5)} | ${BigInt(5)} | ${BigInt(5000)} `( 'from withdrawalThreshold: $withdrawalThreshold, liquidityThresholdLow: $liquidityThresholdLow and liquidityThresholdHigh: $liquidityThresholdHigh', - ({ withdrawalThreshold, liquidityThresholdLow, liquidityThresholdHigh }): void => { + ({ + withdrawalThreshold, + liquidityThresholdLow, + liquidityThresholdHigh + }): void => { let asset: AssetModel beforeEach(async (): Promise => { @@ -643,7 +649,7 @@ describe('Asset Resolvers', (): void => { test.each` withdrawalThreshold | liquidityThresholdLow | liquidityThresholdHigh - ${null} | ${null} | ${null} + ${null} | ${null} | ${null} ${BigInt(0)} | ${null} | ${null} ${BigInt(5)} | ${null} | ${null} ${null} | ${BigInt(0)} | ${BigInt(1000)} diff --git a/packages/backend/src/graphql/resolvers/asset.ts b/packages/backend/src/graphql/resolvers/asset.ts index f2bce9dd98..e6c20d7c1c 100644 --- a/packages/backend/src/graphql/resolvers/asset.ts +++ b/packages/backend/src/graphql/resolvers/asset.ts @@ -94,7 +94,7 @@ export const updateAsset: MutationResolvers['updateAsset'] = id: args.input.id, withdrawalThreshold: args.input.withdrawalThreshold ?? null, liquidityThresholdLow: args.input.liquidityThresholdLow ?? null, - liquidityThresholdHigh: args.input.liquidityThresholdHigh ?? null, + liquidityThresholdHigh: args.input.liquidityThresholdHigh ?? null }) if (isAssetError(assetOrError)) { throw new GraphQLError(errorToMessage[assetOrError], { diff --git a/packages/backend/src/payment-method/ilp/auto-peering/service.test.ts b/packages/backend/src/payment-method/ilp/auto-peering/service.test.ts index af2c2e2282..ff707a31c1 100644 --- a/packages/backend/src/payment-method/ilp/auto-peering/service.test.ts +++ b/packages/backend/src/payment-method/ilp/auto-peering/service.test.ts @@ -214,7 +214,7 @@ describe('Auto Peering Service', (): void => { peerUrl: 'http://peer.rafiki.money', assetId: asset.id, maxPacketAmount: 1000n, - liquidityThreshold: 100n + liquidityThresholdLow: 100n } const peerDetails: PeeringDetails = { @@ -261,7 +261,7 @@ describe('Auto Peering Service', (): void => { }, maxPacketAmount: args.maxPacketAmount, name: peerDetails.name, - liquidityThreshold: args.liquidityThreshold + liquidityThreshold: args.liquidityThresholdLow }) scope.done() @@ -274,7 +274,7 @@ describe('Auto Peering Service', (): void => { peerUrl: 'http://peer.rafiki.money', assetId: asset.id, maxPacketAmount: 1000n, - liquidityThreshold: 100n, + liquidityThresholdLow: 100n, liquidityToDeposit: 10000n } @@ -320,7 +320,7 @@ describe('Auto Peering Service', (): void => { peerUrl: 'http://peer.rafiki.money', assetId: asset.id, maxPacketAmount: 1000n, - liquidityThreshold: 100n, + liquidityThresholdLow: 100n, liquidityToDeposit: -10000n } diff --git a/packages/backend/src/payment-method/ilp/peer/model.test.ts b/packages/backend/src/payment-method/ilp/peer/model.test.ts index a664509eb1..47193c3955 100644 --- a/packages/backend/src/payment-method/ilp/peer/model.test.ts +++ b/packages/backend/src/payment-method/ilp/peer/model.test.ts @@ -89,7 +89,7 @@ describe('Models', (): void => { code: asset.code, scale: asset.scale }, - liquidityThreshold: peer.liquidityThreshold?.toString(), + liquidityThreshold: peer.liquidityThresholdLow?.toString(), balance: balance.toString() } }) diff --git a/packages/backend/src/payment-method/ilp/peer/model.ts b/packages/backend/src/payment-method/ilp/peer/model.ts index ceb524f87c..d517d98765 100644 --- a/packages/backend/src/payment-method/ilp/peer/model.ts +++ b/packages/backend/src/payment-method/ilp/peer/model.ts @@ -54,12 +54,18 @@ export class Peer public name?: string - private async checkAndInsertEvent(balance: bigint, threshold: bigint | null, eventType: PeerEventType) { + private async checkAndInsertEvent( + balance: bigint, + threshold: bigint | null, + eventType: PeerEventType + ) { if (threshold === null) { return } - const isThresholdCrossed = (eventType === PeerEventType.LiquidityLow && balance <= threshold) || (eventType === PeerEventType.LiquidityHigh && balance >= threshold) + const isThresholdCrossed = + (eventType === PeerEventType.LiquidityLow && balance <= threshold) || + (eventType === PeerEventType.LiquidityHigh && balance >= threshold) if (isThresholdCrossed) { await PeerEvent.query().insert({ @@ -81,8 +87,16 @@ export class Peer public async onDebit({ balance }: OnDebitOptions): Promise { await Promise.all([ - this.checkAndInsertEvent(balance, this.liquidityThresholdLow, PeerEventType.LiquidityLow), - this.checkAndInsertEvent(balance, this.liquidityThresholdHigh, PeerEventType.LiquidityHigh) + this.checkAndInsertEvent( + balance, + this.liquidityThresholdLow, + PeerEventType.LiquidityLow + ), + this.checkAndInsertEvent( + balance, + this.liquidityThresholdHigh, + PeerEventType.LiquidityHigh + ) ]) return this } diff --git a/packages/backend/src/payment-method/ilp/peer/service.test.ts b/packages/backend/src/payment-method/ilp/peer/service.test.ts index 9d0a7ae430..81f0051b76 100644 --- a/packages/backend/src/payment-method/ilp/peer/service.test.ts +++ b/packages/backend/src/payment-method/ilp/peer/service.test.ts @@ -39,7 +39,8 @@ describe('Peer Service', (): void => { maxPacketAmount: BigInt(100), staticIlpAddress: 'test.' + uuid(), name: faker.person.fullName(), - liquidityThreshold: BigInt(10000), + liquidityThresholdLow: BigInt(10000), + liquidityThresholdHigh: BigInt(100000), ...override }) @@ -223,12 +224,12 @@ describe('Peer Service', (): void => { describe('Update Peer', (): void => { test.each` - liquidityThreshold + liquidityThresholdLow ${null} ${BigInt(2000)} `( - 'Can update a peer, liquidityThreshold: $liquidityThreshold', - async ({ liquidityThreshold }): Promise => { + 'Can update a peer, liquidityThresholdLow: $liquidityThresholdLow', + async ({ liquidityThresholdLow }): Promise => { const peer = await createPeer(deps) const { http, maxPacketAmount, staticIlpAddress, name } = randomPeer() const updateOptions: UpdateOptions = { @@ -237,7 +238,7 @@ describe('Peer Service', (): void => { maxPacketAmount, staticIlpAddress, name, - liquidityThreshold + liquidityThresholdLow } const peerOrError = await peerService.update(updateOptions) @@ -252,7 +253,7 @@ describe('Peer Service', (): void => { maxPacketAmount: updateOptions.maxPacketAmount, staticIlpAddress: updateOptions.staticIlpAddress, name: updateOptions.name, - liquidityThreshold: updateOptions.liquidityThreshold || null + liquidityThresholdLow: updateOptions.liquidityThresholdLow || null } expect(peerOrError).toMatchObject(expectedPeer) await expect(peerService.get(peer.id)).resolves.toEqual(peerOrError) diff --git a/packages/backend/src/payment-method/ilp/peer/service.ts b/packages/backend/src/payment-method/ilp/peer/service.ts index 1d21b0deee..81ef131750 100644 --- a/packages/backend/src/payment-method/ilp/peer/service.ts +++ b/packages/backend/src/payment-method/ilp/peer/service.ts @@ -140,7 +140,7 @@ async function createPeer( staticIlpAddress: options.staticIlpAddress, name: options.name, liquidityThresholdLow: options.liquidityThresholdLow, - liquidityThresholdHigh: options.liquidityThresholdHigh, + liquidityThresholdHigh: options.liquidityThresholdHigh }) .withGraphFetched('asset') diff --git a/packages/backend/src/tests/peer.ts b/packages/backend/src/tests/peer.ts index 1c723dfcbb..c92ff162d5 100644 --- a/packages/backend/src/tests/peer.ts +++ b/packages/backend/src/tests/peer.ts @@ -36,7 +36,7 @@ export async function createPeer( if (options.liquidityThresholdHigh) { peerOptions.liquidityThresholdHigh = options.liquidityThresholdHigh } - + const peerService = await deps.use('peerService') const peer = await peerService.create(peerOptions) if (isPeerError(peer)) { diff --git a/packages/mock-account-service-lib/src/requesters.ts b/packages/mock-account-service-lib/src/requesters.ts index 113b63279f..9ba1f27829 100644 --- a/packages/mock-account-service-lib/src/requesters.ts +++ b/packages/mock-account-service-lib/src/requesters.ts @@ -78,7 +78,13 @@ export function createRequesters( } { return { createAsset: (code, scale, liquidityThresholdLow, liquidityThresholdHigh) => - createAsset(apolloClient, code, scale, liquidityThresholdLow, liquidityThresholdHigh), + createAsset( + apolloClient, + code, + scale, + liquidityThresholdLow, + liquidityThresholdHigh + ), createPeer: ( staticIlpAddress, outgoingEndpoint, diff --git a/packages/mock-account-service-lib/src/seed.ts b/packages/mock-account-service-lib/src/seed.ts index a2f33a4cc5..bb1c3b0ed6 100644 --- a/packages/mock-account-service-lib/src/seed.ts +++ b/packages/mock-account-service-lib/src/seed.ts @@ -44,12 +44,24 @@ export async function setupFromSeed( } = createRequesters(apolloClient, logger) const assets: Record = {} - for (const { code, scale, liquidity, liquidityThresholdLow, liquidityThresholdHigh } of config.seed - .assets) { - + for (const { + code, + scale, + liquidity, + liquidityThresholdLow, + liquidityThresholdHigh + } of config.seed.assets) { let asset = await getAssetByCodeAndScale(code, scale) if (!asset) { - asset = (await createAsset(code, scale, liquidityThresholdLow, liquidityThresholdHigh)).asset || null + asset = + ( + await createAsset( + code, + scale, + liquidityThresholdLow, + liquidityThresholdHigh + ) + ).asset || null } if (!asset) {