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

feat(profiles): implement ExtendedTransactionDataCollection #675

Merged
merged 2 commits into from Aug 24, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/platform-sdk-profiles/src/dto/transaction-collection.ts
@@ -0,0 +1,29 @@
import { Coins } from "@arkecosystem/platform-sdk";

import { ExtendedTransactionData } from "./transaction";

export class ExtendedTransactionDataCollection extends Coins.Paginator<ExtendedTransactionData> {
public findById(id: string): ExtendedTransactionData | undefined {
return this.find("id", id);
}

public findByType(type: string): ExtendedTransactionData | undefined {
return this.find("type", type);
}

public findByTimestamp(timestamp: string): ExtendedTransactionData | undefined {
return this.find("timestamp", timestamp);
}

public findBySender(sender: string): ExtendedTransactionData | undefined {
return this.find("sender", sender);
}

public findByRecipient(recipient: string): ExtendedTransactionData | undefined {
return this.find("recipient", recipient);
}

private find(key: string, value: string): ExtendedTransactionData | undefined {
return this.items().find((transaction: ExtendedTransactionData) => transaction[key]() === value);
}
}
19 changes: 16 additions & 3 deletions packages/platform-sdk-profiles/src/dto/transaction-mapper.ts
@@ -1,4 +1,4 @@
import { Contracts } from "@arkecosystem/platform-sdk";
import { Coins, Contracts } from "@arkecosystem/platform-sdk";

