@instadapp/avocado-base 0.0.0-dev.0d3d1e2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,94 @@
1
+ export function formatPercent(
2
+ value?: number | string,
3
+ fractionDigits = 2,
4
+ maxValue = null
5
+ ) {
6
+ if (!value || isZero(value)) return "0.00%";
7
+
8
+ const valueAsNumber = toBN(value).toNumber();
9
+
10
+ if (maxValue && gt(times(valueAsNumber, "100"), maxValue))
11
+ return `>${maxValue}%`;
12
+
13
+ const formatter = new Intl.NumberFormat("en-US", {
14
+ style: "percent",
15
+ minimumFractionDigits: fractionDigits,
16
+ maximumFractionDigits: fractionDigits,
17
+ });
18
+
19
+ return formatter.format(valueAsNumber);
20
+ }
21
+
22
+ export function shortenHash(hash: string, length: number = 4) {
23
+ if (!hash) return;
24
+ if (hash.length < 12) return hash;
25
+ const beginningChars = hash.startsWith("0x") ? length + 2 : length;
26
+ const shortened =
27
+ hash.substr(0, beginningChars) + "..." + hash.substr(-length);
28
+ return shortened;
29
+ }
30
+
31
+ export function formatUsd(value: any, fractionDigits = 2) {
32
+ const formatter = new Intl.NumberFormat("en-US", {
33
+ style: "currency",
34
+ currency: "USD",
35
+ minimumFractionDigits: fractionDigits,
36
+ maximumFractionDigits: fractionDigits,
37
+ });
38
+
39
+ return formatter.format(value);
40
+ }
41
+
42
+ export function signedNumber(numb: string | number) {
43
+ return new Intl.NumberFormat("en-US", {
44
+ signDisplay: "exceptZero",
45
+ }).format(toBN(numb).toNumber());
46
+ }
47
+
48
+ function getFractionDigits(value: string | number) {
49
+ const absoluteValue = toBN(value).abs();
50
+
51
+ if (isZero(absoluteValue)) {
52
+ return 2;
53
+ } else if (lt(absoluteValue, 0.01)) {
54
+ return 6;
55
+ } else if (lt(absoluteValue, 1)) {
56
+ return 4;
57
+ } else if (lt(absoluteValue, 10000)) {
58
+ return 2;
59
+ } else {
60
+ return 0;
61
+ }
62
+ }
63
+
64
+ export function formatDecimal(value: string | number, fractionDigits?: number) {
65
+ if (!value) {
66
+ value = "0";
67
+ }
68
+ if (lt(value, "0.0001") && gt(value, "0")) {
69
+ return "< 0.0001";
70
+ } else {
71
+ const num = toBN(value);
72
+ let decimals;
73
+
74
+ if (num.lt(1)) {
75
+ decimals = 8;
76
+ } else if (num.lt(10)) {
77
+ decimals = 6;
78
+ } else if (num.lt(100)) {
79
+ decimals = 4;
80
+ } else if (num.lt(1000)) {
81
+ decimals = 3;
82
+ } else if (num.lt(10000)) {
83
+ decimals = 2;
84
+ } else if (num.lt(100000)) {
85
+ decimals = 1;
86
+ } else {
87
+ decimals = 0;
88
+ }
89
+
90
+ const formattedNumber = num.toFixed(fractionDigits || decimals);
91
+
92
+ return toBN(formattedNumber).toFormat();
93
+ }
94
+ }
@@ -0,0 +1,54 @@
1
+ export const indexSorter = (aIndex: number, bIndex: number) => {
2
+ if (aIndex === -1 && bIndex === -1) {
3
+ return 0; // fallback to other sorting criteria
4
+ } else if (aIndex === -1) {
5
+ return 1; // b comes first if a is not in the priority list
6
+ } else if (bIndex === -1) {
7
+ return -1; // a comes first if b is not in the priority list
8
+ } else {
9
+ return aIndex - bIndex; // sort by the index in the priority list
10
+ }
11
+ };
12
+
13
+ export function sortByMany<T>(
14
+ items: T[],
15
+ callback: ((a: T, b: T) => number)[]
16
+ ) {
17
+ return items.sort(function (a, b) {
18
+ var result = 0;
19
+ for (var i = 0; i < callback.length; i++) {
20
+ var func = callback[i];
21
+ result = func(a, b);
22
+ if (result !== 0) {
23
+ break;
24
+ }
25
+ }
26
+ return result;
27
+ });
28
+ }
29
+
30
+ export function cloneDeep<T>(value: T): T {
31
+ return JSON.parse(JSON.stringify(value));
32
+ }
33
+
34
+ export function filterArray(array: any, filters: any) {
35
+ const filterKeys = Object.keys(filters);
36
+ return array.filter((item: any) => {
37
+ // validates all filter criteria
38
+ return filterKeys.every((key) => {
39
+ // ignores non-function predicates
40
+ if (typeof filters[key] !== "function") return true;
41
+ return filters[key](item[key], item);
42
+ });
43
+ });
44
+ }
45
+
46
+ export function onImageError(this: HTMLImageElement) {
47
+ const parentElement = this.parentElement;
48
+ this.onerror = null;
49
+ this.remove();
50
+
51
+ if (parentElement) {
52
+ parentElement.classList.add("bg-gray-300");
53
+ }
54
+ }
@@ -0,0 +1,367 @@
1
+ import { ethers, utils } from "ethers";
2
+ import { Forwarder__factory } from "@/contracts";
3
+
4
+ const multiMetadataTypes = ["bytes[]"];
5
+
6
+ const metadataTypes = ["bytes32 type", "uint8 version", "bytes data"];
7
+
8
+ const actionMetadataTypes = {
9
+ transfer: ["address token", "uint256 amount", "address receiver"],
10
+ "cross-transfer": [
11
+ "address fromToken",
12
+ "address toToken",
13
+ "uint256 toChainId",
14
+ "uint256 amount",
15
+ "address receiver",
16
+ ],
17
+ bridge: [
18
+ "uint256 amount",
19
+ "address receiver",
20
+ "address fromToken",
21
+ "address toToken",
22
+ "uint256 toChainId",
23
+ "uint256 bridgeFee",
24
+ "address nativeToken",
25
+ ],
26
+ swap: [
27
+ "address sellToken",
28
+ "address buyToken",
29
+ "uint256 sellAmount",
30
+ "uint256 buyAmount",
31
+ "address receiver",
32
+ "bytes32 protocol",
33
+ ],
34
+ "gas-topup": ["uint256 amount", "address token", "address onBehalf"],
35
+ upgrade: ["bytes32 version", "address walletImpl"],
36
+ dapp: ["string name", "string url"],
37
+ deploy: [],
38
+ permit2: [
39
+ "address token",
40
+ "address spender",
41
+ "uint160 amount",
42
+ "uint48 expiration",
43
+ ],
44
+ };
45
+
46
+ const encodeMetadata = (props: MetadataProps) => {
47
+ return ethers.utils.defaultAbiCoder.encode(metadataTypes, [
48
+ ethers.utils.formatBytes32String(props.type),
49
+ props.version || "1",
50
+ props.encodedData,
51
+ ]);
52
+ };
53
+
54
+ export const encodeDappMetadata = (
55
+ params: DappMetadataProps,
56
+ single = true
57
+ ) => {
58
+ const encodedData = ethers.utils.defaultAbiCoder.encode(
59
+ actionMetadataTypes.dapp,
60
+ [params.name, params.url]
61
+ );
62
+
63
+ const data = encodeMetadata({
64
+ type: "dapp",
65
+ encodedData,
66
+ });
67
+
68
+ return single ? encodeMultipleActions(data) : data;
69
+ };
70
+
71
+ export const encodeTransferMetadata = (
72
+ params: SendMetadataProps,
73
+ single = true
74
+ ) => {
75
+ const encodedData = ethers.utils.defaultAbiCoder.encode(
76
+ actionMetadataTypes.transfer,
77
+ [params.token, params.amount, params.receiver]
78
+ );
79
+
80
+ const data = encodeMetadata({
81
+ type: "transfer",
82
+ encodedData,
83
+ });
84
+
85
+ return single ? encodeMultipleActions(data) : data;
86
+ };
87
+
88
+ export const encodeCrossTransferMetadata = (
89
+ params: CrossSendMetadatProps,
90
+ single = true
91
+ ) => {
92
+ const encodedData = ethers.utils.defaultAbiCoder.encode(
93
+ actionMetadataTypes["cross-transfer"],
94
+ [
95
+ params.fromToken,
96
+ params.toToken,
97
+ params.toChainId,
98
+ params.amount,
99
+ params.receiver,
100
+ ]
101
+ );
102
+
103
+ const data = encodeMetadata({
104
+ type: "cross-transfer",
105
+ encodedData,
106
+ });
107
+
108
+ return single ? encodeMultipleActions(data) : data;
109
+ };
110
+
111
+ export const encodeDeployMetadata = (single = true) => {
112
+ const data = encodeMetadata({
113
+ type: "deploy",
114
+ encodedData: "0x",
115
+ });
116
+
117
+ return single ? encodeMultipleActions(data) : data;
118
+ };
119
+
120
+ export const encodeWCSignMetadata = (
121
+ params: SignMetadataProps,
122
+ single = true
123
+ ) => {
124
+ const encodedData = ethers.utils.defaultAbiCoder.encode(
125
+ actionMetadataTypes["permit2"],
126
+ [params.token, params.spender, params.amount, params.expiration]
127
+ );
128
+
129
+ const data = encodeMetadata({
130
+ type: "permit2",
131
+ encodedData,
132
+ });
133
+
134
+ return single ? encodeMultipleActions(data) : data;
135
+ };
136
+
137
+ export const encodeUpgradeMetadata = (
138
+ params: UpgradeMetadataProps,
139
+ single = true
140
+ ) => {
141
+ const encodedData = ethers.utils.defaultAbiCoder.encode(
142
+ actionMetadataTypes.upgrade,
143
+ [params.version, params.walletImpl]
144
+ );
145
+
146
+ const data = encodeMetadata({
147
+ type: "upgrade",
148
+ encodedData,
149
+ });
150
+
151
+ return single ? encodeMultipleActions(data) : data;
152
+ };
153
+
154
+ export const encodeSwapMetadata = (
155
+ params: SwapMetadataProps,
156
+ single = true
157
+ ) => {
158
+ const encodedData = ethers.utils.defaultAbiCoder.encode(
159
+ actionMetadataTypes.swap,
160
+ [
161
+ params.sellToken,
162
+ params.buyToken,
163
+ params.sellAmount,
164
+ params.buyAmount,
165
+ params.receiver,
166
+ params.protocol,
167
+ ]
168
+ );
169
+
170
+ const data = encodeMetadata({
171
+ type: "swap",
172
+ encodedData,
173
+ });
174
+
175
+ return single ? encodeMultipleActions(data) : data;
176
+ };
177
+
178
+ export const encodeTopupMetadata = (
179
+ params: TopupMetadataProps,
180
+ single = true
181
+ ) => {
182
+ const encodedData = ethers.utils.defaultAbiCoder.encode(
183
+ actionMetadataTypes["gas-topup"],
184
+ [params.amount, params.token, params.onBehalf]
185
+ );
186
+
187
+ console.log(params);
188
+
189
+ const data = encodeMetadata({
190
+ type: "gas-topup",
191
+ encodedData,
192
+ });
193
+
194
+ return single ? encodeMultipleActions(data) : data;
195
+ };
196
+
197
+ export const encodeBridgeMetadata = (
198
+ params: BridgeMetadataProps,
199
+ single = true
200
+ ) => {
201
+ const encodedData = ethers.utils.defaultAbiCoder.encode(
202
+ actionMetadataTypes.bridge,
203
+ [
204
+ params.amount,
205
+ params.receiver,
206
+ params.fromToken,
207
+ params.toToken,
208
+ params.toChainId,
209
+ params.bridgeFee,
210
+ params.nativeToken,
211
+ ]
212
+ );
213
+
214
+ const data = encodeMetadata({
215
+ type: "bridge",
216
+ encodedData,
217
+ });
218
+
219
+ return single ? encodeMultipleActions(data) : data;
220
+ };
221
+
222
+ export const encodeMultipleActions = (...actionData: string[]) => {
223
+ return ethers.utils.defaultAbiCoder.encode(multiMetadataTypes, [actionData]);
224
+ };
225
+
226
+ export const decodeMetadata = (data: string) => {
227
+ try {
228
+ const iface = Forwarder__factory.createInterface();
229
+ let metadata = "0x";
230
+ let payload = {};
231
+
232
+ if (!data) return payload;
233
+
234
+ if (data.startsWith("0x18e7f485")) {
235
+ const executeData = iface.decodeFunctionData("execute", data);
236
+ if (executeData.metadata_ === "0x" || !executeData.metadata_) {
237
+ return null;
238
+ } else {
239
+ metadata = executeData.metadata_;
240
+ }
241
+ } else {
242
+ const executeDataV2 = iface.decodeFunctionData("executeV2", data);
243
+ if (
244
+ executeDataV2.params_.metadata === "0x" ||
245
+ !executeDataV2.params_.metadata
246
+ ) {
247
+ return null;
248
+ } else {
249
+ metadata = executeDataV2.params_.metadata;
250
+ }
251
+ }
252
+
253
+ const metadataArr = [];
254
+
255
+ const [decodedMultiMetadata = []] =
256
+ (ethers.utils.defaultAbiCoder.decode(
257
+ multiMetadataTypes,
258
+ metadata
259
+ ) as string[]) || [];
260
+
261
+ for (let metadata of decodedMultiMetadata) {
262
+ const decodedMetadata = ethers.utils.defaultAbiCoder.decode(
263
+ metadataTypes,
264
+ metadata
265
+ );
266
+
267
+ const type = ethers.utils.parseBytes32String(
268
+ decodedMetadata.type
269
+ ) as keyof typeof actionMetadataTypes;
270
+
271
+ const decodedData = ethers.utils.defaultAbiCoder.decode(
272
+ actionMetadataTypes[type],
273
+ decodedMetadata.data
274
+ );
275
+
276
+ switch (type) {
277
+ case "transfer":
278
+ payload = {
279
+ type,
280
+ token: decodedData.token,
281
+ amount: toBN(decodedData.amount).toFixed(),
282
+ receiver: decodedData.receiver,
283
+ };
284
+ break;
285
+ case "bridge":
286
+ payload = {
287
+ type,
288
+ amount: toBN(decodedData.amount).toFixed(),
289
+ receiver: decodedData.receiver,
290
+ toToken: decodedData.toToken,
291
+ fromToken: decodedData.fromToken,
292
+ toChainId: decodedData.toChainId
293
+ ? decodedData.toChainId.toString()
294
+ : null,
295
+ bridgeFee: toBN(decodedData.bridgeFee).toFixed(),
296
+ };
297
+ break;
298
+ case "swap":
299
+ payload = {
300
+ type,
301
+ buyAmount: toBN(decodedData.buyAmount).toFixed(),
302
+ sellAmount: toBN(decodedData.sellAmount).toFixed(),
303
+ buyToken: decodedData.buyToken,
304
+ sellToken: decodedData.sellToken,
305
+ receiver: decodedData.receiver,
306
+ protocol: utils.parseBytes32String(decodedData?.protocol || ""),
307
+ };
308
+ break;
309
+ case "upgrade":
310
+ payload = {
311
+ type,
312
+ version: utils.parseBytes32String(decodedData?.version || ""),
313
+ walletImpl: decodedData?.walletImpl,
314
+ };
315
+ break;
316
+ case "gas-topup":
317
+ payload = {
318
+ type,
319
+ amount: toBN(decodedData.amount).toFixed(),
320
+ token: decodedData.token,
321
+ onBehalf: decodedData.onBehalf,
322
+ };
323
+ break;
324
+ case "dapp":
325
+ payload = {
326
+ type,
327
+ name: decodedData?.name,
328
+ url: decodedData?.url,
329
+ };
330
+ break;
331
+ case "deploy":
332
+ payload = {
333
+ type,
334
+ };
335
+ break;
336
+
337
+ case "permit2":
338
+ payload = {
339
+ type,
340
+ token: decodedData.token,
341
+ spender: decodedData.spender,
342
+ amount: toBN(decodedData.amount).toFixed(),
343
+ expiration: decodedData.expiration,
344
+ };
345
+ break;
346
+
347
+ case "cross-transfer":
348
+ payload = {
349
+ type,
350
+ fromToken: decodedData.fromToken,
351
+ toToken: decodedData.toToken,
352
+ toChainId: decodedData.toChainId,
353
+ amount: toBN(decodedData.amount).toFixed(),
354
+ };
355
+
356
+ break;
357
+ }
358
+
359
+ metadataArr.push(payload);
360
+ }
361
+
362
+ return metadataArr;
363
+ } catch (e) {
364
+ console.log(e);
365
+ return null;
366
+ }
367
+ };