Skip to content

Commit 66a0a16

Browse files
authored
fix: check balance on outgoing payment created webhook (#1956)
1 parent 13205c2 commit 66a0a16

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param { import("knex").Knex } knex
3+
* @returns { Promise<void> }
4+
*/
5+
exports.up = function (knex) {
6+
return knex.schema.alterTable('transactions', async (table) => {
7+
await knex.raw(
8+
[
9+
`ALTER TABLE "transactions" DROP CONSTRAINT IF EXISTS "transactions_status_check";`,
10+
`ALTER TABLE "transactions" ADD CONSTRAINT "transactions_status_check" CHECK ("status" = ANY (ARRAY['PENDING'::text, 'COMPLETED'::text, 'FAILED'::text, 'EXPIRED'::text]))`
11+
].join('\n')
12+
)
13+
})
14+
}
15+
16+
/**
17+
* @param { import("knex").Knex } knex
18+
* @returns { Promise<void> }
19+
*/
20+
exports.down = function (knex) {
21+
return knex.schema.alterTable('transactions', async (table) => {
22+
await knex.raw(
23+
[
24+
`ALTER TABLE "transactions" DROP CONSTRAINT IF EXISTS "transactions_status_check";`,
25+
`ALTER TABLE "transactions" ADD CONSTRAINT "transactions_status_check" CHECK ("status" = ANY (ARRAY['PENDING'::text, 'COMPLETED'::text, 'REJECTED'::text, 'EXPIRED'::text]))`
26+
].join('\n')
27+
)
28+
})
29+
}

packages/wallet/backend/src/rafiki/service.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export class RafikiService implements IRafikiService {
230230
const walletAddress = await this.getWalletAddress(wh)
231231
const amount = this.getAmountFromWebHook(wh)
232232

233-
const { gateHubWalletId } =
233+
const { gateHubWalletId, gateHubUserId } =
234234
await this.getGateHubWalletAddress(walletAddress)
235235

236236
if (!this.validateAmount(amount, wh.type)) {
@@ -247,12 +247,24 @@ export class RafikiService implements IRafikiService {
247247
secondParty
248248
)
249249

250+
const balance = await this.getWalletBalance(
251+
gateHubWalletId,
252+
gateHubUserId,
253+
amount.assetCode
254+
)
255+
const amountValue = this.amountToNumber(amount)
256+
257+
if (balance < amountValue) {
258+
this.logger.info(
259+
`Insufficient funds. Payment amount ${amountValue}, balance ${balance}`
260+
)
261+
throw new Error('Insufficient funds')
262+
}
263+
250264
await this.rafikiClient.depositLiquidity(wh.id)
251265

252266
this.logger.info(
253-
`Succesfully held ${this.amountToNumber(
254-
amount
255-
)} in ${gateHubWalletId} on ${EventType.OutgoingPaymentCreated}`
267+
`Succesfully held ${amountValue} in ${gateHubWalletId} on ${EventType.OutgoingPaymentCreated}`
256268
)
257269
}
258270

@@ -463,4 +475,20 @@ export class RafikiService implements IRafikiService {
463475
this.logger.warn('Error on getting receiver wallet address', e)
464476
}
465477
}
478+
479+
async getWalletBalance(
480+
gateHubWalletId: string,
481+
gateHubUserId: string,
482+
assetCode: string
483+
) {
484+
const balances = await this.gateHubClient.getWalletBalance(
485+
gateHubWalletId,
486+
gateHubUserId
487+
)
488+
489+
return Number(
490+
balances.find((balance) => balance.vault.asset_code === assetCode)
491+
?.available ?? 0
492+
)
493+
}
466494
}

0 commit comments

Comments
 (0)