Skip to content

Commit

Permalink
chore(lint): everything
Browse files Browse the repository at this point in the history
  • Loading branch information
golobitch committed Nov 7, 2024
1 parent 584fd40 commit 9ceb81d
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -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')
})
])
}
6 changes: 4 additions & 2 deletions packages/backend/src/asset/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ describe('Models', (): void => {
async ({ balance }): Promise<void> => {
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({
Expand Down Expand Up @@ -112,7 +115,6 @@ describe('Models', (): void => {
).resolves.toEqual([])
})


test.each`
balance
${BigInt(200)}
Expand Down
22 changes: 18 additions & 4 deletions packages/backend/src/asset/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -57,8 +63,16 @@ export class Asset extends BaseModel implements LiquidityAccount {

public async onDebit({ balance }: OnDebitOptions): Promise<Asset> {
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
Expand Down
20 changes: 14 additions & 6 deletions packages/backend/src/asset/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
async ({
withdrawalThreshold,
liquidityThresholdLow,
liquidityThresholdHigh
}): Promise<void> => {
const options = {
...randomAsset(),
withdrawalThreshold,
liquidityThresholdLow,
liquidityThresholdHigh,
liquidityThresholdHigh
}
const asset = await assetService.create(options)
assert.ok(!isAssetError(asset))
Expand All @@ -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)
}
Expand Down Expand Up @@ -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}
Expand All @@ -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<void> => {
Expand All @@ -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}
Expand Down
23 changes: 19 additions & 4 deletions packages/backend/src/asset/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Asset | AssetError> {
try {
// check if exists but deleted | by code-scale
Expand Down Expand Up @@ -101,7 +107,7 @@ async function createAsset(
scale,
withdrawalThreshold,
liquidityThresholdLow,
liquidityThresholdHigh,
liquidityThresholdHigh
})
await deps.accountingService.createLiquidityAndLinkedSettlementAccount(
asset,
Expand All @@ -120,14 +126,23 @@ async function createAsset(

async function updateAsset(
deps: ServiceDependencies,
{ id, withdrawalThreshold, liquidityThresholdLow, liquidityThresholdHigh }: UpdateOptions
{
id,
withdrawalThreshold,
liquidityThresholdLow,
liquidityThresholdHigh
}: UpdateOptions
): Promise<Asset | AssetError> {
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) {
Expand Down
32 changes: 19 additions & 13 deletions packages/backend/src/graphql/resolvers/asset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ({
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
})

Expand All @@ -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()
})
})
Expand Down Expand Up @@ -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<void> => {
Expand All @@ -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)}
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/graphql/resolvers/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const updateAsset: MutationResolvers<ApolloContext>['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], {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -261,7 +261,7 @@ describe('Auto Peering Service', (): void => {
},
maxPacketAmount: args.maxPacketAmount,
name: peerDetails.name,
liquidityThreshold: args.liquidityThreshold
liquidityThreshold: args.liquidityThresholdLow
})

scope.done()
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/payment-method/ilp/peer/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('Models', (): void => {
code: asset.code,
scale: asset.scale
},
liquidityThreshold: peer.liquidityThreshold?.toString(),
liquidityThreshold: peer.liquidityThresholdLow?.toString(),
balance: balance.toString()
}
})
Expand Down
22 changes: 18 additions & 4 deletions packages/backend/src/payment-method/ilp/peer/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -81,8 +87,16 @@ export class Peer

public async onDebit({ balance }: OnDebitOptions): Promise<Peer> {
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
}
Expand Down
Loading

0 comments on commit 9ceb81d

Please sign in to comment.