From 108ef86b781041b3b793ceca668aa63f6f8c500b Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Mon, 18 Sep 2023 17:19:32 +0900 Subject: [PATCH 1/5] feat: add AinModule --- src/ain.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/ain.ts diff --git a/src/ain.ts b/src/ain.ts new file mode 100644 index 0000000..f084b04 --- /dev/null +++ b/src/ain.ts @@ -0,0 +1,37 @@ +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; + + 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 { + return this.ain ? true : false; + } + + setDefaultAddress(privateKey: string) { + if(!this.checkAinInitiated()) + throw new Error('Set initAin(chainId) First.'); + this.ain!.wallet.addAndSetDefaultAccount(privateKey); + } + + async sendTransaction(data: TransactionBody) { + if (!this.checkAinInitiated()) + throw new Error('Set initAin(chainId) First.'); + return await this.ain!.sendTransaction(data); + } +} \ No newline at end of file From 98421036397c552d7bc41351bc61337831b036fa Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Mon, 18 Sep 2023 17:33:04 +0900 Subject: [PATCH 2/5] fix: static getInstance --- src/ain.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ain.ts b/src/ain.ts index f084b04..5809f00 100644 --- a/src/ain.ts +++ b/src/ain.ts @@ -7,7 +7,7 @@ export default class AinModule { private ain?: Ain; private static instance: AinModule; - getInstance() { + static getInstance() { if (!AinModule.instance) { AinModule.instance = new AinModule(); } From f062f24c3760aff13b1d4038fb1dcf9529acd5cf Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Mon, 18 Sep 2023 17:47:58 +0900 Subject: [PATCH 3/5] feat: add getDefaultAccount --- src/ain.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ain.ts b/src/ain.ts index 5809f00..fbd930a 100644 --- a/src/ain.ts +++ b/src/ain.ts @@ -23,12 +23,19 @@ export default class AinModule { return this.ain ? true : false; } - setDefaultAddress(privateKey: string) { - if(!this.checkAinInitiated()) + setDefaultAccount(privateKey: string) { + if (!this.checkAinInitiated()) throw new Error('Set initAin(chainId) First.'); this.ain!.wallet.addAndSetDefaultAccount(privateKey); } + // FIXME(yoojin): check ain error. + getDefaultAccount() { + if (!this.checkAinInitiated()) + throw new Error('Set initAin(chainId) First.'); + return this.ain!.wallet.defaultAccount; + } + async sendTransaction(data: TransactionBody) { if (!this.checkAinInitiated()) throw new Error('Set initAin(chainId) First.'); From 3023710537d1e77267d7e28b973e516324e6773e Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Mon, 18 Sep 2023 18:02:21 +0900 Subject: [PATCH 4/5] fix: throw error with check --- src/ain.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/ain.ts b/src/ain.ts index fbd930a..b4002ef 100644 --- a/src/ain.ts +++ b/src/ain.ts @@ -20,25 +20,24 @@ export default class AinModule { } checkAinInitiated(): boolean { - return this.ain ? true : false; + if (!this.ain) + throw new Error('Set initAin(chainId) First.'); + return true; } setDefaultAccount(privateKey: string) { - if (!this.checkAinInitiated()) - throw new Error('Set initAin(chainId) First.'); + this.checkAinInitiated() this.ain!.wallet.addAndSetDefaultAccount(privateKey); } // FIXME(yoojin): check ain error. getDefaultAccount() { - if (!this.checkAinInitiated()) - throw new Error('Set initAin(chainId) First.'); + this.checkAinInitiated(); return this.ain!.wallet.defaultAccount; } async sendTransaction(data: TransactionBody) { - if (!this.checkAinInitiated()) - throw new Error('Set initAin(chainId) First.'); + this.checkAinInitiated() return await this.ain!.sendTransaction(data); } } \ No newline at end of file From 597e7b1445f570d4b6fc469867214f176600fa82 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Mon, 18 Sep 2023 18:06:14 +0900 Subject: [PATCH 5/5] feat: add isDefaultAccountExist --- src/ain.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ain.ts b/src/ain.ts index b4002ef..adb5038 100644 --- a/src/ain.ts +++ b/src/ain.ts @@ -25,12 +25,17 @@ export default class AinModule { return true; } + isDefaultAccountExist(): boolean { + if (this.getDefaultAccount()) + return false; + return true; + } + setDefaultAccount(privateKey: string) { - this.checkAinInitiated() + this.checkAinInitiated(); this.ain!.wallet.addAndSetDefaultAccount(privateKey); } - // FIXME(yoojin): check ain error. getDefaultAccount() { this.checkAinInitiated(); return this.ain!.wallet.defaultAccount;