diff --git a/packages/network/src/thor-client/accounts/accounts-module.ts b/packages/network/src/thor-client/accounts/accounts-module.ts index ec0cdeae1..eeb93284d 100644 --- a/packages/network/src/thor-client/accounts/accounts-module.ts +++ b/packages/network/src/thor-client/accounts/accounts-module.ts @@ -1,14 +1,69 @@ import { InvalidDataType } from '@vechain/sdk-errors'; -import { Address, Revision, ThorId } from '@vechain/sdk-core'; +import { + Address, + FixedPointNumber, + Revision, + ThorId, + Units, + VET, + VTHO +} from '@vechain/sdk-core'; import { buildQuery, thorest } from '../../utils'; import { - type AccountDetail, + type AccountData, type AccountInputOptions, type ResponseBytecode, type ResponseStorage } from './types'; import { type ThorClient } from '../thor-client'; +/** + * Represents detailed account information. + * + * Implements the {@link AccountData} interface. + */ +class AccountDetail implements AccountData { + /** + * Return the hexadecimal expression of the wei VET value of the balance. + */ + readonly balance: string; + + /** + * Return the hexadecimal expression of the wei VTHO value of the energy balance. + */ + readonly energy: string; + + /** + * Return `true` if the account is a smart contract, otherwise `false`. + */ + readonly hasCode: boolean; + + /** + * Returns the balance of the account in {@link VET}. + */ + get vet(): VET { + return VET.of(Units.formatEther(FixedPointNumber.of(this.balance))); + } + + /** + * Returns the energy balance of the account in {@link VTHO}. + */ + get vtho(): VTHO { + return VTHO.of(Units.formatEther(FixedPointNumber.of(this.energy))); + } + + /** + * Constructs a new instance of the class. + * + * @param {AccountData} accountData - The data to initialize the account with. + */ + constructor(accountData: AccountData) { + this.balance = accountData.balance; + this.energy = accountData.energy; + this.hasCode = accountData.hasCode; + } +} + /** * The `AccountModule` class provides methods to interact with account-related endpoints * of the VeChainThor blockchain. It allows fetching details, bytecode, and storage data @@ -55,13 +110,15 @@ class AccountsModule { ); } - return (await this.thor.httpClient.http( - 'GET', - thorest.accounts.get.ACCOUNT_DETAIL(address), - { - query: buildQuery({ revision: options?.revision }) - } - )) as AccountDetail; + return new AccountDetail( + (await this.thor.httpClient.http( + 'GET', + thorest.accounts.get.ACCOUNT_DETAIL(address), + { + query: buildQuery({ revision: options?.revision }) + } + )) as AccountData + ); } /** @@ -166,4 +223,4 @@ class AccountsModule { } } -export { AccountsModule }; +export { AccountDetail, AccountsModule }; diff --git a/packages/network/src/thor-client/accounts/types.d.ts b/packages/network/src/thor-client/accounts/types.d.ts index 782fd976c..fd3b65fbc 100644 --- a/packages/network/src/thor-client/accounts/types.d.ts +++ b/packages/network/src/thor-client/accounts/types.d.ts @@ -21,14 +21,14 @@ interface AccountInputOptions { /** * The account details represent the balance, energy & whether the account is a smart contract. */ -interface AccountDetail { +interface AccountData { /** - * The balance of VET of the account. + * The hexadecimal expression of the wei VET value of the balance. */ balance: string; /** - * The balance of VTHO of the account. + * The hexadecimal expression of the wei VTHO value of the energy balance. */ energy: string; @@ -64,7 +64,7 @@ interface ResponseStorage { export type { AccountInputOptions, - AccountDetail, + AccountData, ResponseBytecode, ResponseStorage }; diff --git a/packages/network/tests/thor-client/accounts/accounts.solo.test.ts b/packages/network/tests/thor-client/accounts/accounts.solo.test.ts index b4409b481..25f7469ea 100644 --- a/packages/network/tests/thor-client/accounts/accounts.solo.test.ts +++ b/packages/network/tests/thor-client/accounts/accounts.solo.test.ts @@ -1,6 +1,6 @@ import { beforeEach, describe, expect, test } from '@jest/globals'; import { TEST_ACCOUNTS, TESTING_CONTRACT_ADDRESS } from '../../fixture'; -import { FixedPointNumber, Units } from '@vechain/sdk-core'; +import { VET, VTHO } from '@vechain/sdk-core'; import { TESTING_CONTRACT_BYTECODE } from './fixture'; import { THOR_SOLO_URL, ThorClient } from '../../../src'; @@ -37,21 +37,10 @@ describe('ThorClient - Accounts Module', () => { // Thor-solo is being initialized with 500000000 VET // And at least 500000000 VTHO - const expectedVTHO = 500000000n; - expect( - FixedPointNumber.of( - // In this context Ether is the 10E8 magnitude. - Units.formatEther( - FixedPointNumber.of(accountBefore.balance) - ) - ).isEqual(FixedPointNumber.of(expectedVTHO)) - ).toBe(true); - expect( - FixedPointNumber.of(accountBefore.energy).gt( - // In this context Ether is the 10E8 magnitude. - Units.parseEther(expectedVTHO.toString()) - ) - ).toBe(true); + const expectedVET = VET.of(500000000n); + const expectedVTHO = VTHO.of(500000000n); + expect(accountBefore.vet.isEqual(expectedVET)).toBe(true); + expect(accountBefore.vtho.value.gt(expectedVTHO.value)).toBe(true); const currentBlock = await thorSoloClient.blocks.getBlockCompressed('best');