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

refactor(profiles): decouple DelegateMapper from Wallet #671

Merged
merged 2 commits into from Aug 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/platform-sdk-profiles/src/dto/transaction.ts
@@ -1,6 +1,7 @@
import { Coins, Contracts } from "@arkecosystem/platform-sdk";
import { DateTime } from "@arkecosystem/platform-sdk-intl";
import { BigNumber } from "@arkecosystem/platform-sdk-support";
import { ReadOnlyWallet } from "../wallets/read-only-wallet";

import { ReadWriteWallet } from "../wallets/wallet.models";

Expand Down Expand Up @@ -489,12 +490,10 @@ export class SecondSignatureData extends TransactionData implements Contracts.Se
export class TransferData extends TransactionData implements Contracts.TransferData {}

export class VoteData extends TransactionData implements Contracts.VoteData {
// TODO: map those to delegate wallets
public votes(): string[] {
return this.data<Contracts.VoteData>().votes();
}

// TODO: map those to delegate wallets
public unvotes(): string[] {
return this.data<Contracts.VoteData>().unvotes();
}
Expand Down
Expand Up @@ -7,7 +7,15 @@ export class CoinRepository {
readonly #dataRepository: DataRepository = new DataRepository();

public delegates(coin: string, network: string): any {
return this.#dataRepository.get(`${coin}.${network}.delegates`);
const delegates = this.#dataRepository.get(`${coin}.${network}.delegates`);

if (delegates === undefined) {
throw new Error(
`The delegates for [${coin}.${network}] have not been synchronized yet. Please call [syncDelegates] before using this method.`,
);
}

return delegates;
}

public async syncDelegates(coin: string, network: string): Promise<void> {
Expand Down
Expand Up @@ -23,7 +23,9 @@ export class EntityAggregate {
senderPublicKey: this.#wallet.publicKey(),
});

result.transform((transaction) => transformTransactionData(this.#wallet, transaction));
result.transform((transaction: Contracts.TransactionDataType) =>
transformTransactionData(this.#wallet, transaction),
);

return result;
}
Expand Down
Expand Up @@ -56,7 +56,7 @@ it("should map the public keys to read-only wallets", async () => {

await container.get<CoinRepository>(Identifiers.CoinRepository).syncDelegates(wallet.coinId(), wallet.networkId());

const mappedDelegates = new DelegateMapper(wallet).map(publicKeys);
const mappedDelegates = DelegateMapper.execute("ARK", "devnet", publicKeys);

expect(mappedDelegates).toBeArray();
expect(mappedDelegates).toHaveLength(100);
Expand Down
Expand Up @@ -2,19 +2,12 @@ import { CoinRepository } from "../../environment/coin-repository";
import { container } from "../../environment/container";
import { Identifiers } from "../../environment/container.models";
import { ReadOnlyWallet } from "../read-only-wallet";
import { ReadWriteWallet } from "../wallet.models";

export class DelegateMapper {
readonly #wallet: ReadWriteWallet;

public constructor(wallet: ReadWriteWallet) {
this.#wallet = wallet;
}

public map(publicKeys: string[]): ReadOnlyWallet[] {
public static execute(coin: string, network: string, publicKeys: string[]): ReadOnlyWallet[] {
const delegates: Record<string, string>[] = container
.get<CoinRepository>(Identifiers.CoinRepository)
.delegates(this.#wallet.coinId(), this.#wallet.networkId());
.delegates(coin, network);

return publicKeys
.map((publicKey: string) => {
Expand Down
@@ -1,7 +1,7 @@
import { Contracts, DTO } from "@arkecosystem/platform-sdk";

import { SignedTransactionData } from "./dto/signed-transaction";
import { WalletData } from "./wallet.models";
import { ReadWriteWallet, WalletData } from "./wallet.models";

type SignedTransactionDataDictionary = Record<string, Contracts.SignedTransactionData>;

Expand All @@ -16,11 +16,11 @@ type SignedTransactionDataDictionary = Record<string, Contracts.SignedTransactio
* some use an address, others a public key and again others a WIF.
*/
export class TransactionService {
readonly #wallet;
readonly #wallet: ReadWriteWallet;
readonly #signed: SignedTransactionDataDictionary = {};
readonly #broadcasted: SignedTransactionDataDictionary = {};

public constructor(wallet) {
public constructor(wallet: ReadWriteWallet) {
this.#wallet = wallet;

this.restore();
Expand Down Expand Up @@ -215,7 +215,11 @@ export class TransactionService {
*/
public restore(): void {
const restoreStorage = (storage: object, storageKey: string) => {
const transactions = this.#wallet.data().get(storageKey, {});
const transactions: object | undefined = this.#wallet.data().get(storageKey, {});

if (!transactions) {
return;
}

for (const [id, transaction] of Object.entries(transactions)) {
this.assertHasValidIdentifier(id);
Expand Down
Expand Up @@ -110,7 +110,6 @@ export interface ReadWriteWallet {
entityRegistrationAggregate(): EntityRegistrationAggregate;
entityResignationAggregate(): EntityResignationAggregate;
entityUpdateAggregate(): EntityUpdateAggregate;
mapDelegates(publicKeys: string[]): ReadOnlyWallet[];
syncIdentity(): Promise<void>;
syncVotes(): Promise<void>;
syncExchangeRate(): Promise<void>;
Expand Down
13 changes: 2 additions & 11 deletions packages/platform-sdk-profiles/src/wallets/wallet.ts
Expand Up @@ -370,7 +370,7 @@ export class Wallet implements ReadWriteWallet {
throw new Error("The voting data has not been synced. Please call [syncVotes] before accessing votes.");
}

return this.mapDelegates(votes);
return DelegateMapper.execute(this.coinId(), this.networkId(), votes);
}

public votesAvailable(): number {
Expand Down Expand Up @@ -417,15 +417,6 @@ export class Wallet implements ReadWriteWallet {
return this.#entityUpdateAggregate;
}

/**
* These methods serve as helpers to map wallet identifiers to various
* wallet instances that expose information like an avatar or balances.
*/

public mapDelegates(publicKeys: string[]): ReadOnlyWallet[] {
return new DelegateMapper(this).map(publicKeys);
}

/**
* These methods serve as helpers to keep the wallet data updated.
*/
Expand Down Expand Up @@ -482,7 +473,7 @@ export class Wallet implements ReadWriteWallet {
transaction.setMeta("publicKey", this.publicKey());
}

result.transform((transaction) => transformTransactionData(this, transaction));
result.transform((transaction: Contracts.TransactionDataType) => transformTransactionData(this, transaction));

return result;
}
Expand Down