Skip to content

Commit

Permalink
Merge pull request #26 from ainize-team/feature/yoojin/app_module
Browse files Browse the repository at this point in the history
Add billingConfig and configOfService rule
  • Loading branch information
yoojinko authored Sep 8, 2023
2 parents 26faa8d + c17f28e commit 894bf6a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
31 changes: 24 additions & 7 deletions src/modules/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SetOperation } from "@ainblockchain/ain-js/lib/types";
import { Path } from "../constants";
import { billingConfig, setDefaultFlag, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig } from "../types/type";
import { appBillingConfig, setDefaultFlag, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig } from "../types/type";
import { buildSetOperation } from "../utils/builder";
import ModuleBase from "./moduleBase";

Expand Down Expand Up @@ -59,6 +59,23 @@ const defaultAppRules = (appName: string): { [type: string]: { ref: string, valu
}
},
},
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)",
}
}
}
}
}

Expand Down Expand Up @@ -123,7 +140,7 @@ export default class App extends ModuleBase {
}
}

const defaultConfig: billingConfig = {
const defaultConfig: appBillingConfig = {
depositAddress: this.ain.wallet.defaultAccount!.address,
service: {
default: {
Expand All @@ -147,10 +164,10 @@ export default class App extends ModuleBase {
/**
* Set billing config to app.
* @param {string} appName
* @param {billingConfig} config - The configuration of your app's billing.
* @param {appBillingConfig} config - The configuration of your app's billing.
* @returns Result of transaction.
*/
async setBillingConfig(appName: string, config: billingConfig) {
async setBillingConfig(appName: string, config: appBillingConfig) {
const setConfigOp = this.buildSetBillingConfigOp(appName, config);
const txBody = this.buildTxBody(setConfigOp);
return await this.sendTransaction(txBody);
Expand All @@ -159,9 +176,9 @@ export default class App extends ModuleBase {
/**
* Get billing config of app
* @param {string} appName
* @returns {Promise<billingConfig>}
* @returns {Promise<appBillingConfig>}
*/
async getBillingConfig(appName: string): Promise<billingConfig> {
async getBillingConfig(appName: string): Promise<appBillingConfig> {
return await this.ain.db.ref().getValue(Path.app(appName).billingConfig());
}

Expand Down Expand Up @@ -234,7 +251,7 @@ export default class App extends ModuleBase {
const balancePath = Path.app(appName).balanceOfUser(userAddress);
return await this.ain.db.ref(balancePath).getValue();
}
private buildSetBillingConfigOp(appName: string, config: billingConfig) {
private buildSetBillingConfigOp(appName: string, config: appBillingConfig) {
const path = Path.app(appName).billingConfig();
return buildSetOperation("SET_VALUE", path, config);
}
Expand Down
18 changes: 10 additions & 8 deletions src/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ export type setRuleParam = {
ref: string
} & writeRuleConfig;

export type billingConfig = {
depositAddress: string,
export type serviceBillingConfig = {
costPerToken: number;
minCost: number;
maxCost?: number;
responseTimeout?: number;
}

export type appBillingConfig = {
depositAddress: string;
service: {
[serviceName: string]: {
costPerToken: number,
minCost: number,
maxCost?: number,
responseTimeout?: number,
}
[serviceName: string]: serviceBillingConfig;
}
};

Expand Down

0 comments on commit 894bf6a

Please sign in to comment.