|
| 1 | +import type { EvmActions } from '../../../wallets/core/dist/namespaces/evm/types.js'; |
| 2 | +import type { ProxiedNamespace } from '@rango-dev/wallets-core'; |
| 3 | +import type { EvmTransaction } from 'rango-types/mainApi'; |
| 4 | + |
| 5 | +import { |
| 6 | + isError, |
| 7 | + type TransactionRequest, |
| 8 | + type TransactionResponse, |
| 9 | +} from 'ethers'; |
| 10 | +import { BrowserProvider } from 'ethers'; |
| 11 | +import { |
| 12 | + type GenericSigner, |
| 13 | + RPCErrorCode as RangoRPCErrorCode, |
| 14 | + SignerError, |
| 15 | + SignerErrorCode, |
| 16 | +} from 'rango-types'; |
| 17 | + |
| 18 | +import { cleanEvmError, getTenderlyError, waitMs } from './helper.js'; |
| 19 | + |
| 20 | +const waitWithMempoolCheck = async ( |
| 21 | + namespace: ProxiedNamespace<EvmActions>, |
| 22 | + tx: TransactionResponse, |
| 23 | + txHash: string, |
| 24 | + confirmations?: number |
| 25 | +) => { |
| 26 | + const TIMEOUT = 3_000; |
| 27 | + let finished = false; |
| 28 | + return await Promise.race([ |
| 29 | + (async () => { |
| 30 | + await tx.wait(confirmations); |
| 31 | + finished = true; |
| 32 | + })(), |
| 33 | + (async () => { |
| 34 | + while (!finished) { |
| 35 | + await waitMs(TIMEOUT); |
| 36 | + if (finished) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + try { |
| 40 | + const mempoolTx = await namespace.getTransaction(txHash); |
| 41 | + if (!mempoolTx) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + } catch (error) { |
| 45 | + console.log({ error }); |
| 46 | + return null; |
| 47 | + } |
| 48 | + } |
| 49 | + return null; |
| 50 | + })(), |
| 51 | + ]); |
| 52 | +}; |
| 53 | + |
| 54 | +const checkChainIdChanged = async ( |
| 55 | + namespace: ProxiedNamespace<EvmActions>, |
| 56 | + chainId: string |
| 57 | +) => { |
| 58 | + const evmInstance = namespace.getInstance(); |
| 59 | + if (!evmInstance) { |
| 60 | + return true; |
| 61 | + } |
| 62 | + const provider = new BrowserProvider(evmInstance); |
| 63 | + const signerChainId = (await provider.getNetwork()).chainId; |
| 64 | + if ( |
| 65 | + !signerChainId || |
| 66 | + Number(chainId).toString() !== signerChainId.toString() |
| 67 | + ) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + return false; |
| 72 | +}; |
| 73 | + |
| 74 | +export class HubEvmSigner implements GenericSigner<EvmTransaction> { |
| 75 | + private namespace: ProxiedNamespace<EvmActions>; |
| 76 | + |
| 77 | + constructor(namespace: ProxiedNamespace<EvmActions>) { |
| 78 | + this.namespace = namespace; |
| 79 | + } |
| 80 | + |
| 81 | + static buildTx(evmTx: EvmTransaction, disableV2 = false): TransactionRequest { |
| 82 | + const TO_STRING_BASE = 16; |
| 83 | + let tx: TransactionRequest = {}; |
| 84 | + /* |
| 85 | + * it's better to pass 0x instead of undefined, otherwise some wallets could face issue |
| 86 | + * https://github.com/WalletConnect/web3modal/issues/1082#issuecomment-1637793242 |
| 87 | + */ |
| 88 | + tx = { |
| 89 | + data: evmTx.data || '0x', |
| 90 | + }; |
| 91 | + if (evmTx.from) { |
| 92 | + tx = { ...tx, from: evmTx.from }; |
| 93 | + } |
| 94 | + if (evmTx.to) { |
| 95 | + tx = { ...tx, to: evmTx.to }; |
| 96 | + } |
| 97 | + if (evmTx.value) { |
| 98 | + tx = { ...tx, value: evmTx.value }; |
| 99 | + } |
| 100 | + if (evmTx.nonce) { |
| 101 | + tx = { ...tx, nonce: parseInt(evmTx.nonce) }; |
| 102 | + } |
| 103 | + if (evmTx.gasLimit) { |
| 104 | + tx = { ...tx, gasLimit: evmTx.gasLimit }; |
| 105 | + } |
| 106 | + if (!disableV2 && evmTx.maxFeePerGas && evmTx.maxPriorityFeePerGas) { |
| 107 | + tx = { |
| 108 | + ...tx, |
| 109 | + maxFeePerGas: evmTx.maxFeePerGas, |
| 110 | + maxPriorityFeePerGas: evmTx.maxPriorityFeePerGas, |
| 111 | + }; |
| 112 | + } else if (evmTx.gasPrice) { |
| 113 | + tx = { |
| 114 | + ...tx, |
| 115 | + gasPrice: '0x' + parseInt(evmTx.gasPrice).toString(TO_STRING_BASE), |
| 116 | + }; |
| 117 | + } |
| 118 | + return tx; |
| 119 | + } |
| 120 | + |
| 121 | + async signMessage(msg: string): Promise<string> { |
| 122 | + try { |
| 123 | + return this.namespace.signMessage(msg); |
| 124 | + } catch (error) { |
| 125 | + throw new SignerError(SignerErrorCode.SIGN_TX_ERROR, undefined, error); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + async signAndSendTx( |
| 130 | + tx: EvmTransaction, |
| 131 | + address: string, |
| 132 | + chainId: string | null |
| 133 | + ): Promise<{ hash: string; response: TransactionResponse }> { |
| 134 | + try { |
| 135 | + try { |
| 136 | + const transaction = HubEvmSigner.buildTx(tx); |
| 137 | + const response = await this.namespace.sendTransaction( |
| 138 | + transaction, |
| 139 | + address, |
| 140 | + chainId |
| 141 | + ); |
| 142 | + return { hash: response.hash, response }; |
| 143 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 144 | + } catch (error: any) { |
| 145 | + // retrying EIP-1559 without v2 related fields |
| 146 | + if ( |
| 147 | + !!error?.message && |
| 148 | + typeof error.message === 'string' && |
| 149 | + error.message.indexOf('EIP-1559') !== -1 |
| 150 | + ) { |
| 151 | + console.log('retrying EIP-1559 error without v2 fields ...'); |
| 152 | + const transaction = HubEvmSigner.buildTx(tx, true); |
| 153 | + const response = await this.namespace.sendTransaction( |
| 154 | + transaction, |
| 155 | + address, |
| 156 | + chainId |
| 157 | + ); |
| 158 | + return { hash: response.hash, response }; |
| 159 | + } |
| 160 | + throw error; |
| 161 | + } |
| 162 | + } catch (error) { |
| 163 | + throw cleanEvmError(error); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + async wait( |
| 168 | + txHash: string, |
| 169 | + chainId?: string, |
| 170 | + txResponse?: TransactionResponse, |
| 171 | + confirmations?: number |
| 172 | + ): Promise<{ hash: string; response?: TransactionResponse }> { |
| 173 | + try { |
| 174 | + /* |
| 175 | + * if we have transaction response, use that to wait |
| 176 | + * otherwise, try to get tx response from the wallet provider |
| 177 | + */ |
| 178 | + if (txResponse) { |
| 179 | + // if we use waitWithMempoolCheck here, we can't detect replaced tx anymore |
| 180 | + await txResponse?.wait(confirmations); |
| 181 | + return { hash: txHash }; |
| 182 | + } |
| 183 | + |
| 184 | + // ignore wait if namespace is not connected yet |
| 185 | + if (!this.namespace.state()[0]?.()?.connected) { |
| 186 | + return { hash: txHash }; |
| 187 | + } |
| 188 | + |
| 189 | + // ignore wait if namespace does not support getTransaction |
| 190 | + if (!('getTransaction' in this.namespace)) { |
| 191 | + return { hash: txHash }; |
| 192 | + } |
| 193 | + |
| 194 | + /* |
| 195 | + * don't proceed if signer chain changed or chain id is not specified |
| 196 | + * because if user change the wallet network, we receive null on getTransaction |
| 197 | + */ |
| 198 | + if (!chainId) { |
| 199 | + return { hash: txHash }; |
| 200 | + } |
| 201 | + |
| 202 | + const hasChainIdChanged = await checkChainIdChanged( |
| 203 | + this.namespace, |
| 204 | + chainId |
| 205 | + ); |
| 206 | + if (hasChainIdChanged) { |
| 207 | + return { hash: txHash }; |
| 208 | + } |
| 209 | + |
| 210 | + const tx = await this.namespace.getTransaction(txHash); |
| 211 | + if (!tx) { |
| 212 | + throw Error(`Transaction hash '${txHash}' not found in blockchain.`); |
| 213 | + } |
| 214 | + |
| 215 | + await waitWithMempoolCheck(this.namespace, tx, txHash, confirmations); |
| 216 | + return { hash: txHash }; |
| 217 | + } catch (error) { |
| 218 | + if (isError(error, 'TRANSACTION_REPLACED')) { |
| 219 | + const reason = error.reason; |
| 220 | + if (reason === 'cancelled') { |
| 221 | + throw new SignerError( |
| 222 | + SignerErrorCode.SEND_TX_ERROR, |
| 223 | + undefined, |
| 224 | + 'Transaction replaced and canceled by user', |
| 225 | + undefined, |
| 226 | + error |
| 227 | + ); |
| 228 | + } |
| 229 | + return { hash: error.replacement.hash, response: error.replacement }; |
| 230 | + } else if (isError(error, 'CALL_EXCEPTION')) { |
| 231 | + const tError = await getTenderlyError(chainId, txHash); |
| 232 | + if (!!tError) { |
| 233 | + throw new SignerError( |
| 234 | + SignerErrorCode.TX_FAILED_IN_BLOCKCHAIN, |
| 235 | + 'Trannsaction failed in blockchain', |
| 236 | + tError, |
| 237 | + RangoRPCErrorCode.CALL_EXCEPTION, |
| 238 | + error |
| 239 | + ); |
| 240 | + } else { |
| 241 | + /** |
| 242 | + * In cases where the is no error returen from tenderly, we could ignore |
| 243 | + * the error and proceed with check status flow. |
| 244 | + */ |
| 245 | + return { hash: txHash }; |
| 246 | + } |
| 247 | + } |
| 248 | + /** |
| 249 | + * Ignore other errors in confirming transaction and proceed with check status flow, |
| 250 | + * Some times rpc gives internal error or other type of errors even if the transaction succeeded |
| 251 | + */ |
| 252 | + return { hash: txHash }; |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments