diff --git a/web-wallet/__mocks__/Wallet.js b/web-wallet/__mocks__/Wallet.js index f465ef3a9..1a670daa1 100644 --- a/web-wallet/__mocks__/Wallet.js +++ b/web-wallet/__mocks__/Wallet.js @@ -6,6 +6,10 @@ class Wallet { this.wasm = {}; } + static get networkBlockHeight() { + return Promise.resolve(0); + } + gasLimit; gasPrice; seed; diff --git a/web-wallet/package-lock.json b/web-wallet/package-lock.json index a463cb784..f1669f4f3 100644 --- a/web-wallet/package-lock.json +++ b/web-wallet/package-lock.json @@ -9,7 +9,7 @@ "version": "0.5.0", "license": "MPL-2.0", "dependencies": { - "@dusk-network/dusk-wallet-js": "0.4.2", + "@dusk-network/dusk-wallet-js": "0.4.4", "@floating-ui/dom": "1.6.3", "@mdi/js": "7.4.47", "bip39": "3.1.0", @@ -539,11 +539,11 @@ } }, "node_modules/@dusk-network/dusk-wallet-js": { - "version": "0.4.2", - "resolved": "https://registry.npmjs.org/@dusk-network/dusk-wallet-js/-/dusk-wallet-js-0.4.2.tgz", - "integrity": "sha512-9Jy/Amm4oKkPvjw9/xo7/RRwm9U6fRobkFArVcPXwN5sL2uz6/UPd7b6w4X5bBTrdEyCZHNBwtslmwAHrwV6dA==", + "version": "0.4.4", + "resolved": "https://registry.npmjs.org/@dusk-network/dusk-wallet-js/-/dusk-wallet-js-0.4.4.tgz", + "integrity": "sha512-JKRdFgQ4oRGwdrROyYGw+Fo3mp9P0WBty17wMnB2dM2hJW8XF1fohEPw+aEFhBCxDg2qXcBa8NSE9Dst5C4swg==", "dependencies": { - "dexie": "3.2.4", + "dexie": "3.2.7", "fake-indexeddb": "5.0.1" } }, @@ -3080,9 +3080,9 @@ "dev": true }, "node_modules/dexie": { - "version": "3.2.4", - "resolved": "https://registry.npmjs.org/dexie/-/dexie-3.2.4.tgz", - "integrity": "sha512-VKoTQRSv7+RnffpOJ3Dh6ozknBqzWw/F3iqMdsZg958R0AS8AnY9x9d1lbwENr0gzeGJHXKcGhAMRaqys6SxqA==", + "version": "3.2.7", + "resolved": "https://registry.npmjs.org/dexie/-/dexie-3.2.7.tgz", + "integrity": "sha512-2a+BXvVhY5op+smDRLxeBAivE7YcYaneXJ1la3HOkUfX9zKkE/AJ8CNgjiXbtXepFyFmJNGSbmjOwqbT749r/w==", "engines": { "node": ">=6.0" } diff --git a/web-wallet/package.json b/web-wallet/package.json index a2b273174..f2b876c5c 100644 --- a/web-wallet/package.json +++ b/web-wallet/package.json @@ -35,7 +35,7 @@ "type": "module", "version": "0.5.0", "dependencies": { - "@dusk-network/dusk-wallet-js": "0.4.2", + "@dusk-network/dusk-wallet-js": "0.4.4", "@floating-ui/dom": "1.6.3", "@mdi/js": "7.4.47", "bip39": "3.1.0", diff --git a/web-wallet/src/lib/containers/StakeContract/StakeContract.svelte b/web-wallet/src/lib/containers/StakeContract/StakeContract.svelte index 94e6e52eb..ef5db5aca 100644 --- a/web-wallet/src/lib/containers/StakeContract/StakeContract.svelte +++ b/web-wallet/src/lib/containers/StakeContract/StakeContract.svelte @@ -55,13 +55,15 @@ const executeOperations = { stake: (amount, gasPrice, gasLimit) => walletStore - .stake(amount, gasPrice, gasLimit) + .stake(amount, { limit: gasLimit, price: gasPrice }) .then(getLastTransactionHash), unstake: (gasPrice, gasLimit) => - walletStore.unstake(gasPrice, gasLimit).then(getLastTransactionHash), + walletStore + .unstake({ limit: gasLimit, price: gasPrice }) + .then(getLastTransactionHash), "withdraw-rewards": (gasPrice, gasLimit) => walletStore - .withdrawReward(gasPrice, gasLimit) + .withdrawReward({ limit: gasLimit, price: gasPrice }) .then(getLastTransactionHash), }; diff --git a/web-wallet/src/lib/containers/TransferContract/TransferContract.svelte b/web-wallet/src/lib/containers/TransferContract/TransferContract.svelte index 245841915..7b43cf280 100644 --- a/web-wallet/src/lib/containers/TransferContract/TransferContract.svelte +++ b/web-wallet/src/lib/containers/TransferContract/TransferContract.svelte @@ -33,7 +33,7 @@ /** @type {(to: string, amount: number, gasPrice:number, gasLimit:number) => Promise} */ const executeSend = (to, amount, gasPrice, gasLimit) => walletStore - .transfer(to, amount, gasPrice, gasLimit) + .transfer(to, amount, { limit: gasLimit, price: gasPrice }) .then(getLastTransactionHash); const collectSettings = collect([ diff --git a/web-wallet/src/lib/services/wallet/__tests__/getWallet.spec.js b/web-wallet/src/lib/services/wallet/__tests__/getWallet.spec.js index a220c097f..4cbc08a4d 100644 --- a/web-wallet/src/lib/services/wallet/__tests__/getWallet.spec.js +++ b/web-wallet/src/lib/services/wallet/__tests__/getWallet.spec.js @@ -21,8 +21,6 @@ describe("getWallet", () => { [ "wasm", "seed", - "gasLimit", - "gasPrice", "constructor", "getBalance", "getPsks", diff --git a/web-wallet/src/lib/services/wallet/getWallet.js b/web-wallet/src/lib/services/wallet/getWallet.js index 49016adde..554efc586 100644 --- a/web-wallet/src/lib/services/wallet/getWallet.js +++ b/web-wallet/src/lib/services/wallet/getWallet.js @@ -3,12 +3,9 @@ import { Wallet } from "@dusk-network/dusk-wallet-js"; /** * Gets a `Wallet` instance. * @param {Uint8Array} seed - * @param {Number} [gasLimit=2900000000] - * @param {Number} [gasPrice=1] * @returns {Wallet} */ -const getWallet = (seed, gasLimit, gasPrice) => - new Wallet(Array.from(seed), gasLimit, gasPrice); +const getWallet = (seed) => new Wallet(Array.from(seed)); export default getWallet; diff --git a/web-wallet/src/lib/stores/__tests__/walletStore.spec.js b/web-wallet/src/lib/stores/__tests__/walletStore.spec.js index 698eb2a81..784a14649 100644 --- a/web-wallet/src/lib/stores/__tests__/walletStore.spec.js +++ b/web-wallet/src/lib/stores/__tests__/walletStore.spec.js @@ -21,6 +21,9 @@ describe("walletStore", async () => { const balance = { maximum: 100, value: 1 }; const wallet = new Wallet([]); + const blockHeightSpy = vi + .spyOn(Wallet, "networkBlockHeight", "get") + .mockResolvedValue(1536); const getBalanceSpy = vi .spyOn(Wallet.prototype, "getBalance") .mockResolvedValue(balance); @@ -68,12 +71,13 @@ describe("walletStore", async () => { currentAddress: addresses[0], initialized: true, }; - const gas = { + const gasSettings = { limit: 30000000, price: 1, }; afterEach(() => { + blockHeightSpy.mockClear(); getBalanceSpy.mockClear(); getPsksSpy.mockClear(); historySpy.mockClear(); @@ -87,6 +91,7 @@ describe("walletStore", async () => { }); afterAll(() => { + blockHeightSpy.mockRestore(); getBalanceSpy.mockRestore(); getPsksSpy.mockRestore(); historySpy.mockRestore(); @@ -135,6 +140,35 @@ describe("walletStore", async () => { expect(get(walletStore)).toStrictEqual(initializedStore); }); + it("should allow to start the sync from a specific block height after initializing the wallet", async () => { + const from = 9999; + + await walletStore.init(wallet, from); + + expect(get(walletStore)).toStrictEqual({ + ...initialState, + addresses: addresses, + currentAddress: addresses[0], + error: null, + initialized: true, + isSyncing: true, + }); + + expect(getPsksSpy).toHaveBeenCalledTimes(1); + expect(getBalanceSpy).not.toHaveBeenCalled(); + + await vi.advanceTimersToNextTimerAsync(); + + expect(syncSpy).toHaveBeenCalledTimes(1); + expect(syncSpy).toHaveBeenCalledWith({ + from, + signal: expect.any(AbortSignal), + }); + expect(getBalanceSpy).toHaveBeenCalledTimes(1); + expect(getBalanceSpy).toHaveBeenCalledWith(addresses[0]); + expect(get(walletStore)).toStrictEqual(initializedStore); + }); + it("should set the sync error in the store if the sync fails", async () => { walletStore.reset(); @@ -293,16 +327,59 @@ describe("walletStore", async () => { expect(getBalanceSpy).toHaveBeenCalledTimes(1); expect(getBalanceSpy).toHaveBeenCalledWith(addresses[0]); expect(syncSpy).toHaveBeenCalledTimes(1); + expect(syncSpy).toHaveBeenCalledWith({ signal: expect.any(AbortSignal) }); await vi.advanceTimersToNextTimerAsync(); expect(get(walletStore)).toStrictEqual(initializedStore); }); + it("should allow to start the sync from a specific block height after clearing and initializing the wallet", async () => { + getPsksSpy.mockClear(); + getBalanceSpy.mockClear(); + syncSpy.mockClear(); + walletStore.reset(); + + const from = 4276; + + await walletStore.clearLocalDataAndInit(wallet, from); + + expect(get(walletStore)).toStrictEqual({ + ...initialState, + addresses: addresses, + currentAddress: addresses[0], + error: null, + initialized: true, + isSyncing: true, + }); + + await vi.advanceTimersToNextTimerAsync(); + + expect(getPsksSpy).toHaveBeenCalledTimes(1); + expect(getBalanceSpy).toHaveBeenCalledTimes(1); + expect(getBalanceSpy).toHaveBeenCalledWith(addresses[0]); + expect(syncSpy).toHaveBeenCalledTimes(1); + expect(syncSpy).toHaveBeenCalledWith({ + from, + signal: expect.any(AbortSignal), + }); + + await vi.advanceTimersToNextTimerAsync(); + + expect(get(walletStore)).toStrictEqual(initializedStore); + }); + + it("should expose a method to retrieve the current block height", async () => { + await walletStore.getCurrentBlockHeight(); + + expect(blockHeightSpy).toHaveBeenCalledTimes(1); + }); + it("should expose a method to retrieve the stake info", async () => { await walletStore.getStakeInfo(); expect(syncSpy).toHaveBeenCalledTimes(1); + expect(syncSpy).toHaveBeenCalledWith({ signal: expect.any(AbortSignal) }); expect(stakeInfoSpy).toHaveBeenCalledTimes(1); expect(stakeInfoSpy).toHaveBeenCalledWith(currentAddress); expect(syncSpy.mock.invocationCallOrder[0]).toBeLessThan( @@ -329,6 +406,7 @@ describe("walletStore", async () => { const result = await walletStore.getStakeInfo(); expect(syncSpy).toHaveBeenCalledTimes(1); + expect(syncSpy).toHaveBeenCalledWith({ signal: expect.any(AbortSignal) }); expect(stakeInfoSpy).toHaveBeenCalledTimes(1); expect(stakeInfoSpy).toHaveBeenCalledWith(currentAddress); expect(syncSpy.mock.invocationCallOrder[0]).toBeLessThan( @@ -341,6 +419,7 @@ describe("walletStore", async () => { await walletStore.getTransactionsHistory(); expect(syncSpy).toHaveBeenCalledTimes(1); + expect(syncSpy).toHaveBeenCalledWith({ signal: expect.any(AbortSignal) }); expect(historySpy).toHaveBeenCalledTimes(1); expect(historySpy).toHaveBeenCalledWith(currentAddress); expect(syncSpy.mock.invocationCallOrder[0]).toBeLessThan( @@ -362,6 +441,7 @@ describe("walletStore", async () => { await walletStore.setCurrentAddress(addresses[1]); expect(syncSpy).toHaveBeenCalledTimes(1); + expect(syncSpy).toHaveBeenCalledWith({ signal: expect.any(AbortSignal) }); expect(get(walletStore).currentAddress).toBe(addresses[1]); expect(setCurrentAddressSpy.mock.invocationCallOrder[0]).toBeLessThan( syncSpy.mock.invocationCallOrder[0] @@ -378,14 +458,17 @@ describe("walletStore", async () => { }); it("should expose a method to allow to stake an amount of Dusk", async () => { - await walletStore.stake(10, gas.price, gas.limit); - - expect(wallet.gasLimit).toBe(gas.limit); - expect(wallet.gasPrice).toBe(gas.price); + await walletStore.stake(10, gasSettings); expect(syncSpy).toHaveBeenCalledTimes(2); + expect(syncSpy).toHaveBeenNthCalledWith(1, { + signal: expect.any(AbortSignal), + }); + expect(syncSpy).toHaveBeenNthCalledWith(2, { + signal: expect.any(AbortSignal), + }); expect(stakeSpy).toHaveBeenCalledTimes(1); - expect(stakeSpy).toHaveBeenCalledWith(currentAddress, 10); + expect(stakeSpy).toHaveBeenCalledWith(currentAddress, 10, gasSettings); expect(syncSpy.mock.invocationCallOrder[0]).toBeLessThan( stakeSpy.mock.invocationCallOrder[0] ); @@ -394,18 +477,41 @@ describe("walletStore", async () => { ); }); - it("should expose a method to allow to transfer an amount of Dusk", async () => { - await walletStore.transfer(addresses[1], 10, gas.price, gas.limit); + it("should expose a method to manually start a synchronization", async () => { + await walletStore.sync(); - expect(wallet.gasLimit).toBe(gas.limit); - expect(wallet.gasPrice).toBe(gas.price); + expect(syncSpy).toHaveBeenCalledTimes(1); + expect(syncSpy).toHaveBeenCalledWith({ signal: expect.any(AbortSignal) }); + }); + + it("should allow to start a synchronization from a specific block height", async () => { + const from = 7654; + + await walletStore.sync(from); + + expect(syncSpy).toHaveBeenCalledTimes(1); + expect(syncSpy).toHaveBeenCalledWith({ + from, + signal: expect.any(AbortSignal), + }); + }); + + it("should expose a method to allow to transfer an amount of Dusk", async () => { + await walletStore.transfer(addresses[1], 10, gasSettings); expect(syncSpy).toHaveBeenCalledTimes(2); + expect(syncSpy).toHaveBeenNthCalledWith(1, { + signal: expect.any(AbortSignal), + }); + expect(syncSpy).toHaveBeenNthCalledWith(2, { + signal: expect.any(AbortSignal), + }); expect(transferSpy).toHaveBeenCalledTimes(1); expect(transferSpy).toHaveBeenCalledWith( currentAddress, addresses[1], - 10 + 10, + gasSettings ); expect(syncSpy.mock.invocationCallOrder[0]).toBeLessThan( transferSpy.mock.invocationCallOrder[0] @@ -416,14 +522,17 @@ describe("walletStore", async () => { }); it("should expose a method to allow to unstake the current address", async () => { - await walletStore.unstake(gas.price, gas.limit); - - expect(wallet.gasLimit).toBe(gas.limit); - expect(wallet.gasPrice).toBe(gas.price); + await walletStore.unstake(gasSettings); expect(syncSpy).toHaveBeenCalledTimes(2); + expect(syncSpy).toHaveBeenNthCalledWith(1, { + signal: expect.any(AbortSignal), + }); + expect(syncSpy).toHaveBeenNthCalledWith(2, { + signal: expect.any(AbortSignal), + }); expect(unstakeSpy).toHaveBeenCalledTimes(1); - expect(unstakeSpy).toHaveBeenCalledWith(currentAddress); + expect(unstakeSpy).toHaveBeenCalledWith(currentAddress, gasSettings); expect(syncSpy.mock.invocationCallOrder[0]).toBeLessThan( unstakeSpy.mock.invocationCallOrder[0] ); @@ -433,14 +542,20 @@ describe("walletStore", async () => { }); it("should expose a method to allow to withdraw a reward", async () => { - await walletStore.withdrawReward(gas.price, gas.limit); - - expect(wallet.gasLimit).toBe(gas.limit); - expect(wallet.gasPrice).toBe(gas.price); + await walletStore.withdrawReward(gasSettings); expect(syncSpy).toHaveBeenCalledTimes(2); + expect(syncSpy).toHaveBeenNthCalledWith(1, { + signal: expect.any(AbortSignal), + }); + expect(syncSpy).toHaveBeenNthCalledWith(2, { + signal: expect.any(AbortSignal), + }); expect(withdrawRewardSpy).toHaveBeenCalledTimes(1); - expect(withdrawRewardSpy).toHaveBeenCalledWith(currentAddress); + expect(withdrawRewardSpy).toHaveBeenCalledWith( + currentAddress, + gasSettings + ); expect(syncSpy.mock.invocationCallOrder[0]).toBeLessThan( withdrawRewardSpy.mock.invocationCallOrder[0] ); @@ -452,7 +567,7 @@ describe("walletStore", async () => { describe("State changing failures", () => { /** @typedef {"stake" | "transfer" | "unstake" | "withdrawReward"} Operation */ - /** @type {Record>} */ + /** @type {Record>} */ const operationsMap = { stake: stakeSpy, transfer: transferSpy, @@ -466,7 +581,7 @@ describe("walletStore", async () => { keys(operationsMap).forEach((operation) => { const spy = operationsMap[operation]; - it("should return a resolved promise with the operation result if an operation succeeds", async () => { + it("should return a resolved promise with the operation result if an operation succeeds even if the last sync fails", async () => { await walletStore.init(wallet); await vi.advanceTimersToNextTimerAsync(); @@ -480,6 +595,16 @@ describe("walletStore", async () => { // @ts-ignore it's a mock and we don't care to pass the correct arguments expect(await walletStore[operation]()).toBe(fakeSuccess); expect(get(walletStore).error).toBe(fakeSyncError); + expect(syncSpy).toHaveBeenCalledTimes(3); + expect(syncSpy).toHaveBeenNthCalledWith(1, { + signal: expect.any(AbortSignal), + }); + expect(syncSpy).toHaveBeenNthCalledWith(2, { + signal: expect.any(AbortSignal), + }); + expect(syncSpy).toHaveBeenNthCalledWith(3, { + signal: expect.any(AbortSignal), + }); walletStore.reset(); }); @@ -501,6 +626,16 @@ describe("walletStore", async () => { await vi.advanceTimersToNextTimerAsync(); expect(get(walletStore).error).toBe(fakeSyncError); + expect(syncSpy).toHaveBeenCalledTimes(3); + expect(syncSpy).toHaveBeenNthCalledWith(1, { + signal: expect.any(AbortSignal), + }); + expect(syncSpy).toHaveBeenNthCalledWith(2, { + signal: expect.any(AbortSignal), + }); + expect(syncSpy).toHaveBeenNthCalledWith(3, { + signal: expect.any(AbortSignal), + }); walletStore.reset(); }); diff --git a/web-wallet/src/lib/stores/stores.d.ts b/web-wallet/src/lib/stores/stores.d.ts index cb9d9eeaf..d1c674e7a 100644 --- a/web-wallet/src/lib/stores/stores.d.ts +++ b/web-wallet/src/lib/stores/stores.d.ts @@ -17,19 +17,29 @@ type WalletStoreContent = { isSyncing: boolean; }; +type Gas = { + price: number; + limit: number; +}; + type WalletStoreServices = { abortSync: () => void; clearLocalData: () => Promise; - clearLocalDataAndInit: (wallet: Wallet) => Promise; + clearLocalDataAndInit: ( + wallet: Wallet, + syncFromBlock?: number + ) => Promise; + + getCurrentBlockHeight: () => Promise; getStakeInfo: () => Promise & ReturnType; // The return type apparently is not in a promise here getTransactionsHistory: () => Promise>; - init: (wallet: Wallet) => Promise; + init: (wallet: Wallet, syncFromBlock?: number) => Promise; reset: () => void; @@ -37,27 +47,21 @@ type WalletStoreServices = { stake: ( amount: number, - gasPrice: number, - gasLimit: number + gas?: Gas ) => Promise & ReturnType; - sync: () => Promise; + sync: (from?: number) => Promise; transfer: ( to: string, amount: number, - gasPrice: number, - gasLimit: number + gas?: Gas ) => Promise & ReturnType; - unstake: ( - gasPrice: number, - gasLimit: number - ) => Promise & ReturnType; + unstake: (gas?: Gas) => Promise & ReturnType; withdrawReward: ( - gasPrice: number, - gasLimit: number + gas?: Gas ) => Promise & ReturnType; }; diff --git a/web-wallet/src/lib/stores/walletStore.js b/web-wallet/src/lib/stores/walletStore.js index 0b62b3ff4..378766fcb 100644 --- a/web-wallet/src/lib/stores/walletStore.js +++ b/web-wallet/src/lib/stores/walletStore.js @@ -6,7 +6,7 @@ import { getKey, uniquesBy } from "lamb"; */ /** - * @typedef {import("./stores").WalletStoreServices["getTransactionsHistory"]} GetTransactionsHistory + * @typedef {import("./stores").WalletStoreServices} WalletStoreServices */ /** @type {AbortController} */ @@ -55,50 +55,58 @@ const getCurrentAddress = () => get(walletStore).currentAddress; /** @type {(action: (...args: any[]) => Promise) => Promise} */ const syncedAction = (action) => sync().then(action).finally(sync); -const abortSync = () => syncPromise && syncController?.abort(); +async function updateAfterSync() { + const store = get(walletStore); + + // @ts-expect-error + const balance = await walletInstance.getBalance(store.currentAddress); + + set({ + ...store, + balance, + isSyncing: false, + }); +} -/** @type {() => Promise} */ +/** @type {WalletStoreServices["abortSync"]} */ +const abortSync = () => { + syncPromise && syncController?.abort(); +}; + +/** @type {WalletStoreServices["clearLocalData"]} */ const clearLocalData = async () => walletInstance?.reset(); -/** @type {(wallet: Wallet) => Promise} */ -const clearLocalDataAndInit = (wallet) => - wallet.reset().then(() => init(wallet)); +/** @type {WalletStoreServices["clearLocalDataAndInit"]} */ +const clearLocalDataAndInit = (wallet, syncFromBlock) => + wallet.reset().then(() => init(wallet, syncFromBlock)); + +/** @type {WalletStoreServices["getCurrentBlockHeight"]} */ +const getCurrentBlockHeight = async () => + // @ts-expect-error + walletInstance?.constructor.networkBlockHeight; -/** @type {import("./stores").WalletStoreServices["getStakeInfo"]} */ +/** @type {WalletStoreServices["getStakeInfo"]} */ const getStakeInfo = async () => sync() // @ts-expect-error .then(() => walletInstance.stakeInfo(getCurrentAddress())) .then(fixStakeInfo); -/** @type {GetTransactionsHistory} */ - +/** @type {WalletStoreServices["getTransactionsHistory"]} */ const getTransactionsHistory = async () => sync() // @ts-expect-error .then(() => walletInstance.history(getCurrentAddress())) .then(uniquesById); +/** @type {WalletStoreServices["reset"]} */ function reset() { walletInstance = null; set(initialState); } -async function updateAfterSync() { - const store = get(walletStore); - - // @ts-expect-error - const balance = await walletInstance.getBalance(store.currentAddress); - - set({ - ...store, - balance, - isSyncing: false, - }); -} - -/** @param {Wallet} wallet */ -async function init(wallet) { +/** @type {WalletStoreServices["init"]} */ +async function init(wallet, syncFromBlock) { walletInstance = wallet; const addresses = await walletInstance.getPsks(); @@ -110,34 +118,29 @@ async function init(wallet) { currentAddress, initialized: true, }); - sync(); + sync(syncFromBlock); } -/** @type {import("./stores").WalletStoreServices["setCurrentAddress"]} */ +/** @type {WalletStoreServices["setCurrentAddress"]} */ async function setCurrentAddress(address) { const store = get(walletStore); return store.addresses.includes(address) - ? Promise.resolve(set({ ...store, currentAddress: address })).then(sync) + ? Promise.resolve(set({ ...store, currentAddress: address })).then(() => + sync() + ) : Promise.reject(new Error("The received address is not in the list")); } -/** @type {import("./stores").WalletStoreServices["stake"]} */ - -const stake = async (amount, gasPrice, gasLimit) => +/** @type {WalletStoreServices["stake"]} */ +const stake = async (amount, gasSettings) => syncedAction(() => { // @ts-expect-error - walletInstance.gasLimit = gasLimit; - - // @ts-expect-error - walletInstance.gasPrice = gasPrice; - - // @ts-expect-error - return walletInstance.stake(getCurrentAddress(), amount); + return walletInstance.stake(getCurrentAddress(), amount, gasSettings); }); -/** @type {import("./stores").WalletStoreServices["sync"]} */ -function sync() { +/** @type {WalletStoreServices["sync"]} */ +function sync(from) { if (!walletInstance) { throw new Error("No wallet instance to sync"); } @@ -149,7 +152,7 @@ function sync() { syncController = new AbortController(); syncPromise = walletInstance - .sync({ signal: syncController.signal }) + .sync({ from, signal: syncController.signal }) .then(updateAfterSync, (error) => { set({ ...store, error, isSyncing: false }); }) @@ -161,43 +164,30 @@ function sync() { return syncPromise; } -/** @type {import("./stores").WalletStoreServices["transfer"]} */ -const transfer = async (to, amount, gasPrice, gasLimit) => +/** @type {WalletStoreServices["transfer"]} */ +const transfer = async (to, amount, gasSettings) => syncedAction(() => { // @ts-expect-error - walletInstance.gasLimit = gasLimit; - - // @ts-expect-error - walletInstance.gasPrice = gasPrice; - - // @ts-expect-error - return walletInstance.transfer(getCurrentAddress(), to, amount); + return walletInstance.transfer( + getCurrentAddress(), + to, + amount, + gasSettings + ); }); -/** @type {import("./stores").WalletStoreServices["unstake"]} */ -const unstake = async (gasPrice, gasLimit) => +/** @type {WalletStoreServices["unstake"]} */ +const unstake = async (gasSettings) => syncedAction(() => { // @ts-expect-error - walletInstance.gasLimit = gasLimit; - - // @ts-expect-error - walletInstance.gasPrice = gasPrice; - - // @ts-expect-error - return walletInstance.unstake(getCurrentAddress()); + return walletInstance.unstake(getCurrentAddress(), gasSettings); }); -/** @type {import("./stores").WalletStoreServices["withdrawReward"]} */ -const withdrawReward = async (gasPrice, gasLimit) => +/** @type {WalletStoreServices["withdrawReward"]} */ +const withdrawReward = async (gasSettings) => syncedAction(() => { // @ts-expect-error - walletInstance.gasLimit = gasLimit; - - // @ts-expect-error - walletInstance.gasPrice = gasPrice; - - // @ts-expect-error - return walletInstance.withdrawReward(getCurrentAddress()); + return walletInstance.withdrawReward(getCurrentAddress(), gasSettings); }); /** @type {import("./stores").WalletStore} */ @@ -205,6 +195,7 @@ export default { abortSync, clearLocalData, clearLocalDataAndInit, + getCurrentBlockHeight, getStakeInfo, getTransactionsHistory, init, diff --git a/web-wallet/src/routes/(app)/__tests__/layout.spec.js b/web-wallet/src/routes/(app)/__tests__/layout.spec.js index 7e40f7efb..f53c9d210 100644 --- a/web-wallet/src/routes/(app)/__tests__/layout.spec.js +++ b/web-wallet/src/routes/(app)/__tests__/layout.spec.js @@ -42,7 +42,7 @@ describe("App layout.js", () => { }); it("should do nothing otherwise", async () => { - await walletStore.init(new Wallet([], 0, 0)); + await walletStore.init(new Wallet([])); // @ts-ignore await expect(load()).resolves.toBe(void 0);