|
| 1 | +import { CancelError, SigningError } from '@aave/core-next'; |
| 2 | +import type { TransactionRequest } from '@aave/graphql-next'; |
| 3 | +import { |
| 4 | + assertErr, |
| 5 | + assertOk, |
| 6 | + type BlockchainData, |
| 7 | + chainId, |
| 8 | + evmAddress, |
| 9 | +} from '@aave/types-next'; |
| 10 | +import { HttpResponse } from 'msw'; |
| 11 | +import { |
| 12 | + MethodNotSupportedRpcError, |
| 13 | + SwitchChainError, |
| 14 | + UserRejectedRequestError, |
| 15 | +} from 'viem'; |
| 16 | +import { describe, expect, it } from 'vitest'; |
| 17 | +import { setupRpcInterceptor } from './rpc.helpers'; |
| 18 | +import { createNewWallet } from './test-utils'; |
| 19 | +import { sendWith } from './viem'; |
| 20 | + |
| 21 | +const walletClient = createNewWallet(); |
| 22 | + |
| 23 | +describe(`Given a viem's WalletClient instance`, () => { |
| 24 | + describe(`And the '${sendWith.name}' handler is used to send a TransactionRequest`, () => { |
| 25 | + const request: TransactionRequest = { |
| 26 | + __typename: 'TransactionRequest', |
| 27 | + to: evmAddress(walletClient.account!.address), |
| 28 | + from: evmAddress(walletClient.account!.address), |
| 29 | + data: '0x' as BlockchainData, |
| 30 | + value: 0n, |
| 31 | + chainId: chainId(1), |
| 32 | + operations: [], |
| 33 | + }; |
| 34 | + |
| 35 | + describe('When the wallet is on a different chain than the TransactionRequest chain', () => { |
| 36 | + let walletChainId = `0x${(42).toString(16)}`; |
| 37 | + |
| 38 | + setupRpcInterceptor((request) => { |
| 39 | + switch (request.method) { |
| 40 | + case 'wallet_switchEthereumChain': |
| 41 | + walletChainId = request.params[0].chainId; |
| 42 | + return HttpResponse.json({ |
| 43 | + jsonrpc: '2.0', |
| 44 | + id: request.id, |
| 45 | + result: null, |
| 46 | + }); |
| 47 | + |
| 48 | + case 'eth_chainId': |
| 49 | + return HttpResponse.json({ |
| 50 | + jsonrpc: '2.0', |
| 51 | + id: request.id, |
| 52 | + result: walletChainId, |
| 53 | + }); |
| 54 | + } |
| 55 | + return; |
| 56 | + }); |
| 57 | + |
| 58 | + it('Then it should switch the chain and continue', async () => { |
| 59 | + const result = await sendWith(walletClient)(request); |
| 60 | + |
| 61 | + assertOk(result); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('When the wallet does not support the TransactionRequest chain', () => { |
| 66 | + let walletChainId = `0x${(42).toString(16)}`; |
| 67 | + |
| 68 | + setupRpcInterceptor((request) => { |
| 69 | + switch (request.method) { |
| 70 | + case 'wallet_switchEthereumChain': |
| 71 | + return HttpResponse.json({ |
| 72 | + jsonrpc: '2.0', |
| 73 | + id: request.id, |
| 74 | + error: { |
| 75 | + code: SwitchChainError.code, |
| 76 | + message: 'Unrecognized chain ID', |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + case 'wallet_addEthereumChain': |
| 81 | + walletChainId = request.params[0].chainId; |
| 82 | + return HttpResponse.json({ |
| 83 | + jsonrpc: '2.0', |
| 84 | + id: request.id, |
| 85 | + result: null, |
| 86 | + }); |
| 87 | + |
| 88 | + case 'eth_chainId': |
| 89 | + return HttpResponse.json({ |
| 90 | + jsonrpc: '2.0', |
| 91 | + id: request.id, |
| 92 | + result: walletChainId, |
| 93 | + }); |
| 94 | + } |
| 95 | + return; |
| 96 | + }); |
| 97 | + |
| 98 | + it('Then it should add the chain to the wallet and continue', async () => { |
| 99 | + const result = await sendWith(walletClient)(request); |
| 100 | + |
| 101 | + assertOk(result); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('When the wallet fails to add the chain to the wallet', () => { |
| 106 | + setupRpcInterceptor((request) => { |
| 107 | + switch (request.method) { |
| 108 | + case 'wallet_switchEthereumChain': |
| 109 | + return HttpResponse.json({ |
| 110 | + jsonrpc: '2.0', |
| 111 | + id: request.id, |
| 112 | + error: { |
| 113 | + code: SwitchChainError.code, |
| 114 | + message: 'Unrecognized chain ID', |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + case 'wallet_addEthereumChain': |
| 119 | + return HttpResponse.json({ |
| 120 | + jsonrpc: '2.0', |
| 121 | + id: request.id, |
| 122 | + error: { |
| 123 | + code: MethodNotSupportedRpcError.code, |
| 124 | + message: 'Resource not available', |
| 125 | + }, |
| 126 | + }); |
| 127 | + |
| 128 | + case 'eth_chainId': |
| 129 | + return HttpResponse.json({ |
| 130 | + jsonrpc: '2.0', |
| 131 | + id: request.id, |
| 132 | + result: `0x${(42).toString(16)}`, |
| 133 | + }); |
| 134 | + } |
| 135 | + return; |
| 136 | + }); |
| 137 | + |
| 138 | + it('Then it should fail with a SigningError', async () => { |
| 139 | + const result = await sendWith(walletClient)(request); |
| 140 | + |
| 141 | + assertErr(result); |
| 142 | + expect(result.error).toBeInstanceOf(SigningError); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe('When the user rejects the add chain request in their wallet', () => { |
| 147 | + setupRpcInterceptor((request) => { |
| 148 | + switch (request.method) { |
| 149 | + case 'wallet_switchEthereumChain': |
| 150 | + return HttpResponse.json({ |
| 151 | + jsonrpc: '2.0', |
| 152 | + id: request.id, |
| 153 | + error: { |
| 154 | + code: SwitchChainError.code, |
| 155 | + message: 'Unrecognized chain ID', |
| 156 | + }, |
| 157 | + }); |
| 158 | + |
| 159 | + case 'wallet_addEthereumChain': |
| 160 | + return HttpResponse.json({ |
| 161 | + jsonrpc: '2.0', |
| 162 | + id: request.id, |
| 163 | + error: { |
| 164 | + code: UserRejectedRequestError.code, |
| 165 | + message: 'User rejected the request.', |
| 166 | + }, |
| 167 | + }); |
| 168 | + |
| 169 | + case 'eth_chainId': |
| 170 | + return HttpResponse.json({ |
| 171 | + jsonrpc: '2.0', |
| 172 | + id: request.id, |
| 173 | + result: `0x${(42).toString(16)}`, |
| 174 | + }); |
| 175 | + } |
| 176 | + return; |
| 177 | + }); |
| 178 | + |
| 179 | + it('Then it should fail with a CancelError', async () => { |
| 180 | + const result = await sendWith(walletClient)(request); |
| 181 | + |
| 182 | + assertErr(result); |
| 183 | + expect(result.error).toBeInstanceOf(CancelError); |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + describe(`When the wallet does not support 'wallet_switchEthereumChain'`, () => { |
| 188 | + setupRpcInterceptor((request) => { |
| 189 | + switch (request.method) { |
| 190 | + case 'wallet_switchEthereumChain': |
| 191 | + return HttpResponse.json({ |
| 192 | + jsonrpc: '2.0', |
| 193 | + id: request.id, |
| 194 | + error: { |
| 195 | + code: MethodNotSupportedRpcError.code, |
| 196 | + message: 'method wallet_switchEthereumChain not supported', |
| 197 | + }, |
| 198 | + }); |
| 199 | + |
| 200 | + case 'eth_chainId': |
| 201 | + return HttpResponse.json({ |
| 202 | + jsonrpc: '2.0', |
| 203 | + id: request.id, |
| 204 | + result: `0x${(42).toString(16)}`, |
| 205 | + }); |
| 206 | + } |
| 207 | + return; |
| 208 | + }); |
| 209 | + |
| 210 | + it('Then it should fail with a SigningError', async () => { |
| 211 | + const result = await sendWith(walletClient)(request); |
| 212 | + |
| 213 | + assertErr(result); |
| 214 | + expect(result.error).toBeInstanceOf(SigningError); |
| 215 | + }); |
| 216 | + }); |
| 217 | + }); |
| 218 | +}); |
0 commit comments