forked from gnosisguild/zodiac-pilot
/
ForkProvider.ts
67 lines (54 loc) · 1.53 KB
/
ForkProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { ITxData } from '@walletconnect/types'
import { GanacheProvider } from './ProvideGanache'
class UnsupportedMethodError extends Error {
code = 4200
}
interface Handlers {
onTransactionReceived(txData: ITxData, hash: string): void
}
class ForkProvider {
private avatarAddress: string
private provider: GanacheProvider
private handlers: Handlers
constructor(
provider: GanacheProvider,
avatarAddress: string,
handlers: Handlers
) {
this.provider = provider
this.avatarAddress = avatarAddress
this.handlers = handlers
}
async request(request: {
method: string
params?: Array<any>
}): Promise<any> {
const { method, params = [] } = request
switch (method) {
case 'eth_requestAccounts': {
return [this.avatarAddress]
}
case 'eth_accounts': {
return [this.avatarAddress]
}
// curve.fi is unhappy without this
case 'wallet_switchEthereumChain': {
return true
}
// Uniswap will try to use this for ERC-20 permits, but this wont fly with a contract wallet
case 'eth_signTypedData_v4': {
throw new UnsupportedMethodError(
'eth_signTypedData_v4 is not supported'
)
}
case 'eth_sendTransaction': {
// record the transaction
const result = await this.provider.request(request)
this.handlers.onTransactionReceived(params[0] as ITxData, result)
return result
}
}
return await this.provider.request(request)
}
}
export default ForkProvider