Skip to content

Commit 0b2c0d7

Browse files
committed
fix(app2): retry and last throws
1 parent 6365a9b commit 0b2c0d7

File tree

2 files changed

+104
-30
lines changed

2 files changed

+104
-30
lines changed

app2/src/lib/components/Transfer/transfer.svelte.ts

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ import {
66
hasFailedExit as hasCosmosFailedExit,
77
isComplete as isCosmosComplete,
88
nextState as cosmosNextState,
9-
TransferSubmission as CosmosTransferSubmission
9+
TransferSubmission as CosmosTransferSubmission,
10+
SwitchChainState,
11+
ApprovalSubmitState,
12+
TransferSubmitState
1013
} from "$lib/services/transfer-ucs03-cosmos"
1114
import {
1215
hasFailedExit as hasEvmFailedExit,
1316
isComplete as isEvmComplete,
1417
nextState as evmNextState,
15-
TransferSubmission as EvmTransferSubmission
18+
TransferSubmission as EvmTransferSubmission,
19+
SwitchChainState as EvmSwitchChainState,
20+
ApprovalSubmitState as EvmApprovalSubmitState,
21+
ApprovalReceiptState,
22+
TransferSubmitState as EvmTransferSubmitState,
23+
TransferReceiptState
1624
} from "$lib/services/transfer-ucs03-evm"
1725
import { chains } from "$lib/stores/chains.svelte.ts"
1826
import { type Address, fromHex, type Hex } from "viem"
@@ -291,8 +299,45 @@ export class Transfer {
291299
const sourceChainValue = this.sourceChain.value
292300

293301
if (sourceChainValue.rpc_type === "evm") {
294-
const evmState =
295-
this.state._tag === "EVM" ? this.state.state : EvmTransferSubmission.Filling()
302+
let evmState: EvmTransferSubmission
303+
if (this.state._tag === "EVM") {
304+
// If failed, reset the failed step to InProgress
305+
if (hasEvmFailedExit(this.state.state)) {
306+
switch (this.state.state._tag) {
307+
case "SwitchChain":
308+
evmState = EvmTransferSubmission.SwitchChain({
309+
state: EvmSwitchChainState.InProgress()
310+
})
311+
break
312+
case "ApprovalSubmit":
313+
evmState = EvmTransferSubmission.ApprovalSubmit({
314+
state: EvmApprovalSubmitState.InProgress()
315+
})
316+
break
317+
case "ApprovalReceipt":
318+
evmState = EvmTransferSubmission.ApprovalReceipt({
319+
state: ApprovalReceiptState.InProgress({ hash: this.state.state.state.hash })
320+
})
321+
break
322+
case "TransferSubmit":
323+
evmState = EvmTransferSubmission.TransferSubmit({
324+
state: EvmTransferSubmitState.InProgress()
325+
})
326+
break
327+
case "TransferReceipt":
328+
evmState = EvmTransferSubmission.TransferReceipt({
329+
state: TransferReceiptState.InProgress({ hash: this.state.state.state.hash })
330+
})
331+
break
332+
default:
333+
evmState = EvmTransferSubmission.Filling()
334+
}
335+
} else {
336+
evmState = this.state.state
337+
}
338+
} else {
339+
evmState = EvmTransferSubmission.Filling()
340+
}
296341

297342
const newState = await evmNextState(evmState, this.transferResult.args, sourceChainValue)
298343
this._stateOverride = newState !== null ? TransferState.EVM(newState) : TransferState.Empty()
@@ -311,8 +356,35 @@ export class Transfer {
311356
if (currentEvmState !== null && isEvmComplete(currentEvmState)) break
312357
}
313358
} else {
314-
const cosmosState =
315-
this.state._tag === "Cosmos" ? this.state.state : CosmosTransferSubmission.Filling()
359+
let cosmosState: CosmosTransferSubmission
360+
if (this.state._tag === "Cosmos") {
361+
// If failed, reset the failed step to InProgress
362+
if (hasCosmosFailedExit(this.state.state)) {
363+
switch (this.state.state._tag) {
364+
case "SwitchChain":
365+
cosmosState = CosmosTransferSubmission.SwitchChain({
366+
state: SwitchChainState.InProgress()
367+
})
368+
break
369+
case "ApprovalSubmit":
370+
cosmosState = CosmosTransferSubmission.ApprovalSubmit({
371+
state: ApprovalSubmitState.InProgress()
372+
})
373+
break
374+
case "TransferSubmit":
375+
cosmosState = CosmosTransferSubmission.TransferSubmit({
376+
state: TransferSubmitState.InProgress()
377+
})
378+
break
379+
default:
380+
cosmosState = CosmosTransferSubmission.Filling()
381+
}
382+
} else {
383+
cosmosState = this.state.state
384+
}
385+
} else {
386+
cosmosState = CosmosTransferSubmission.Filling()
387+
}
316388

317389
const newState = await cosmosNextState(
318390
cosmosState,

app2/src/lib/services/cosmos/clients.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import { CosmWasmError } from "$lib/services/transfer-ucs03-cosmos"
1111
export const getCosmWasmClient = (chain: Chain, connectedWallet: CosmosWalletId) =>
1212
Effect.gen(function* () {
1313
if (!chain.rpcs) {
14-
throw new CosmWasmError({
15-
cause: "No RPCs available for chain"
16-
})
14+
return yield* Effect.fail(
15+
new CosmWasmError({
16+
cause: "No RPCs available for chain"
17+
})
18+
)
1719
}
1820

1921
const offlineSigner = yield* Effect.mapError(
@@ -25,10 +27,9 @@ export const getCosmWasmClient = (chain: Chain, connectedWallet: CosmosWalletId)
2527
)
2628

2729
if (!offlineSigner) {
28-
throw new CosmWasmError({ cause: "Offline signer is undefined" })
30+
return yield* Effect.fail(new CosmWasmError({ cause: "Offline signer is undefined" }))
2931
}
3032

31-
// Get gas price
3233
const gasPriceInfo = yield* Effect.mapError(
3334
getGasPriceForChain(chain, connectedWallet),
3435
error =>
@@ -41,9 +42,11 @@ export const getCosmWasmClient = (chain: Chain, connectedWallet: CosmosWalletId)
4142

4243
const rpcUrl = chain.getRpcUrl("rpc")
4344
if (Option.isNone(rpcUrl)) {
44-
throw new CosmWasmError({
45-
cause: "No RPC URL of type 'rpc' available for chain"
46-
})
45+
return yield* Effect.fail(
46+
new CosmWasmError({
47+
cause: "No RPC URL of type 'rpc' available for chain"
48+
})
49+
)
4750
}
4851

4952
return yield* Effect.tryPromise({
@@ -65,23 +68,22 @@ export const getCosmosPublicClient = (rpc: URL | string) =>
6568
return CosmWasmClient.connect(rpcString)
6669
},
6770
catch: err =>
68-
new Error(`Failed to create CosmWasm client with RPC ${rpc} ${err}`, { cause: err })
71+
new CosmWasmError({
72+
cause: `Failed to create CosmWasm client with RPC ${rpc}: ${String(err)}`
73+
})
6974
})
7075

71-
export const getCosmosWalletClient = (): Effect.Effect<CosmosWallet, Error, never> =>
72-
Effect.try({
73-
try: () => {
74-
const { connectedWallet, connectionStatus } = cosmosStore
75-
if (connectionStatus === "connected" && connectedWallet) {
76-
// Type assertion to help TypeScript understand this will be a cosmos wallet
77-
const wallet = window[connectedWallet as keyof Window] as CosmosWallet
78-
if (!wallet) {
79-
throw new Error(`Wallet ${connectedWallet} not found`)
80-
}
81-
return wallet
82-
}
76+
export const getCosmosWalletClient = (): Effect.Effect<CosmosWallet, CosmWasmError, never> =>
77+
Effect.gen(function* () {
78+
const { connectedWallet, connectionStatus } = cosmosStore
79+
if (connectionStatus !== "connected" || !connectedWallet) {
80+
return yield* Effect.fail(new CosmWasmError({ cause: "Wallet not connected" }))
81+
}
8382

84-
throw new Error("Wallet not connected")
85-
},
86-
catch: err => new Error(`Failed to get cosmos wallet client`, { cause: err })
83+
const wallet = window[connectedWallet as keyof Window] as CosmosWallet
84+
if (!wallet) {
85+
return yield* Effect.fail(new CosmWasmError({ cause: `Wallet ${connectedWallet} not found` }))
86+
}
87+
88+
return wallet
8789
})

0 commit comments

Comments
 (0)