@waku/rln 0.1.5-bc093d3.0 → 0.1.5-d17562d.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.
Files changed (119) hide show
  1. package/bundle/index.js +2 -1
  2. package/bundle/packages/rln/dist/contract/constants.js +5 -1
  3. package/bundle/packages/rln/dist/contract/rln_base_contract.js +33 -35
  4. package/bundle/packages/rln/dist/contract/rln_contract.js +1 -1
  5. package/bundle/packages/rln/dist/utils/bytes.js +8 -2
  6. package/bundle/packages/rln/dist/utils/metamask.js +2 -2
  7. package/dist/.tsbuildinfo +1 -1
  8. package/dist/contract/index.d.ts +1 -0
  9. package/dist/contract/index.js +1 -0
  10. package/dist/contract/index.js.map +1 -1
  11. package/dist/contract/rln_base_contract.d.ts +4 -4
  12. package/dist/contract/rln_base_contract.js +31 -33
  13. package/dist/contract/rln_base_contract.js.map +1 -1
  14. package/dist/index.d.ts +2 -1
  15. package/dist/index.js +1 -0
  16. package/dist/index.js.map +1 -1
  17. package/dist/keystore/keystore.js.map +1 -1
  18. package/dist/keystore/types.d.ts +2 -2
  19. package/dist/utils/bytes.js +8 -2
  20. package/dist/utils/bytes.js.map +1 -1
  21. package/package.json +1 -1
  22. package/src/contract/index.ts +1 -0
  23. package/src/contract/rln_base_contract.ts +47 -47
  24. package/src/index.ts +3 -1
  25. package/src/keystore/keystore.ts +4 -2
  26. package/src/keystore/types.ts +2 -2
  27. package/src/utils/bytes.ts +10 -2
  28. package/bundle/_virtual/__node-resolve_empty.js +0 -6
  29. package/bundle/_virtual/_node-resolve_empty.js +0 -3
  30. package/bundle/_virtual/bn.js +0 -3
  31. package/bundle/_virtual/common.js +0 -3
  32. package/bundle/_virtual/common2.js +0 -3
  33. package/bundle/_virtual/hash.js +0 -3
  34. package/bundle/_virtual/inherits_browser.js +0 -3
  35. package/bundle/_virtual/ripemd.js +0 -3
  36. package/bundle/_virtual/sha.js +0 -3
  37. package/bundle/_virtual/sha3.js +0 -3
  38. package/bundle/_virtual/utils3.js +0 -3
  39. package/bundle/node_modules/@ethersproject/abi/lib.esm/_version.js +0 -3
  40. package/bundle/node_modules/@ethersproject/abi/lib.esm/abi-coder.js +0 -96
  41. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/abstract-coder.js +0 -148
  42. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/address.js +0 -26
  43. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/anonymous.js +0 -20
  44. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/array.js +0 -210
  45. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/boolean.js +0 -18
  46. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/bytes.js +0 -30
  47. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/fixed-bytes.js +0 -26
  48. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/null.js +0 -22
  49. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/number.js +0 -43
  50. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/string.js +0 -19
  51. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/tuple.js +0 -58
  52. package/bundle/node_modules/@ethersproject/abi/lib.esm/fragments.js +0 -854
  53. package/bundle/node_modules/@ethersproject/abi/lib.esm/interface.js +0 -609
  54. package/bundle/node_modules/@ethersproject/abstract-provider/lib.esm/_version.js +0 -3
  55. package/bundle/node_modules/@ethersproject/abstract-provider/lib.esm/index.js +0 -66
  56. package/bundle/node_modules/@ethersproject/abstract-signer/lib.esm/_version.js +0 -3
  57. package/bundle/node_modules/@ethersproject/abstract-signer/lib.esm/index.js +0 -302
  58. package/bundle/node_modules/@ethersproject/address/lib.esm/_version.js +0 -3
  59. package/bundle/node_modules/@ethersproject/address/lib.esm/index.js +0 -110
  60. package/bundle/node_modules/@ethersproject/base64/lib.esm/base64.js +0 -20
  61. package/bundle/node_modules/@ethersproject/basex/lib.esm/index.js +0 -120
  62. package/bundle/node_modules/@ethersproject/bignumber/lib.esm/_version.js +0 -3
  63. package/bundle/node_modules/@ethersproject/bignumber/lib.esm/bignumber.js +0 -287
  64. package/bundle/node_modules/@ethersproject/bytes/lib.esm/_version.js +0 -3
  65. package/bundle/node_modules/@ethersproject/bytes/lib.esm/index.js +0 -402
  66. package/bundle/node_modules/@ethersproject/constants/lib.esm/addresses.js +0 -3
  67. package/bundle/node_modules/@ethersproject/constants/lib.esm/bignumbers.js +0 -8
  68. package/bundle/node_modules/@ethersproject/constants/lib.esm/hashes.js +0 -3
  69. package/bundle/node_modules/@ethersproject/contracts/lib.esm/_version.js +0 -3
  70. package/bundle/node_modules/@ethersproject/contracts/lib.esm/index.js +0 -893
  71. package/bundle/node_modules/@ethersproject/hash/lib.esm/_version.js +0 -3
  72. package/bundle/node_modules/@ethersproject/hash/lib.esm/ens-normalize/decoder.js +0 -256
  73. package/bundle/node_modules/@ethersproject/hash/lib.esm/ens-normalize/include.js +0 -36
  74. package/bundle/node_modules/@ethersproject/hash/lib.esm/ens-normalize/lib.js +0 -135
  75. package/bundle/node_modules/@ethersproject/hash/lib.esm/id.js +0 -8
  76. package/bundle/node_modules/@ethersproject/hash/lib.esm/namehash.js +0 -64
  77. package/bundle/node_modules/@ethersproject/hash/lib.esm/typed-data.js +0 -443
  78. package/bundle/node_modules/@ethersproject/keccak256/lib.esm/index.js +0 -8
  79. package/bundle/node_modules/@ethersproject/keccak256/node_modules/js-sha3/src/sha3.js +0 -660
  80. package/bundle/node_modules/@ethersproject/logger/lib.esm/_version.js +0 -3
  81. package/bundle/node_modules/@ethersproject/logger/lib.esm/index.js +0 -352
  82. package/bundle/node_modules/@ethersproject/networks/lib.esm/_version.js +0 -3
  83. package/bundle/node_modules/@ethersproject/networks/lib.esm/index.js +0 -248
  84. package/bundle/node_modules/@ethersproject/properties/lib.esm/_version.js +0 -3
  85. package/bundle/node_modules/@ethersproject/properties/lib.esm/index.js +0 -127
  86. package/bundle/node_modules/@ethersproject/providers/lib.esm/_version.js +0 -3
  87. package/bundle/node_modules/@ethersproject/providers/lib.esm/base-provider.js +0 -2007
  88. package/bundle/node_modules/@ethersproject/providers/lib.esm/formatter.js +0 -422
  89. package/bundle/node_modules/@ethersproject/providers/lib.esm/json-rpc-provider.js +0 -674
  90. package/bundle/node_modules/@ethersproject/providers/lib.esm/web3-provider.js +0 -132
  91. package/bundle/node_modules/@ethersproject/rlp/lib.esm/_version.js +0 -3
  92. package/bundle/node_modules/@ethersproject/rlp/lib.esm/index.js +0 -120
  93. package/bundle/node_modules/@ethersproject/sha2/lib.esm/sha2.js +0 -8
  94. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/_version.js +0 -3
  95. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/elliptic.js +0 -2430
  96. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/index.js +0 -76
  97. package/bundle/node_modules/@ethersproject/strings/lib.esm/_version.js +0 -3
  98. package/bundle/node_modules/@ethersproject/strings/lib.esm/utf8.js +0 -219
  99. package/bundle/node_modules/@ethersproject/transactions/lib.esm/_version.js +0 -3
  100. package/bundle/node_modules/@ethersproject/transactions/lib.esm/index.js +0 -279
  101. package/bundle/node_modules/@ethersproject/web/lib.esm/_version.js +0 -3
  102. package/bundle/node_modules/@ethersproject/web/lib.esm/geturl.js +0 -69
  103. package/bundle/node_modules/@ethersproject/web/lib.esm/index.js +0 -404
  104. package/bundle/node_modules/bech32/index.js +0 -187
  105. package/bundle/node_modules/bn.js/lib/bn.js +0 -3361
  106. package/bundle/node_modules/hash.js/lib/hash/common.js +0 -97
  107. package/bundle/node_modules/hash.js/lib/hash/hmac.js +0 -51
  108. package/bundle/node_modules/hash.js/lib/hash/ripemd.js +0 -152
  109. package/bundle/node_modules/hash.js/lib/hash/sha/1.js +0 -81
  110. package/bundle/node_modules/hash.js/lib/hash/sha/224.js +0 -33
  111. package/bundle/node_modules/hash.js/lib/hash/sha/256.js +0 -113
  112. package/bundle/node_modules/hash.js/lib/hash/sha/384.js +0 -39
  113. package/bundle/node_modules/hash.js/lib/hash/sha/512.js +0 -336
  114. package/bundle/node_modules/hash.js/lib/hash/sha/common.js +0 -53
  115. package/bundle/node_modules/hash.js/lib/hash/sha.js +0 -14
  116. package/bundle/node_modules/hash.js/lib/hash/utils.js +0 -282
  117. package/bundle/node_modules/hash.js/lib/hash.js +0 -33
  118. package/bundle/node_modules/inherits/inherits_browser.js +0 -33
  119. package/bundle/node_modules/minimalistic-assert/index.js +0 -13
@@ -1,893 +0,0 @@
1
- import { Interface, Indexed } from '../../abi/lib.esm/interface.js';
2
- import { Provider } from '../../abstract-provider/lib.esm/index.js';
3
- import { Signer, VoidSigner } from '../../abstract-signer/lib.esm/index.js';
4
- import { getAddress, getContractAddress } from '../../address/lib.esm/index.js';
5
- import { isHexString, arrayify } from '../../bytes/lib.esm/index.js';
6
- import { defineReadOnly, getStatic, shallowCopy, deepCopy, resolveProperties } from '../../properties/lib.esm/index.js';
7
- import { accessListify } from '../../transactions/lib.esm/index.js';
8
- import { Logger } from '../../logger/lib.esm/index.js';
9
- import { version } from './_version.js';
10
- import { checkResultErrors } from '../../abi/lib.esm/coders/abstract-coder.js';
11
- import { BigNumber } from '../../bignumber/lib.esm/bignumber.js';
12
-
13
- var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- const logger = new Logger(version);
23
- function resolveName(resolver, nameOrPromise) {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- const name = yield nameOrPromise;
26
- if (typeof (name) !== "string") {
27
- logger.throwArgumentError("invalid address or ENS name", "name", name);
28
- }
29
- // If it is already an address, just use it (after adding checksum)
30
- try {
31
- return getAddress(name);
32
- }
33
- catch (error) { }
34
- if (!resolver) {
35
- logger.throwError("a provider or signer is needed to resolve ENS names", Logger.errors.UNSUPPORTED_OPERATION, {
36
- operation: "resolveName"
37
- });
38
- }
39
- const address = yield resolver.resolveName(name);
40
- if (address == null) {
41
- logger.throwArgumentError("resolver or addr is not configured for ENS name", "name", name);
42
- }
43
- return address;
44
- });
45
- }
46
- // Recursively replaces ENS names with promises to resolve the name and resolves all properties
47
- function resolveAddresses(resolver, value, paramType) {
48
- return __awaiter(this, void 0, void 0, function* () {
49
- if (Array.isArray(paramType)) {
50
- return yield Promise.all(paramType.map((paramType, index) => {
51
- return resolveAddresses(resolver, ((Array.isArray(value)) ? value[index] : value[paramType.name]), paramType);
52
- }));
53
- }
54
- if (paramType.type === "address") {
55
- return yield resolveName(resolver, value);
56
- }
57
- if (paramType.type === "tuple") {
58
- return yield resolveAddresses(resolver, value, paramType.components);
59
- }
60
- if (paramType.baseType === "array") {
61
- if (!Array.isArray(value)) {
62
- return Promise.reject(logger.makeError("invalid value for array", Logger.errors.INVALID_ARGUMENT, {
63
- argument: "value",
64
- value
65
- }));
66
- }
67
- return yield Promise.all(value.map((v) => resolveAddresses(resolver, v, paramType.arrayChildren)));
68
- }
69
- return value;
70
- });
71
- }
72
- function populateTransaction(contract, fragment, args) {
73
- return __awaiter(this, void 0, void 0, function* () {
74
- // If an extra argument is given, it is overrides
75
- let overrides = {};
76
- if (args.length === fragment.inputs.length + 1 && typeof (args[args.length - 1]) === "object") {
77
- overrides = shallowCopy(args.pop());
78
- }
79
- // Make sure the parameter count matches
80
- logger.checkArgumentCount(args.length, fragment.inputs.length, "passed to contract");
81
- // Populate "from" override (allow promises)
82
- if (contract.signer) {
83
- if (overrides.from) {
84
- // Contracts with a Signer are from the Signer's frame-of-reference;
85
- // but we allow overriding "from" if it matches the signer
86
- overrides.from = resolveProperties({
87
- override: resolveName(contract.signer, overrides.from),
88
- signer: contract.signer.getAddress()
89
- }).then((check) => __awaiter(this, void 0, void 0, function* () {
90
- if (getAddress(check.signer) !== check.override) {
91
- logger.throwError("Contract with a Signer cannot override from", Logger.errors.UNSUPPORTED_OPERATION, {
92
- operation: "overrides.from"
93
- });
94
- }
95
- return check.override;
96
- }));
97
- }
98
- else {
99
- overrides.from = contract.signer.getAddress();
100
- }
101
- }
102
- else if (overrides.from) {
103
- overrides.from = resolveName(contract.provider, overrides.from);
104
- //} else {
105
- // Contracts without a signer can override "from", and if
106
- // unspecified the zero address is used
107
- //overrides.from = AddressZero;
108
- }
109
- // Wait for all dependencies to be resolved (prefer the signer over the provider)
110
- const resolved = yield resolveProperties({
111
- args: resolveAddresses(contract.signer || contract.provider, args, fragment.inputs),
112
- address: contract.resolvedAddress,
113
- overrides: (resolveProperties(overrides) || {})
114
- });
115
- // The ABI coded transaction
116
- const data = contract.interface.encodeFunctionData(fragment, resolved.args);
117
- const tx = {
118
- data: data,
119
- to: resolved.address
120
- };
121
- // Resolved Overrides
122
- const ro = resolved.overrides;
123
- // Populate simple overrides
124
- if (ro.nonce != null) {
125
- tx.nonce = BigNumber.from(ro.nonce).toNumber();
126
- }
127
- if (ro.gasLimit != null) {
128
- tx.gasLimit = BigNumber.from(ro.gasLimit);
129
- }
130
- if (ro.gasPrice != null) {
131
- tx.gasPrice = BigNumber.from(ro.gasPrice);
132
- }
133
- if (ro.maxFeePerGas != null) {
134
- tx.maxFeePerGas = BigNumber.from(ro.maxFeePerGas);
135
- }
136
- if (ro.maxPriorityFeePerGas != null) {
137
- tx.maxPriorityFeePerGas = BigNumber.from(ro.maxPriorityFeePerGas);
138
- }
139
- if (ro.from != null) {
140
- tx.from = ro.from;
141
- }
142
- if (ro.type != null) {
143
- tx.type = ro.type;
144
- }
145
- if (ro.accessList != null) {
146
- tx.accessList = accessListify(ro.accessList);
147
- }
148
- // If there was no "gasLimit" override, but the ABI specifies a default, use it
149
- if (tx.gasLimit == null && fragment.gas != null) {
150
- // Compute the intrinsic gas cost for this transaction
151
- // @TODO: This is based on the yellow paper as of Petersburg; this is something
152
- // we may wish to parameterize in v6 as part of the Network object. Since this
153
- // is always a non-nil to address, we can ignore G_create, but may wish to add
154
- // similar logic to the ContractFactory.
155
- let intrinsic = 21000;
156
- const bytes = arrayify(data);
157
- for (let i = 0; i < bytes.length; i++) {
158
- intrinsic += 4;
159
- if (bytes[i]) {
160
- intrinsic += 64;
161
- }
162
- }
163
- tx.gasLimit = BigNumber.from(fragment.gas).add(intrinsic);
164
- }
165
- // Populate "value" override
166
- if (ro.value) {
167
- const roValue = BigNumber.from(ro.value);
168
- if (!roValue.isZero() && !fragment.payable) {
169
- logger.throwError("non-payable method cannot override value", Logger.errors.UNSUPPORTED_OPERATION, {
170
- operation: "overrides.value",
171
- value: overrides.value
172
- });
173
- }
174
- tx.value = roValue;
175
- }
176
- if (ro.customData) {
177
- tx.customData = shallowCopy(ro.customData);
178
- }
179
- if (ro.ccipReadEnabled) {
180
- tx.ccipReadEnabled = !!ro.ccipReadEnabled;
181
- }
182
- // Remove the overrides
183
- delete overrides.nonce;
184
- delete overrides.gasLimit;
185
- delete overrides.gasPrice;
186
- delete overrides.from;
187
- delete overrides.value;
188
- delete overrides.type;
189
- delete overrides.accessList;
190
- delete overrides.maxFeePerGas;
191
- delete overrides.maxPriorityFeePerGas;
192
- delete overrides.customData;
193
- delete overrides.ccipReadEnabled;
194
- // Make sure there are no stray overrides, which may indicate a
195
- // typo or using an unsupported key.
196
- const leftovers = Object.keys(overrides).filter((key) => (overrides[key] != null));
197
- if (leftovers.length) {
198
- logger.throwError(`cannot override ${leftovers.map((l) => JSON.stringify(l)).join(",")}`, Logger.errors.UNSUPPORTED_OPERATION, {
199
- operation: "overrides",
200
- overrides: leftovers
201
- });
202
- }
203
- return tx;
204
- });
205
- }
206
- function buildPopulate(contract, fragment) {
207
- return function (...args) {
208
- return populateTransaction(contract, fragment, args);
209
- };
210
- }
211
- function buildEstimate(contract, fragment) {
212
- const signerOrProvider = (contract.signer || contract.provider);
213
- return function (...args) {
214
- return __awaiter(this, void 0, void 0, function* () {
215
- if (!signerOrProvider) {
216
- logger.throwError("estimate require a provider or signer", Logger.errors.UNSUPPORTED_OPERATION, {
217
- operation: "estimateGas"
218
- });
219
- }
220
- const tx = yield populateTransaction(contract, fragment, args);
221
- return yield signerOrProvider.estimateGas(tx);
222
- });
223
- };
224
- }
225
- function addContractWait(contract, tx) {
226
- const wait = tx.wait.bind(tx);
227
- tx.wait = (confirmations) => {
228
- return wait(confirmations).then((receipt) => {
229
- receipt.events = receipt.logs.map((log) => {
230
- let event = deepCopy(log);
231
- let parsed = null;
232
- try {
233
- parsed = contract.interface.parseLog(log);
234
- }
235
- catch (e) { }
236
- // Successfully parsed the event log; include it
237
- if (parsed) {
238
- event.args = parsed.args;
239
- event.decode = (data, topics) => {
240
- return contract.interface.decodeEventLog(parsed.eventFragment, data, topics);
241
- };
242
- event.event = parsed.name;
243
- event.eventSignature = parsed.signature;
244
- }
245
- // Useful operations
246
- event.removeListener = () => { return contract.provider; };
247
- event.getBlock = () => {
248
- return contract.provider.getBlock(receipt.blockHash);
249
- };
250
- event.getTransaction = () => {
251
- return contract.provider.getTransaction(receipt.transactionHash);
252
- };
253
- event.getTransactionReceipt = () => {
254
- return Promise.resolve(receipt);
255
- };
256
- return event;
257
- });
258
- return receipt;
259
- });
260
- };
261
- }
262
- function buildCall(contract, fragment, collapseSimple) {
263
- const signerOrProvider = (contract.signer || contract.provider);
264
- return function (...args) {
265
- return __awaiter(this, void 0, void 0, function* () {
266
- // Extract the "blockTag" override if present
267
- let blockTag = undefined;
268
- if (args.length === fragment.inputs.length + 1 && typeof (args[args.length - 1]) === "object") {
269
- const overrides = shallowCopy(args.pop());
270
- if (overrides.blockTag != null) {
271
- blockTag = yield overrides.blockTag;
272
- }
273
- delete overrides.blockTag;
274
- args.push(overrides);
275
- }
276
- // If the contract was just deployed, wait until it is mined
277
- if (contract.deployTransaction != null) {
278
- yield contract._deployed(blockTag);
279
- }
280
- // Call a node and get the result
281
- const tx = yield populateTransaction(contract, fragment, args);
282
- const result = yield signerOrProvider.call(tx, blockTag);
283
- try {
284
- let value = contract.interface.decodeFunctionResult(fragment, result);
285
- if (collapseSimple && fragment.outputs.length === 1) {
286
- value = value[0];
287
- }
288
- return value;
289
- }
290
- catch (error) {
291
- if (error.code === Logger.errors.CALL_EXCEPTION) {
292
- error.address = contract.address;
293
- error.args = args;
294
- error.transaction = tx;
295
- }
296
- throw error;
297
- }
298
- });
299
- };
300
- }
301
- function buildSend(contract, fragment) {
302
- return function (...args) {
303
- return __awaiter(this, void 0, void 0, function* () {
304
- if (!contract.signer) {
305
- logger.throwError("sending a transaction requires a signer", Logger.errors.UNSUPPORTED_OPERATION, {
306
- operation: "sendTransaction"
307
- });
308
- }
309
- // If the contract was just deployed, wait until it is mined
310
- if (contract.deployTransaction != null) {
311
- yield contract._deployed();
312
- }
313
- const txRequest = yield populateTransaction(contract, fragment, args);
314
- const tx = yield contract.signer.sendTransaction(txRequest);
315
- // Tweak the tx.wait so the receipt has extra properties
316
- addContractWait(contract, tx);
317
- return tx;
318
- });
319
- };
320
- }
321
- function buildDefault(contract, fragment, collapseSimple) {
322
- if (fragment.constant) {
323
- return buildCall(contract, fragment, collapseSimple);
324
- }
325
- return buildSend(contract, fragment);
326
- }
327
- function getEventTag(filter) {
328
- if (filter.address && (filter.topics == null || filter.topics.length === 0)) {
329
- return "*";
330
- }
331
- return (filter.address || "*") + "@" + (filter.topics ? filter.topics.map((topic) => {
332
- if (Array.isArray(topic)) {
333
- return topic.join("|");
334
- }
335
- return topic;
336
- }).join(":") : "");
337
- }
338
- class RunningEvent {
339
- constructor(tag, filter) {
340
- defineReadOnly(this, "tag", tag);
341
- defineReadOnly(this, "filter", filter);
342
- this._listeners = [];
343
- }
344
- addListener(listener, once) {
345
- this._listeners.push({ listener: listener, once: once });
346
- }
347
- removeListener(listener) {
348
- let done = false;
349
- this._listeners = this._listeners.filter((item) => {
350
- if (done || item.listener !== listener) {
351
- return true;
352
- }
353
- done = true;
354
- return false;
355
- });
356
- }
357
- removeAllListeners() {
358
- this._listeners = [];
359
- }
360
- listeners() {
361
- return this._listeners.map((i) => i.listener);
362
- }
363
- listenerCount() {
364
- return this._listeners.length;
365
- }
366
- run(args) {
367
- const listenerCount = this.listenerCount();
368
- this._listeners = this._listeners.filter((item) => {
369
- const argsCopy = args.slice();
370
- // Call the callback in the next event loop
371
- setTimeout(() => {
372
- item.listener.apply(this, argsCopy);
373
- }, 0);
374
- // Reschedule it if it not "once"
375
- return !(item.once);
376
- });
377
- return listenerCount;
378
- }
379
- prepareEvent(event) {
380
- }
381
- // Returns the array that will be applied to an emit
382
- getEmit(event) {
383
- return [event];
384
- }
385
- }
386
- class ErrorRunningEvent extends RunningEvent {
387
- constructor() {
388
- super("error", null);
389
- }
390
- }
391
- // @TODO Fragment should inherit Wildcard? and just override getEmit?
392
- // or have a common abstract super class, with enough constructor
393
- // options to configure both.
394
- // A Fragment Event will populate all the properties that Wildcard
395
- // will, and additionally dereference the arguments when emitting
396
- class FragmentRunningEvent extends RunningEvent {
397
- constructor(address, contractInterface, fragment, topics) {
398
- const filter = {
399
- address: address
400
- };
401
- let topic = contractInterface.getEventTopic(fragment);
402
- if (topics) {
403
- if (topic !== topics[0]) {
404
- logger.throwArgumentError("topic mismatch", "topics", topics);
405
- }
406
- filter.topics = topics.slice();
407
- }
408
- else {
409
- filter.topics = [topic];
410
- }
411
- super(getEventTag(filter), filter);
412
- defineReadOnly(this, "address", address);
413
- defineReadOnly(this, "interface", contractInterface);
414
- defineReadOnly(this, "fragment", fragment);
415
- }
416
- prepareEvent(event) {
417
- super.prepareEvent(event);
418
- event.event = this.fragment.name;
419
- event.eventSignature = this.fragment.format();
420
- event.decode = (data, topics) => {
421
- return this.interface.decodeEventLog(this.fragment, data, topics);
422
- };
423
- try {
424
- event.args = this.interface.decodeEventLog(this.fragment, event.data, event.topics);
425
- }
426
- catch (error) {
427
- event.args = null;
428
- event.decodeError = error;
429
- }
430
- }
431
- getEmit(event) {
432
- const errors = checkResultErrors(event.args);
433
- if (errors.length) {
434
- throw errors[0].error;
435
- }
436
- const args = (event.args || []).slice();
437
- args.push(event);
438
- return args;
439
- }
440
- }
441
- // A Wildcard Event will attempt to populate:
442
- // - event The name of the event name
443
- // - eventSignature The full signature of the event
444
- // - decode A function to decode data and topics
445
- // - args The decoded data and topics
446
- class WildcardRunningEvent extends RunningEvent {
447
- constructor(address, contractInterface) {
448
- super("*", { address: address });
449
- defineReadOnly(this, "address", address);
450
- defineReadOnly(this, "interface", contractInterface);
451
- }
452
- prepareEvent(event) {
453
- super.prepareEvent(event);
454
- try {
455
- const parsed = this.interface.parseLog(event);
456
- event.event = parsed.name;
457
- event.eventSignature = parsed.signature;
458
- event.decode = (data, topics) => {
459
- return this.interface.decodeEventLog(parsed.eventFragment, data, topics);
460
- };
461
- event.args = parsed.args;
462
- }
463
- catch (error) {
464
- // No matching event
465
- }
466
- }
467
- }
468
- class BaseContract {
469
- constructor(addressOrName, contractInterface, signerOrProvider) {
470
- // @TODO: Maybe still check the addressOrName looks like a valid address or name?
471
- //address = getAddress(address);
472
- defineReadOnly(this, "interface", getStatic(new.target, "getInterface")(contractInterface));
473
- if (signerOrProvider == null) {
474
- defineReadOnly(this, "provider", null);
475
- defineReadOnly(this, "signer", null);
476
- }
477
- else if (Signer.isSigner(signerOrProvider)) {
478
- defineReadOnly(this, "provider", signerOrProvider.provider || null);
479
- defineReadOnly(this, "signer", signerOrProvider);
480
- }
481
- else if (Provider.isProvider(signerOrProvider)) {
482
- defineReadOnly(this, "provider", signerOrProvider);
483
- defineReadOnly(this, "signer", null);
484
- }
485
- else {
486
- logger.throwArgumentError("invalid signer or provider", "signerOrProvider", signerOrProvider);
487
- }
488
- defineReadOnly(this, "callStatic", {});
489
- defineReadOnly(this, "estimateGas", {});
490
- defineReadOnly(this, "functions", {});
491
- defineReadOnly(this, "populateTransaction", {});
492
- defineReadOnly(this, "filters", {});
493
- {
494
- const uniqueFilters = {};
495
- Object.keys(this.interface.events).forEach((eventSignature) => {
496
- const event = this.interface.events[eventSignature];
497
- defineReadOnly(this.filters, eventSignature, (...args) => {
498
- return {
499
- address: this.address,
500
- topics: this.interface.encodeFilterTopics(event, args)
501
- };
502
- });
503
- if (!uniqueFilters[event.name]) {
504
- uniqueFilters[event.name] = [];
505
- }
506
- uniqueFilters[event.name].push(eventSignature);
507
- });
508
- Object.keys(uniqueFilters).forEach((name) => {
509
- const filters = uniqueFilters[name];
510
- if (filters.length === 1) {
511
- defineReadOnly(this.filters, name, this.filters[filters[0]]);
512
- }
513
- else {
514
- logger.warn(`Duplicate definition of ${name} (${filters.join(", ")})`);
515
- }
516
- });
517
- }
518
- defineReadOnly(this, "_runningEvents", {});
519
- defineReadOnly(this, "_wrappedEmits", {});
520
- if (addressOrName == null) {
521
- logger.throwArgumentError("invalid contract address or ENS name", "addressOrName", addressOrName);
522
- }
523
- defineReadOnly(this, "address", addressOrName);
524
- if (this.provider) {
525
- defineReadOnly(this, "resolvedAddress", resolveName(this.provider, addressOrName));
526
- }
527
- else {
528
- try {
529
- defineReadOnly(this, "resolvedAddress", Promise.resolve(getAddress(addressOrName)));
530
- }
531
- catch (error) {
532
- // Without a provider, we cannot use ENS names
533
- logger.throwError("provider is required to use ENS name as contract address", Logger.errors.UNSUPPORTED_OPERATION, {
534
- operation: "new Contract"
535
- });
536
- }
537
- }
538
- // Swallow bad ENS names to prevent Unhandled Exceptions
539
- this.resolvedAddress.catch((e) => { });
540
- const uniqueNames = {};
541
- const uniqueSignatures = {};
542
- Object.keys(this.interface.functions).forEach((signature) => {
543
- const fragment = this.interface.functions[signature];
544
- // Check that the signature is unique; if not the ABI generation has
545
- // not been cleaned or may be incorrectly generated
546
- if (uniqueSignatures[signature]) {
547
- logger.warn(`Duplicate ABI entry for ${JSON.stringify(signature)}`);
548
- return;
549
- }
550
- uniqueSignatures[signature] = true;
551
- // Track unique names; we only expose bare named functions if they
552
- // are ambiguous
553
- {
554
- const name = fragment.name;
555
- if (!uniqueNames[`%${name}`]) {
556
- uniqueNames[`%${name}`] = [];
557
- }
558
- uniqueNames[`%${name}`].push(signature);
559
- }
560
- if (this[signature] == null) {
561
- defineReadOnly(this, signature, buildDefault(this, fragment, true));
562
- }
563
- // We do not collapse simple calls on this bucket, which allows
564
- // frameworks to safely use this without introspection as well as
565
- // allows decoding error recovery.
566
- if (this.functions[signature] == null) {
567
- defineReadOnly(this.functions, signature, buildDefault(this, fragment, false));
568
- }
569
- if (this.callStatic[signature] == null) {
570
- defineReadOnly(this.callStatic, signature, buildCall(this, fragment, true));
571
- }
572
- if (this.populateTransaction[signature] == null) {
573
- defineReadOnly(this.populateTransaction, signature, buildPopulate(this, fragment));
574
- }
575
- if (this.estimateGas[signature] == null) {
576
- defineReadOnly(this.estimateGas, signature, buildEstimate(this, fragment));
577
- }
578
- });
579
- Object.keys(uniqueNames).forEach((name) => {
580
- // Ambiguous names to not get attached as bare names
581
- const signatures = uniqueNames[name];
582
- if (signatures.length > 1) {
583
- return;
584
- }
585
- // Strip off the leading "%" used for prototype protection
586
- name = name.substring(1);
587
- const signature = signatures[0];
588
- // If overwriting a member property that is null, swallow the error
589
- try {
590
- if (this[name] == null) {
591
- defineReadOnly(this, name, this[signature]);
592
- }
593
- }
594
- catch (e) { }
595
- if (this.functions[name] == null) {
596
- defineReadOnly(this.functions, name, this.functions[signature]);
597
- }
598
- if (this.callStatic[name] == null) {
599
- defineReadOnly(this.callStatic, name, this.callStatic[signature]);
600
- }
601
- if (this.populateTransaction[name] == null) {
602
- defineReadOnly(this.populateTransaction, name, this.populateTransaction[signature]);
603
- }
604
- if (this.estimateGas[name] == null) {
605
- defineReadOnly(this.estimateGas, name, this.estimateGas[signature]);
606
- }
607
- });
608
- }
609
- static getContractAddress(transaction) {
610
- return getContractAddress(transaction);
611
- }
612
- static getInterface(contractInterface) {
613
- if (Interface.isInterface(contractInterface)) {
614
- return contractInterface;
615
- }
616
- return new Interface(contractInterface);
617
- }
618
- // @TODO: Allow timeout?
619
- deployed() {
620
- return this._deployed();
621
- }
622
- _deployed(blockTag) {
623
- if (!this._deployedPromise) {
624
- // If we were just deployed, we know the transaction we should occur in
625
- if (this.deployTransaction) {
626
- this._deployedPromise = this.deployTransaction.wait().then(() => {
627
- return this;
628
- });
629
- }
630
- else {
631
- // @TODO: Once we allow a timeout to be passed in, we will wait
632
- // up to that many blocks for getCode
633
- // Otherwise, poll for our code to be deployed
634
- this._deployedPromise = this.provider.getCode(this.address, blockTag).then((code) => {
635
- if (code === "0x") {
636
- logger.throwError("contract not deployed", Logger.errors.UNSUPPORTED_OPERATION, {
637
- contractAddress: this.address,
638
- operation: "getDeployed"
639
- });
640
- }
641
- return this;
642
- });
643
- }
644
- }
645
- return this._deployedPromise;
646
- }
647
- // @TODO:
648
- // estimateFallback(overrides?: TransactionRequest): Promise<BigNumber>
649
- // @TODO:
650
- // estimateDeploy(bytecode: string, ...args): Promise<BigNumber>
651
- fallback(overrides) {
652
- if (!this.signer) {
653
- logger.throwError("sending a transactions require a signer", Logger.errors.UNSUPPORTED_OPERATION, { operation: "sendTransaction(fallback)" });
654
- }
655
- const tx = shallowCopy(overrides || {});
656
- ["from", "to"].forEach(function (key) {
657
- if (tx[key] == null) {
658
- return;
659
- }
660
- logger.throwError("cannot override " + key, Logger.errors.UNSUPPORTED_OPERATION, { operation: key });
661
- });
662
- tx.to = this.resolvedAddress;
663
- return this.deployed().then(() => {
664
- return this.signer.sendTransaction(tx);
665
- });
666
- }
667
- // Reconnect to a different signer or provider
668
- connect(signerOrProvider) {
669
- if (typeof (signerOrProvider) === "string") {
670
- signerOrProvider = new VoidSigner(signerOrProvider, this.provider);
671
- }
672
- const contract = new (this.constructor)(this.address, this.interface, signerOrProvider);
673
- if (this.deployTransaction) {
674
- defineReadOnly(contract, "deployTransaction", this.deployTransaction);
675
- }
676
- return contract;
677
- }
678
- // Re-attach to a different on-chain instance of this contract
679
- attach(addressOrName) {
680
- return new (this.constructor)(addressOrName, this.interface, this.signer || this.provider);
681
- }
682
- static isIndexed(value) {
683
- return Indexed.isIndexed(value);
684
- }
685
- _normalizeRunningEvent(runningEvent) {
686
- // Already have an instance of this event running; we can re-use it
687
- if (this._runningEvents[runningEvent.tag]) {
688
- return this._runningEvents[runningEvent.tag];
689
- }
690
- return runningEvent;
691
- }
692
- _getRunningEvent(eventName) {
693
- if (typeof (eventName) === "string") {
694
- // Listen for "error" events (if your contract has an error event, include
695
- // the full signature to bypass this special event keyword)
696
- if (eventName === "error") {
697
- return this._normalizeRunningEvent(new ErrorRunningEvent());
698
- }
699
- // Listen for any event that is registered
700
- if (eventName === "event") {
701
- return this._normalizeRunningEvent(new RunningEvent("event", null));
702
- }
703
- // Listen for any event
704
- if (eventName === "*") {
705
- return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface));
706
- }
707
- // Get the event Fragment (throws if ambiguous/unknown event)
708
- const fragment = this.interface.getEvent(eventName);
709
- return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment));
710
- }
711
- // We have topics to filter by...
712
- if (eventName.topics && eventName.topics.length > 0) {
713
- // Is it a known topichash? (throws if no matching topichash)
714
- try {
715
- const topic = eventName.topics[0];
716
- if (typeof (topic) !== "string") {
717
- throw new Error("invalid topic"); // @TODO: May happen for anonymous events
718
- }
719
- const fragment = this.interface.getEvent(topic);
720
- return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment, eventName.topics));
721
- }
722
- catch (error) { }
723
- // Filter by the unknown topichash
724
- const filter = {
725
- address: this.address,
726
- topics: eventName.topics
727
- };
728
- return this._normalizeRunningEvent(new RunningEvent(getEventTag(filter), filter));
729
- }
730
- return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface));
731
- }
732
- _checkRunningEvents(runningEvent) {
733
- if (runningEvent.listenerCount() === 0) {
734
- delete this._runningEvents[runningEvent.tag];
735
- // If we have a poller for this, remove it
736
- const emit = this._wrappedEmits[runningEvent.tag];
737
- if (emit && runningEvent.filter) {
738
- this.provider.off(runningEvent.filter, emit);
739
- delete this._wrappedEmits[runningEvent.tag];
740
- }
741
- }
742
- }
743
- // Subclasses can override this to gracefully recover
744
- // from parse errors if they wish
745
- _wrapEvent(runningEvent, log, listener) {
746
- const event = deepCopy(log);
747
- event.removeListener = () => {
748
- if (!listener) {
749
- return;
750
- }
751
- runningEvent.removeListener(listener);
752
- this._checkRunningEvents(runningEvent);
753
- };
754
- event.getBlock = () => { return this.provider.getBlock(log.blockHash); };
755
- event.getTransaction = () => { return this.provider.getTransaction(log.transactionHash); };
756
- event.getTransactionReceipt = () => { return this.provider.getTransactionReceipt(log.transactionHash); };
757
- // This may throw if the topics and data mismatch the signature
758
- runningEvent.prepareEvent(event);
759
- return event;
760
- }
761
- _addEventListener(runningEvent, listener, once) {
762
- if (!this.provider) {
763
- logger.throwError("events require a provider or a signer with a provider", Logger.errors.UNSUPPORTED_OPERATION, { operation: "once" });
764
- }
765
- runningEvent.addListener(listener, once);
766
- // Track this running event and its listeners (may already be there; but no hard in updating)
767
- this._runningEvents[runningEvent.tag] = runningEvent;
768
- // If we are not polling the provider, start polling
769
- if (!this._wrappedEmits[runningEvent.tag]) {
770
- const wrappedEmit = (log) => {
771
- let event = this._wrapEvent(runningEvent, log, listener);
772
- // Try to emit the result for the parameterized event...
773
- if (event.decodeError == null) {
774
- try {
775
- const args = runningEvent.getEmit(event);
776
- this.emit(runningEvent.filter, ...args);
777
- }
778
- catch (error) {
779
- event.decodeError = error.error;
780
- }
781
- }
782
- // Always emit "event" for fragment-base events
783
- if (runningEvent.filter != null) {
784
- this.emit("event", event);
785
- }
786
- // Emit "error" if there was an error
787
- if (event.decodeError != null) {
788
- this.emit("error", event.decodeError, event);
789
- }
790
- };
791
- this._wrappedEmits[runningEvent.tag] = wrappedEmit;
792
- // Special events, like "error" do not have a filter
793
- if (runningEvent.filter != null) {
794
- this.provider.on(runningEvent.filter, wrappedEmit);
795
- }
796
- }
797
- }
798
- queryFilter(event, fromBlockOrBlockhash, toBlock) {
799
- const runningEvent = this._getRunningEvent(event);
800
- const filter = shallowCopy(runningEvent.filter);
801
- if (typeof (fromBlockOrBlockhash) === "string" && isHexString(fromBlockOrBlockhash, 32)) {
802
- if (toBlock != null) {
803
- logger.throwArgumentError("cannot specify toBlock with blockhash", "toBlock", toBlock);
804
- }
805
- filter.blockHash = fromBlockOrBlockhash;
806
- }
807
- else {
808
- filter.fromBlock = ((fromBlockOrBlockhash != null) ? fromBlockOrBlockhash : 0);
809
- filter.toBlock = ((toBlock != null) ? toBlock : "latest");
810
- }
811
- return this.provider.getLogs(filter).then((logs) => {
812
- return logs.map((log) => this._wrapEvent(runningEvent, log, null));
813
- });
814
- }
815
- on(event, listener) {
816
- this._addEventListener(this._getRunningEvent(event), listener, false);
817
- return this;
818
- }
819
- once(event, listener) {
820
- this._addEventListener(this._getRunningEvent(event), listener, true);
821
- return this;
822
- }
823
- emit(eventName, ...args) {
824
- if (!this.provider) {
825
- return false;
826
- }
827
- const runningEvent = this._getRunningEvent(eventName);
828
- const result = (runningEvent.run(args) > 0);
829
- // May have drained all the "once" events; check for living events
830
- this._checkRunningEvents(runningEvent);
831
- return result;
832
- }
833
- listenerCount(eventName) {
834
- if (!this.provider) {
835
- return 0;
836
- }
837
- if (eventName == null) {
838
- return Object.keys(this._runningEvents).reduce((accum, key) => {
839
- return accum + this._runningEvents[key].listenerCount();
840
- }, 0);
841
- }
842
- return this._getRunningEvent(eventName).listenerCount();
843
- }
844
- listeners(eventName) {
845
- if (!this.provider) {
846
- return [];
847
- }
848
- if (eventName == null) {
849
- const result = [];
850
- for (let tag in this._runningEvents) {
851
- this._runningEvents[tag].listeners().forEach((listener) => {
852
- result.push(listener);
853
- });
854
- }
855
- return result;
856
- }
857
- return this._getRunningEvent(eventName).listeners();
858
- }
859
- removeAllListeners(eventName) {
860
- if (!this.provider) {
861
- return this;
862
- }
863
- if (eventName == null) {
864
- for (const tag in this._runningEvents) {
865
- const runningEvent = this._runningEvents[tag];
866
- runningEvent.removeAllListeners();
867
- this._checkRunningEvents(runningEvent);
868
- }
869
- return this;
870
- }
871
- // Delete any listeners
872
- const runningEvent = this._getRunningEvent(eventName);
873
- runningEvent.removeAllListeners();
874
- this._checkRunningEvents(runningEvent);
875
- return this;
876
- }
877
- off(eventName, listener) {
878
- if (!this.provider) {
879
- return this;
880
- }
881
- const runningEvent = this._getRunningEvent(eventName);
882
- runningEvent.removeListener(listener);
883
- this._checkRunningEvents(runningEvent);
884
- return this;
885
- }
886
- removeListener(eventName, listener) {
887
- return this.off(eventName, listener);
888
- }
889
- }
890
- class Contract extends BaseContract {
891
- }
892
-
893
- export { BaseContract, Contract };