Skip to content

Commit

Permalink
Merge pull request #30 from ainize-team/feature/yoojin/get_default_ac…
Browse files Browse the repository at this point in the history
…count

Add getDefaultAccount to baseModule and change wallet's getDefaultAccount to getDefaultAddress
  • Loading branch information
yoojinko authored Sep 12, 2023
2 parents fd2b39f + 57174f8 commit 478859c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
10 changes: 3 additions & 7 deletions src/modules/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/modules/moduleBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/service/depositService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down
4 changes: 2 additions & 2 deletions src/modules/service/useService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
13 changes: 5 additions & 8 deletions src/modules/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -27,7 +24,7 @@ export default class Wallet extends ModuleBase{
*/
setDefaultAccount(privateKey: string) {
this.ain.wallet.addAndSetDefaultAccount(privateKey);
return this.getDefaultAccount();
return this.getDefaultAddress();
}

/**
Expand All @@ -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);
}
Expand All @@ -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");
Expand Down

0 comments on commit 478859c

Please sign in to comment.