@@ -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