Skip to content

Commit

Permalink
Merge pull request #39 from ainize-team/feature/yoojin/seperate_ain
Browse files Browse the repository at this point in the history
Add ainModule
  • Loading branch information
yoojinko authored Sep 18, 2023
2 parents a4a9ba5 + 597e7b1 commit 45aad18
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/ain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Ain from "@ainblockchain/ain-js";
import { getBlockChainEndpoint } from "./constants";
import { TransactionBody } from "@ainblockchain/ain-util";

// NOTE(yoojin): Plz suggest a good name.
export default class AinModule {
private ain?: Ain;
private static instance: AinModule;

static getInstance() {
if (!AinModule.instance) {
AinModule.instance = new AinModule();
}
return AinModule.instance;
}

initAin(chainId: 0 | 1) {
const blockchainEndpoint = getBlockChainEndpoint(chainId);
this.ain = new Ain(blockchainEndpoint, chainId);
}

checkAinInitiated(): boolean {
if (!this.ain)
throw new Error('Set initAin(chainId) First.');
return true;
}

isDefaultAccountExist(): boolean {
if (this.getDefaultAccount())
return false;
return true;
}

setDefaultAccount(privateKey: string) {
this.checkAinInitiated();
this.ain!.wallet.addAndSetDefaultAccount(privateKey);
}

getDefaultAccount() {
this.checkAinInitiated();
return this.ain!.wallet.defaultAccount;
}

async sendTransaction(data: TransactionBody) {
this.checkAinInitiated()
return await this.ain!.sendTransaction(data);
}
}

0 comments on commit 45aad18

Please sign in to comment.