Skip to content

Commit

Permalink
Merge pull request #148 from ainize-team/fix/yoojin/create_account
Browse files Browse the repository at this point in the history
Fix create account and example codes
  • Loading branch information
yoojinko authored Dec 18, 2024
2 parents cca2cc2 + 2d7192e commit 941ea68
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 20 deletions.
6 changes: 3 additions & 3 deletions examples/charge.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Ainize } from '@ainize-team/ainize-js';
import Ainize from '@ainize-team/ainize-js';
const ainPrivateKey = ''; // Insert your private key here
const main = async () => {
try {
const ainize = new Ainize(1); // 0 for testnet, 1 for mainnet. You can earn testnet AIN at https://faucet.ainetwork.ai/.
await ainize.login(ainPrivateKey);
console.log('balance: ',await ainize.getAinBalance());
const model = await ainize.getModel('ainize_free_inference');
const model = await ainize.getModel('meta-llama/Llama-3.1-8B-instruct');
console.log(model.modelName);
console.log("before charge: ",await model.getCreditBalance());
await model.chargeCredit(10);
console.log("after charge: ",await model.getCreditBalance());
ainize.logout();
}catch(e) {
} catch(e) {
console.log(e);
}
}
Expand Down
8 changes: 4 additions & 4 deletions examples/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Ainize } from '@ainize-team/ainize-js';
import Ainize from '@ainize-team/ainize-js';
const ainPrivateKey = ''; // Insert your private key here

const main = async () => {
try {
const ainize = new Ainize(1); // 0 for testnet, 1 for mainnet. You can earn testnet AIN at https://faucet.ainetwork.ai/.
await ainize.login(ainPrivateKey);
console.log('balance: ',await ainize.getAinBalance());
console.log('balance: ', await ainize.getAinBalance());
const deployConfig = {
modelName: 'YOUR_MODEL_NAME',
modelUrl: 'YOUR_MODEL_INFERENCE_URL' // e.g. https://ainize-free-inference.ainetwork.xyz
modelUrl: 'YOUR_MODEL_INFERENCE_URL'
}
const model = await ainize.deploy(deployConfig);
console.log(model.modelName);
ainize.logout();
}catch(e) {
} catch(e) {
console.log(e);
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/request.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Ainize } from '@ainize-team/ainize-js';
import Ainize from '@ainize-team/ainize-js';
const ainPrivateKey = ''; // Insert your private key here

const main = async () => {
try {
const ainize = new Ainize(1); // 0 for testnet, 1 for mainnet. You can earn testnet AIN at https://faucet.ainetwork.ai/.
await ainize.login(ainPrivateKey);
console.log('balance: ',await ainize.getAinBalance());
const inferenceModel = await ainize.getModel('ainize_free_inference');
const inferenceModel = await ainize.getModel('meta-llama/Llama-3.1-8B-instruct');
console.log(inferenceModel.modelName);
console.log(await inferenceModel.getCreditBalance());
const request = {
Expand All @@ -17,7 +17,7 @@ const main = async () => {
const response = await inferenceModel.request(request);
console.log(response);
ainize.logout();
}catch(e) {
} catch(e) {
console.log(e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"docs": "yarn build && typedoc --options typedoc.json --out docs"
},
"engines": {
"node": ">=16"
"node": ">=18"
},
"repository": {
"type": "git",
Expand Down
19 changes: 11 additions & 8 deletions src/ain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ export default class AinModule {
return AinModule.instance;
}

static createAccount() {
const blockchainAPIEndpoint = getBlockChainAPIEndpoint(0);
const blockchainEventEndpoint = getBlockChainEventEndpoint(0);
const ain = new Ain(blockchainAPIEndpoint, blockchainEventEndpoint, 0);

const newAccount = ain.wallet.create(1)[0];
const wallet = ain.wallet.accounts[newAccount];

return wallet;
}

initAin(chainId: 0 | 1) {
const blockchainAPIEndpoint = getBlockChainAPIEndpoint(chainId);
const blockchainEventEndpoint = getBlockChainEventEndpoint(chainId);
this.ain = new Ain(blockchainAPIEndpoint, blockchainEventEndpoint, chainId);
}

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

setDefaultAccount(privateKey: string) {
this.checkAinInitiated();
this.ain!.wallet.addAndSetDefaultAccount(privateKey);
Expand Down
2 changes: 1 addition & 1 deletion src/ainize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class Ainize {
* @returns {Account} created account.
*/
static createAinAccount (): Account {
return AinModule.getInstance().createAccount();
return AinModule.createAccount();
}

/**
Expand Down

0 comments on commit 941ea68

Please sign in to comment.