From 526fe8d046d00097308240327748ef9bcabc7995 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Tue, 12 Sep 2023 16:40:22 +0900 Subject: [PATCH 1/4] refactor: add req,res type --- src/modules/admin.ts | 24 ++++++++++++++---------- src/modules/service/useService.ts | 5 +++-- src/types/type.ts | 27 +++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/modules/admin.ts b/src/modules/admin.ts index 1891c40..629bb16 100644 --- a/src/modules/admin.ts +++ b/src/modules/admin.ts @@ -3,7 +3,7 @@ import { Request } from "express"; import ModuleBase from "./moduleBase"; import DepositService from "./service/depositService"; import UseService from "./service/useService"; -import { RESPONSE_STATUS } from "../types/type"; +import { RESPONSE_STATUS, requestData, response } from "../types/type"; export default class Admin extends ModuleBase { private depositService: DepositService; @@ -50,27 +50,31 @@ export default class Admin extends ModuleBase { * @returns Result of transaction. */ async writeResponse(req:Request, amount: number, responseData: string, status: RESPONSE_STATUS ) { - const appName = req.body.valuePath[1]; - const serviceName = req.body.valuePath[3]; - const requesterAddress = req.body.auth.addr; - const requestKey = req.body.valuePath[5]; - return await this.useService.writeResponse(status , appName, serviceName, requesterAddress, requestKey, responseData, amount); + const requestData = this.getDataFromServiceRequest(req); + const response: response = { + status: status, + amount: amount, + responseData: responseData, + ...requestData, + } + return await this.useService.writeResponse(response); } /** * Get data from service request. You should use it only with service trigger. * @param {Request} request - Request data from request trigger. If req data is not from trigger function, it will throw error. - * @returns Object with appName, serviceName, requesterAddress, requestKey, responseData. + * @returns RequestData type. */ getDataFromServiceRequest(req: Request) { if(!req.body.valuePath[1] || !req.body.valuePath[3] || !req.body.valuePath[5] || !req.body.value.prompt) { throw new Error("Not from service request"); } - return { + const requestData: requestData = { appName: req.body.valuePath[1], serviceName: req.body.valuePath[3], requesterAddress: req.body.auth.addr, requestKey: req.body.valuePath[5], - responseData: req.body.value.prompt, + prompt: req.body.value.prompt, } + return requestData; } -} +} \ No newline at end of file diff --git a/src/modules/service/useService.ts b/src/modules/service/useService.ts index 0fa93fe..f6ceb20 100644 --- a/src/modules/service/useService.ts +++ b/src/modules/service/useService.ts @@ -1,6 +1,6 @@ import { SetOperation } from "@ainblockchain/ain-js/lib/types"; import { Path } from "../../constants"; -import { HISTORY_TYPE, RESPONSE_STATUS } from "../../types/type"; +import { HISTORY_TYPE, RESPONSE_STATUS, response } from "../../types/type"; import { buildSetOperation } from "../../utils/builder"; import ServiceBase from "./serviceBase"; @@ -40,7 +40,8 @@ export default class UseService extends ServiceBase{ return amount; } - async writeResponse(status: RESPONSE_STATUS, appName: string, serviceName: string, requesterAddress: string, requestKey: string, responseData: string, amount: number) { + async writeResponse(response: response) { + const { responseData, status, requesterAddress, requestKey, appName, serviceName, amount } = response; const responsePath = Path.app(appName).response(serviceName, requesterAddress, requestKey); const responseValue = { status, diff --git a/src/types/type.ts b/src/types/type.ts index b16c174..a37f0b8 100644 --- a/src/types/type.ts +++ b/src/types/type.ts @@ -17,10 +17,10 @@ export type setRuleParam = { } & writeRuleConfig; export type serviceBillingConfig = { - costPerToken: number; - minCost: number; - maxCost?: number; - responseTimeout?: number; + costPerToken: number; + minCost: number; + maxCost?: number; + responseTimeout?: number; } export type appBillingConfig = { @@ -54,3 +54,22 @@ export enum RESPONSE_STATUS { SUCCESS = "SUCCESS", FAIL = "FAIL", } + +export type requestData = { + prompt: string, + requesterAddress: string, + requestKey: string, + serviceName: string, + appName: string, +}; + +export type response = { + responseData: string, + amount: number, + status: RESPONSE_STATUS, + prompt: string, + requesterAddress: string, + requestKey: string, + appName: string, + serviceName: string, +} \ No newline at end of file From 344e8c19e940edee5d3d9de27205a56dc605b5de Mon Sep 17 00:00:00 2001 From: akaster99 Date: Tue, 12 Sep 2023 16:41:37 +0900 Subject: [PATCH 2/4] refactor: requestData to request --- src/modules/admin.ts | 4 ++-- src/types/type.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/admin.ts b/src/modules/admin.ts index 629bb16..c83cf2e 100644 --- a/src/modules/admin.ts +++ b/src/modules/admin.ts @@ -3,7 +3,7 @@ import { Request } from "express"; import ModuleBase from "./moduleBase"; import DepositService from "./service/depositService"; import UseService from "./service/useService"; -import { RESPONSE_STATUS, requestData, response } from "../types/type"; +import { RESPONSE_STATUS, request, response } from "../types/type"; export default class Admin extends ModuleBase { private depositService: DepositService; @@ -68,7 +68,7 @@ export default class Admin extends ModuleBase { if(!req.body.valuePath[1] || !req.body.valuePath[3] || !req.body.valuePath[5] || !req.body.value.prompt) { throw new Error("Not from service request"); } - const requestData: requestData = { + const requestData: request = { appName: req.body.valuePath[1], serviceName: req.body.valuePath[3], requesterAddress: req.body.auth.addr, diff --git a/src/types/type.ts b/src/types/type.ts index a37f0b8..7853b5e 100644 --- a/src/types/type.ts +++ b/src/types/type.ts @@ -55,7 +55,7 @@ export enum RESPONSE_STATUS { FAIL = "FAIL", } -export type requestData = { +export type request = { prompt: string, requesterAddress: string, requestKey: string, From 7da0224de9ad5b76b2d1951c31819a280c8dc5f9 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Tue, 12 Sep 2023 16:42:33 +0900 Subject: [PATCH 3/4] refactor: prompt to requestData --- src/modules/admin.ts | 2 +- src/types/type.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/admin.ts b/src/modules/admin.ts index c83cf2e..789b446 100644 --- a/src/modules/admin.ts +++ b/src/modules/admin.ts @@ -73,7 +73,7 @@ export default class Admin extends ModuleBase { serviceName: req.body.valuePath[3], requesterAddress: req.body.auth.addr, requestKey: req.body.valuePath[5], - prompt: req.body.value.prompt, + requestData: req.body.value.prompt, } return requestData; } diff --git a/src/types/type.ts b/src/types/type.ts index 7853b5e..c95dfba 100644 --- a/src/types/type.ts +++ b/src/types/type.ts @@ -56,7 +56,7 @@ export enum RESPONSE_STATUS { } export type request = { - prompt: string, + requestData: string, requesterAddress: string, requestKey: string, serviceName: string, @@ -67,7 +67,7 @@ export type response = { responseData: string, amount: number, status: RESPONSE_STATUS, - prompt: string, + requestData: string, requesterAddress: string, requestKey: string, appName: string, From 71793cc7c0a36e6b02fbdacfe6a177f8171aadd2 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Wed, 13 Sep 2023 10:14:29 +0900 Subject: [PATCH 4/4] refactor: amount to cost --- src/middlewares/middleware.ts | 2 +- src/modules/admin.ts | 10 +++++----- src/modules/service/useService.ts | 20 ++++++++++---------- src/types/type.ts | 9 ++------- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/middlewares/middleware.ts b/src/middlewares/middleware.ts index 79d1f9e..e373036 100644 --- a/src/middlewares/middleware.ts +++ b/src/middlewares/middleware.ts @@ -11,7 +11,7 @@ export default class Middleware { * Middleware for AI Network trigger call. It will filter duplicated request triggered by same transaction. * It will pass request which is not from AI Network trigger. * @param {Request} request - Request data - * @param {Res} amount - Response data + * @param {Res} response - Response data * @param {NextFunction} next - Next function * @returns Null if if request is duplicated. */ diff --git a/src/modules/admin.ts b/src/modules/admin.ts index 789b446..49d7e10 100644 --- a/src/modules/admin.ts +++ b/src/modules/admin.ts @@ -44,17 +44,17 @@ export default class Admin extends ModuleBase { * Write response. Then change balance of requester and write history of user balance if response status is success. * You should match this function with service trigger. * @param {Request} request - Request data from request trigger. If req data is not from trigger function, it will throw error. - * @param {number} amount - Cost of service. Calculate it with checkCostAndBalance function. + * @param {number} cost - Cost of service. Calculate it with checkCostAndBalance function. * @param {string} responseData - Data you want to response to requester. * @param {RESPONSE_STATUS} status - Status of response. If status is success, it will change balance of requester and write history of user balance. * @returns Result of transaction. */ - async writeResponse(req:Request, amount: number, responseData: string, status: RESPONSE_STATUS ) { + async writeResponse(req:Request, cost: number, responseData: string, status: RESPONSE_STATUS ) { const requestData = this.getDataFromServiceRequest(req); const response: response = { - status: status, - amount: amount, - responseData: responseData, + status, + cost, + responseData, ...requestData, } return await this.useService.writeResponse(response); diff --git a/src/modules/service/useService.ts b/src/modules/service/useService.ts index f6ceb20..45acc72 100644 --- a/src/modules/service/useService.ts +++ b/src/modules/service/useService.ts @@ -27,21 +27,21 @@ export default class UseService extends ServiceBase{ serviceBillingConfig = billingConfig.service[serviceName]; } const token = value.split(' ').length; - let amount = token * serviceBillingConfig.costPerToken; - if (serviceBillingConfig.minCost && amount < serviceBillingConfig.minCost) { - amount = serviceBillingConfig.minCost; - } else if (serviceBillingConfig.maxCost && amount > serviceBillingConfig.maxCost) { - amount = serviceBillingConfig.maxCost; + 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 < amount) { + if (balance < cost) { throw new Error("not enough balance"); } - return amount; + return cost; } async writeResponse(response: response) { - const { responseData, status, requesterAddress, requestKey, appName, serviceName, amount } = response; + const { responseData, status, requesterAddress, requestKey, appName, serviceName, cost } = response; const responsePath = Path.app(appName).response(serviceName, requesterAddress, requestKey); const responseValue = { status, @@ -51,8 +51,8 @@ export default class UseService extends ServiceBase{ const responseOp = buildSetOperation("SET_VALUE", responsePath, responseValue); ops.push(responseOp); if (status === RESPONSE_STATUS.SUCCESS) { - const changeBalanceOp = await this.getChangeBalanceOp(appName, requesterAddress, 'DEC_VALUE', amount); - const writeHistoryOp = await this.getWriteHistoryOp(appName, requesterAddress, HISTORY_TYPE.USAGE, amount, requestKey); + const changeBalanceOp = await this.getChangeBalanceOp(appName, requesterAddress, 'DEC_VALUE', cost); + const writeHistoryOp = await this.getWriteHistoryOp(appName, requesterAddress, HISTORY_TYPE.USAGE, cost, requestKey); ops.push(changeBalanceOp); ops.push(writeHistoryOp); } diff --git a/src/types/type.ts b/src/types/type.ts index c95dfba..5830713 100644 --- a/src/types/type.ts +++ b/src/types/type.ts @@ -63,13 +63,8 @@ export type request = { appName: string, }; -export type response = { +export type response = request & { responseData: string, - amount: number, + cost: number, status: RESPONSE_STATUS, - requestData: string, - requesterAddress: string, - requestKey: string, - appName: string, - serviceName: string, } \ No newline at end of file