Skip to content

Commit

Permalink
Merge pull request #2351 from dusk-network/feature-2350
Browse files Browse the repository at this point in the history
web-wallet: Add base functionalities for the cache layer
  • Loading branch information
ascartabelli authored Sep 12, 2024
2 parents 6cbca76 + 5707b44 commit 81be86e
Show file tree
Hide file tree
Showing 14 changed files with 2,162 additions and 11 deletions.
38 changes: 28 additions & 10 deletions web-wallet/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions web-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@web3modal/wagmi": "5.1.0",
"bip39": "3.1.0",
"css-doodle": "0.39.2",
"dexie": "4.0.8",
"lamb": "0.61.1",
"qr-scanner": "1.4.2",
"qrcode": "1.5.3",
Expand All @@ -66,6 +67,7 @@
"eslint-config-prettier": "9.1.0",
"eslint-import-resolver-custom-alias": "1.3.2",
"eslint-plugin-svelte": "2.41.0",
"fake-indexeddb": "6.0.0",
"jsdom": "24.1.0",
"jsdom-worker": "0.3.0",
"lamb-types": "0.61.7",
Expand Down
26 changes: 26 additions & 0 deletions web-wallet/src/lib/dusk/array/__tests__/arrayMaxByKey.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, it } from "vitest";

import { arrayMaxByKey } from "..";

describe("arrayMaxByKey", () => {
const testArr = [
{ active: true, date: new Date(2024, 1, 2), uid: "id3", value: 6, x: 0 },
{ active: false, date: new Date(2024, 1, 3), uid: "id2", value: 3, x: -0 },
{ active: false, date: new Date(2024, 1, 4), uid: "id1", value: 5, x: -1 },
];

it("should get the max value of a key holding a comparable value in an array of objects", () => {
expect(arrayMaxByKey("active")(testArr)).toBe(true);
expect(arrayMaxByKey("date")(testArr)).toStrictEqual(new Date(2024, 1, 4));
expect(arrayMaxByKey("uid")(testArr)).toBe("id3");
expect(arrayMaxByKey("value")(testArr)).toBe(6);
});

it("will use the last encountered value if `0` or `-0` are the max value", () => {
expect(arrayMaxByKey("x")(testArr)).toBe(-0);
});

it("should return `undefined` if given an empty array", () => {
expect(arrayMaxByKey("value")([])).toBe(undefined);
});
});
27 changes: 27 additions & 0 deletions web-wallet/src/lib/dusk/array/arrayMaxByKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { reduce } from "lamb";

/**
* Gets the max value of the given key in an array of objects.
*
*
* @example
* const scores = [
* { score: 7, user: "John" },
* { score: 9, user: "Jane" },
* { score: 5, user: "Mario" }
* ];
* const getMaxScore = arrayMaxByKey("score");
*
* getMaxScore(scores) // => 9
*
* @template {string} K
* @template {Record<PropertyKey, any> & Record<K, import("lamb").Ord>} S
* @param {K} key
* @returns {<const L extends S[]>(array: L) => L extends [] ? undefined : typeof array[number][K]}
*/
const arrayMaxByKey = (key) => (array) =>
array.length === 0
? undefined
: reduce(array, (r, c) => (r[key] > c[key] ? r : c))[key];

export default arrayMaxByKey;
1 change: 1 addition & 0 deletions web-wallet/src/lib/dusk/array/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as arraysEqual } from "./arraysEqual";
export { default as arrayMaxByKey } from "./arrayMaxByKey";
export { default as shuffleArray } from "./shuffleArray";
export { default as findFirstNMatches } from "./findFirstNMatches";
73 changes: 73 additions & 0 deletions web-wallet/src/lib/mock-data/cache-history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-disable camelcase */

/** @type {WalletCacheHistoryEntry[]} */
export default [
{
history: [
{
amount: 400,
block_height: 201605,
direction: "In",
fee: 0.000290986,
id: "678e9430fd8ca2e8b6161dd6ea30e4b923505a446dac77bdbe911ef3f50e450d",
tx_type: "TRANSFER",
},
{
amount: 5000,
block_height: 201643,
direction: "In",
fee: 0.00030326,
id: "eeeca070ed7650fc2692be43ba36f5d74c0168eca92680debe0de172f64a130b",
tx_type: "TRANSFER",
},
{
amount: -15.00030345,
block_height: 3504088,
direction: "Out",
fee: 0.00030345,
id: "e3d9b492474d76da6edc415dd04d47d3fc40ecb0b49ee8e1dd087eb3dab0100a",
tx_type: "TRANSFER",
},
{
amount: -8.000291559,
block_height: 3504122,
direction: "Out",
fee: 0.000291559,
id: "8e435db8584eac07e3a7f69afbdf2d00b8176271695ea1e1b4da79922f3c050c",
tx_type: "TRANSFER",
},
],
lastBlockHeight: 3504122,
psk: "43UC3K4oy7wNBDQR4AUdkwrT1iB9ucU65GvJ8kkqB6BGwz9fwPEqzwqsJD2Rgc1UYegxXSsJmrqN2zEm2ycHitWG",
},
{
history: [
{
amount: 10,
block_height: 3101148,
direction: "In",
fee: 0.00029775,
id: "5847cc9b96407fdc9ff973c3095f0d34958a47ba7272053f43038ba8e6afaf02",
tx_type: "TRANSFER",
},
{
amount: -5.000284098,
block_height: 3101191,
direction: "Out",
fee: 0.000284098,
id: "84d0c5727b93c47e9827524a77ea1c7492cc775faf117d26a53df6a97b791309",
tx_type: "TRANSFER",
},
{
amount: 1,
block_height: 3101764,
direction: "In",
fee: 0.000296467,
id: "4d9bb16589f6b723408305744eada6c05aaa4b27296ec440055c487694ce0a05",
tx_type: "TRANSFER",
},
],
lastBlockHeight: 3101764,
psk: "4Vd2cY4XAuqpcpiVFVCtveuZq7o9PV1WhmqqdNNAsZwWEojKwctU3ud7PqciKCpUnJWjZPspudQwbNwYT6Fq8Bzo",
},
];
Loading

0 comments on commit 81be86e

Please sign in to comment.