Skip to content

Commit f126b5f

Browse files
#279 Tweak approve and deposit calls for erc 20 form (#288)
1 parent 3237a80 commit f126b5f

File tree

2 files changed

+129
-88
lines changed

2 files changed

+129
-88
lines changed

packages/ui/src/ERC20DepositForm.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export const ERC20DepositForm: FC<ERC20DepositFormProps> = (props) => {
200200
enabled:
201201
amountBigInt !== undefined &&
202202
allowance !== undefined &&
203+
amountBigInt > 0 &&
203204
amountBigInt > allowance,
204205
},
205206
});
@@ -213,12 +214,10 @@ export const ERC20DepositForm: FC<ERC20DepositFormProps> = (props) => {
213214
args: [erc20Address, applicationAddress, amountBigInt!, execLayerData],
214215
query: {
215216
enabled:
217+
form.isValid() &&
216218
amountBigInt !== undefined &&
217219
balance !== undefined &&
218220
allowance !== undefined &&
219-
!form.errors.erc20Address &&
220-
!form.errors.application &&
221-
isHex(execLayerData) &&
222221
amountBigInt <= balance &&
223222
amountBigInt <= allowance,
224223
},
@@ -233,6 +232,7 @@ export const ERC20DepositForm: FC<ERC20DepositFormProps> = (props) => {
233232
allowance !== undefined &&
234233
decimals !== undefined &&
235234
amountBigInt !== undefined &&
235+
amountBigInt > 0 &&
236236
allowance < amountBigInt;
237237

238238
const canDeposit =

packages/ui/test/ERC20DepositForm.test.tsx

+126-85
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
2-
import { describe, it } from "vitest";
2+
import { beforeEach, describe, it } from "vitest";
33
import { ERC20DepositForm } from "../src";
44
import withMantineTheme from "./utils/WithMantineTheme";
55
import { getAddress } from "viem";
6+
import {
7+
useSimulateErc20Approve,
8+
useSimulateErc20PortalDepositErc20Tokens,
9+
useWriteErc20Approve,
10+
useWriteErc20PortalDepositErc20Tokens,
11+
} from "@cartesi/rollups-wagmi";
612

713
const Component = withMantineTheme(ERC20DepositForm);
814

@@ -32,34 +38,21 @@ const defaultProps = {
3238
onSuccess: () => undefined,
3339
};
3440

35-
vi.mock("@cartesi/rollups-wagmi", async () => {
36-
const actual = await vi.importActual("@cartesi/rollups-wagmi");
37-
return {
38-
...(actual as any),
39-
useSimulateErc20Approve: () => ({
40-
data: {
41-
request: {},
42-
},
43-
config: {},
44-
}),
45-
useWriteErc20Approve: () => ({
46-
data: {},
47-
wait: vi.fn(),
48-
reset: vi.fn(),
49-
}),
50-
useSimulateErc20PortalDepositErc20Tokens: () => ({
51-
data: {
52-
request: {},
53-
},
54-
config: {},
55-
}),
56-
useWriteErc20PortalDepositErc20Tokens: () => ({
57-
data: {},
58-
wait: vi.fn(),
59-
reset: vi.fn(),
60-
}),
61-
};
41+
vi.mock("@cartesi/rollups-wagmi");
42+
const useSimulateErc20ApproveMock = vi.mocked(useSimulateErc20Approve, {
43+
partial: true,
6244
});
45+
const useWriteErc20ApproveMock = vi.mocked(useWriteErc20Approve, {
46+
partial: true,
47+
});
48+
const useSimulateErc20PortalDepositErc20TokensMock = vi.mocked(
49+
useSimulateErc20PortalDepositErc20Tokens,
50+
{ partial: true },
51+
);
52+
const useWriteErc20PortalDepositErc20TokensMock = vi.mocked(
53+
useWriteErc20PortalDepositErc20Tokens,
54+
{ partial: true },
55+
);
6356

6457
vi.mock("wagmi", async () => {
6558
return {
@@ -112,6 +105,26 @@ vi.mock("../src/hooks/useWatchQueryOnBlockChange", () => ({
112105
}));
113106

114107
describe("Rollups ERC20DepositForm", () => {
108+
beforeEach(() => {
109+
useSimulateErc20ApproveMock.mockReturnValue({
110+
data: {
111+
request: {},
112+
},
113+
});
114+
useWriteErc20ApproveMock.mockReturnValue({
115+
data: "0x721be000f6054b5e0e57aaab791015b53f0a18f4",
116+
reset: vi.fn(),
117+
});
118+
useSimulateErc20PortalDepositErc20TokensMock.mockReturnValue({
119+
data: {
120+
request: {},
121+
},
122+
});
123+
useWriteErc20PortalDepositErc20TokensMock.mockReturnValue({
124+
data: "0x721be000f6054b5e0e57aaab791015b53f0a18f4",
125+
reset: vi.fn(),
126+
});
127+
});
115128
describe("ApplicationAutocomplete", () => {
116129
it("should display correct label", () => {
117130
render(<Component {...defaultProps} />);
@@ -190,16 +203,17 @@ describe("Rollups ERC20DepositForm", () => {
190203
expect(onSuccessMock).toHaveBeenCalled();
191204
});
192205

193-
it("should correctly format address", async () => {
194-
const rollupsWagmi = await import("@cartesi/rollups-wagmi");
206+
it("should correctly format address", () => {
195207
const mockedHook = vi.fn().mockReturnValue({
196-
...rollupsWagmi.useSimulateErc20PortalDepositErc20Tokens,
208+
data: {
209+
request: {},
210+
},
197211
loading: false,
198212
error: null,
199213
});
200-
rollupsWagmi.useSimulateErc20PortalDepositErc20Tokens = vi
201-
.fn()
202-
.mockImplementation(mockedHook);
214+
useSimulateErc20PortalDepositErc20TokensMock.mockImplementation(
215+
mockedHook,
216+
);
203217

204218
const { container } = render(<Component {...defaultProps} />);
205219
const input = container.querySelector("input") as HTMLInputElement;
@@ -320,16 +334,15 @@ describe("Rollups ERC20DepositForm", () => {
320334
expect(screen.getByDisplayValue(value) === amountInput).toBe(true);
321335
});
322336

323-
it("should correctly process small decimal numbers", async () => {
324-
const rollupsWagmi = await import("@cartesi/rollups-wagmi");
337+
it("should correctly process small decimal numbers", () => {
325338
const mockedHook = vi.fn().mockReturnValue({
326-
...rollupsWagmi.useSimulateErc20Approve,
339+
data: {
340+
request: {},
341+
},
327342
loading: false,
328343
error: null,
329344
});
330-
rollupsWagmi.useSimulateErc20Approve = vi
331-
.fn()
332-
.mockImplementation(mockedHook);
345+
useSimulateErc20ApproveMock.mockImplementation(mockedHook);
333346

334347
const { container } = render(<Component {...defaultProps} />);
335348
const amountInput = container.querySelector(
@@ -353,6 +366,72 @@ describe("Rollups ERC20DepositForm", () => {
353366
},
354367
});
355368
});
369+
370+
it("should disable approve call when amount is '0'", () => {
371+
const mockedHook = vi.fn().mockReturnValue({
372+
data: {
373+
request: {},
374+
},
375+
loading: false,
376+
error: null,
377+
});
378+
useSimulateErc20ApproveMock.mockImplementation(mockedHook);
379+
380+
const { container } = render(<Component {...defaultProps} />);
381+
const amountInput = container.querySelector(
382+
'[type="number"]',
383+
) as HTMLInputElement;
384+
385+
fireEvent.change(amountInput, {
386+
target: {
387+
value: "0",
388+
},
389+
});
390+
391+
expect(mockedHook).toHaveBeenLastCalledWith({
392+
address: "0x0000000000000000000000000000000000000000",
393+
args: ["0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB", 0n],
394+
query: {
395+
enabled: false,
396+
},
397+
});
398+
});
399+
400+
it("should disable deposit call when amount is '0'", () => {
401+
const mockedHook = vi.fn().mockReturnValue({
402+
data: {
403+
request: {},
404+
},
405+
loading: false,
406+
error: null,
407+
});
408+
useSimulateErc20PortalDepositErc20TokensMock.mockImplementation(
409+
mockedHook,
410+
);
411+
412+
const { container } = render(<Component {...defaultProps} />);
413+
const amountInput = container.querySelector(
414+
'[type="number"]',
415+
) as HTMLInputElement;
416+
417+
fireEvent.change(amountInput, {
418+
target: {
419+
value: "0",
420+
},
421+
});
422+
423+
expect(mockedHook).toHaveBeenLastCalledWith({
424+
args: [
425+
"0x0000000000000000000000000000000000000000",
426+
"0x0000000000000000000000000000000000000000",
427+
0n,
428+
"0x",
429+
],
430+
query: {
431+
enabled: false,
432+
},
433+
});
434+
});
356435
});
357436
describe("Max Amount Button", () => {
358437
vi.mock("wagmi", async () => {
@@ -435,45 +514,6 @@ describe("Rollups ERC20DepositForm", () => {
435514
});
436515
});
437516

438-
describe("Amount input", () => {
439-
it("should correctly process small decimal numbers", async () => {
440-
const rollupsWagmi = await import("@cartesi/rollups-wagmi");
441-
const mockedHook = vi.fn().mockReturnValue({
442-
...rollupsWagmi.useSimulateErc20Approve,
443-
data: {
444-
request: {},
445-
},
446-
loading: false,
447-
error: null,
448-
});
449-
rollupsWagmi.useSimulateErc20Approve = vi
450-
.fn()
451-
.mockImplementation(mockedHook);
452-
453-
const { container } = render(<Component {...defaultProps} />);
454-
const amountInput = container.querySelector(
455-
'[type="number"]',
456-
) as HTMLInputElement;
457-
458-
fireEvent.change(amountInput, {
459-
target: {
460-
value: "0.0000001",
461-
},
462-
});
463-
464-
expect(mockedHook).toHaveBeenLastCalledWith({
465-
address: "0x0000000000000000000000000000000000000000",
466-
args: [
467-
"0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB",
468-
100000000000n,
469-
],
470-
query: {
471-
enabled: true,
472-
},
473-
});
474-
});
475-
});
476-
477517
describe("Deposit button", () => {
478518
it("should invoke onSearchApplications function after successful deposit", async () => {
479519
const wagmi = await import("wagmi");
@@ -494,16 +534,17 @@ describe("Rollups ERC20DepositForm", () => {
494534
expect(onSearchApplicationsMock).toHaveBeenCalledWith("");
495535
});
496536

497-
it("should correctly format extra data", async () => {
498-
const rollupsWagmi = await import("@cartesi/rollups-wagmi");
537+
it("should correctly format extra data", () => {
499538
const mockedHook = vi.fn().mockReturnValue({
500-
...rollupsWagmi.useSimulateErc20PortalDepositErc20Tokens,
539+
data: {
540+
request: {},
541+
},
501542
loading: false,
502543
error: null,
503544
});
504-
rollupsWagmi.useSimulateErc20PortalDepositErc20Tokens = vi
505-
.fn()
506-
.mockImplementation(mockedHook);
545+
useSimulateErc20PortalDepositErc20TokensMock.mockImplementation(
546+
mockedHook,
547+
);
507548

508549
const { container } = render(<Component {...defaultProps} />);
509550
const textarea = container.querySelector(

0 commit comments

Comments
 (0)