Skip to content

Commit

Permalink
998 (no-breaking changes) get account utility functions (#1364)
Browse files Browse the repository at this point in the history
* feat: 998 AccountDetail is a class exposing VET for balance and VTHO for energy properties

* feat: 998 AccountDetail is a class exposing VET for balance and VTHO for energy properties

* feat: 998 AccountDetail is a class exposing VET for balance and VTHO for energy properties
  • Loading branch information
lucanicoladebiasi authored Sep 30, 2024
1 parent 6fddb83 commit 72cccf5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 30 deletions.
77 changes: 67 additions & 10 deletions packages/network/src/thor-client/accounts/accounts-module.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
);
}

/**
Expand Down Expand Up @@ -166,4 +223,4 @@ class AccountsModule {
}
}

export { AccountsModule };
export { AccountDetail, AccountsModule };
8 changes: 4 additions & 4 deletions packages/network/src/thor-client/accounts/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -64,7 +64,7 @@ interface ResponseStorage {

export type {
AccountInputOptions,
AccountDetail,
AccountData,
ResponseBytecode,
ResponseStorage
};
21 changes: 5 additions & 16 deletions packages/network/tests/thor-client/accounts/accounts.solo.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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');
Expand Down

1 comment on commit 72cccf5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 97%
97.75% (4178/4274) 95.71% (1364/1425) 96.98% (868/895)
Title Tests Skipped Failures Errors Time
core 782 0 💤 0 ❌ 0 🔥 1m 51s ⏱️
network 706 0 💤 0 ❌ 0 🔥 4m 45s ⏱️
errors 42 0 💤 0 ❌ 0 🔥 18.063s ⏱️

Please sign in to comment.