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, diff --git a/src/modules/moduleBase.ts b/src/modules/moduleBase.ts index 6fa0c23..163d948 100644 --- a/src/modules/moduleBase.ts +++ b/src/modules/moduleBase.ts @@ -21,6 +21,13 @@ export default class ModuleBase { } } + protected getDefaultAccount() { + const defaultAccount = this.ain.wallet.defaultAccount; + if (!defaultAccount) + throw new Error("You need to set default account."); + return defaultAccount; + } + private async _sendTransaction(txBody: TransactionBody) { return await this.ain.sendTransaction(txBody); } 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; 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");