diff --git a/src/account.ts b/src/account.ts index 3143b467a..b7bc4502d 100644 --- a/src/account.ts +++ b/src/account.ts @@ -2,7 +2,7 @@ import { Address } from "./address"; import { Nonce } from "./nonce"; import { Balance } from "./balance"; import { Egld } from "./balanceBuilder"; -import BigNumber from "bignumber.js"; +import { IAccountBalance, INonce } from "./interface"; /** * An abstraction representing an account (user or Smart Contract) on the Network. @@ -33,9 +33,9 @@ export class Account { /** * Updates account properties (such as nonce, balance). */ - async update(obj: { nonce: Nonce, balance: Balance}) { - this.nonce = obj.nonce; - this.balance = obj.balance; + async update(obj: { nonce: INonce, balance: IAccountBalance}) { + this.nonce = new Nonce(obj.nonce.valueOf()); + this.balance = Balance.fromString(obj.balance.toString()); } /** @@ -65,52 +65,3 @@ export class Account { }; } } - -/** - * A plain view of an account, as queried from the Network. - */ -export class AccountOnNetwork { - address: Address = new Address(); - nonce: Nonce = new Nonce(0); - balance: Balance = Egld(0); - code: string = ""; - userName: string = ""; - - constructor(init?: Partial) { - Object.assign(this, init); - } - - static fromHttpResponse(payload: any): AccountOnNetwork { - let result = new AccountOnNetwork(); - - result.address = new Address(payload["address"] || 0); - result.nonce = new Nonce(payload["nonce"] || 0); - result.balance = Balance.fromString(payload["balance"] || "0"); - result.code = payload["code"] || ""; - result.userName = payload["username"] || ""; - - return result; - } -} - -export class TokenOfAccountOnNetwork { - tokenIdentifier: string = ""; - attributes: Buffer = Buffer.from([]); - balance: BigNumber = new BigNumber(0); - nonce: Nonce = new Nonce(0); - creator: Address = new Address(""); - royalties: BigNumber = new BigNumber(0); - - static fromHttpResponse(payload: any): TokenOfAccountOnNetwork { - let result = new TokenOfAccountOnNetwork(); - - result.tokenIdentifier = payload.tokenIdentifier; - result.attributes = Buffer.from(payload.attributes || "", "base64"); - result.balance = new BigNumber(payload.balance || 0); - result.nonce = new Nonce(payload.nonce || 0); - result.creator = new Address(payload.creator || ""); - result.royalties = new BigNumber(payload.royalties || 0); - - return result; - } -} diff --git a/src/interface.ts b/src/interface.ts index b4ccaf9e8..5fa6fcfbd 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -1,7 +1,6 @@ import { Transaction } from "./transaction"; import { NetworkConfig } from "./networkConfig"; import { Signature } from "./signature"; -import { AccountOnNetwork, TokenOfAccountOnNetwork } from "./account"; import { Query } from "./smartcontracts"; import { QueryResponse } from "./smartcontracts"; import { NetworkStake } from "./networkStake"; @@ -9,7 +8,7 @@ import { Stats } from "./stats"; import { NetworkStatus } from "./networkStatus"; import { Token } from "./token"; import BigNumber from "bignumber.js"; -import { ITransactionOnNetwork, ITransactionStatus } from "./interfaceOfNetwork"; +import { IAccountOnNetwork, IFungibleTokenOfAccountOnNetwork, ITransactionOnNetwork, ITransactionStatus } from "./interfaceOfNetwork"; export interface ITransactionFetcher { /** @@ -40,12 +39,12 @@ export interface IProvider extends ITransactionFetcher { /** * Fetches the state of an {@link Account}. */ - getAccount(address: IBech32Address): Promise; + getAccount(address: IBech32Address): Promise; /** * Fetches the list of ESDT data for all the tokens of an address. */ - getAddressEsdtList(address: IBech32Address): Promise; + getAddressEsdtList(address: IBech32Address): Promise; /** * Fetches the ESDT data for a token of an address. @@ -147,5 +146,6 @@ export interface ISignature { hex(): string; } export interface IHash { hex(): string; } export interface IBech32Address { bech32(): string; } export interface ITransactionValue { toString(): string; } +export interface IAccountBalance { toString(): string; } export interface ITransactionPayload { encoded(): string; } export interface INonce { valueOf(): number; } diff --git a/src/interfaceOfNetwork.ts b/src/interfaceOfNetwork.ts index 2d366e1d6..1fab82825 100644 --- a/src/interfaceOfNetwork.ts +++ b/src/interfaceOfNetwork.ts @@ -1,4 +1,18 @@ -import { IBech32Address, IHash, INonce, ITransactionPayload, ITransactionValue } from "./interface"; +import { IAccountBalance, IBech32Address, IHash, INonce, ITransactionPayload, ITransactionValue } from "./interface"; + +/** + * @deprecated This interface will be removed upon the extraction of networkProvider package. + */ +export interface IAccountOnNetwork { + nonce: INonce; + balance: IAccountBalance; +} + +/** + * @deprecated This interface will be removed upon the extraction of networkProvider package. + */ +export interface IFungibleTokenOfAccountOnNetwork { +} export interface ITransactionOnNetwork { hash: IHash; diff --git a/src/networkProvider/accounts.ts b/src/networkProvider/accounts.ts new file mode 100644 index 000000000..6c7133588 --- /dev/null +++ b/src/networkProvider/accounts.ts @@ -0,0 +1,30 @@ +import { IAccountBalance, IAddress, INonce } from "./interface"; +import { AccountBalance, Address, Nonce } from "./primitives"; + +/** + * A plain view of an account, as queried from the Network. + */ + export class AccountOnNetwork { + address: IAddress = new Address(""); + nonce: INonce = new Nonce(0); + balance: IAccountBalance = new AccountBalance(""); + code: string = ""; + userName: string = ""; + + constructor(init?: Partial) { + Object.assign(this, init); + } + + static fromHttpResponse(payload: any): AccountOnNetwork { + let result = new AccountOnNetwork(); + + result.address = new Address(payload["address"] || 0); + result.nonce = new Nonce(payload["nonce"] || 0); + result.balance = new AccountBalance(payload["balance"] || "0"); + result.code = payload["code"] || ""; + result.userName = payload["username"] || ""; + + return result; + } +} + diff --git a/src/networkProvider/apiNetworkProvider.ts b/src/networkProvider/apiNetworkProvider.ts index 6bed3490c..4be146c44 100644 --- a/src/networkProvider/apiNetworkProvider.ts +++ b/src/networkProvider/apiNetworkProvider.ts @@ -1,5 +1,5 @@ import axios, { AxiosRequestConfig } from "axios"; -import { AccountOnNetwork } from "../account"; +import { AccountOnNetwork } from "./accounts"; import { IAddress, IContractQueryResponse, IDefinitionOfFungibleTokenOnNetwork, IDefinitionOfTokenCollectionOnNetwork, IFungibleTokenOfAccountOnNetwork, IHash, INetworkProvider, INonce, INonFungibleTokenOfAccountOnNetwork, ITransaction, Pagination } from "./interface"; import { NetworkConfig } from "../networkConfig"; import { NetworkStake } from "../networkStake"; diff --git a/src/networkProvider/interface.ts b/src/networkProvider/interface.ts index 5d4a2c453..6bd5232e3 100644 --- a/src/networkProvider/interface.ts +++ b/src/networkProvider/interface.ts @@ -1,5 +1,5 @@ import { BigNumber } from "bignumber.js"; -import { AccountOnNetwork } from "../account"; +import { AccountOnNetwork } from "./accounts"; import { NetworkConfig } from "../networkConfig"; import { NetworkStake } from "../networkStake"; import { NetworkStatus } from "../networkStatus"; @@ -195,3 +195,4 @@ export interface INonce extends IHexable { valueOf(): number; } export interface ITransactionPayload { encoded(): string; } export interface IGasLimit { valueOf(): number; } export interface IGasPrice { valueOf(): number; } +export interface IAccountBalance { toString(): string; } diff --git a/src/networkProvider/primitives.ts b/src/networkProvider/primitives.ts index 2f77a8769..0adef98a7 100644 --- a/src/networkProvider/primitives.ts +++ b/src/networkProvider/primitives.ts @@ -80,6 +80,18 @@ export class ContractReturnCode { } } +export class AccountBalance { + private readonly value: string; + + constructor(value: string) { + this.value = value; + } + + toString(): string { + return this.value; + } +} + export function numberToPaddedHex(value: number) { let hex = value.toString(16); return zeroPadStringIfOddLength(hex); diff --git a/src/networkProvider/proxyNetworkProvider.ts b/src/networkProvider/proxyNetworkProvider.ts index 00201c284..1d7ab16dd 100644 --- a/src/networkProvider/proxyNetworkProvider.ts +++ b/src/networkProvider/proxyNetworkProvider.ts @@ -1,5 +1,5 @@ import axios, { AxiosRequestConfig } from "axios"; -import { AccountOnNetwork } from "../account"; +import { AccountOnNetwork } from "./accounts"; import { IAddress, IContractQueryResponse, IDefinitionOfFungibleTokenOnNetwork, IDefinitionOfTokenCollectionOnNetwork, IFungibleTokenOfAccountOnNetwork, IHash, INetworkProvider, INonce, INonFungibleTokenOfAccountOnNetwork, ITransaction, Pagination } from "./interface"; import { NetworkConfig } from "../networkConfig"; import { NetworkStake } from "../networkStake"; diff --git a/src/proxyProvider.ts b/src/proxyProvider.ts index e2df595c3..b15564b37 100644 --- a/src/proxyProvider.ts +++ b/src/proxyProvider.ts @@ -6,14 +6,13 @@ import { Transaction, TransactionHash } from "./transaction"; import { NetworkConfig } from "./networkConfig"; import { Address } from "./address"; import * as errors from "./errors"; -import { AccountOnNetwork, TokenOfAccountOnNetwork } from "./account"; import { Query } from "./smartcontracts/query"; import { QueryResponse } from "./smartcontracts/queryResponse"; import { Logger } from "./logger"; import { NetworkStatus } from "./networkStatus"; import { defaultConfig } from "./constants"; import { ProxyNetworkProvider } from "./networkProvider/proxyNetworkProvider"; -import { ITransactionOnNetwork, ITransactionStatus } from "./interfaceOfNetwork"; +import { IAccountOnNetwork, IFungibleTokenOfAccountOnNetwork, ITransactionOnNetwork, ITransactionStatus } from "./interfaceOfNetwork"; /** * This will be deprecated once all the endpoints move to ApiProvider @@ -40,19 +39,14 @@ export class ProxyProvider implements IProvider { } /** - * Fetches the state of an {@link Account}. + * Fetches the state of an account. */ - async getAccount(address: Address): Promise { - return this.doGetGeneric(`address/${address.bech32()}`, (response) => - AccountOnNetwork.fromHttpResponse(response.account) - ); + async getAccount(address: Address): Promise { + return await this.backingProvider.getAccount(address); } - async getAddressEsdtList(address: Address): Promise { - let url = `address/${address.bech32()}/esdt`; - let raw = await this.doGetGeneric(url, response => response.esdts); - let tokens = Object.values(raw).map(item => TokenOfAccountOnNetwork.fromHttpResponse(item)); - return tokens; + async getAddressEsdtList(address: Address): Promise { + return await this.backingProvider.getFungibleTokensOfAccount(address); } async getAddressEsdt(address: Address, tokenIdentifier: string): Promise { diff --git a/src/smartcontracts/wrapper/systemWrapper.ts b/src/smartcontracts/wrapper/systemWrapper.ts index 0cc8e04db..c90ef1c4d 100644 --- a/src/smartcontracts/wrapper/systemWrapper.ts +++ b/src/smartcontracts/wrapper/systemWrapper.ts @@ -119,7 +119,9 @@ export class SystemWrapper extends ChainSendContext { async getBalance(address: NativeTypes.NativeAddress, balanceBuilder: BalanceBuilder): Promise { let typedAddress = NativeSerializer.convertNativeToAddress(address, new ArgumentErrorContext("getBalance", "0", new EndpointParameterDefinition("account", "", new AddressType()))); if (balanceBuilder.getToken().isEgld()) { - return await this.provider.getAccount(typedAddress).then((account) => account.balance); + let account = await this.provider.getAccount(typedAddress); + let balance = Balance.fromString(account.balance.toString()); + return balance; } let tokenData = await this.getTokenData(typedAddress, balanceBuilder); return balanceBuilder.raw(tokenData.balance); diff --git a/src/testutils/mockProvider.ts b/src/testutils/mockProvider.ts index d9bf8d357..de3dc2450 100644 --- a/src/testutils/mockProvider.ts +++ b/src/testutils/mockProvider.ts @@ -4,7 +4,6 @@ import { NetworkConfig } from "../networkConfig"; import { Address } from "../address"; import { Nonce } from "../nonce"; import { AsyncTimer } from "../asyncTimer"; -import { AccountOnNetwork } from "../account"; import { Balance } from "../balance"; import * as errors from "../errors"; import { Query } from "../smartcontracts/query"; @@ -17,6 +16,7 @@ import { ContractResultItem, ContractResults } from "../networkProvider/contract import { TransactionOnNetwork } from "../networkProvider/transactions"; import { ITransactionOnNetwork, ITransactionStatus } from "../interfaceOfNetwork"; import { TransactionStatus } from "../networkProvider/transactionStatus"; +import { AccountOnNetwork } from "../networkProvider/accounts"; const DummyHyperblockNonce = 42; const DummyHyperblockHash = "a".repeat(32);