Skip to content

Commit

Permalink
Merge pull request #46 from ainize-team/feature/yoojin/ainize_methods
Browse files Browse the repository at this point in the history
Add ainize methods
  • Loading branch information
yoojinko authored Sep 20, 2023
2 parents c409c54 + d8ec315 commit cc06695
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
28 changes: 22 additions & 6 deletions src/ain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export default class AinModule {
return false;
}

createAccount() {
this.checkAinInitiated();
const newAccount = this.ain!.wallet.create(1)[0];
return newAccount;
}

setDefaultAccount(privateKey: string) {
this.checkAinInitiated();
this.ain!.wallet.addAndSetDefaultAccount(privateKey);
Expand All @@ -35,6 +41,21 @@ export default class AinModule {
return this.ain!.wallet.defaultAccount;
}

removeDefaultAccount() {
this.checkAinInitiated();
this.ain!.wallet.removeDefaultAccount();
}

getAddress() {
this.isDefaultAccountExist();
return this.ain!.wallet.defaultAccount!.address;
}

async getBalance() {
this.isDefaultAccountExist();
return await this.ain!.wallet.getBalance();
}

async getValue(path: string) {
this.checkAinInitiated();
return await this.ain!.db.ref(path).getValue();
Expand All @@ -51,13 +72,8 @@ export default class AinModule {
return true;
}

getAddress() {
this.isDefaultAccountExist();
return this.ain!.wallet.defaultAccount!.address;
}

getEventManager() {
this.checkAinInitiated();
return this.ain!.em;
}
}
}
33 changes: 24 additions & 9 deletions src/ainize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Ain from "@ainblockchain/ain-js";
import * as NodeCache from "node-cache";
import Middleware from "./middlewares/middleware";
import { DEFAULT_BILLING_CONFIG, getBlockChainEndpoint } from "./constants";
Expand All @@ -9,36 +8,52 @@ import { deployConfig } from "./types/type";
import AinModule from "./ain";
export default class Ainize {
private cache: NodeCache;
ain: Ain;
ain: AinModule = AinModule.getInstance();
middleware: Middleware;
appController: AppController = AppController.getInstance();

constructor(chainId: 1 | 0) {
const blockChainEndpoint = getBlockChainEndpoint(chainId);
this.ain = new Ain(blockChainEndpoint, chainId);
this.ain.initAin(chainId);
this.cache = new NodeCache();
this.middleware = new Middleware(this.cache);
}

static createAinAccount () {
return AinModule.getInstance().createAccount();
}

login(privateKey: string) {
this.ain.setDefaultAccount(privateKey);
}

logout() {
this.ain.removeDefaultAccount();
}

async getAinBalance(): Promise<number> {
return await this.ain.getBalance();
}

// FIXME(yoojin): add config type and change param type.
deploy({modelName, billingConfig, serviceUrl}: deployConfig) {
async deploy({modelName, billingConfig, serviceUrl}: deployConfig): Promise<Model> {
// TODO(yoojin, woojae): Deploy container, advanced.
const deployer = AinModule.getInstance().getAddress();
const deployer = this.ain.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 })
await this.appController.createApp({ appName: modelName, serviceUrl, billingConfig });
return new Model(modelName);
}

model(modelName: string) {
model(modelName: string): Model {
return new Model(modelName);
}

Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ export const Path = {
app: (appName: string): any => {
return {
root: () => `/apps/${appName}`,
status: () => `${Path.app(appName).root()}/status`,
balance: () => `${Path.app(appName).root()}/balance`,
balanceOfUser: (userAddress: string) => `${Path.app(appName).balance()}/${userAddress}/balance`,
historyOfUser: (userAddress: string) => `${Path.app(appName).balance()}/${userAddress}/history`,
deposit: () => `${Path.app(appName).root()}/deposit`,
depositOfUser: (userAddress: string) => `${Path.app(appName).deposit()}/${userAddress}`,
billingConfig: () => `${Path.app(appName).root()}/billingConfig`,
service: () => `${Path.app(appName).root()}/service/`,
service: () => `${Path.app(appName).root()}/service`,
userOfService: (userAddress: string) =>
`${Path.app(appName).service()}/${userAddress}`,
request: (userAddress: string, requestKey: string) =>
Expand Down

0 comments on commit cc06695

Please sign in to comment.