From 06394114e56d75c18ee494d3fc581f6936d9f7a5 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Tue, 27 Feb 2024 17:21:59 +0900 Subject: [PATCH 1/3] fix: remove unused param --- src/internal.ts | 11 +++++++++-- src/utils/operator.ts | 1 - 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/internal.ts b/src/internal.ts index 57b676e..607995d 100644 --- a/src/internal.ts +++ b/src/internal.ts @@ -22,8 +22,8 @@ export default class Internal { async handleRequest(req: Request, cost: number, status: RESPONSE_STATUS, responseData: string) { const { requesterAddress, requestKey, appName } = this.getDataFromServiceRequest(req); - const ops:SetOperation[] = []; - const responseOp = getResponseOp(appName, requesterAddress, requestKey, status, responseData, cost); + const ops: SetOperation[] = []; + const responseOp = getResponseOp(appName, requesterAddress, requestKey, status, responseData); ops.push(responseOp); if(cost > 0) { const changeBalanceOp = getChangeBalanceOp(appName, requesterAddress, 'DEC_VALUE', cost); @@ -42,4 +42,11 @@ export default class Internal { private getDataFromDepositRequest(req: Request) { return extractDataFromDepositRequest(req); } + + private async checkBalance(address: string, cost: number) { + const userBalance = await this.ain.getBalance(address); + if (userBalance < cost) { + throw new Error("User does not have sufficient balance."); + } + } } \ No newline at end of file diff --git a/src/utils/operator.ts b/src/utils/operator.ts index 035a2df..04865ff 100644 --- a/src/utils/operator.ts +++ b/src/utils/operator.ts @@ -66,7 +66,6 @@ export const getResponseOp = ( requestKey: string, status: RESPONSE_STATUS, responseData: string, - cost: number, ) => { const responsePath = Path.app(appName).response(requesterAddress, requestKey); const responseValue = { From 0599985266cf9df4be6411000d2dba6f9027c4c1 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Tue, 27 Feb 2024 17:53:15 +0900 Subject: [PATCH 2/3] fix: getBalance with address --- src/ain.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ain.ts b/src/ain.ts index 8b6c3fc..28cd969 100644 --- a/src/ain.ts +++ b/src/ain.ts @@ -82,8 +82,8 @@ export default class AinModule { this.ain!.setSigner(new DefaultSigner(wallet, provider)) } - async getBalance() { - const address = await this.getAddress(); + async getBalance(address?: string) { + address = address || await this.getAddress(); const balancePath = `/accounts/${address}/balance`; return await this.ain!.db.ref(balancePath).getValue(); } From 71bc8d7777913877a556381609958e9b6d02b9b7 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Tue, 27 Feb 2024 17:58:01 +0900 Subject: [PATCH 3/3] feat: checkBalance before request call --- src/internal.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/internal.ts b/src/internal.ts index 607995d..868935d 100644 --- a/src/internal.ts +++ b/src/internal.ts @@ -43,10 +43,11 @@ export default class Internal { return extractDataFromDepositRequest(req); } - private async checkBalance(address: string, cost: number) { - const userBalance = await this.ain.getBalance(address); + async checkBalance(req: Request, cost: number, errorMsg?: string) { + const { requesterAddress } = this.getDataFromDepositRequest(req); + const userBalance = await this.ain.getBalance(requesterAddress); if (userBalance < cost) { - throw new Error("User does not have sufficient balance."); + throw new Error(errorMsg ? errorMsg : "User does not have sufficient balance."); } } } \ No newline at end of file