@@ -3,8 +3,7 @@ import type { Address } from "@/src/types";
33
44import { CollateralRegistry } from "@/src/abi/CollateralRegistry" ;
55import { 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" ;
87import { LEGACY_CHECK } from "@/src/env" ;
98import { TransactionDetailsRow } from "@/src/screens/TransactionsScreen/TransactionsScreen" ;
109import { 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}
0 commit comments