From 38b5c83aefb98ee4fbcf28b6c3b8226f4c01a620 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Tue, 12 Sep 2023 14:56:56 +0900 Subject: [PATCH 1/6] feat: add get default account to moduleBase --- src/modules/moduleBase.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/modules/moduleBase.ts b/src/modules/moduleBase.ts index 6fa0c23..fad028e 100644 --- a/src/modules/moduleBase.ts +++ b/src/modules/moduleBase.ts @@ -21,6 +21,10 @@ export default class ModuleBase { } } + protected getDefaultAccount() { + return this.ain.wallet.defaultAccount; + } + private async _sendTransaction(txBody: TransactionBody) { return await this.ain.sendTransaction(txBody); } From 341e8b9efef5ed400c218f031d8f09fb03a5470c Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Tue, 12 Sep 2023 15:00:46 +0900 Subject: [PATCH 2/6] feat: check defaultAccount at getDefaultAccount --- src/modules/moduleBase.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/modules/moduleBase.ts b/src/modules/moduleBase.ts index fad028e..a564d6b 100644 --- a/src/modules/moduleBase.ts +++ b/src/modules/moduleBase.ts @@ -22,7 +22,10 @@ export default class ModuleBase { } protected getDefaultAccount() { - return this.ain.wallet.defaultAccount; + const defaultAccount = this.ain.wallet.defaultAccount; + if (!defaultAccount) + throw new Error("You need to initialize ainize with private key."); + return defaultAccount; } private async _sendTransaction(txBody: TransactionBody) { From c6bb3d7914ee50284228d2a636403b76e11d0fe1 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Tue, 12 Sep 2023 15:01:05 +0900 Subject: [PATCH 3/6] feat: use getDefaultAccount --- src/modules/app.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/modules/app.ts b/src/modules/app.ts index 007b5df..50bb1f4 100644 --- a/src/modules/app.ts +++ b/src/modules/app.ts @@ -30,9 +30,9 @@ export default class App extends ModuleBase { const value = this.buildSetFunctionValue(depositParam); const funcOp = buildSetOperation("SET_FUNCTION", depositParam.ref, value); setFunctionOps.push(funcOp); - + const depositAddress = this.getDefaultAccount().address; const defaultConfig: appBillingConfig = { - depositAddress: this.ain.wallet.defaultAccount!.address, + depositAddress, service: { default: { costPerToken: 0, @@ -149,11 +149,7 @@ export default class App extends ModuleBase { private buildCreateAppOp(appName: string): SetOperation { const path = `/manage_app/${appName}/create/${Date.now()}`; - const adminAccount = this.ain.wallet.defaultAccount!; - if (!adminAccount || !adminAccount.address) { - // FIXME(yoojin): change Error to Custom error when it added. - throw new Error("You need to enter your private key when initialize sdk."); - } + const adminAccount = this.getDefaultAccount(); const value = { admin: { [adminAccount.address]: true, From 2ef321784e96d7491f86be167395b8d177142e8f Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Tue, 12 Sep 2023 15:02:45 +0900 Subject: [PATCH 4/6] refactor: change getDefaultAccount to address --- src/modules/wallet.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/modules/wallet.ts b/src/modules/wallet.ts index eb23349..e585072 100644 --- a/src/modules/wallet.ts +++ b/src/modules/wallet.ts @@ -13,11 +13,8 @@ export default class Wallet extends ModuleBase{ * Get defult AI Network blockchain account information set in ainize. * @returns Default account's address. */ - getDefaultAccount() { - if (!this.ain.wallet.defaultAccount) { - throw new Error("You need to set default account."); - } - return this.ain.wallet.defaultAccount.address; + getDefaultAddress() { + return this.getDefaultAccount().address; } /** @@ -27,7 +24,7 @@ export default class Wallet extends ModuleBase{ */ setDefaultAccount(privateKey: string) { this.ain.wallet.addAndSetDefaultAccount(privateKey); - return this.getDefaultAccount(); + return this.getDefaultAddress(); } /** @@ -46,7 +43,7 @@ export default class Wallet extends ModuleBase{ */ getAinBalance(address?: string) { if (!address) { - address = this.getDefaultAccount(); + address = this.getDefaultAddress(); } return this.ain.wallet.getBalance(address); } @@ -59,7 +56,7 @@ export default class Wallet extends ModuleBase{ */ async sendTxWithAddress(txBody: TransactionInput, signerAddress?: string) { if (!signerAddress) { - signerAddress = this.getDefaultAccount(); + signerAddress = this.getDefaultAddress(); } if (!this.ain.wallet.isAdded(signerAddress)) { throw new Error ("You need to add account"); From 6a2f1017fc3c3d43803f41be259e4c135c46a784 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Tue, 12 Sep 2023 15:03:57 +0900 Subject: [PATCH 5/6] refactor: change to getDefaultAddress --- src/modules/service/depositService.ts | 2 +- src/modules/service/useService.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/service/depositService.ts b/src/modules/service/depositService.ts index 5e9b6ad..f2f3bca 100644 --- a/src/modules/service/depositService.ts +++ b/src/modules/service/depositService.ts @@ -7,7 +7,7 @@ import { HISTORY_TYPE } from "../../types/type"; export default class DepositService extends ServiceBase { async requestDeposit(appName: string, amount: number, userAddress?: string) { const transferKey = Date.now(); - userAddress = userAddress ? userAddress : this.wallet.getDefaultAccount(); + userAddress = userAddress ? userAddress : this.wallet.getDefaultAddress(); const depositAddress = await this.getDepositAddress(appName); const op_list: SetOperation[] = [ diff --git a/src/modules/service/useService.ts b/src/modules/service/useService.ts index 991231c..0fa93fe 100644 --- a/src/modules/service/useService.ts +++ b/src/modules/service/useService.ts @@ -7,7 +7,7 @@ import ServiceBase from "./serviceBase"; export default class UseService extends ServiceBase{ async writeRequest(appName: string, serviceName: string, value: string, requesterAddress?: string) { const requestKey = Date.now(); - requesterAddress = requesterAddress ? requesterAddress : this.wallet.getDefaultAccount(); + requesterAddress = requesterAddress ? requesterAddress : this.wallet.getDefaultAddress(); const requestPath = Path.app(appName).request(serviceName, requesterAddress, requestKey); const requestData = { prompt: value, @@ -19,7 +19,7 @@ export default class UseService extends ServiceBase{ } async calculateCostAndCheckBalance(appName: string, serviceName: string, value: string, requesterAddress?: string) { - requesterAddress = requesterAddress ? requesterAddress : this.wallet.getDefaultAccount(); + requesterAddress = requesterAddress ? requesterAddress : this.wallet.getDefaultAddress(); const billingConfig = await this.app.getBillingConfig(appName); // TODO(woojae): calculate cost more accurately let serviceBillingConfig = billingConfig.service.default; From 57174f828540af75e586663f1a5df3eb301e0936 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Tue, 12 Sep 2023 15:19:03 +0900 Subject: [PATCH 6/6] fix: error msg --- src/modules/moduleBase.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/moduleBase.ts b/src/modules/moduleBase.ts index a564d6b..163d948 100644 --- a/src/modules/moduleBase.ts +++ b/src/modules/moduleBase.ts @@ -24,7 +24,7 @@ export default class ModuleBase { protected getDefaultAccount() { const defaultAccount = this.ain.wallet.defaultAccount; if (!defaultAccount) - throw new Error("You need to initialize ainize with private key."); + throw new Error("You need to set default account."); return defaultAccount; }