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.4 #83

Merged
merged 16 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# ainize-sdk
# ainize-js

A Typescript SDK for the Ainize, a system for running AI services on the AI Network.
A Typescript JS for the Ainize, a system for running AI services on the AI Network.

## Requirements
node >= 16

## usage
### Install
```bash
npm install @ainize-team/ainize-sdk
npm install @ainize-team/ainize-js

yarn install @ainize-team/ainize-sdk
yarn install @ainize-team/ainize-js
```

### Import
```typescript
import Ainize from '@ainize-team/ainize-sdk'
import Ainize from '@ainize-team/ainize-js'
const ainize = new Ainize(<CHAIN_ID>);
```

Expand Down Expand Up @@ -49,9 +49,9 @@ service.run();
```

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

You should deposit AIN to credit before using service.
Expand All @@ -62,5 +62,5 @@ const balance = await service.getCreditBalance();

If you have enough credit, you can use the service.
```typescript
const result = await service.use(<REQUEST_DATA>);
const result = await service.request(<REQUEST_DATA>);
```
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ainize-team/ainize-sdk",
"version": "1.0.3",
"name": "@ainize-team/ainize-js",
"version": "1.0.4",
"main": "dist/ainize.js",
"types": "dist/ainize.d.ts",
"scripts": {
Expand All @@ -14,7 +14,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/ainize-team/ainize-sdk.git"
"url": "git+https://github.com/ainize-team/ainize-js.git"
},
"keywords": [
"ainize",
Expand Down
22 changes: 9 additions & 13 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ export const Path = {
export 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: {
Expand All @@ -56,7 +48,7 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin
},
},
balanceHistory: {
ref: `${rootRef}/balance/$userAddress/history/$timestamp_and_type`,
ref: `${rootRef}/balance/$userAddress/history/$timestamp`,
value: {
".rule": {
write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && util.isNumber(newData.amount) && (newData.type === 'DEPOSIT' || newData.type === 'USAGE')"
Expand All @@ -68,7 +60,8 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin
value: {
".rule": {
write:
"auth.addr === $userAddress && getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) !== null && " +
"auth.addr === $userAddress && " +
"(getValue(`/apps/" + `${appName}` + "/billingConfig/minCost`) === 0 || " +
"(getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) >= getValue(`/apps/" + `${appName}` + "/billingConfig/minCost`))"
},
},
Expand All @@ -85,9 +78,12 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin
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) && util.isNumber(newData.costPerToken) && util.isNumber(newData.minCost) && " +
"util.isEmpty(newData.maxCost) || (util.isNumber(newData.maxCost) && newData.maxCost >= newData.minCost)",
write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && " +
"util.isDict(newData) && " +
"util.isString(newData.depositAddress) && " +
"util.isNumber(newData.costPerToken) && " +
"util.isNumber(newData.minCost) && newData.minCost >= 0 &&" +
"(util.isEmpty(newData.maxCost) || (util.isNumber(newData.maxCost) && newData.maxCost >= newData.minCost))",
},
},
},
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/serviceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export default class ServiceController {
return await 'information of service';
}

async calculateCost(serviceName: string, requestData: string): Promise<number> {
// FIXME(yoojin): Temporary deprecated. Need new pricing rules.
private async calculateCost(serviceName: string, requestData: string): Promise<number> {
const billingConfig = await this.ain.getValue(Path.app(serviceName).billingConfig());
const token = requestData.split(' ').length;
let cost = token * billingConfig.costPerToken;
Expand Down Expand Up @@ -71,7 +72,7 @@ export default class ServiceController {
return await this.ain.getValue(creditHistoryPath) as creditHistories;
}

async request(serviceName: string, requestData: string) : Promise<string> {
async request(serviceName: string, requestData: any) : Promise<any> {
this.isRunning(serviceName);
const result = await new Promise(async (resolve, reject) => {
const requestKey = Date.now();
Expand Down
6 changes: 4 additions & 2 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export default class Service {
* @returns {number} Estimated cost.
*/
async calculateCost (requestData: string) {
return await this.serviceController.calculateCost(this.serviceName, requestData);
// FIXME(yoojin): Temporary deprecated. Need new pricing rules.
// return await this.serviceController.calculateCost(this.serviceName, requestData);
return 0;
}

/**
Expand Down Expand Up @@ -76,7 +78,7 @@ export default class Service {
* @param {string} requestData String data for request to service.
* @returns {string} Response data from service.
*/
async request(requestData: string) {
async request(requestData: any) {
this.isLoggedIn();
return await this.serviceController.request(this.serviceName, requestData);
}
Expand Down