Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

feat(profiles): add profile aggregates #469

Merged
merged 6 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/platform-sdk-profiles/__tests__/profile.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import "jest-extended";
import nock from "nock";

import { ARK } from "@arkecosystem/platform-sdk-ark";

import { Profile, ContactRepository, SettingRepository, WalletRepository, Identifiers } from "../src";
import { container } from "../src/container";

import { identity } from "./__fixtures__/identity";
import { HttpClient } from "./stubs/client";
import { DataRepository } from "../src/repositories/data-repository";

Expand Down Expand Up @@ -54,7 +57,7 @@ it("should have a settings repository", () => {
expect(subject.settings()).toBeInstanceOf(SettingRepository);
});

test("Profile#toObject", () => {
test("#toObject", () => {
expect(subject.toObject()).toEqual({
id: "uuid",
name: "John Doe",
Expand All @@ -65,3 +68,16 @@ test("Profile#toObject", () => {
wallets: {},
});
});

test("#balancePerCoin", async () => {
container.set(Identifiers.HttpClient, new HttpClient());

await subject.wallets().import(identity.mnemonic, ARK, "devnet");

expect(subject.balancePerCoin()).toEqual({
DARK: {
percentage: "100.00",
total: "55827093444556",
},
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test("#all", async () => {

test("#allByCoin", async () => {
expect(subject.allByCoin()).toBeObject();
expect(subject.allByCoin().ARK).toBeObject();
expect(subject.allByCoin().DARK).toBeObject();
});

test("#import", async () => {
Expand Down
20 changes: 19 additions & 1 deletion packages/platform-sdk-profiles/__tests__/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,25 @@ test("#coin", () => {
});

test("#network", () => {
expect(subject.network()).toEqual("devnet");
expect(subject.network()).toEqual({
crypto: { slip44: 111 },
currency: { symbol: "DѦ", ticker: "DARK" },
explorer: "https://dexplorer.ark.io/",
hosts: [
"https://dexplorer.ark.io",
"http://167.114.29.51:4003",
"http://167.114.29.52:4003",
"http://167.114.29.53:4003",
"http://167.114.29.54:4003",
"http://167.114.29.55:4003",
],
id: "devnet",
name: "Devnet",
voting: {
enabled: true,
singular: true,
},
});
});

test("#address", () => {
Expand Down
36 changes: 36 additions & 0 deletions packages/platform-sdk-profiles/src/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,40 @@ export class Profile {
settings: this.settings().all(),
};
}

/**
* These methods serve as helpers to aggregate certain data for UI consumption.
*/

public countContacts(): number {
return this.contacts().count();
}

public countNotifications(): number {
return this.notifications().count();
}

public countWallets(): number {
return this.wallets().count();
}

public balancePerCoin(): Record<string, { total: number; percentage: number }> {
const result = {};

const totalByProfile: BigNumber = this.balance();

for (const [coin, wallets] of Object.entries(this.wallets().allByCoin())) {
const totalByCoin: BigNumber = Object.values(wallets).reduce(
(total: BigNumber, wallet: Wallet) => total.plus(wallet.balance()),
BigNumber.ZERO,
);

result[coin] = {
total: totalByCoin.toFixed(),
percentage: totalByCoin.divide(totalByProfile).times(100).toFixed(2),
};
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export class ContactRepository {
this.#data.flush();
}

public count(): number {
return this.keys().length;
}

public findByAddress(value: string): Contact[] {
return this.findByColumn("address", value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export class NotificationRepository {
this.#storage.flush();
}

public count(): number {
return this.keys().length;
}

/**
* Convenience methods to interact with notifications states.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class WalletRepository {
const result = {};

for (const [id, wallet] of Object.entries(this.all())) {
const coin: string | undefined = wallet.coin().manifest().get<string>("name");
const coin: string | undefined = wallet.currency();

if (coin) {
if (!result[coin]) {
Expand Down Expand Up @@ -103,6 +103,10 @@ export class WalletRepository {
this.#data.flush();
}

public count(): number {
return this.keys().length;
}

public toObject(): Record<string, object> {
const result: Record<string, object> = {};

Expand Down
10 changes: 7 additions & 3 deletions packages/platform-sdk-profiles/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ export class Wallet {
return this.#coin;
}

public network(): string {
return this.#coin.network().id;
public network(): Coins.CoinNetwork {
return this.#coin.network();
}

public currency(): string {
return this.network().currency.ticker;
}

public alias(): string | undefined {
Expand Down Expand Up @@ -157,7 +161,7 @@ export class Wallet {
id: this.id(),
coin: this.coin().manifest().get<string>("name"),
coinConfig,
network: this.network(),
network: this.network().id,
address: this.address(),
publicKey: this.publicKey(),
data: this.data().all(),
Expand Down