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(); } diff --git a/src/internal.ts b/src/internal.ts index 57b676e..868935d 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,12 @@ export default class Internal { private getDataFromDepositRequest(req: Request) { return extractDataFromDepositRequest(req); } + + 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(errorMsg ? errorMsg : "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 = {