77} from '@aave/core' ;
88import type {
99 CancelSwapTypedData ,
10+ Chain ,
1011 ExecutionPlan ,
1112 PermitTypedDataResponse ,
1213 SwapByIntentTypedData ,
@@ -26,7 +27,6 @@ import {
2627} from '@aave/types' ;
2728import {
2829 type Account ,
29- type Chain ,
3030 defineChain ,
3131 type ProviderRpcError ,
3232 type RpcError ,
@@ -36,6 +36,7 @@ import {
3636 type TypedData ,
3737 type TypedDataDomain ,
3838 UserRejectedRequestError ,
39+ type Chain as ViemChain ,
3940 type WalletClient ,
4041} from 'viem' ;
4142import {
@@ -44,6 +45,9 @@ import {
4445 signTypedData ,
4546 waitForTransactionReceipt ,
4647} from 'viem/actions' ;
48+ import { mainnet , sepolia } from 'viem/chains' ;
49+ import type { AaveClient } from './AaveClient' ;
50+ import { chain as fetchChain } from './actions' ;
4751import type {
4852 ERC20PermitHandler ,
4953 ExecutionPlanHandler ,
@@ -66,10 +70,7 @@ function isProviderRpcError(
6670 : true ;
6771}
6872
69- /**
70- * @internal
71- */
72- export const devnetChain : Chain = defineChain ( {
73+ const devnetChain : ViemChain = defineChain ( {
7374 id : Number . parseInt ( import . meta. env . ETHEREUM_TENDERLY_FORK_ID , 10 ) ,
7475 name : 'Devnet' ,
7576 network : 'ethereum-fork' ,
@@ -87,57 +88,101 @@ export const devnetChain: Chain = defineChain({
8788
8889/**
8990 * @internal
91+ * @deprecated
9092 */
91- export const supportedChains : Record <
92- ChainId ,
93- ReturnType < typeof defineChain >
94- > = {
95- // TODO add them back when deployed on these chains
96- // [chainId(mainnet.id)]: mainnet,
97- // [chainId(sepolia.id)]: sepolia,
93+ export const supportedChains : Record < ChainId , ViemChain > = {
9894 [ chainId ( devnetChain . id ) ] : devnetChain ,
9995} ;
10096
101- function ensureChain (
97+ /**
98+ * @internal
99+ */
100+ export function toViemChain ( chain : Chain ) : ViemChain {
101+ // known chains
102+ switch ( chain . chainId ) {
103+ case chainId ( mainnet . id ) :
104+ return mainnet ;
105+
106+ case chainId ( sepolia . id ) :
107+ return sepolia ;
108+ }
109+
110+ // most likely a tenderly fork
111+ return defineChain ( {
112+ id : chain . chainId ,
113+ name : chain . name ,
114+ nativeCurrency : {
115+ name : chain . nativeInfo . name ,
116+ symbol : chain . nativeInfo . symbol ,
117+ decimals : chain . nativeInfo . decimals ,
118+ } ,
119+ rpcUrls : { default : { http : [ chain . rpcUrl ] } } ,
120+ blockExplorers : {
121+ default : {
122+ name : `${ chain . name } Explorer` ,
123+ url : chain . explorerUrl ,
124+ } ,
125+ } ,
126+ } ) ;
127+ }
128+
129+ /**
130+ * @internal
131+ */
132+ export function viemChainsFrom ( chains : Chain [ ] ) : ViemChain [ ] {
133+ return chains . map ( toViemChain ) ;
134+ }
135+
136+ /**
137+ * @internal
138+ */
139+ export function ensureChain (
140+ aaveClient : AaveClient ,
102141 walletClient : WalletClient ,
103142 request : TransactionRequest ,
104- ) : ResultAsync < void , CancelError | SigningError > {
143+ ) : ResultAsync < void , CancelError | SigningError | UnexpectedError > {
105144 return ResultAsync . fromPromise ( walletClient . getChainId ( ) , ( err ) =>
106145 SigningError . from ( err ) ,
107146 ) . andThen ( ( chainId ) => {
108147 if ( chainId === request . chainId ) {
109148 return okAsync ( ) ;
110149 }
111150
112- return ResultAsync . fromPromise (
113- walletClient . switchChain ( { id : request . chainId } ) ,
114- ( err ) => SigningError . from ( err ) ,
115- ) . orElse ( ( err ) => {
116- const code = isRpcError ( err . cause )
117- ? err . cause . code
118- : // Unwrapping for MetaMask Mobile
119- // https://github.com/MetaMask/metamask-mobile/issues/2944#issuecomment-976988719
120- isProviderRpcError ( err . cause )
121- ? err . cause . data ?. originalError ?. code
122- : undefined ;
123-
124- if (
125- code === SwitchChainError . code &&
126- request . chainId in supportedChains
127- ) {
151+ return fetchChain ( aaveClient , { chainId : request . chainId } ) . andThen (
152+ ( chain ) => {
153+ invariant ( chain , `Chain ${ request . chainId } is not supported` ) ;
154+
128155 return ResultAsync . fromPromise (
129- walletClient . addChain ( { chain : supportedChains [ request . chainId ] } ) ,
130- ( err ) => {
131- if ( isRpcError ( err ) && err . code === UserRejectedRequestError . code ) {
132- return CancelError . from ( err ) ;
133- }
134- return SigningError . from ( err ) ;
135- } ,
136- ) ;
137- }
156+ walletClient . switchChain ( { id : request . chainId } ) ,
157+ ( err ) => SigningError . from ( err ) ,
158+ ) . orElse ( ( err ) => {
159+ const code = isRpcError ( err . cause )
160+ ? err . cause . code
161+ : // Unwrapping for MetaMask Mobile
162+ // https://github.com/MetaMask/metamask-mobile/issues/2944#issuecomment-976988719
163+ isProviderRpcError ( err . cause )
164+ ? err . cause . data ?. originalError ?. code
165+ : undefined ;
138166
139- return err . asResultAsync ( ) ;
140- } ) ;
167+ if ( code === SwitchChainError . code ) {
168+ return ResultAsync . fromPromise (
169+ walletClient . addChain ( { chain : toViemChain ( chain ) } ) ,
170+ ( err ) => {
171+ if (
172+ isRpcError ( err ) &&
173+ err . code === UserRejectedRequestError . code
174+ ) {
175+ return CancelError . from ( err ) ;
176+ }
177+ return SigningError . from ( err ) ;
178+ } ,
179+ ) ;
180+ }
181+
182+ return err . asResultAsync ( ) ;
183+ } ) ;
184+ } ,
185+ ) ;
141186 } ) ;
142187}
143188
@@ -157,7 +202,7 @@ function estimateGas(
157202}
158203
159204function sendEip1559Transaction (
160- walletClient : WalletClient < Transport , Chain , Account > ,
205+ walletClient : WalletClient < Transport , ViemChain , Account > ,
161206 request : TransactionRequest ,
162207) : ResultAsync < TxHash , CancelError | SigningError > {
163208 return estimateGas ( walletClient , request )
@@ -190,7 +235,7 @@ function sendEip1559Transaction(
190235
191236function isWalletClientWithAccount (
192237 walletClient : WalletClient ,
193- ) : walletClient is WalletClient < Transport , Chain , Account > {
238+ ) : walletClient is WalletClient < Transport , ViemChain , Account > {
194239 return walletClient . account !== undefined ;
195240}
196241
@@ -206,16 +251,14 @@ export function sendTransaction(
206251 'Wallet client with account is required' ,
207252 ) ;
208253
209- return ensureChain ( walletClient , request ) . andThen ( ( _ ) =>
210- sendEip1559Transaction ( walletClient , request ) ,
211- ) ;
254+ return sendEip1559Transaction ( walletClient , request ) ;
212255}
213256
214257/**
215258 * @internal
216259 */
217260export function transactionError (
218- chain : Chain | undefined ,
261+ chain : ViemChain | undefined ,
219262 txHash : TxHash ,
220263 request : TransactionRequest ,
221264) : TransactionError {
0 commit comments