Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check balance. #105

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
12 changes: 10 additions & 2 deletions src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.");
}
}
}
1 change: 0 additions & 1 deletion src/utils/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Loading