Skip to content

Commit

Permalink
Merge pull request #57 from ainize-team/docs/yoojin/typedocs
Browse files Browse the repository at this point in the history
Add ainize doc
  • Loading branch information
yoojinko authored Sep 21, 2023
2 parents 40ef701 + f43e185 commit 8476ab1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 45 deletions.
25 changes: 24 additions & 1 deletion src/ainize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Model from "./model";
import { deployConfig } from "./types/type";
import AinModule from "./ain";
import Internal from "./internal";
import { Account } from "@ainblockchain/ain-util";

export default class Ainize {
private cache: NodeCache;
Expand All @@ -23,16 +24,27 @@ export default class Ainize {
this.internal = new Internal();
}

static createAinAccount () {
/**
* Create a new AI Network account.
* @returns {Account} created account.
*/
static createAinAccount (): Account {
return AinModule.getInstance().createAccount();
}

/**
* Login to ainize using AI Network account private key.
* @param {string} privateKey
*/
async login(privateKey: string) {
this.ain.setDefaultAccount(privateKey);
await this.handler.connect();
console.log('login success! address:', this.ain.getAddress());
}

/**
* Logout from ainize.
*/
async logout() {
this.ain.removeDefaultAccount();
await this.handler.disconnect();
Expand All @@ -48,11 +60,17 @@ export default class Ainize {
}

// FIXME(yoojin): add config type and change param type.
/**
* Deploy AI model container.
* @param {deployConfig} deployConfig Set configuration for setting container. modelName, billingConfig, etc.
* @returns {Model} Deployed model object.
*/
// TODO(yoojin, woojae): Deploy container, advanced.
async deploy({modelName, billingConfig, serviceUrl}: deployConfig): Promise<Model> {
if(!this.ain.isDefaultAccountExist()) {
throw new Error('you should login first');
}
// TODO(yoojin, woojae): Add container deploy logic.
const result = await new Promise(async (resolve, reject) => {
const deployer = this.ain.getAddress();
if (!billingConfig) {
Expand All @@ -73,6 +91,11 @@ export default class Ainize {
return this.model(modelName);
}

/**
* Get deployed model.
* @param modelName
* @returns {Model} Deployed model object.
*/
async model(modelName: string): Promise<Model> {
const modelPath = Path.app(modelName).root();
const modelData = await this.ain.getValue(modelPath);
Expand Down
44 changes: 0 additions & 44 deletions src/controllers/appController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ export default class AppController {
return AppController.instance;
}

/**
* Create App for your AI Service on AI Network.
* @param {string} appName - The name of app you will create.
* @param {TriggerFunctionUrlMap} functioniUrls - The urls of trigger function you set.
* @param {setDefaultFlag} setDefaultFlag - Set true which you wan to set config as default.
* @returns Result of transaction.
*/
async createApp({ appName, serviceUrl, billingConfig }: createAppConfig) {
const setRuleOps: SetOperation[] = [];
const setFunctionOps: SetOperation[] = [];
Expand Down Expand Up @@ -74,33 +67,16 @@ export default class AppController {
return await this.ain.sendTransaction(txBody);
}

/**
* Set billing config to app.
* @param {string} appName
* @param {appBillingConfig} config - The configuration of your app's billing.
* @returns Result of transaction.
*/
async setAppBillingConfig(appName: string, config: appBillingConfig) {
const setConfigOp = this.buildSetAppBillingConfigOp(appName, config);
const txBody = buildTxBody(setConfigOp);
return await this.ain.sendTransaction(txBody);
}

/**
* Get billing config of app
* @param {string} appName
* @returns {Promise<appBillingConfig>}
*/
async getBillingConfig(appName: string): Promise<appBillingConfig> {
return await this.ain.getValue(Path.app(appName).billingConfig());
}

/**
* Set trigger function to app.
* @param {string} appName
* @param {setTriggerFunctionParam[]} functions
* @returns Result of transaction.
*/
async setTriggerFunctions(appName: string, functions: setTriggerFunctionParm[]) {
const setFunctionOps: SetOperation[] = [];
for (const param of Object.values(functions)) {
Expand All @@ -117,12 +93,6 @@ export default class AppController {
return await this.ain.sendTransaction(txBody);
}

/**
* Set rules to app.
* @param {string} appName
* @param {setRuleParam} rules
* @returns Result of transaction.
*/
async setRules(appName: string, rules: setRuleParam[]) {
const setRuleOps: SetOperation[] = [];
for (const rule of Object.values(rules)) {
Expand Down Expand Up @@ -153,26 +123,12 @@ export default class AppController {
return await this.ain.sendTransaction(txBody);
}

/**
* Remove admin on app.
* @param {string} appName
* @param {string} userAddress
* @returns Result of transaction.
*/
async deleteAdmin(appName: string, userAddress: string) {
const op = this.buildSetAdminOp(appName, userAddress, true);
const txBody = buildTxBody(op);
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.
* @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, value: string) {
const requesterAddress = this.ain.getAddress();
const billingConfig = (await this.getBillingConfig(appName));
Expand Down

0 comments on commit 8476ab1

Please sign in to comment.