This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(profiles): implement
CoinRepository
for direct coin interactio…
…ns (#669)
- Loading branch information
1 parent
0f5ae56
commit 7e5bac8
Showing
10 changed files
with
120 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/platform-sdk-profiles/src/environment/coin-repository.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import "jest-extended"; | ||
|
||
import { ARK } from "@arkecosystem/platform-sdk-ark"; | ||
import { Request } from "@arkecosystem/platform-sdk-http-got"; | ||
import nock from "nock"; | ||
|
||
import { container } from "../environment/container"; | ||
import { Identifiers } from "../environment/container.models"; | ||
import { CoinRepository } from "./coin-repository"; | ||
|
||
let subject: CoinRepository; | ||
|
||
beforeAll(() => { | ||
nock.disableNetConnect(); | ||
|
||
nock(/.+/) | ||
.get("/api/node/configuration") | ||
.reply(200, require("../../test/fixtures/client/configuration.json")) | ||
.get("/api/node/configuration/crypto") | ||
.reply(200, require("../../test/fixtures/client/cryptoConfiguration.json")) | ||
.get("/api/node/syncing") | ||
.reply(200, require("../../test/fixtures/client/syncing.json")) | ||
.get("/api/peers") | ||
.reply(200, require("../../test/fixtures/client/peers.json")) | ||
.get("/api/delegates?page=1") | ||
.reply(200, require("../../test/fixtures/client/delegates-1.json")) | ||
.get("/api/delegates?page=2") | ||
.reply(200, require("../../test/fixtures/client/delegates-2.json")) | ||
.persist(); | ||
|
||
container.set(Identifiers.HttpClient, new Request()); | ||
container.set(Identifiers.Coins, { ARK }); | ||
}); | ||
|
||
beforeEach(() => (subject = new CoinRepository())); | ||
|
||
it("should sync the delegates", async () => { | ||
expect(subject.delegates("ARK", "devnet")).toBeUndefined(); | ||
|
||
await subject.syncDelegates("ARK", "devnet"); | ||
|
||
expect(subject.delegates("ARK", "devnet")).toBeArray(); | ||
expect(subject.delegates("ARK", "devnet")).toHaveLength(200); | ||
}); |
37 changes: 37 additions & 0 deletions
37
packages/platform-sdk-profiles/src/environment/coin-repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Coins, Contracts } from "@arkecosystem/platform-sdk"; | ||
|
||
import { DataRepository } from "../repositories/data-repository"; | ||
import { makeCoin } from "./container.helpers"; | ||
|
||
export class CoinRepository { | ||
readonly #dataRepository: DataRepository = new DataRepository(); | ||
|
||
public delegates(coin: string, network: string): any { | ||
return this.#dataRepository.get(`${coin}.${network}.delegates`); | ||
} | ||
|
||
public async syncDelegates(coin: string, network: string): Promise<void> { | ||
const instance: Coins.Coin = await makeCoin(coin, network); | ||
const instanceKey = `${coin}.${network}.delegates`; | ||
|
||
let result: Contracts.WalletData[] = []; | ||
let hasMore = true; | ||
// TODO: use the nextPage() method as cursor like aggregates | ||
let cursor = 1; | ||
|
||
while (hasMore) { | ||
const response = await instance.client().delegates({ cursor }); | ||
|
||
result = result.concat(response.items()); | ||
|
||
hasMore = response.hasMorePages(); | ||
|
||
cursor++; | ||
} | ||
|
||
this.#dataRepository.set( | ||
instanceKey, | ||
result.map((delegate: Contracts.WalletData) => delegate.toObject()), | ||
); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/platform-sdk-profiles/src/environment/container.models.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 9 additions & 4 deletions
13
packages/platform-sdk-profiles/src/wallets/mappers/delegate-mapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters