@subsquid/evm-typegen 3.3.0 → 4.0.0

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.
@@ -1,116 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.ContractBase = exports.isFunctionResultDecodingError = exports.Func = exports.LogEvent = void 0;
30
- const assert_1 = __importDefault(require("assert"));
31
- const ethers = __importStar(require("ethers"));
32
- class LogEvent {
33
- constructor(abi, topic) {
34
- this.abi = abi;
35
- this.topic = topic;
36
- let fragment = abi.getEvent(topic);
37
- (0, assert_1.default)(fragment != null, 'Missing fragment');
38
- this.fragment = fragment;
39
- }
40
- is(rec) {
41
- return rec.topics[0] === this.topic;
42
- }
43
- decode(rec) {
44
- return this.abi.decodeEventLog(this.fragment, rec.data, rec.topics);
45
- }
46
- }
47
- exports.LogEvent = LogEvent;
48
- class Func {
49
- constructor(abi, sighash) {
50
- this.abi = abi;
51
- this.sighash = sighash;
52
- let fragment = abi.getFunction(sighash);
53
- (0, assert_1.default)(fragment != null, 'Missing fragment');
54
- this.fragment = fragment;
55
- }
56
- is(rec) {
57
- let sighash = rec.sighash ? rec.sighash : rec.input.slice(0, 10);
58
- return sighash === this.sighash;
59
- }
60
- decode(inputOrRec) {
61
- const input = ethers.isBytesLike(inputOrRec) ? inputOrRec : inputOrRec.input;
62
- return this.abi.decodeFunctionData(this.fragment, input);
63
- }
64
- encode(args) {
65
- return this.abi.encodeFunctionData(this.fragment, args);
66
- }
67
- decodeResult(output) {
68
- const decoded = this.abi.decodeFunctionResult(this.fragment, output);
69
- return decoded.length > 1 ? decoded : decoded[0];
70
- }
71
- tryDecodeResult(output) {
72
- try {
73
- return this.decodeResult(output);
74
- }
75
- catch (err) {
76
- return undefined;
77
- }
78
- }
79
- }
80
- exports.Func = Func;
81
- function isFunctionResultDecodingError(val) {
82
- if (!(val instanceof Error))
83
- return false;
84
- let err = val;
85
- return err.code == 'CALL_EXCEPTION'
86
- && typeof err.data == 'string'
87
- && !err.errorArgs
88
- && !err.errorName;
89
- }
90
- exports.isFunctionResultDecodingError = isFunctionResultDecodingError;
91
- class ContractBase {
92
- constructor(ctx, blockOrAddress, address) {
93
- this._chain = ctx._chain;
94
- if (typeof blockOrAddress === 'string') {
95
- this.blockHeight = ctx.block.height;
96
- this.address = ethers.getAddress(blockOrAddress);
97
- }
98
- else {
99
- if (address == null) {
100
- throw new Error('missing contract address');
101
- }
102
- this.blockHeight = blockOrAddress.height;
103
- this.address = ethers.getAddress(address);
104
- }
105
- }
106
- async eth_call(func, args) {
107
- let data = func.encode(args);
108
- let result = await this._chain.client.call('eth_call', [
109
- { to: this.address, data },
110
- '0x' + this.blockHeight.toString(16)
111
- ]);
112
- return func.decodeResult(result);
113
- }
114
- }
115
- exports.ContractBase = ContractBase;
116
- //# sourceMappingURL=abi.support.js.map
@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AAA;AACzB,CAAC;AAPD,sEAOC;AA0BD,MAAa,YAAY;IAOrB,YAAY,GAAiB,EAAE,cAA8B,EAAE,OAAgB;QAC3E,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAAA;QACxB,IAAI,OAAO,cAAc,KAAK,QAAQ,EAAG,CAAC;YACtC,IAAI,CAAC,WAAW,GAAG,GAAG,CAAC,KAAK,CAAC,MAAM,CAAA;YACnC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,cAAc,CAAC,CAAA;QACpD,CAAC;aAAO,CAAC;YACL,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;gBAClB,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAA;YAC/C,CAAC;YACD,IAAI,CAAC,WAAW,GAAG,cAAc,CAAC,MAAM,CAAA;YACxC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAA;QAC7C,CAAC;IACL,CAAC;IAED,KAAK,CAAC,QAAQ,CAAwC,IAAmC,EAAE,IAAU;QACjG,IAAI,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAC5B,IAAI,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE;YACnD,EAAC,EAAE,EAAE,IAAI,CAAC,OAAO,EAAE,IAAI,EAAC;YACxB,IAAI,GAAC,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC;SACrC,CAAC,CAAA;QACF,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAA;IACpC,CAAC;CACJ;AA7BD,oCA6BC"}
@@ -1,135 +0,0 @@
1
- import assert from 'assert'
2
- import * as ethers from 'ethers'
3
-
4
- export interface EventRecord {
5
- topics: string[]
6
- data: string
7
- }
8
- export type LogRecord = EventRecord
9
-
10
- export class LogEvent<Args> {
11
- private fragment: ethers.EventFragment
12
-
13
- constructor(private abi: ethers.Interface, public readonly topic: string) {
14
- let fragment = abi.getEvent(topic)
15
- assert(fragment != null, 'Missing fragment')
16
- this.fragment = fragment
17
- }
18
-
19
- is(rec: EventRecord): boolean {
20
- return rec.topics[0] === this.topic
21
- }
22
-
23
- decode(rec: EventRecord): Args {
24
- return this.abi.decodeEventLog(this.fragment, rec.data, rec.topics) as any as Args
25
- }
26
- }
27
-
28
- export interface FuncRecord {
29
- sighash?: string
30
- input: string
31
- }
32
-
33
- export class Func<Args extends any[], FieldArgs, Result> {
34
- private fragment: ethers.FunctionFragment
35
-
36
- constructor(private abi: ethers.Interface, public readonly sighash: string) {
37
- let fragment = abi.getFunction(sighash)
38
- assert(fragment != null, 'Missing fragment')
39
- this.fragment = fragment
40
- }
41
-
42
- is(rec: FuncRecord): boolean {
43
- let sighash = rec.sighash ? rec.sighash : rec.input.slice(0, 10)
44
- return sighash === this.sighash
45
- }
46
-
47
- decode(input: ethers.BytesLike): Args & FieldArgs
48
- decode(rec: FuncRecord): Args & FieldArgs
49
- decode(inputOrRec: ethers.BytesLike | FuncRecord): Args & FieldArgs {
50
- const input = ethers.isBytesLike(inputOrRec) ? inputOrRec : inputOrRec.input
51
- return this.abi.decodeFunctionData(this.fragment, input) as any as Args & FieldArgs
52
- }
53
-
54
- encode(args: Args): string {
55
- return this.abi.encodeFunctionData(this.fragment, args)
56
- }
57
-
58
- decodeResult(output: ethers.BytesLike): Result {
59
- const decoded = this.abi.decodeFunctionResult(this.fragment, output)
60
- return decoded.length > 1 ? decoded : decoded[0]
61
- }
62
-
63
- tryDecodeResult(output: ethers.BytesLike): Result | undefined {
64
- try {
65
- return this.decodeResult(output)
66
- } catch(err: any) {
67
- return undefined
68
- }
69
- }
70
- }
71
-
72
-
73
- export function isFunctionResultDecodingError(val: unknown): val is Error & {data: string} {
74
- if (!(val instanceof Error)) return false
75
- let err = val as any
76
- return err.code == 'CALL_EXCEPTION'
77
- && typeof err.data == 'string'
78
- && !err.errorArgs
79
- && !err.errorName
80
- }
81
-
82
-
83
- export interface ChainContext {
84
- _chain: Chain
85
- }
86
-
87
-
88
- export interface BlockContext {
89
- _chain: Chain
90
- block: Block
91
- }
92
-
93
-
94
- export interface Block {
95
- height: number
96
- }
97
-
98
-
99
- export interface Chain {
100
- client: {
101
- call: <T=any>(method: string, params?: unknown[]) => Promise<T>
102
- }
103
- }
104
-
105
-
106
- export class ContractBase {
107
- private readonly _chain: Chain
108
- private readonly blockHeight: number
109
- readonly address: string
110
-
111
- constructor(ctx: BlockContext, address: string)
112
- constructor(ctx: ChainContext, block: Block, address: string)
113
- constructor(ctx: BlockContext, blockOrAddress: Block | string, address?: string) {
114
- this._chain = ctx._chain
115
- if (typeof blockOrAddress === 'string') {
116
- this.blockHeight = ctx.block.height
117
- this.address = ethers.getAddress(blockOrAddress)
118
- } else {
119
- if (address == null) {
120
- throw new Error('missing contract address')
121
- }
122
- this.blockHeight = blockOrAddress.height
123
- this.address = ethers.getAddress(address)
124
- }
125
- }
126
-
127
- async eth_call<Args extends any[], FieldArgs, Result>(func: Func<Args, FieldArgs, Result>, args: Args): Promise<Result> {
128
- let data = func.encode(args)
129
- let result = await this._chain.client.call('eth_call', [
130
- {to: this.address, data},
131
- '0x'+this.blockHeight.toString(16)
132
- ])
133
- return func.decodeResult(result)
134
- }
135
- }