Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade version to 1.0.1 #68

Merged
merged 14 commits into from
Nov 1, 2023
Merged
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,37 @@ ainize.login(<YOUR_PRIVATE_KEY>);
```

### Deploy
You can deploy your AI model to ainize.
You can deploy your AI service to ainize.
```typescript
const model = await ainize.deploy(<CONFIGURATION>);
const service = await ainize.deploy(<CONFIGURATION>);
```
CONFIGURATION
- modelName: The name you want to deploying model.
- billingConfig: Billing configuration required for model usage.
- serviceName: The name you want to deploying service.
- billingConfig: Billing configuration required for service usage.
- depositAddress: The address for receiving AIN deposits.
- costPerToken: Cost per token for model usage.
- costPerToken: Cost per token for service usage.
- minCost: Minimum cost.
- maxCost: Maximum cost. (optional)

You can stop or run your model container. Model deployer only can use this.
You can stop or run your service container. Only service deployer can use this.
```typescript
model.stop();
model.run();
service.stop();
service.run();
```

### Using Model
You can use a model using `ainize.model(<MODEL_NAME>)`.
### Using Service
You can use a service using `ainize.service(<SERVICE_NAME>)`.
```typescript
const model = await ainize.model(<MODEL_NAME>);
const service = await ainize.service(<SERVICE_NAME>);
```

You should deposit AIN to credit before using model.
You should deposit AIN to credit before using service.
```typescript
await model.deposit(<AMOUNT>);
const balance = await model.getCreditBalance();
await service.chargeCredit(<AMOUNT>);
const balance = await service.getCreditBalance();
```

If you have enough credit, you can use the model.
If you have enough credit, you can use the service.
```typescript
const result = await model.use(<REQUEST_DATA>);
const result = await service.use(<REQUEST_DATA>);
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ainize-team/ainize-sdk",
"version": "1.0.0",
"version": "1.0.1",
"main": "dist/ainize.js",
"types": "dist/ainize.d.ts",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions src/ainize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as NodeCache from "node-cache";
import NodeCache from "node-cache";
import Middleware from "./middlewares/middleware";
import { DEFAULT_BILLING_CONFIG, Path, getBlockChainEndpoint } from "./constants";
import { DEFAULT_BILLING_CONFIG, Path } from "./constants";
import Handler from "./handlers/handler";
import AppController from "./controllers/appController";
import Service from "./service";
Expand All @@ -13,9 +13,9 @@ export default class Ainize {
private cache: NodeCache;
private handler: Handler = Handler.getInstance();
private ain: AinModule = AinModule.getInstance();
private appController: AppController = AppController.getInstance();
middleware: Middleware;
internal: Internal;
private appController: AppController = AppController.getInstance();

constructor(chainId: 1 | 0) {
this.ain.initAin(chainId);
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin
ref: Path.app(appName).balanceOfUser("$userAddress"),
value: {
".rule": {
write: "(util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true) && util.isNumber(newData) && newData > 0",
write: "(util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true) && util.isNumber(newData) && newData >= 0",
},
},
},
Expand Down
6 changes: 0 additions & 6 deletions src/controllers/appController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,6 @@ export default class AppController {
return await this.ain.sendTransaction(txBody);
}

/**
* Add admin on app.
* @param {string} appName
* @param {string} userAddress
* @returns Result of transaction.
*/
async addAdmin(appName: string, userAddress: string) {
const op = this.buildSetAdminOp(appName, userAddress);
const txBody = buildTxBody(op);
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/serviceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class ServiceController {
}
}

//TODO(woojae): implement this
// TODO(woojae): implement this
async getInformation(serviceName: string): Promise<any> {
return await 'information of service';
}
Expand Down Expand Up @@ -54,7 +54,7 @@ export default class ServiceController {
return this.ain.sendTransaction(txBody);
}

//TODO(woojae): implement this
// TODO(woojae): implement this
async withdrawCredit(serviceName: string, amount: number) {
return await true;
}
Expand Down Expand Up @@ -101,7 +101,7 @@ export default class ServiceController {
await this.ain.sendTransaction(txBody);
}

//TODO:(woojae): implement this
// TODO:(woojae): implement this
async changeServiceInfo(serviceName: string, config: any): Promise<void> {
await true;
}
Expand Down
5 changes: 1 addition & 4 deletions src/handlers/handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const _ = require("lodash");
import Ain from "@ainblockchain/ain-js";
import Ainize from "../ainize";
import { Path } from "../constants";
import AinModule from "../ain";
import _ from "lodash";

export default class Handler {
private static instance: Handler | undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ export default class Internal {
}

getDataFromServiceRequest(req: Request) {
if(!req.body.valuePath[1] || !req.body.valuePath[3] || !req.body.valuePath[4] || !req.body.value.prompt) {
if(!req.body.valuePath[1] || !req.body.valuePath[3] || !req.body.valuePath[4] || !req.body.value) {
throw new Error("Not from service request");
}
const requestData: request = {
appName: req.body.valuePath[1],
requesterAddress: req.body.auth.addr,
requestKey: req.body.valuePath[4],
requestData: req.body.value.prompt,
requestData: req.body.value,
}
return requestData;
}
Expand Down
1 change: 0 additions & 1 deletion src/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ServiceController from "./controllers/serviceController";
import { creditHistories } from "./types/type";

export default class Service {
serviceName: string;
Expand Down
24 changes: 12 additions & 12 deletions src/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ export type appBillingConfig = {
export enum HISTORY_TYPE {
DEPOSIT = "DEPOSIT",
USAGE = "USAGE",
}
};

export type creditHistories = {
[timestamp: string]: creditHistory;
}
};

export type creditHistory = {
type: HISTORY_TYPE;
amount: number;
transferKey?: string;
requestTimestamp?: string;
}
};

export type opResult = {
code: number;
bandwidth_gas_amount: number;
message?: string;
}
};

export type txResult = {
gas_amount_total: object;
Expand All @@ -58,10 +58,10 @@ export type txResult = {
export enum RESPONSE_STATUS {
SUCCESS = "SUCCESS",
FAIL = "FAIL",
}
};

export type request = {
requestData: string,
requestData: any,
requesterAddress: string,
requestKey: string,
appName: string,
Expand All @@ -71,28 +71,28 @@ export type response = request & {
responseData: string,
cost: number,
status: RESPONSE_STATUS,
}
};

export type deposit = {
transferKey: string,
transferValue: number,
appName: string,
requesterAddress: string,
}
};

export type deployConfig = {
serviceName: string,
billingConfig?: appBillingConfig,
serviceUrl?: string, // NOTE(yoojin): for test.
}
serviceUrl?: string, // NOTE(yoojin): Remove when container deploy logic was available.
};

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

export enum ContainerStatus {
RUNNING = "RUNNING",
STOP = "STOP",
}
};
5 changes: 3 additions & 2 deletions src/utils/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export const buildSetOperation = (type: SetOperationType, ref: string, value: an
type,
ref,
value,
}
};
}

export const buildTxBody = (operation: SetOperation | SetOperation[], timestamp? : number): TransactionBody => {
return {
operation: Array.isArray(operation) ? {
Expand All @@ -16,5 +17,5 @@ export const buildTxBody = (operation: SetOperation | SetOperation[], timestamp?
gas_price: 500,
timestamp: timestamp? timestamp : Date.now(),
nonce: -1,
}
};
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"declaration": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"noEmitOnError": true,
Expand Down