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

Commit

Permalink
[WIP] LL-4407 Portfolio V2 (#1083)
Browse files Browse the repository at this point in the history
* add portfolio-new mod

* [WIP] getCurrencyPortfolio

* [WIP] test

* add test for getBalanceHistoryWithCountervalue

* fix

* [WIP] getPortfolio test

* small fix

* fix getCurrencyPortfolio

* mv portfolio-new to portfolio/v2

* remove memo

* fix all time count

* fix count and flow

* Add benchmark-portfolio

* fixes tests

* rm log

Co-authored-by: Gaëtan Renaudeau <[email protected]>
  • Loading branch information
JunichiSugiura and gre authored Mar 15, 2021
1 parent 3857781 commit 145cd83
Show file tree
Hide file tree
Showing 14 changed files with 2,632 additions and 12 deletions.
18 changes: 14 additions & 4 deletions cli/src/commands/botPortfolio.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import { from, defer } from "rxjs";
import { filter, map, mergeAll } from "rxjs/operators";
import { listSupportedCurrencies } from "@ledgerhq/live-common/lib/currencies";
import { getCurrencyBridge } from "@ledgerhq/live-common/lib/bridge";
import { formatAccount } from "@ledgerhq/live-common/lib/account/formatters";
import { accountFormatters } from "@ledgerhq/live-common/lib/account";

const blacklist = ["decred", "tezos", "stellar", "ethereum_classic"];

export default {
description:
"Use speculos and a list of supported coins to retrieve all accounts",
args: [],
job: () => {
args: [
{
name: "format",
alias: "f",
type: String,
typeDesc: Object.keys(accountFormatters).join(" | "),
desc: "how to display the data",
},
],
job: (opts: { format: string }) => {
return from(listSupportedCurrencies()).pipe(
filter((c) => !blacklist.includes(c.id) && !c.isTestnetFor),
map((currency) =>
Expand All @@ -25,7 +33,9 @@ export default {
),
mergeAll(5),
filter((e) => e.type === "discovered"),
map((e) => formatAccount(e.account))
map((e) =>
(accountFormatters[opts.format] || accountFormatters.default)(e.account)
)
);
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"@testing-library/react-hooks": "^4.0.1",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.6.3",
"benchmark": "^2.1.4",
"cross-env": "^7.0.3",
"eslint": "^7.21.0",
"eslint-config-airbnb": "^18.2.1",
Expand Down
71 changes: 71 additions & 0 deletions scripts/benchmark-portfolio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* eslint-disable no-console */
var Benchmark = require("benchmark");
var { reduce } = require("rxjs/operators");
var { BigNumber } = require("bignumber.js");
var { getFiatCurrencyByTicker } = require("../lib/currencies");
var { fromAccountRaw } = require("../lib/account");
var portfolioV1 = require("../lib/portfolio");
var portfolioV2 = require("../lib/portfolio/v2");
var {
initialState,
calculate,
loadCountervalues,
inferTrackingPairForAccounts,
} = require("../lib/countervalues/logic");
var { jsonFromFile } = require("../cli/lib/stream");

const cvCurrency = getFiatCurrencyByTicker("USD");

async function main() {
const accountsRaw = await jsonFromFile("test-data/mere-denis.json")
.pipe(reduce((acc, a) => acc.concat(a), []))
.toPromise();
const accounts = accountsRaw.map(fromAccountRaw);

const cvState = await loadCountervalues(initialState, {
trackingPairs: inferTrackingPairForAccounts(accounts, cvCurrency),
autofillGaps: true,
});

const calc = (c, v, date) =>
BigNumber(
calculate(cvState, {
date,
value: v.toNumber(),
from: c,
to: cvCurrency,
}) || 0
);

new Benchmark.Suite()
.on("cycle", function (event) {
console.log(String(event.target));
})
.on("complete", function () {
console.log("Fastest is " + this.filter("fastest").map("name"));
})
.add("V1: getPortfolio", function () {
portfolioV1.getPortfolio(accounts, "year", calc);
})
.add("V2: getPortfolio", function () {
portfolioV2.getPortfolio(accounts, "year", cvState, cvCurrency);
})
.run();

new Benchmark.Suite()
.on("cycle", function (event) {
console.log(String(event.target));
})
.on("complete", function () {
console.log("Fastest is " + this.filter("fastest").map("name"));
})
.add("V1: getAssetsDistribution", function () {
portfolioV1.getAssetsDistribution(accounts, calc);
})
.add("V2: getAssetsDistribution", function () {
portfolioV2.getAssetsDistribution(accounts, cvState, cvCurrency);
})
.run();
}

main();
Loading

1 comment on commit 145cd83

@vercel
Copy link

@vercel vercel bot commented on 145cd83 Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Please sign in to comment.