Skip to content

Commit

Permalink
Up
Browse files Browse the repository at this point in the history
  • Loading branch information
nortonandreev committed Apr 30, 2024
1 parent cd01354 commit 04a87cd
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/** @type {(to: string, amount: number, gasPrice:number, gasLimit:number) => Promise<string>} */
const executeSend = (to, amount, gasPrice, gasLimit) =>
walletStore
.transfer(to, amount, gasPrice, gasLimit)
.transfer(to, amount, { limit: gasLimit, price: gasPrice })
.then(getLastTransactionHash);
const collectSettings = collect([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ describe("getWallet", () => {
[
"wasm",
"seed",
"gasLimit",
"gasPrice",
"constructor",
"getBalance",
"getPsks",
Expand Down
5 changes: 1 addition & 4 deletions web-wallet/src/lib/services/wallet/getWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
40 changes: 13 additions & 27 deletions web-wallet/src/lib/stores/__tests__/walletStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import { walletStore } from "..";

vi.useFakeTimers();

// TODO remove
Object.defineProperty(Wallet, "networkBlockHeight", {
configurable: true,
get: () => 0,
});

describe("walletStore", async () => {
const balance = { maximum: 100, value: 1 };
const wallet = new Wallet([]);
Expand Down Expand Up @@ -77,7 +71,7 @@ describe("walletStore", async () => {
currentAddress: addresses[0],
initialized: true,
};
const gas = {
const gasSettings = {
limit: 30000000,
price: 1,
};
Expand Down Expand Up @@ -464,10 +458,7 @@ 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, {
Expand All @@ -477,7 +468,7 @@ describe("walletStore", async () => {
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]
);
Expand Down Expand Up @@ -506,10 +497,7 @@ 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);

expect(wallet.gasLimit).toBe(gas.limit);
expect(wallet.gasPrice).toBe(gas.price);
await walletStore.transfer(addresses[1], 10, gasSettings);

expect(syncSpy).toHaveBeenCalledTimes(2);
expect(syncSpy).toHaveBeenNthCalledWith(1, {
Expand All @@ -522,7 +510,8 @@ describe("walletStore", async () => {
expect(transferSpy).toHaveBeenCalledWith(
currentAddress,
addresses[1],
10
10,
gasSettings
);
expect(syncSpy.mock.invocationCallOrder[0]).toBeLessThan(
transferSpy.mock.invocationCallOrder[0]
Expand All @@ -533,10 +522,7 @@ 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, {
Expand All @@ -546,7 +532,7 @@ describe("walletStore", async () => {
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]
);
Expand All @@ -556,10 +542,7 @@ 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, {
Expand All @@ -569,7 +552,10 @@ describe("walletStore", async () => {
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]
);
Expand Down
19 changes: 9 additions & 10 deletions web-wallet/src/lib/stores/stores.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type WalletStoreContent = {
isSyncing: boolean;
};

type Gas = {
price: number;
limit: number;
};

type WalletStoreServices = {
abortSync: () => void;

Expand All @@ -42,27 +47,21 @@ type WalletStoreServices = {

stake: (
amount: number,
gasPrice: number,
gasLimit: number
gas?: Gas
) => Promise<any> & ReturnType<Wallet["stake"]>;

sync: (from?: number) => Promise<void>;

transfer: (
to: string,
amount: number,
gasPrice: number,
gasLimit: number
gas?: Gas
) => Promise<any> & ReturnType<Wallet["transfer"]>;

unstake: (
gasPrice: number,
gasLimit: number
) => Promise<any> & ReturnType<Wallet["unstake"]>;
unstake: (gas?: Gas) => Promise<any> & ReturnType<Wallet["unstake"]>;

withdrawReward: (
gasPrice: number,
gasLimit: number
gas?: Gas
) => Promise<any> & ReturnType<Wallet["withdrawReward"]>;
};

Expand Down
45 changes: 13 additions & 32 deletions web-wallet/src/lib/stores/walletStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,10 @@ async function setCurrentAddress(address) {
}

/** @type {WalletStoreServices["stake"]} */
const stake = async (amount, gasPrice, gasLimit) =>
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 {WalletStoreServices["sync"]} */
Expand Down Expand Up @@ -171,42 +165,29 @@ function sync(from) {
}

/** @type {WalletStoreServices["transfer"]} */
const transfer = async (to, amount, gasPrice, gasLimit) =>
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 {WalletStoreServices["unstake"]} */
const unstake = async (gasPrice, gasLimit) =>
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 {WalletStoreServices["withdrawReward"]} */
const withdrawReward = async (gasPrice, gasLimit) =>
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} */
Expand Down
2 changes: 1 addition & 1 deletion web-wallet/src/routes/(app)/__tests__/layout.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 04a87cd

Please sign in to comment.