From 6f8bc20012edf1d04b7acd55bdb875d77d51b6b4 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Mon, 11 Sep 2023 09:12:54 +0900 Subject: [PATCH 1/5] fix: remove default set flag --- src/modules/app.ts | 19 +++++++------------ src/types/type.ts | 5 ----- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/modules/app.ts b/src/modules/app.ts index 23932b1..3b09afb 100644 --- a/src/modules/app.ts +++ b/src/modules/app.ts @@ -4,7 +4,6 @@ import { appBillingConfig, setDefaultFlag, setRuleParam, setTriggerFunctionParm, import { buildSetOperation } from "../utils/builder"; import ModuleBase from "./moduleBase"; -// FIXME(yoojin): move to constant. const defaultAppRules = (appName: string): { [type: string]: { ref: string, value: object } } => { const rootRef = Path.app(appName).root(); return { @@ -115,9 +114,7 @@ export default class App extends ModuleBase { * @returns Result of transaction. */ // FIXME(yoojin): need to fix getting function urls. - async create(appName: string, functionUrls: TriggerFunctionUrlMap, setDefaultFlag?: setDefaultFlag) { - if (!setDefaultFlag) - setDefaultFlag = { triggerFuncton: true }; + async create(appName: string, functionUrls: TriggerFunctionUrlMap) { const setRuleOps: SetOperation[] = []; const setFunctionOps: SetOperation[] = []; const setBillingConfigOps: SetOperation[] = [] ; @@ -130,14 +127,12 @@ export default class App extends ModuleBase { setRuleOps.push(ruleOp); } - if (setDefaultFlag.triggerFuncton) { - const defaultFunctions = defaultAppFunctions(appName); - for (const [type, func] of Object.entries(defaultFunctions)) { - const { ref, function_id, function_type, function_url } = func(functionUrls[type]); - const value = this.buildSetFunctionValue({function_id, function_type, function_url}); - const funcOp = buildSetOperation("SET_FUNCTION", ref, value); - setFunctionOps.push(funcOp); - } + const defaultFunctions = defaultAppFunctions(appName); + for (const [type, func] of Object.entries(defaultFunctions)) { + const { ref, function_id, function_type, function_url } = func(functionUrls[type]); + const value = this.buildSetFunctionValue({function_id, function_type, function_url}); + const funcOp = buildSetOperation("SET_FUNCTION", ref, value); + setFunctionOps.push(funcOp); } const defaultConfig: appBillingConfig = { diff --git a/src/types/type.ts b/src/types/type.ts index 4041af1..964e040 100644 --- a/src/types/type.ts +++ b/src/types/type.ts @@ -1,8 +1,3 @@ -export type setDefaultFlag = { - triggerFuncton: boolean, -}; - -// NOTE(yoojin): pls suggest good name. export type triggerFunctionConfig = { function_type: string, function_url: string, From dc15174513112f3c0dd354241a74d75d5d1785c8 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Mon, 11 Sep 2023 09:14:21 +0900 Subject: [PATCH 2/5] fix: move default set functions in app module --- src/modules/app.ts | 196 +++++++++++++++++++++++---------------------- 1 file changed, 100 insertions(+), 96 deletions(-) diff --git a/src/modules/app.ts b/src/modules/app.ts index 3b09afb..c3a8571 100644 --- a/src/modules/app.ts +++ b/src/modules/app.ts @@ -1,103 +1,12 @@ import { SetOperation } from "@ainblockchain/ain-js/lib/types"; import { Path } from "../constants"; -import { appBillingConfig, setDefaultFlag, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig } from "../types/type"; +import { appBillingConfig, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig } from "../types/type"; import { buildSetOperation } from "../utils/builder"; import ModuleBase from "./moduleBase"; -const defaultAppRules = (appName: string): { [type: string]: { ref: string, value: object } } => { - const rootRef = Path.app(appName).root(); - return { - root: { - ref: rootRef, - value: { - ".rule": { - write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true" - } - } - }, - deposit: { - ref: `${Path.app(appName).depositOfUser("$userAddress")}/$transferKey`, - value: { - ".rule": { - write: "data === null && util.isNumber(newData) && getValue(`/transfer/` + $userAddress + `/` + getValue(`/apps/" + `${appName}` + "/billingConfig/depositAddress`) + `/` + $transferKey + `/value`) === newData" - } - } - }, - balance: { - ref: Path.app(appName).balanceOfUser("$userAddress"), - value: { - ".rule": { - write: "(util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true) && util.isNumber(newData)" - } - } - }, - balanceHistory: { - ref: `${rootRef}/balance/$userAddress/history/$timestamp_and_type`, - value: { - ".rule": { - write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && util.isNumber(newData.amount) && (newData.type === 'DEPOSIT' || newData.type === 'USAGE')" - } - } - }, - request: { - ref: Path.app(appName).request("$serviceName", "$userAddress", "$requestKey"), - value: { - ".rule": { - write: - "auth.addr === $userAddress && getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) !== null && " + - "((getValue(`/apps/" + `${appName}` + "/billingConfig/` + $serviceName) !== null) && (getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) >= getValue(`/apps/" + `${appName}` + "/billingConfig/` + $serviceName + `/minCost`)) || " + - "getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) >= getValue(`/apps/" + `${appName}` + "/billingConfig/service/default/minCost`)" - } - } - }, - response: { - ref: Path.app(appName).response("$serviceName", "userAddress", "$requestKey"), - value: { - ".rule": { - write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && util.isString(newData.status)" - } - }, - }, - billingConfig: { - ref: Path.app(appName).billingConfig(), - value: { - ".rule": { - write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && " + - "util.isString(newData.depositAddress) && util.isDict(newData.service) && util.isDict(newData.service.default)", - } - } - }, - billingConfigOfService: { - ref: Path.app(appName).billingConfigOfService("$serviceName"), - value: { - ".rule": { - write: "util.isAppAdmin(`" + `${appName}` + "`, auth,addr, getValue) === true && util.isDict(newData) && util.isNumber(newData.minCost)", - } - } - } - } -} -const defaultAppFunctions = (appName: string) => { - return { - deposit: (url: string) => { - return { - ref: `${Path.app(appName).depositOfUser("$userAddress")}/$transferKey`, - function_type: "REST", - function_id: "deposit-trigger", - function_url: url, - } - }, - service: (url: string) => { - return { - ref: Path.app(appName).request("$serviceName", "$userAddress", "$requestKey"), - function_type: "REST", - function_id: "service-trigger", - function_url: url, - } - } - } -} + + // FIXME(yoojin): move to types. // NOTE(yoojin): temporary type. service url may be changed to array? @@ -120,14 +29,14 @@ export default class App extends ModuleBase { const setBillingConfigOps: SetOperation[] = [] ; const createAppOp = this.buildCreateAppOp(appName); - const defaultRules = defaultAppRules(appName); + const defaultRules = this.defaultAppRules(appName); for (const rule of Object.values(defaultRules)) { const { ref, value } = rule; const ruleOp = buildSetOperation("SET_RULE", ref, value); setRuleOps.push(ruleOp); } - const defaultFunctions = defaultAppFunctions(appName); + const defaultFunctions = this.defaultAppFunctions(appName); for (const [type, func] of Object.entries(defaultFunctions)) { const { ref, function_id, function_type, function_url } = func(functionUrls[type]); const value = this.buildSetFunctionValue({function_id, function_type, function_url}); @@ -283,4 +192,99 @@ export default class App extends ModuleBase { const value = !isRemoveOp ? null : true; return buildSetOperation("SET_VALUE", path, value); } + + private defaultAppRules = (appName: string): { [type: string]: { ref: string, value: object } } => { + const rootRef = Path.app(appName).root(); + return { + root: { + ref: rootRef, + value: { + ".rule": { + write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true" + } + } + }, + deposit: { + ref: `${Path.app(appName).depositOfUser("$userAddress")}/$transferKey`, + value: { + ".rule": { + write: "data === null && util.isNumber(newData) && getValue(`/transfer/` + $userAddress + `/` + getValue(`/apps/" + `${appName}` + "/billingConfig/depositAddress`) + `/` + $transferKey + `/value`) === newData" + } + } + }, + balance: { + ref: Path.app(appName).balanceOfUser("$userAddress"), + value: { + ".rule": { + write: "(util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true) && util.isNumber(newData)" + } + } + }, + balanceHistory: { + ref: `${rootRef}/balance/$userAddress/history/$timestamp_and_type`, + value: { + ".rule": { + write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && util.isNumber(newData.amount) && (newData.type === 'DEPOSIT' || newData.type === 'USAGE')" + } + } + }, + request: { + ref: Path.app(appName).request("$serviceName", "$userAddress", "$requestKey"), + value: { + ".rule": { + write: + "auth.addr === $userAddress && getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) !== null && " + + "((getValue(`/apps/" + `${appName}` + "/billingConfig/` + $serviceName) !== null) && (getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) >= getValue(`/apps/" + `${appName}` + "/billingConfig/` + $serviceName + `/minCost`)) || " + + "getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) >= getValue(`/apps/" + `${appName}` + "/billingConfig/service/default/minCost`)" + } + } + }, + response: { + ref: Path.app(appName).response("$serviceName", "userAddress", "$requestKey"), + value: { + ".rule": { + write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && util.isString(newData.status)" + } + }, + }, + billingConfig: { + ref: Path.app(appName).billingConfig(), + value: { + ".rule": { + write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && " + + "util.isString(newData.depositAddress) && util.isDict(newData.service) && util.isDict(newData.service.default)", + } + } + }, + billingConfigOfService: { + ref: Path.app(appName).billingConfigOfService("$serviceName"), + value: { + ".rule": { + write: "util.isAppAdmin(`" + `${appName}` + "`, auth,addr, getValue) === true && util.isDict(newData) && util.isNumber(newData.minCost)", + } + } + } + } + } + + private defaultAppFunctions = (appName: string) => { + return { + deposit: (url: string) => { + return { + ref: `${Path.app(appName).depositOfUser("$userAddress")}/$transferKey`, + function_type: "REST", + function_id: "deposit-trigger", + function_url: url, + } + }, + service: (url: string) => { + return { + ref: Path.app(appName).request("$serviceName", "$userAddress", "$requestKey"), + function_type: "REST", + function_id: "service-trigger", + function_url: url, + } + } + } + } } From 10cdf2dba1ba77f23d3cdd7f4056b21699aaac25 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Mon, 11 Sep 2023 09:16:19 +0900 Subject: [PATCH 3/5] fix: move trigger function url map to types --- src/modules/app.ts | 12 +----------- src/types/type.ts | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/modules/app.ts b/src/modules/app.ts index c3a8571..e1308f6 100644 --- a/src/modules/app.ts +++ b/src/modules/app.ts @@ -1,19 +1,9 @@ import { SetOperation } from "@ainblockchain/ain-js/lib/types"; import { Path } from "../constants"; -import { appBillingConfig, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig } from "../types/type"; +import { appBillingConfig, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig, TriggerFunctionUrlMap } from "../types/type"; import { buildSetOperation } from "../utils/builder"; import ModuleBase from "./moduleBase"; - - - - -// FIXME(yoojin): move to types. -// NOTE(yoojin): temporary type. service url may be changed to array? -interface TriggerFunctionUrlMap { - [type: string]: string -} - export default class App extends ModuleBase { /** * Create App for your AI Service on AI Network. diff --git a/src/types/type.ts b/src/types/type.ts index 964e040..e1fe44b 100644 --- a/src/types/type.ts +++ b/src/types/type.ts @@ -1,19 +1,25 @@ export type triggerFunctionConfig = { - function_type: string, - function_url: string, - function_id: string, + function_type: string; + function_url: string; + function_id: string; }; +export type TriggerFunctionUrlMap = { + deposit: string; + service: string; + [type: string]: string; +} + export type setTriggerFunctionParm = { - ref: string + ref: string; } & triggerFunctionConfig; export type writeRuleConfig = { - write: string, + write: string; }; export type setRuleParam = { - ref: string + ref: string; } & writeRuleConfig; export type serviceBillingConfig = { @@ -36,9 +42,9 @@ export enum HISTORY_TYPE { } export type opResult = { - code: number, + code: number; bandwidth_gas_amount: number; - message?: string + message?: string; } export type txResult = { From ecf04503e42c9d9fbaeb38432363922c8e94aff0 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Mon, 11 Sep 2023 10:29:10 +0900 Subject: [PATCH 4/5] refactor: remove service tf set when app create --- src/modules/app.ts | 39 ++++++++++++--------------------------- src/types/type.ts | 6 ------ 2 files changed, 12 insertions(+), 33 deletions(-) diff --git a/src/modules/app.ts b/src/modules/app.ts index e1308f6..5818c76 100644 --- a/src/modules/app.ts +++ b/src/modules/app.ts @@ -1,6 +1,6 @@ import { SetOperation } from "@ainblockchain/ain-js/lib/types"; import { Path } from "../constants"; -import { appBillingConfig, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig, TriggerFunctionUrlMap } from "../types/type"; +import { appBillingConfig, defaultTriggerFunctionParam, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig } from "../types/type"; import { buildSetOperation } from "../utils/builder"; import ModuleBase from "./moduleBase"; @@ -13,7 +13,7 @@ export default class App extends ModuleBase { * @returns Result of transaction. */ // FIXME(yoojin): need to fix getting function urls. - async create(appName: string, functionUrls: TriggerFunctionUrlMap) { + async create(appName: string, serviceUrl: string) { const setRuleOps: SetOperation[] = []; const setFunctionOps: SetOperation[] = []; const setBillingConfigOps: SetOperation[] = [] ; @@ -26,14 +26,11 @@ export default class App extends ModuleBase { setRuleOps.push(ruleOp); } - const defaultFunctions = this.defaultAppFunctions(appName); - for (const [type, func] of Object.entries(defaultFunctions)) { - const { ref, function_id, function_type, function_url } = func(functionUrls[type]); - const value = this.buildSetFunctionValue({function_id, function_type, function_url}); - const funcOp = buildSetOperation("SET_FUNCTION", ref, value); - setFunctionOps.push(funcOp); - } - + const { ref, function_id, function_type, function_url } = this.depositTriggerFunction(appName, serviceUrl); + const value = this.buildSetFunctionValue({ function_id, function_type, function_url }); + const funcOp = buildSetOperation("SET_FUNCTION", ref, value); + setFunctionOps.push(funcOp); + const defaultConfig: appBillingConfig = { depositAddress: this.ain.wallet.defaultAccount!.address, service: { @@ -257,24 +254,12 @@ export default class App extends ModuleBase { } } - private defaultAppFunctions = (appName: string) => { + private depositTriggerFunction = (appName: string, serviceUrl: string) => { return { - deposit: (url: string) => { - return { - ref: `${Path.app(appName).depositOfUser("$userAddress")}/$transferKey`, - function_type: "REST", - function_id: "deposit-trigger", - function_url: url, - } - }, - service: (url: string) => { - return { - ref: Path.app(appName).request("$serviceName", "$userAddress", "$requestKey"), - function_type: "REST", - function_id: "service-trigger", - function_url: url, - } - } + ref: `${Path.app(appName).depositOfUser("$userAddress")}/$transferKey`, + function_type: "REST", + function_id: "deposit-trigger", + function_url: `${serviceUrl}/deposit`, } } } diff --git a/src/types/type.ts b/src/types/type.ts index e1fe44b..b16c174 100644 --- a/src/types/type.ts +++ b/src/types/type.ts @@ -4,12 +4,6 @@ export type triggerFunctionConfig = { function_id: string; }; -export type TriggerFunctionUrlMap = { - deposit: string; - service: string; - [type: string]: string; -} - export type setTriggerFunctionParm = { ref: string; } & triggerFunctionConfig; From 24a7fea46fd2c207d73260ceb518b2fd8ec1c800 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Mon, 11 Sep 2023 10:32:13 +0900 Subject: [PATCH 5/5] refactor: param --- src/modules/app.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/modules/app.ts b/src/modules/app.ts index 5818c76..04233f9 100644 --- a/src/modules/app.ts +++ b/src/modules/app.ts @@ -1,6 +1,6 @@ import { SetOperation } from "@ainblockchain/ain-js/lib/types"; import { Path } from "../constants"; -import { appBillingConfig, defaultTriggerFunctionParam, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig } from "../types/type"; +import { appBillingConfig, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig } from "../types/type"; import { buildSetOperation } from "../utils/builder"; import ModuleBase from "./moduleBase"; @@ -26,9 +26,9 @@ export default class App extends ModuleBase { setRuleOps.push(ruleOp); } - const { ref, function_id, function_type, function_url } = this.depositTriggerFunction(appName, serviceUrl); - const value = this.buildSetFunctionValue({ function_id, function_type, function_url }); - const funcOp = buildSetOperation("SET_FUNCTION", ref, value); + const depositParam = this.depositTriggerFunctionConfig(appName, serviceUrl); + const value = this.buildSetFunctionValue(depositParam); + const funcOp = buildSetOperation("SET_FUNCTION", depositParam.ref, value); setFunctionOps.push(funcOp); const defaultConfig: appBillingConfig = { @@ -81,10 +81,9 @@ export default class App extends ModuleBase { */ async setTriggerFunctions(appName: string, functions: setTriggerFunctionParm[]) { const setFunctionOps: SetOperation[] = []; - for (const func of Object.values(functions)) { - const { ref } = func; - const value = this.buildSetFunctionValue(func); - const op = buildSetOperation("SET_FUNCTION", ref, value); + for (const param of Object.values(functions)) { + const value = this.buildSetFunctionValue(param); + const op = buildSetOperation("SET_FUNCTION", param.ref, value); setFunctionOps.push(op); } if (setFunctionOps.length <= 0) { @@ -254,7 +253,7 @@ export default class App extends ModuleBase { } } - private depositTriggerFunction = (appName: string, serviceUrl: string) => { + private depositTriggerFunctionConfig = (appName: string, serviceUrl: string): setTriggerFunctionParm => { return { ref: `${Path.app(appName).depositOfUser("$userAddress")}/$transferKey`, function_type: "REST",