import { ReadWriteWallet } from "../wallets/wallet.models";
import {
Expand All @@ -13,6 +13,7 @@ import {
EntityRegistrationData,
EntityResignationData,
EntityUpdateData,
ExtendedTransactionData,
HtlcClaimData,
HtlcLockData,
HtlcRefundData,
Expand All @@ -24,12 +25,13 @@ import {
TransferData,
VoteData,
} from "./transaction";
import { ExtendedTransactionDataCollection } from "./transaction-collection";

export const transformTransactionData = (
wallet: ReadWriteWallet,
transaction: Contracts.TransactionDataType,
): Contracts.TransactionDataType => {
const instance: Contracts.TransactionDataType = new TransactionData(wallet, transaction);
): ExtendedTransactionData => {
const instance: ExtendedTransactionData = new TransactionData(wallet, transaction);

if (instance.isLegacyBridgechainRegistration()) {
return new BridgechainRegistrationData(wallet, transaction);
Expand Down Expand Up @@ -117,3 +119,14 @@ export const transformTransactionData = (

return instance;
};

export const transformTransactionDataCollection = (
wallet: ReadWriteWallet,
transactions: Coins.TransactionDataCollection,
): ExtendedTransactionDataCollection =>
new ExtendedTransactionDataCollection(
transactions
.getData()
.map((transaction: Contracts.TransactionData) => transformTransactionData(wallet, transaction)),
transactions.getPagination(),
);
22 changes: 22 additions & 0 deletions packages/platform-sdk-profiles/src/dto/transaction.ts
Expand Up @@ -514,3 +514,25 @@ export class VoteData extends TransactionData {
return this.data<Contracts.VoteData>().unvotes();
}
}

export type ExtendedTransactionData =
| BridgechainRegistrationData
| BridgechainResignationData
| BridgechainUpdateData
| BusinessRegistrationData
| BusinessResignationData
| BusinessUpdateData
| DelegateRegistrationData
| DelegateResignationData
| EntityRegistrationData
| EntityResignationData
| EntityUpdateData
| HtlcClaimData
| HtlcLockData
| HtlcRefundData
| IpfsData
| MultiPaymentData
| MultiSignatureData
| SecondSignatureData
| TransferData
| VoteData;
@@ -1,12 +1,14 @@
import { Coins, Contracts } from "@arkecosystem/platform-sdk";

import { transformTransactionData } from "../../dto/transaction-mapper";
import { ExtendedTransactionData } from "../../dto/transaction";
import { ExtendedTransactionDataCollection } from "../../dto/transaction-collection";
import { transformTransactionData, transformTransactionDataCollection } from "../../dto/transaction-mapper";
import { promiseAllSettledByKey } from "../../helpers/promise";
import { Wallet } from "../../wallets/wallet";
import { ReadWriteWallet } from "../../wallets/wallet.models";
import { ProfileContract } from "../profile.models";

type HistoryMethod = string;
type HistoryWallet = Coins.TransactionDataCollection;
type HistoryWallet = ExtendedTransactionDataCollection;

export class EntityAggregate {
readonly #profile: ProfileContract;
Expand All @@ -31,14 +33,14 @@ export class EntityAggregate {
entityType: string,
entityAction: string,
query: Contracts.ClientPagination,
): Promise<Coins.TransactionDataCollection> {
): Promise<ExtendedTransactionDataCollection> {
const historyKey = `${entityType}.${entityAction}`;

if (!this.#history[historyKey]) {
this.#history[historyKey] = {};
}

const syncedWallets: Wallet[] = this.getWallets();
const syncedWallets: ReadWriteWallet[] = this.getWallets();

const requests: Record<string, Promise<Coins.TransactionDataCollection>> = {};

Expand All @@ -60,6 +62,7 @@ export class EntityAggregate {
senderPublicKey: syncedWallet.publicKey(),
};

let transactions;
if (lastResponse && lastResponse.hasMorePages()) {
return resolve(syncedWallet.client().transactions({ cursor: lastResponse.nextPage(), ...query }));
}
Expand All @@ -68,8 +71,8 @@ export class EntityAggregate {
});
}

const responses = await promiseAllSettledByKey<Coins.TransactionDataCollection>(requests);
const result: Contracts.TransactionDataTypeCollection = [];
const responses = await promiseAllSettledByKey<ExtendedTransactionDataCollection>(requests);
const result: ExtendedTransactionData[] = [];

for (const [id, request] of Object.entries(responses || {})) {
if (request.status === "rejected" || request.value instanceof Error) {
Expand All @@ -87,21 +90,21 @@ export class EntityAggregate {
this.#history[historyKey][id] = request.value;
}

return new Coins.TransactionDataCollection(result, {
return new ExtendedTransactionDataCollection(result, {
prev: undefined,
self: undefined,
next: Number(this.hasMore(historyKey)),
});
}

private getWallet(id: string): Wallet {
private getWallet(id: string): ReadWriteWallet {
return this.#profile.wallets().findById(id);
}

private getWallets(): Wallet[] {
private getWallets(): ReadWriteWallet[] {
return this.#profile
.wallets()
.values()
.filter((wallet: Wallet) => wallet.hasSyncedWithNetwork());
.filter((wallet: ReadWriteWallet) => wallet.hasSyncedWithNetwork());
}
}
@@ -1,31 +1,32 @@
import { Coins, Contracts } from "@arkecosystem/platform-sdk";
import { Contracts } from "@arkecosystem/platform-sdk";

import { ExtendedTransactionDataCollection } from "../../dto/transaction-collection";
import { EntityAggregate } from "./entity-aggregate";

export class EntityRegistrationAggregate extends EntityAggregate {
public async all(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async all(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("all", "register", query);
}

public async businesses(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async businesses(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("business", "register", query);
}

public async delegates(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async delegates(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("delegate", "register", query);
}

public async plugins(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async plugins(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("plugin", "register", query);
}

public async corePlugins(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async corePlugins(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("corePlugin", "register", query);
}

public async desktopWalletPlugins(
query: Contracts.ClientPagination = {},
): Promise<Coins.TransactionDataCollection> {
): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("desktopWalletPlugin", "register", query);
}
}
@@ -1,31 +1,32 @@
import { Coins, Contracts } from "@arkecosystem/platform-sdk";
import { Contracts } from "@arkecosystem/platform-sdk";

import { ExtendedTransactionDataCollection } from "../../dto/transaction-collection";
import { EntityAggregate } from "./entity-aggregate";

export class EntityResignationAggregate extends EntityAggregate {
public async all(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async all(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("all", "resign", query);
}

public async businesses(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async businesses(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("business", "resign", query);
}

public async delegates(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async delegates(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("delegate", "resign", query);
}

public async plugins(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async plugins(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("plugin", "resign", query);
}

public async corePlugins(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async corePlugins(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("corePlugin", "resign", query);
}

public async desktopWalletPlugins(
query: Contracts.ClientPagination = {},
): Promise<Coins.TransactionDataCollection> {
): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("desktopWalletPlugin", "resign", query);
}
}
@@ -1,31 +1,32 @@
import { Coins, Contracts } from "@arkecosystem/platform-sdk";
import { Contracts } from "@arkecosystem/platform-sdk";

import { ExtendedTransactionDataCollection } from "../../dto/transaction-collection";
import { EntityAggregate } from "./entity-aggregate";

export class EntityUpdateAggregate extends EntityAggregate {
public async all(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async all(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("all", "update", query);
}

public async businesses(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async businesses(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("business", "update", query);
}

public async delegates(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async delegates(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("delegate", "update", query);
}

public async plugins(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async plugins(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("plugin", "update", query);
}

public async corePlugins(query: Contracts.ClientPagination = {}): Promise<Coins.TransactionDataCollection> {
public async corePlugins(query: Contracts.ClientPagination = {}): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("corePlugin", "update", query);
}

public async desktopWalletPlugins(
query: Contracts.ClientPagination = {},
): Promise<Coins.TransactionDataCollection> {
): Promise<ExtendedTransactionDataCollection> {
return this.aggregate("desktopWalletPlugin", "update", query);
}
}
@@ -1,4 +1,4 @@
import { Wallet } from "../../wallets/wallet";
import { ReadWriteWallet } from "../../wallets/wallet.models";
import { ProfileContract } from "../profile.models";

export class RegistrationAggregate {
Expand All @@ -8,10 +8,10 @@ export class RegistrationAggregate {
this.#profile = profile;
}

public delegates(): Wallet[] {
public delegates(): ReadWriteWallet[] {
return this.#profile
.wallets()
.values()
.filter((wallet: Wallet) => wallet.isDelegate());
.filter((wallet: ReadWriteWallet) => wallet.isDelegate());
}
}
@@ -1,11 +1,11 @@
import "jest-extended";

import { Coins } from "@arkecosystem/platform-sdk";
import { ARK } from "@arkecosystem/platform-sdk-ark";
import { Request } from "@arkecosystem/platform-sdk-http-got";
import nock from "nock";

import { identity } from "../../../test/fixtures/identity";
import { ExtendedTransactionDataCollection } from "../../dto/transaction-collection";
import { container } from "../../environment/container";
import { Identifiers } from "../../environment/container.models";
import { Profile } from "../profile";
Expand Down Expand Up @@ -51,7 +51,7 @@ describe.each(["transactions", "sentTransactions", "receivedTransactions"])("%s"

const result = await subject[method]();

expect(result).toBeInstanceOf(Coins.TransactionDataCollection);
expect(result).toBeInstanceOf(ExtendedTransactionDataCollection);
expect(result.items()).toHaveLength(100);
});

Expand All @@ -62,7 +62,7 @@ describe.each(["transactions", "sentTransactions", "receivedTransactions"])("%s"

const result = await subject[method]();

expect(result).toBeInstanceOf(Coins.TransactionDataCollection);
expect(result).toBeInstanceOf(ExtendedTransactionDataCollection);
expect(result.items()).toHaveLength(100);
expect(subject.hasMore(method)).toBeFalse();
});
Expand All @@ -72,7 +72,7 @@ describe.each(["transactions", "sentTransactions", "receivedTransactions"])("%s"

const result = await subject[method]();

expect(result).toBeInstanceOf(Coins.TransactionDataCollection);
expect(result).toBeInstanceOf(ExtendedTransactionDataCollection);
expect(result.items()).toHaveLength(0);
expect(subject.hasMore(method)).toBeFalse();
});
Expand All @@ -84,7 +84,7 @@ describe.each(["transactions", "sentTransactions", "receivedTransactions"])("%s"

const result = await subject[method]();

expect(result).toBeInstanceOf(Coins.TransactionDataCollection);
expect(result).toBeInstanceOf(ExtendedTransactionDataCollection);
expect(result.items()).toHaveLength(0);
expect(subject.hasMore(method)).toBeFalse();
});
Expand All @@ -99,21 +99,21 @@ describe.each(["transactions", "sentTransactions", "receivedTransactions"])("%s"
// We receive a response that does contain a "next" cursor
const firstRequest = await subject[method]();

expect(firstRequest).toBeInstanceOf(Coins.TransactionDataCollection);
expect(firstRequest).toBeInstanceOf(ExtendedTransactionDataCollection);
expect(firstRequest.items()).toHaveLength(100);
expect(subject.hasMore(method)).toBeTrue();

// We receive a response that does not contain a "next" cursor
const secondRequest = await subject[method]();

expect(secondRequest).toBeInstanceOf(Coins.TransactionDataCollection);
expect(secondRequest).toBeInstanceOf(ExtendedTransactionDataCollection);
expect(secondRequest.items()).toHaveLength(100);
expect(subject.hasMore(method)).toBeFalse();

// We do not send any requests because no more data is available
const thirdRequest = await subject[method]();

expect(thirdRequest).toBeInstanceOf(Coins.TransactionDataCollection);
expect(thirdRequest).toBeInstanceOf(ExtendedTransactionDataCollection);
expect(thirdRequest.items()).toHaveLength(0);
expect(subject.hasMore(method)).toBeFalse();
});
Expand Down