Skip to content

Commit da7ec49

Browse files
authored
Pass nonce=0 to client.simulateCalls() (#903)
* Fix simulateCalls() by overriding the nonce * Upgrade dependencies
1 parent 1c63a63 commit da7ec49

File tree

4 files changed

+555
-207
lines changed

4 files changed

+555
-207
lines changed

frontend/app/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@
2525
"@liquity2/uikit": "workspace:*",
2626
"@next/bundle-analyzer": "15.3.1",
2727
"@react-spring/web": "^10.0.0-beta.0",
28-
"@tanstack/react-query": "^5.75.2",
28+
"@tanstack/react-query": "^5.75.4",
2929
"@vercel/analytics": "^1.5.0",
3030
"@wagmi/core": "^2.17.1",
3131
"abitype": "^1.0.8",
3232
"blo": "^2.0.0",
3333
"connectkit": "^1.9.0",
3434
"dnum": "^2.14.0",
3535
"focus-trap-react": "^11.0.3",
36-
"geist": "^1.4.1",
36+
"geist": "^1.4.2",
3737
"next": "15.3.1",
3838
"react": "19.1.0",
3939
"react-dom": "19.1.0",
4040
"sharp": "^0.34.1",
4141
"ts-pattern": "^5.7.0",
42-
"valibot": "^1.0.0",
43-
"viem": "^2.28.3",
42+
"valibot": "^1.1.0",
43+
"viem": "^2.29.0",
4444
"wagmi": "^2.15.2"
4545
},
4646
"devDependencies": {
@@ -51,8 +51,8 @@
5151
"@testing-library/jest-dom": "^6.6.3",
5252
"@testing-library/react": "^16.3.0",
5353
"@testing-library/user-event": "^14.6.1",
54-
"@types/node": "^22.15.3",
55-
"@types/react": "19.1.2",
54+
"@types/node": "^22.15.12",
55+
"@types/react": "19.1.3",
5656
"@types/react-dom": "19.1.3",
5757
"@vitejs/plugin-react": "^4.4.1",
5858
"@vitest/coverage-v8": "^3.1.3",

frontend/app/src/tx-flows/legacyRedeemCollateral.tsx

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import type { Address } from "@/src/types";
33

44
import { CollateralRegistry } from "@/src/abi/CollateralRegistry";
55
import { Amount } from "@/src/comps/Amount/Amount";
6-
import { LOCAL_STORAGE_PREFIX } from "@/src/constants";
7-
import { dnum18, jsonParseWithDnum, jsonStringifyWithDnum } from "@/src/dnum-utils";
6+
import { dnum18, jsonStringifyWithDnum } from "@/src/dnum-utils";
87
import { LEGACY_CHECK } from "@/src/env";
98
import { TransactionDetailsRow } from "@/src/screens/TransactionsScreen/TransactionsScreen";
109
import { TransactionStatus } from "@/src/screens/TransactionsScreen/TransactionStatus";
@@ -52,7 +51,7 @@ export const legacyRedeemCollateral: FlowDeclaration<LegacyRedeemCollateralReque
5251
<Amount
5352
key="start"
5453
value={boldChange}
55-
fallback="fetching…"
54+
fallback={estimatedGains.isError ? "loading error." : "fetching…"}
5655
suffix=" BOLD"
5756
/>,
5857
<>Estimated BOLD that will be redeemed.</>,
@@ -69,7 +68,7 @@ export const legacyRedeemCollateral: FlowDeclaration<LegacyRedeemCollateralReque
6968
<Amount
7069
key="start"
7170
value={collChange}
72-
fallback="fetching…"
71+
fallback={estimatedGains.isError ? "loading error." : "fetching…"}
7372
suffix={` ${symbol_}`}
7473
/>,
7574
<>Estimated {symbol_} you will receive.</>,
@@ -175,22 +174,6 @@ export function useSimulatedBalancesChange({
175174
throw new Error("LEGACY_CHECK is not defined");
176175
}
177176

178-
let stored: v.InferOutput<typeof StoredBalancesChangeSchema> | null = null;
179-
try {
180-
stored = v.parse(
181-
StoredBalancesChangeSchema,
182-
jsonParseWithDnum(
183-
localStorage.getItem(
184-
`${LOCAL_STORAGE_PREFIX}:simulatedBalancesChange`,
185-
) ?? "",
186-
),
187-
);
188-
} catch (_) {}
189-
190-
if (stored && stored.stringifiedRequest === jsonStringifyWithDnum(request)) {
191-
return stored.balanceChanges;
192-
}
193-
194177
const [chain] = wagmiConfig.chains;
195178
const [rpcUrl] = chain.rpcUrls.default.http;
196179
const client = createPublicClient({ chain, transport: http(rpcUrl) });
@@ -212,17 +195,25 @@ export function useSimulatedBalancesChange({
212195
const simulation = await client.simulateCalls({
213196
account,
214197
calls: [
198+
// 1. get balances before
215199
boldBalanceCall,
216200
...branchesBalanceCalls,
201+
202+
// 2. redeem
217203
{
218204
to: LEGACY_CHECK.COLLATERAL_REGISTRY,
219205
abi: CollateralRegistry,
220206
functionName: "redeemCollateral",
221207
args: [request.amount[0], 0n, request.maxFee[0]],
222208
},
209+
210+
// 3. get balances after
223211
boldBalanceCall,
224212
...branchesBalanceCalls,
225213
],
214+
215+
// This is needed to avoid a “nonce too low” error with certain RPCs
216+
stateOverrides: [{ address: account, nonce: 0 }],
226217
});
227218

228219
const getBalancesFromSimulated = (position: number) => {
@@ -246,24 +237,14 @@ export function useSimulatedBalancesChange({
246237
const balancesBefore = getBalancesFromSimulated(0);
247238
const balancesAfter = getBalancesFromSimulated(LEGACY_CHECK.BRANCHES.length + 2);
248239

249-
const balanceChanges = balancesBefore.map((balanceBefore, index) => {
240+
return balancesBefore.map((balanceBefore, index) => {
250241
const balanceAfter = balancesAfter[index];
251242
if (!balanceAfter) throw new Error();
252243
return {
253244
symbol: balanceBefore.symbol,
254245
change: dn.sub(balanceAfter.balance, balanceBefore.balance),
255246
};
256247
});
257-
258-
localStorage.setItem(
259-
`${LOCAL_STORAGE_PREFIX}:simulatedBalancesChange`,
260-
jsonStringifyWithDnum({
261-
stringifiedRequest: jsonStringifyWithDnum(request),
262-
balanceChanges,
263-
}),
264-
);
265-
266-
return balanceChanges;
267248
},
268249
});
269250
}

frontend/app/src/tx-flows/redeemCollateral.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,25 @@ export function useSimulatedBalancesChange({
206206
const simulation = await client.simulateCalls({
207207
account,
208208
calls: [
209+
// 1. get balances before
209210
boldBalanceCall,
210211
...branchesBalanceCalls,
212+
213+
// 2. redeem
211214
{
212215
to: CollateralRegistry.address,
213216
abi: CollateralRegistry.abi,
214217
functionName: "redeemCollateral",
215218
args: [request.amount[0], 0n, request.maxFee[0]],
216219
},
220+
221+
// 3. get balances after
217222
boldBalanceCall,
218223
...branchesBalanceCalls,
219224
],
225+
226+
// This is needed to avoid a “nonce too low” error with certain RPCs
227+
stateOverrides: [{ address: account, nonce: 0 }],
220228
});
221229

222230
const getBalancesFromSimulated = (position: number) => {

0 commit comments

Comments
 (0)