Skip to content

Commit

Permalink
Merge pull request #44 from ainize-team/feature/yoojin/deployConfig
Browse files Browse the repository at this point in the history
Add deploy config and set contaner status
  • Loading branch information
yoojinko authored Sep 19, 2023
2 parents 557ae07 + cd9cf07 commit 766c2b8
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 32 deletions.
25 changes: 19 additions & 6 deletions src/ainize.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import Ain from "@ainblockchain/ain-js";
import * as NodeCache from "node-cache";
import Middleware from "./middlewares/middleware";
import { getBlockChainEndpoint } from "./constants";
import { DEFAULT_BILLING_CONFIG, getBlockChainEndpoint } from "./constants";
import Handler from "./handlers/handler";
import AppController from "./controller/appController";
import AppController from "./controllers/appController";
import Model from "./model";
import { deployConfig } from "./types/type";
import AinModule from "./ain";
export default class Ainize {
private cache: NodeCache;
ain: Ain;
middleware: Middleware;
handler: Handler;
appController: AppController = AppController.getInstance();

constructor(chainId: 1|0) {
constructor(chainId: 1 | 0) {
const blockChainEndpoint = getBlockChainEndpoint(chainId);
this.ain = new Ain(blockChainEndpoint, chainId);
this.cache = new NodeCache();
Expand All @@ -21,10 +23,21 @@ export default class Ainize {
}

// FIXME(yoojin): add config type and change param type.
deploy(modelName: string, config: any) {
deploy({modelName, billingConfig, serviceUrl}: deployConfig) {
// TODO(yoojin, woojae): Deploy container, advanced.
// TODO(yoojin): add createApp
// this.appController.createApp(modelName, )
const deployer = AinModule.getInstance().getAddress();
if (!billingConfig) {
billingConfig = {
...DEFAULT_BILLING_CONFIG,
depositAddress: deployer,
}
}
// NOTE(yoojin): For test. We make fixed url on service.
if (!serviceUrl) {
serviceUrl = `https://${modelName}.ainetwork.xyz`;
}

this.appController.createApp({ appName: modelName, serviceUrl, billingConfig })
}

model(modelName: string) {
Expand Down
37 changes: 22 additions & 15 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const getBlockChainEndpoint = (chainId:number) =>{
import { appBillingConfig } from "./types/type"

export const getBlockChainEndpoint = (chainId: number) =>{
return chainId === 1 ? "https://mainnet-event.ainetwork.ai" : "https://testnet-event.ainetwork.ai"
}

Expand Down Expand Up @@ -33,32 +35,32 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin
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) && newData > 0",
}
}
},
},
},
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("$userAddress", "$requestKey"),
Expand All @@ -67,15 +69,15 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin
write:
"auth.addr === $userAddress && getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) !== null && " +
"(!util.isEmpty(getValue(`/apps/" + `${appName}` + "/billingConfig/minCost`))) && (getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) >= getValue(`/apps/" + `${appName}` + "/billingConfig/minCost`))"
}
}
},
},
},
response: {
ref: Path.app(appName).response("userAddress", "$requestKey"),
value: {
".rule": {
write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && util.isString(newData.status)"
}
},
},
},
billingConfig: {
Expand All @@ -85,11 +87,16 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin
write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && util.isString(newData.depositAddress) && " +
"util.isDict(newData.service) && util.isDict(newData.service.default) && util.isNumber(newData.service.default.costPerToken) && util.isNumber(newData.service.default.minCost) && " +
"util.isEmpty(newData.service.default.maxCost) || (util.isNumber(newData.service.default.maxCost) && newData.service.default.maxCost >= newData.service.default.minCost)",
}
}
},
},
},
}
};
}

export const DEFAULT_BILLING_CONFIG: Omit<appBillingConfig, "depositAddress"> = {
costPerToken: 0,
minCost: 0,
};

export const SECOND = 1000;
export const HANDLER_HEARBEAT_INTERVAL = 15 * SECOND;
28 changes: 18 additions & 10 deletions src/controllers/appController.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, defaultAppRules } from "../constants";
import { appBillingConfig, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig } from "../types/type";
import { ContainerStatus, appBillingConfig, createAppConfig, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig } from "../types/type";
import { buildSetOperation, buildTxBody } from "../utils/builder";
import AinModule from '../ain';

Expand All @@ -22,7 +22,7 @@ export default class AppController {
* @param {setDefaultFlag} setDefaultFlag - Set true which you wan to set config as default.
* @returns Result of transaction.
*/
async createApp(appName: string, serviceUrl: string) {
async createApp({ appName, serviceUrl, billingConfig }: createAppConfig) {
const setRuleOps: SetOperation[] = [];
const setFunctionOps: SetOperation[] = [];
const setBillingConfigOps: SetOperation[] = [] ;
Expand All @@ -39,20 +39,17 @@ export default class AppController {
const value = this.buildSetFunctionValue(depositParam);
const funcOp = buildSetOperation("SET_FUNCTION", depositParam.ref, value);
setFunctionOps.push(funcOp);
const depositAddress = this.ain.getDefaultAccount()!.address;
const defaultConfig: appBillingConfig = {
depositAddress,
costPerToken: 0,
minCost: 0,
}
const configOp = this.buildSetAppBillingConfigOp(appName, defaultConfig);
const configOp = this.buildSetAppBillingConfigOp(appName, billingConfig);
setBillingConfigOps.push(configOp);

const statusOp = this.buildSetContainerStatusOp(appName, ContainerStatus.RUNNING);

const txBody = buildTxBody([
createAppOp,
...setRuleOps,
...setFunctionOps,
...setBillingConfigOps,
statusOp,
]);
return await this.ain.sendTransaction(txBody);
}
Expand Down Expand Up @@ -118,6 +115,12 @@ export default class AppController {
return await this.ain.sendTransaction(txBody);
}

async setContainerStatus(appName: string, status: ContainerStatus) {
const op = this.buildSetContainerStatusOp(appName, status);
const txBody = buildTxBody(op);
return await this.ain.sendTransaction(txBody);
}

/**
* Add admin on app.
* @param {string} appName
Expand All @@ -142,7 +145,7 @@ export default class AppController {
return await this.ain.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.
Expand Down Expand Up @@ -172,6 +175,11 @@ export default class AppController {
return await this.ain.getValue(balancePath);
}

private buildSetContainerStatusOp(appName: string, status: ContainerStatus) {
const path = Path.app(appName).status();
return buildSetOperation("SET_VALUE", path, status);
}

private buildSetAppBillingConfigOp(appName: string, config: appBillingConfig) {
const path = Path.app(appName).billingConfig();
return buildSetOperation("SET_VALUE", path, config);
Expand Down
19 changes: 18 additions & 1 deletion src/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,21 @@ export type deposit = {
transferValue: number,
appName: string,
requesterAddress: string,
}
}

export type deployConfig = {
modelName: string,
billingConfig?: appBillingConfig,
serviceUrl?: string, // NOTE(yoojin): for test.
}

export type createAppConfig = {
appName: string,
billingConfig: appBillingConfig,
serviceUrl: string,
}

export enum ContainerStatus {
RUNNING = "RUNNING",
STOP = "STOP",
}

0 comments on commit 766c2b8

Please sign in to comment.