Skip to content

Commit

Permalink
[Relay] Add endpoints that provide loyalty types and balances
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Oct 27, 2023
1 parent 52541df commit 708d1b6
Show file tree
Hide file tree
Showing 3 changed files with 498 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/relay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"debug:Endpoints": "NODE_ENV=test hardhat test test/Endpoints.test.ts",
"debug:Config": "NODE_ENV=test hardhat test test/Config.test.ts",
"debug:Shop": "NODE_ENV=test hardhat test test/Shop.test.ts",
"debug:ShopWithdraw": "NODE_ENV=test hardhat test test/ShopWithdraw.test.ts"
"debug:ShopWithdraw": "NODE_ENV=test hardhat test test/ShopWithdraw.test.ts",
"debug:UserInfo": "NODE_ENV=test hardhat test test/UserInfo.test.ts"
},
"repository": {
"type": "git",
Expand Down
48 changes: 47 additions & 1 deletion packages/relay/src/routers/DefaultRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Validation } from "../validation";

import { NonceManager } from "@ethersproject/experimental";
import { Signer, Wallet } from "ethers";
import { body, validationResult } from "express-validator";
import { body, query, validationResult } from "express-validator";
import * as hre from "hardhat";

import express from "express";
Expand Down Expand Up @@ -342,6 +342,12 @@ export class DefaultRouter {
],
this.shop_closeWithdrawal.bind(this)
);

this.app.get(
"/user/balance",
[query("account").exists().trim().isEthereumAddress()],
this.user_balance.bind(this)
);
}

private async getHealthStatus(req: express.Request, res: express.Response) {
Expand Down Expand Up @@ -839,4 +845,44 @@ export class DefaultRouter {
this.releaseRelaySigner(signerItem);
}
}

/**
* 사용자 정보 / 로열티 종류와 잔고를 제공하는 엔드포인트
* GET /user/balance
* @private
*/
private async user_balance(req: express.Request, res: express.Response) {
logger.http(`GET /user/balance`);

const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(200).json(
this.makeResponseData(501, undefined, {
message: "Failed to check the validity of parameters.",
validation: errors.array(),
})
);
}

try {
const account: string = String(req.query.account).trim();
const loyaltyType = await (await this.getLedgerContract()).loyaltyTypeOf(account);
const balance =
loyaltyType === 0
? await (await this.getLedgerContract()).pointBalanceOf(account)
: await (await this.getLedgerContract()).tokenBalanceOf(account);
return res
.status(200)
.json(this.makeResponseData(200, { account, loyaltyType, balance: balance.toString() }));
} catch (error: any) {
let message = ContractUtils.cacheEVMError(error as any);
if (message === "") message = "Failed /user/balance";
logger.error(`GET /user/balance :`, message);
return res.status(200).json(
this.makeResponseData(500, undefined, {
message,
})
);
}
}
}
Loading

0 comments on commit 708d1b6

Please sign in to comment.