This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
OperationResults.ts
181 lines (153 loc) · 4.6 KB
/
OperationResults.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { BigNumber, ContractReceipt, utils } from "ethers";
import assert from "./helpers/assert";
import { ActionData } from "./signer";
const errorSelectors = {
Error: calculateAndCheckSelector("Error(string)", "0x08c379a0"),
Panic: calculateAndCheckSelector("Panic(uint256)", "0x4e487b71"),
ActionError: calculateAndCheckSelector(
"ActionError(uint256,bytes)",
"0x5c667601",
),
};
const actionErrorId = utils
.keccak256(new TextEncoder().encode("ActionError(uint256,bytes)"))
.slice(0, 10);
assert(actionErrorId === "0x5c667601");
export type OperationResultError = {
actionIndex?: BigNumber;
message: string;
};
export type OperationResult = {
walletAddress: string;
nonce: BigNumber;
actions: ActionData[];
success: boolean;
results: string[];
error?: OperationResultError;
};
/**
* Checks if a operation result error string is valid and returns
* the decoded error.
*
* @param errorData An error string returned by an operation result.
*/
export const decodeError = (errorData: string): OperationResultError => {
if (!errorData.startsWith(errorSelectors.ActionError)) {
throw new Error(
[
`errorResult does not begin with ActionError selector`,
`(${errorSelectors.ActionError}): ${errorData}`,
].join(" "),
);
}
// remove methodId (4bytes after 0x)
const actionErrorArgBytes = `0x${errorData.slice(10)}`;
let actionIndex: BigNumber | undefined;
let message: string;
try {
const [actionIndexDecoded, actionErrorData] = utils.defaultAbiCoder.decode(
["uint256", "bytes"],
actionErrorArgBytes,
) as [BigNumber, string];
actionIndex = actionIndexDecoded;
const actionErrorDataBody = `0x${actionErrorData.slice(10)}`;
if (actionErrorData.startsWith(errorSelectors.Error)) {
[message] = utils.defaultAbiCoder.decode(["string"], actionErrorDataBody);
} else if (actionErrorData.startsWith(errorSelectors.Panic)) {
const [panicCode] = utils.defaultAbiCoder.decode(
["uint256"],
actionErrorDataBody,
) as [BigNumber];
message = [
`Panic: ${panicCode.toHexString()}`,
"(See Panic(uint256) in the solidity docs:",
"https://docs.soliditylang.org/_/downloads/en/latest/pdf/)",
].join(" ");
} else {
message = `Unexpected action error data: ${actionErrorData}`;
}
} catch (error) {
console.error(error);
message = `Unexpected error data: ${errorData}`;
}
return {
actionIndex,
message,
};
};
const getError = (
success: boolean,
results: string[],
): OperationResultError | undefined => {
if (success) {
return undefined;
}
// Single event "WalletOperationProcessed(address indexed wallet, uint256 nonce, bool success, bytes[] results)"
// Get the first (only) result from "results" argument.
const [errorData] = results;
return decodeError(errorData);
};
export const getOperationResults = (
txnReceipt: ContractReceipt,
): OperationResult[] => {
if (!txnReceipt.events || !txnReceipt.events.length) {
throw new Error(
`no events found in transaction ${txnReceipt.transactionHash}`,
);
}
const walletOpProcessedEvents = txnReceipt.events.filter(
(e) => e.event === "WalletOperationProcessed",
);
if (!walletOpProcessedEvents.length) {
throw new Error(
`no WalletOperationProcessed events found in transaction ${txnReceipt.transactionHash}`,
);
}
return walletOpProcessedEvents.reduce<OperationResult[]>(
(opResults, { args }) => {
if (!args) {
throw new Error("WalletOperationProcessed event missing args");
}
const { wallet, nonce, actions: rawActions, success, results } = args;
const actions = rawActions.map(
({
ethValue,
contractAddress,
encodedFunction,
}: {
ethValue: BigNumber;
contractAddress: string;
encodedFunction: string;
}) => ({
ethValue,
contractAddress,
encodedFunction,
}),
);
const error = getError(success, results);
return [
...opResults,
{
walletAddress: wallet,
nonce,
actions,
success,
results,
error,
},
];
},
[],
);
};
function calculateSelector(signature: string) {
return utils.keccak256(new TextEncoder().encode(signature)).slice(0, 10);
}
function calculateAndCheckSelector(signature: string, expected: string) {
const selector = calculateSelector(signature);
assert(
selector === expected,
`Selector for ${signature} was not ${expected}`,
);
return selector;
}