-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextensions.ts
664 lines (630 loc) · 19.3 KB
/
extensions.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import type { AbiEvent } from 'abitype';
import {
createPublicClient,
createWalletClient,
decodeFunctionResult,
encodeFunctionData,
getContractError,
toBytes,
toHex,
type Abi,
type Account,
type Address,
type BaseError,
type CallParameters,
type Chain,
type CreateContractEventFilterParameters,
type DecodeFunctionResultParameters,
type EncodeFunctionDataParameters,
type EstimateContractGasReturnType,
type EstimateGasParameters,
type GetContractParameters,
type ReadContractParameters,
type ReadContractReturnType,
type SimulateContractParameters,
type SimulateContractReturnType,
type Transport,
type WatchContractEventParameters,
} from 'viem';
import { parseAccount } from 'viem/accounts';
import { estimateGas } from 'viem/actions';
import { getPublicClientRs, getWalletClientRs } from './index';
import * as Redstone from './redstone/index.js';
import * as LocalTypes from './types';
export const getRsReadFn = (
redstone: Redstone.RedstoneHelper,
client: ReturnType<typeof createPublicClient>,
shouldMock: (chain: Chain) => boolean
) =>
async function rsRead<TAbi extends Abi | readonly unknown[], TFunctionName extends string | undefined = undefined>({
abi,
functionName,
args,
address,
account,
dataFeeds = redstone.dataFeeds,
mockDataFeedValues = redstone.mockDataFeedValues,
...callParams
}: LocalTypes.RsReadParams<TAbi, TFunctionName>) {
try {
const argsForEncoding = {
abi,
args,
functionName,
} as unknown as EncodeFunctionDataParameters<TAbi, TFunctionName>;
const calldata =
encodeFunctionData(argsForEncoding) +
(dataFeeds ? await redstone.getPayload(shouldMock(client.chain), dataFeeds, mockDataFeedValues) : '');
const result = await client.call({
to: address,
from: account,
data: calldata,
...callParams,
} as unknown as CallParameters);
return decodeFunctionResult({
abi,
args,
functionName,
data: result?.data ?? '0x',
} as DecodeFunctionResultParameters<TAbi, TFunctionName>) as ReadContractReturnType<TAbi, TFunctionName>;
} catch (err) {
throw getContractError(err as BaseError, {
abi: abi as Abi,
address,
args,
docsPath: '/docs/contract/readContract',
functionName,
});
}
};
export const getRsWriteFn = (
redstone: Redstone.RedstoneHelper,
client: ReturnType<typeof createWalletClient>,
shouldMock: (chain: Chain) => boolean
) =>
async function <
TAcc extends Account | undefined = Account | undefined,
TAbi extends Abi | readonly unknown[] = Abi,
TChain extends Chain | undefined = Chain,
TFN extends string | undefined = string,
TChainOverride extends Chain | undefined = undefined
>({
abi,
functionName,
args,
address,
account,
dataFeeds = redstone.dataFeeds,
mockDataFeedValues = redstone.mockDataFeedValues,
dataSuffix,
chain,
...request
}: LocalTypes.WriteParamsRs<TAbi, TFN, TChain, TAcc, TChainOverride>) {
try {
const argsForEncoding = {
abi,
args,
functionName,
} as unknown as EncodeFunctionDataParameters<TAbi, TFN>;
const calldata =
encodeFunctionData(argsForEncoding) +
(dataFeeds ? await redstone.getPayload(shouldMock(chain || client.chain), dataFeeds, mockDataFeedValues) : '');
return await client.sendTransaction({
data: `${calldata}${dataSuffix ? dataSuffix.replace('0x', '') : ''}`,
to: address,
account: account || (client.account as Account),
chain: chain || client.chain,
...request,
});
} catch (err) {
throw err;
}
};
export const getRsEstimateFn = (
redstone: Redstone.RedstoneHelper,
client: ReturnType<typeof createWalletClient>,
shouldMock: (chain: Chain) => boolean
) =>
async function <
TAcc extends Account | undefined = Account,
TAbi extends Abi | readonly unknown[] = Abi,
TChain extends Chain | undefined = Chain,
TFN extends string | undefined = string,
TChainOverride extends Chain | undefined = undefined
>({
abi,
functionName,
args,
address,
account,
dataFeeds = redstone.dataFeeds,
mockDataFeedValues = redstone.mockDataFeedValues,
dataSuffix,
chain,
...request
}: LocalTypes.WriteParamsRs<TAbi, TFN, TChain, TAcc, TChainOverride>) {
try {
const argsForEncoding = {
abi,
args,
functionName,
} as unknown as EncodeFunctionDataParameters<TAbi, TFN>;
const calldata =
encodeFunctionData(argsForEncoding) +
(dataFeeds ? await redstone.getPayload(shouldMock(chain || client.chain), dataFeeds, mockDataFeedValues) : '');
return await client.sendTransaction({
data: `${calldata}${dataSuffix ? dataSuffix.replace('0x', '') : ''}`,
to: address,
account: account || (client.account as Account),
chain: chain || client.chain,
...request,
});
} catch (err) {
throw err;
}
};
export const getEstimateContractGasFn = (
redstone: Redstone.RedstoneHelper,
client: ReturnType<typeof createPublicClient> | ReturnType<typeof createWalletClient>,
shouldMock: (chain: Chain) => boolean
) =>
async function <
TAbi extends Abi | readonly unknown[],
TFunctionName extends string,
TChain extends Chain | undefined,
TAccount extends Account | undefined = undefined
>({
abi,
address,
args,
functionName,
dataFeeds = redstone.dataFeeds,
mockDataFeedValues = redstone.mockDataFeedValues,
chain,
...request
}: LocalTypes.EstimateContractGasParamsRs<
TAbi,
TFunctionName,
TChain,
TAccount
>): Promise<EstimateContractGasReturnType> {
const data =
encodeFunctionData({
abi,
args,
functionName,
} as unknown as EncodeFunctionDataParameters<TAbi, TFunctionName>) +
(await redstone.getPayload(shouldMock(chain || client.chain), dataFeeds, mockDataFeedValues));
try {
const gas = await estimateGas(
client as any,
{
data,
to: address,
chain,
...request,
} as unknown as EstimateGasParameters<TChain>
);
return gas;
} catch (err) {
const account = request.account ? parseAccount(request.account) : undefined;
throw getContractError(err as BaseError, {
abi: abi as Abi,
address,
args,
docsPath: '/docs/contract/simulateContract',
functionName,
sender: account?.address,
});
}
};
/**
* Gets type-safe interface for performing contract-related actions with a specific `abi` and `address`.
*
* - Docs https://viem.sh/docs/contract/getContract.html
*
* Using Contract Instances can make it easier to work with contracts if you don't want to pass the `abi` and `address` properites every time you perform contract actions, e.g. [`readContract`](https://viem.sh/docs/contract/readContract.html), [`writeContract`](https://viem.sh/docs/contract/writeContract.html), [`estimateContractGas`](https://viem.sh/docs/contract/estimateContractGas.html), etc.
*
* @example
* import { createPublicClient, getContract, http, parseAbi } from 'viem'
* import { mainnet } from 'viem/chains'
*
* const publicClient = createPublicClient({
* chain: mainnet,
* transport: http(),
* })
* const contract = getContract({
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
* abi: parseAbi([
* 'function balanceOf(address owner) view returns (uint256)',
* 'function ownerOf(uint256 tokenId) view returns (address)',
* 'function totalSupply() view returns (uint256)',
* ]),
* publicClient,
* })
*/
export const getSimulateContractFn = (
redstone: Redstone.RedstoneHelper,
client: ReturnType<typeof createPublicClient>,
shouldMock: (chain: Chain) => boolean
) =>
async function <
TChain extends Chain | undefined,
TAbi extends Abi | readonly unknown[],
TFunctionName extends string,
TChainOverride extends Chain | undefined
>({
abi,
address,
args,
dataSuffix,
functionName,
dataFeeds = redstone.dataFeeds,
mockDataFeedValues = redstone.mockDataFeedValues,
chain,
...callRequest
}: LocalTypes.SimulateContractParametersRs<TAbi, TFunctionName, TChain, TChainOverride>): Promise<
SimulateContractReturnType<TAbi, TFunctionName, TChain, TChainOverride> & {
dataFeeds?: string[];
mockDataFeedValues?: number[];
}
> {
const account = callRequest.account ? parseAccount(callRequest.account) : undefined;
const calldata =
encodeFunctionData({
abi,
args,
functionName,
} as unknown as EncodeFunctionDataParameters<TAbi, TFunctionName>) +
(await redstone.getPayload(shouldMock(chain || client.chain), dataFeeds, mockDataFeedValues));
try {
const { data } = await client.call({
batch: false,
data: `${calldata}${dataSuffix ? dataSuffix.replace('0x', '') : ''}`,
to: address,
chain,
...callRequest,
} as unknown as CallParameters<TChain>);
const result = decodeFunctionResult({
abi,
args,
functionName,
data: data || '0x',
} as DecodeFunctionResultParameters);
return {
result,
request: {
abi,
address,
args,
dataSuffix,
functionName,
chain,
...callRequest,
},
} as unknown as SimulateContractReturnType<TAbi, TFunctionName, TChain, TChainOverride>;
} catch (err) {
throw getContractError(err as BaseError, {
abi: abi as Abi,
address,
args,
docsPath: '/docs/contract/simulateContract',
functionName,
sender: account?.address,
});
}
};
/**
* @internal exporting for testing only
*/
export function getFunctionParameters(values: [args?: readonly unknown[], options?: object]) {
const hasArgs = values.length && Array.isArray(values[0]);
const args = hasArgs ? values[0]! : [];
const options = (hasArgs ? values[1] : values[0]) ?? {};
return { args, options };
}
/**
* @internal exporting for testing only
*/
export function getEventParameters(values: [args?: object | unknown[], options?: object], abiEvent: AbiEvent) {
let hasArgs = false;
// If first item is array, must be `args`
if (Array.isArray(values[0])) hasArgs = true;
// Check if first item is `args` or `options`
else if (values.length === 1) {
// if event has indexed inputs, must have `args`
hasArgs = abiEvent.inputs.some((x) => x.indexed);
// If there are two items in array, must have `args`
} else if (values.length === 2) {
hasArgs = true;
}
const args = hasArgs ? values[0]! : undefined;
const options = (hasArgs ? values[1] : values[0]) ?? {};
return { args, options };
}
export function getContract<
TTransport extends Transport,
TAddress extends Address,
TAbi extends Abi | readonly unknown[],
TChain extends Chain | undefined = Chain | undefined,
TAccount extends Account | undefined = Account | undefined,
TPublicClient extends ReturnType<typeof getPublicClientRs> = ReturnType<typeof getPublicClientRs>,
TWalletClient extends ReturnType<typeof getWalletClientRs> = ReturnType<typeof getWalletClientRs>
>({
abi,
address,
publicClient,
walletClient,
dataFeeds,
mockDataFeedValues,
}: GetContractParameters<TTransport, TChain, TAccount, TAbi, TPublicClient, TWalletClient, TAddress> & {
dataFeeds?: string[];
mockDataFeedValues?: number[];
}): LocalTypes.GetContractReturnType<TAbi, TPublicClient, TWalletClient, TAddress> & {
dataFeeds: typeof dataFeeds;
mockDataFeedValues?: number[];
stash: TPublicClient extends ReturnType<typeof getPublicClientRs> ? TPublicClient['stash'] : TWalletClient['stash'];
rs: TPublicClient extends ReturnType<typeof getPublicClientRs> ? TPublicClient['rs'] : TWalletClient['rs'];
rsMocking: TPublicClient extends ReturnType<typeof getPublicClientRs>
? TPublicClient['rsMocking']
: TWalletClient['rsMocking'];
} {
const hasPublicClient = publicClient != null;
const hasWalletClient = walletClient != null;
const contract: {
[_ in
| 'abi'
| 'address'
| 'rs'
| 'stash'
| 'createEventFilter'
| 'estimateGas'
| 'read'
| 'rsMocking'
| 'dataFeeds'
| 'mockDataFeedValues'
| 'simulate'
| 'watchEvent'
| 'write']?: unknown;
} = {};
let hasReadFunction = false;
let hasWriteFunction = false;
let hasEvent = false;
for (const item of abi as Abi) {
if (item.type === 'function')
if (item.stateMutability === 'view' || item.stateMutability === 'pure') hasReadFunction = true;
else hasWriteFunction = true;
else if (item.type === 'event') hasEvent = true;
// Exit early if all flags are `true`
if (hasReadFunction && hasWriteFunction && hasEvent) break;
}
if (hasPublicClient) {
if (hasReadFunction)
contract.read = new Proxy(
{},
{
get(_, functionName: string) {
return (
...parameters: [
args?: readonly unknown[],
options?: Omit<LocalTypes.RsReadParams, 'abi' | 'address' | 'functionName' | 'args'>
]
) => {
const { args, options } = getFunctionParameters(parameters);
return publicClient.rsRead({
abi,
address,
functionName,
args,
...options,
dataFeeds: parameters[1]?.dataFeeds || dataFeeds,
mockDataFeedValues: parameters[1]?.mockDataFeedValues || mockDataFeedValues,
} as ReadContractParameters);
};
},
}
);
if (hasWriteFunction)
contract.simulate = new Proxy(
{},
{
get(_, functionName: string) {
return (
...parameters: [
args?: readonly unknown[],
options?: Omit<LocalTypes.SimulateContractParametersRs, 'abi' | 'address' | 'functionName' | 'args'>
]
) => {
const { args, options } = getFunctionParameters(parameters);
return publicClient.rsSimulate({
abi,
address,
functionName,
dataFeeds: parameters[1]?.dataFeeds || dataFeeds,
mockDataFeedValues: parameters[1]?.mockDataFeedValues || mockDataFeedValues,
args,
...options,
} as SimulateContractParameters & {
dataFeeds?: string[];
mockDataFeedValues?: number[];
});
};
},
}
);
if (hasEvent) {
contract.createEventFilter = new Proxy(
{},
{
get(_, eventName: string) {
return (
...parameters: [
args?: readonly unknown[] | object,
options?: Omit<CreateContractEventFilterParameters, 'abi' | 'address' | 'eventName' | 'args'>
]
) => {
const abiEvent = (abi as readonly any[]).find(
(x: AbiEvent) => x.type === 'event' && x.name === eventName
);
const { args, options } = getEventParameters(parameters, abiEvent!);
return publicClient.createContractEventFilter({
abi,
address,
eventName,
args,
...options,
} as unknown as CreateContractEventFilterParameters);
};
},
}
);
contract.watchEvent = new Proxy(
{},
{
get(_, eventName: string) {
return (
...parameters: [
args?: readonly unknown[] | object,
options?: Omit<WatchContractEventParameters, 'abi' | 'address' | 'eventName'>
]
) => {
const abiEvent = (abi as readonly AbiEvent[]).find(
(x: AbiEvent) => x.type === 'event' && x.name === eventName
);
const { args, options } = getEventParameters(parameters, abiEvent!);
return publicClient.watchContractEvent({
abi,
address,
eventName,
args,
...options,
} as unknown as WatchContractEventParameters);
};
},
}
);
}
}
if (hasWalletClient) {
if (hasWriteFunction)
contract.write = new Proxy(
{},
{
get(_, functionName: string) {
return (
...parameters: [
args?: readonly unknown[],
options?: Omit<LocalTypes.WriteParamsRs, 'abi' | 'address' | 'functionName' | 'args'>
]
) => {
const { args, options } = getFunctionParameters(parameters);
return walletClient.rsWrite({
abi,
address,
functionName,
args,
...options,
dataFeeds: parameters[1]?.dataFeeds || dataFeeds,
mockDataFeedValues: parameters[1]?.mockDataFeedValues || mockDataFeedValues,
} as unknown as LocalTypes.WriteParamsRs<TAbi, typeof functionName, TChain, TAccount>);
};
},
}
);
}
if (hasPublicClient || hasWalletClient)
if (hasWriteFunction)
contract.estimateGas = new Proxy(
{},
{
get(_, functionName: string) {
return (
...parameters: [
args?: readonly unknown[],
options?: Omit<LocalTypes.EstimateContractGasParamsRs, 'abi' | 'address' | 'functionName' | 'args'>
]
) => {
const { args, options } = getFunctionParameters(parameters);
const client = walletClient ?? publicClient;
return client.rsEstimateGas({
abi,
address,
functionName,
args,
...options,
dataFeeds: parameters[1]?.dataFeeds || dataFeeds,
mockDataFeedValues: parameters[1]?.mockDataFeedValues || mockDataFeedValues,
account: (options as LocalTypes.EstimateContractGasParamsRs).account ?? walletClient.account,
} as LocalTypes.EstimateContractGasParamsRs);
};
},
}
);
contract.dataFeeds = dataFeeds || walletClient?.rs.dataFeeds || publicClient?.rs.dataFeeds;
contract.mockDataFeedValues =
mockDataFeedValues || walletClient?.rs.mockDataFeedValues || publicClient?.rs.mockDataFeedValues;
contract.stash = walletClient?.stash || publicClient?.stash;
contract.rsMocking = walletClient?.rsMocking || publicClient?.rsMocking;
contract.address = address;
contract.abi = abi;
const rs = new Redstone.RedstoneHelper({
dataServiceId: walletClient?.rs.dataServiceId || publicClient?.rs.dataServiceId,
uniqueSignersCount: walletClient?.rs.uniqueSignersCount || publicClient?.rs.uniqueSignersCount,
urls: walletClient?.rs.urls || publicClient?.rs.urls,
mockDataFeedValues:
mockDataFeedValues || walletClient?.rs.mockDataFeedValues || publicClient?.rs.mockDataFeedValues,
dataFeeds: dataFeeds || walletClient?.rs.dataFeeds || publicClient?.rs.dataFeeds,
disablePayloadsDryRun: walletClient?.rs.disablePayloadsDryRun || publicClient?.rs.disablePayloadsDryRun,
shouldMock: walletClient?.rsMocking || publicClient?.rsMocking,
});
contract.rs = rs;
return contract as unknown as LocalTypes.GetContractReturnType<TAbi, TPublicClient, TWalletClient, TAddress> & {
dataFeeds: typeof dataFeeds;
mockDataFeedValues?: number[];
stash: TPublicClient extends ReturnType<typeof getPublicClientRs> ? TPublicClient['stash'] : TWalletClient['stash'];
rs: TPublicClient extends ReturnType<typeof getPublicClientRs> ? TPublicClient['rs'] : TWalletClient['rs'];
rsMocking: TPublicClient extends ReturnType<typeof getPublicClientRs>
? TPublicClient['rsMocking']
: TWalletClient['rsMocking'];
};
}
export const splitSignatureBase64 = (signature: string) => {
const bufferSignature = Buffer.from(signature, 'base64');
const [r, s, v] = [
toHex(bufferSignature.subarray(0, 32)),
toHex(bufferSignature.subarray(32, 64)),
bufferSignature[64],
];
const recoveryParam = 1 - (v % 2);
// Compute _vs from recoveryParam and s
if (recoveryParam) {
bufferSignature[32] |= 0x80;
}
const _vs = toHex(bufferSignature.subarray(32, 64));
const compact = r + _vs.substring(2);
return {
r,
s,
v,
_vs: toHex(bufferSignature.subarray(32, 64)),
compact,
};
};
export const splitSignature = (signature: string) => {
const bytesSignature = toBytes(signature);
const [r, s, v] = [toHex(bytesSignature.slice(0, 32)), toHex(bytesSignature.slice(32, 64)), bytesSignature[64]];
const recoveryParam = 1 - (v % 2);
// Compute _vs from recoveryParam and s
if (recoveryParam) {
bytesSignature[32] |= 0x80;
}
const _vs = toHex(bytesSignature.subarray(32, 64));
const compact = r + _vs.substring(2);
return {
r,
s,
v,
_vs: toHex(bytesSignature.subarray(32, 64)),
compact,
};
};