-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-aa.ts
More file actions
167 lines (143 loc) · 4.46 KB
/
use-aa.ts
File metadata and controls
167 lines (143 loc) · 4.46 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { useCallback } from 'react';
import invariant from 'tiny-invariant';
import { useCapabilities, useSendCalls } from 'wagmi/experimental';
import {
eip5792Actions,
type GetCallsStatusReturnType,
} from 'viem/experimental';
import { TransactionCallbackStage } from '@lidofinance/lido-ethereum-sdk';
import { useDappStatus } from './use-dapp-status';
import { useLidoSDK } from '../web3-provider';
import { config } from 'config';
import type { Address, Hash } from 'viem';
const retry = (retryCount: number, error: object) => {
if (
'code' in error &&
typeof error.code === 'number' &&
error.code === -32601
)
return false;
return retryCount <= 3;
};
export const useAA = () => {
const { chainId } = useDappStatus();
const capabilitiesQuery = useCapabilities({
query: {
retry,
},
});
const capabilities =
capabilitiesQuery.data && capabilitiesQuery.data[chainId];
// use new AA flow only for batching supported accounts
// because some wallets cannot handle non-atomic batching (e.g Ambire EOA)
const isAA = !!capabilities?.atomicBatch?.supported;
const areAuxiliaryFundsSupported = !!capabilities?.auxiliaryFunds?.supported;
return {
...capabilitiesQuery,
isAA,
capabilities,
areAuxiliaryFundsSupported,
};
};
type SendCallsStages =
| {
stage: TransactionCallbackStage.SIGN;
}
| {
stage: TransactionCallbackStage.RECEIPT;
callId: string;
}
| {
stage: TransactionCallbackStage.CONFIRMATION;
callStatus: GetCallsStatusReturnType;
}
| {
stage: TransactionCallbackStage.DONE;
txHash: Hash;
}
| {
stage: TransactionCallbackStage.ERROR;
error: unknown;
};
export class SendCallsError extends Error {}
export type AACall = { to: Address; data?: Hash; value?: bigint };
export const useSendAACalls = () => {
const { sendCallsAsync } = useSendCalls();
const { core } = useLidoSDK();
return useCallback(
async (
// falsish calls will be filtered out
calls: (AACall | null | undefined | false)[],
callback: (props: SendCallsStages) => Promise<void> = async () => {},
) => {
try {
invariant(core.web3Provider);
const extendedWalletClient = core.web3Provider.extend(eip5792Actions());
await callback({
stage: TransactionCallbackStage.SIGN,
});
const callId = await sendCallsAsync({
calls: (calls.filter((call) => !!call) as AACall[]).map((call) => ({
to: call.to,
data: call.data,
value: call.value,
})),
});
await callback({
stage: TransactionCallbackStage.RECEIPT,
callId,
});
const poll = async () => {
const timeoutAt = Date.now() + config.AA_TX_POLLING_TIMEOUT;
while (Date.now() < timeoutAt) {
const callStatus = await extendedWalletClient
.getCallsStatus({
id: callId,
})
.catch(() => {
// workaround for gnosis safe bug
return { status: 'PENDING' } as const;
});
if (callStatus.status === 'CONFIRMED') {
return callStatus;
}
await new Promise((resolve) =>
setTimeout(resolve, config.PROVIDER_POLLING_INTERVAL),
);
}
throw new SendCallsError(
'Timeout for transaction confirmation exceeded.',
);
};
const callStatus = await poll();
await callback({
stage: TransactionCallbackStage.CONFIRMATION,
callStatus,
});
if (
callStatus.receipts?.find((receipt) => receipt.status === 'reverted')
) {
throw new SendCallsError(
'Some operation were reverted. Check your wallet for details.',
);
}
// extract last receipt if there was no atomic batch
const txHash = callStatus.receipts
? callStatus?.receipts[callStatus.receipts.length - 1].transactionHash
: undefined;
if (!txHash) {
throw new SendCallsError('Could not locate tx hash');
}
await callback({
stage: TransactionCallbackStage.DONE,
txHash,
});
return { callStatus, txHash };
} catch (error) {
await callback({ stage: TransactionCallbackStage.ERROR, error });
throw error;
}
},
[core.web3Provider, sendCallsAsync],
);
};