From e404ba1874c6a42c12a3017a11f4be2c71daa2b8 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Wed, 13 Sep 2023 10:48:18 +0900 Subject: [PATCH] refactor: checkCostAndBalance --- src/modules/admin.ts | 13 ------------- src/modules/app.ts | 31 +++++++++++++++++++++++++++++++ src/modules/service.ts | 5 ++++- src/modules/service/useService.ts | 22 ---------------------- 4 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/modules/admin.ts b/src/modules/admin.ts index 49d7e10..6207d47 100644 --- a/src/modules/admin.ts +++ b/src/modules/admin.ts @@ -26,19 +26,6 @@ export default class Admin extends ModuleBase { const requesterAddress = req.body.auth.addr; return await this.depositService.handleDeposit(appName, transferKey, transferValue,requesterAddress); } - - /** - * Check cost of request and check if account can pay. You should use this function before send or handle request. - * If you don't set address, it will use default account's address. - * @param {string} appName - App name you want to request service to. - * @param {string} serviceName - Service name you want to request to. - * @param {string} prompt - Data you want to request to service . - * @param {string=} userAddress - Address of account you want to check balance. You should set default account if you don't provide address. - * @returns Result cost of service. It throws error when user can't pay. - */ - async checkCostAndBalance(appName: string, serviceName: string, prompt: string, userAddress?: string) { - return await this.useService.calculateCostAndCheckBalance(appName, serviceName, prompt, userAddress); - } /** * Write response. Then change balance of requester and write history of user balance if response status is success. diff --git a/src/modules/app.ts b/src/modules/app.ts index 80351b1..7f92ce5 100644 --- a/src/modules/app.ts +++ b/src/modules/app.ts @@ -150,6 +150,37 @@ export default class App extends ModuleBase { return await this.sendTransaction(txBody); } + /** + * Check cost of request and check if account can pay. You should use this function before send or handle request. + * If you don't set address, it will use default account's address. + * @param {string} appName - App name you want to request service to. + * @param {string} serviceName - Service name you want to request to. + * @param {string} prompt - Data you want to request to service . + * @param {string=} userAddress - Address of account you want to check balance. You should set default account if you don't provide address. + * @returns Result cost of service. It throws error when user can't pay. + */ + async checkCostAndBalance(appName: string, serviceName: string, value: string, requesterAddress?: string) { + requesterAddress = requesterAddress ? requesterAddress : this.getDefaultAccount().address; + const billingConfig = await this.getBillingConfig(appName); + // TODO(woojae): calculate cost more accurately + let serviceBillingConfig = billingConfig.service.default; + if(billingConfig.service[serviceName]) { + serviceBillingConfig = billingConfig.service[serviceName]; + } + const token = value.split(' ').length; + let cost = token * serviceBillingConfig.costPerToken; + if (serviceBillingConfig.minCost && cost < serviceBillingConfig.minCost) { + cost = serviceBillingConfig.minCost; + } else if (serviceBillingConfig.maxCost && cost > serviceBillingConfig.maxCost) { + cost = serviceBillingConfig.maxCost; + } + const balance = await this.getCreditBalance(appName, requesterAddress); + if (balance < cost) { + throw new Error("not enough balance"); + } + return cost; + } + async getCreditBalance(appName: string, userAddress: string) { const balancePath = Path.app(appName).balanceOfUser(userAddress); return await this.ain.db.ref(balancePath).getValue(); diff --git a/src/modules/service.ts b/src/modules/service.ts index f28b1ad..2128738 100644 --- a/src/modules/service.ts +++ b/src/modules/service.ts @@ -2,14 +2,17 @@ import ModuleBase from "./moduleBase"; import DepositService from "./service/depositService"; import UseService from "./service/useService"; import Ainize from "../ainize"; +import App from "./app"; export default class Service extends ModuleBase { private depositService: DepositService; private useService: UseService; + private app: App; constructor(ainize: Ainize, depositService: DepositService, useService: UseService) { super(ainize); this.depositService = depositService; this.useService = useService; + this.app = ainize.app; } /** @@ -33,7 +36,7 @@ export default class Service extends ModuleBase { * @returns RequestKey. You can use it to get response by handler. */ async writeRequest(appName: string, serviceName: string, prompt: string, userAddress?: string) { - await this.useService.calculateCostAndCheckBalance(appName, serviceName, prompt, userAddress); + await this.app.checkCostAndBalance(appName, serviceName, prompt, userAddress); return await this.useService.writeRequest(appName, serviceName, prompt, userAddress); } } diff --git a/src/modules/service/useService.ts b/src/modules/service/useService.ts index 45acc72..1aa364a 100644 --- a/src/modules/service/useService.ts +++ b/src/modules/service/useService.ts @@ -18,28 +18,6 @@ export default class UseService extends ServiceBase{ return requestKey; } - async calculateCostAndCheckBalance(appName: string, serviceName: string, value: string, requesterAddress?: string) { - requesterAddress = requesterAddress ? requesterAddress : this.wallet.getDefaultAddress(); - const billingConfig = await this.app.getBillingConfig(appName); - // TODO(woojae): calculate cost more accurately - let serviceBillingConfig = billingConfig.service.default; - if(billingConfig.service[serviceName]) { - serviceBillingConfig = billingConfig.service[serviceName]; - } - const token = value.split(' ').length; - let cost = token * serviceBillingConfig.costPerToken; - if (serviceBillingConfig.minCost && cost < serviceBillingConfig.minCost) { - cost = serviceBillingConfig.minCost; - } else if (serviceBillingConfig.maxCost && cost > serviceBillingConfig.maxCost) { - cost = serviceBillingConfig.maxCost; - } - const balance = await this.app.getCreditBalance(appName, requesterAddress); - if (balance < cost) { - throw new Error("not enough balance"); - } - return cost; - } - async writeResponse(response: response) { const { responseData, status, requesterAddress, requestKey, appName, serviceName, cost } = response; const responsePath = Path.app(appName).response(serviceName, requesterAddress, requestKey);