@alephium/web3 2.0.7 → 2.0.10
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.
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/contract/contract.js +33 -4
- package/dist/src/contract/events.d.ts +2 -0
- package/dist/src/contract/events.js +11 -2
- package/dist/src/utils/webcrypto.js +2 -1
- package/package.json +1 -1
- package/src/contract/contract.ts +44 -5
- package/src/contract/events.ts +12 -2
- package/src/utils/webcrypto.ts +2 -1
|
@@ -823,7 +823,8 @@ function subscribeEventsFromContract(options, address, eventIndex, decodeFunc, f
|
|
|
823
823
|
pollingInterval: options.pollingInterval,
|
|
824
824
|
messageCallback: messageCallback,
|
|
825
825
|
errorCallback: errorCallback,
|
|
826
|
-
onEventCountChanged: options.onEventCountChanged
|
|
826
|
+
onEventCountChanged: options.onEventCountChanged,
|
|
827
|
+
parallel: options.parallel
|
|
827
828
|
};
|
|
828
829
|
return (0, events_1.subscribeToEvents)(opt, address, fromCount);
|
|
829
830
|
}
|
|
@@ -1242,7 +1243,8 @@ function subscribeContractEvents(contract, instance, options, fromCount) {
|
|
|
1242
1243
|
pollingInterval: options.pollingInterval,
|
|
1243
1244
|
messageCallback: messageCallback,
|
|
1244
1245
|
errorCallback: errorCallback,
|
|
1245
|
-
onEventCountChanged: options.onEventCountChanged
|
|
1246
|
+
onEventCountChanged: options.onEventCountChanged,
|
|
1247
|
+
parallel: options.parallel
|
|
1246
1248
|
};
|
|
1247
1249
|
return (0, events_1.subscribeToEvents)(opt, instance.address, fromCount);
|
|
1248
1250
|
}
|
|
@@ -1250,13 +1252,39 @@ exports.subscribeContractEvents = subscribeContractEvents;
|
|
|
1250
1252
|
async function callMethod(contract, instance, methodName, params, getContractByCodeHash) {
|
|
1251
1253
|
const methodIndex = contract.contract.getMethodIndex(methodName);
|
|
1252
1254
|
const txId = params?.txId ?? randomTxId();
|
|
1253
|
-
const
|
|
1255
|
+
const callArgs = getCallMethodArgs(params.args ?? {}, instance.groupIndex);
|
|
1256
|
+
const callParams = contract.contract.toApiCallContract({ ...params, txId: txId, args: callArgs }, instance.groupIndex, instance.address, methodIndex);
|
|
1254
1257
|
const result = await (0, global_1.getCurrentNodeProvider)().contracts.postContractsCallContract(callParams);
|
|
1255
1258
|
const callResult = contract.contract.fromApiCallContractResult(result, txId, methodIndex, getContractByCodeHash);
|
|
1256
1259
|
contract.contract.printDebugMessages(methodName, callResult.debugMessages);
|
|
1257
1260
|
return callResult;
|
|
1258
1261
|
}
|
|
1259
1262
|
exports.callMethod = callMethod;
|
|
1263
|
+
function tryGetCallMethodAddressArg(maybeAddress, groupIndex) {
|
|
1264
|
+
if (!(0, address_1.isValidAddress)(maybeAddress))
|
|
1265
|
+
return maybeAddress;
|
|
1266
|
+
if ((0, address_1.isGrouplessAddress)(maybeAddress)) {
|
|
1267
|
+
const rawAddress = (0, address_1.addressWithoutExplicitGroupIndex)(maybeAddress);
|
|
1268
|
+
return `${rawAddress}:${groupIndex}`;
|
|
1269
|
+
}
|
|
1270
|
+
return maybeAddress;
|
|
1271
|
+
}
|
|
1272
|
+
function getCallMethodArgsFromVal(val, groupIndex) {
|
|
1273
|
+
if (typeof val === 'string') {
|
|
1274
|
+
return tryGetCallMethodAddressArg(val, groupIndex);
|
|
1275
|
+
}
|
|
1276
|
+
if (Array.isArray(val)) {
|
|
1277
|
+
return val.map((v) => getCallMethodArgsFromVal(v, groupIndex));
|
|
1278
|
+
}
|
|
1279
|
+
return val;
|
|
1280
|
+
}
|
|
1281
|
+
function getCallMethodArgs(args, groupIndex) {
|
|
1282
|
+
const newArgs = {};
|
|
1283
|
+
for (const [key, value] of Object.entries(args)) {
|
|
1284
|
+
newArgs[`${key}`] = getCallMethodArgsFromVal(value, groupIndex);
|
|
1285
|
+
}
|
|
1286
|
+
return newArgs;
|
|
1287
|
+
}
|
|
1260
1288
|
async function signExecuteMethod(contract, instance, methodName, params) {
|
|
1261
1289
|
const methodIndex = contract.contract.getMethodIndex(methodName);
|
|
1262
1290
|
const functionSig = contract.contract.functions[methodIndex];
|
|
@@ -1449,7 +1477,8 @@ async function multicallMethods(contract, instance, _callss, getContractByCodeHa
|
|
|
1449
1477
|
const [methodName, params] = entry;
|
|
1450
1478
|
const methodIndex = contract.contract.getMethodIndex(methodName);
|
|
1451
1479
|
const txId = params?.txId ?? randomTxId();
|
|
1452
|
-
|
|
1480
|
+
const callArgs = getCallMethodArgs(params.args ?? {}, instance.groupIndex);
|
|
1481
|
+
return contract.contract.toApiCallContract({ ...params, txId: txId, args: callArgs }, instance.groupIndex, instance.address, methodIndex);
|
|
1453
1482
|
});
|
|
1454
1483
|
});
|
|
1455
1484
|
const result = await (0, global_1.getCurrentNodeProvider)().contracts.postContractsMulticallContract({ calls: callsParams.flat() });
|
|
@@ -2,11 +2,13 @@ import { node } from '../api';
|
|
|
2
2
|
import { Subscription, SubscribeOptions } from '../utils';
|
|
3
3
|
export interface EventSubscribeOptions<Message> extends SubscribeOptions<Message> {
|
|
4
4
|
onEventCountChanged?: (eventCount: number) => Promise<void> | void;
|
|
5
|
+
parallel?: boolean;
|
|
5
6
|
}
|
|
6
7
|
export declare class EventSubscription extends Subscription<node.ContractEvent> {
|
|
7
8
|
readonly contractAddress: string;
|
|
8
9
|
private fromCount;
|
|
9
10
|
private onEventCountChanged?;
|
|
11
|
+
private parallel;
|
|
10
12
|
constructor(options: EventSubscribeOptions<node.ContractEvent>, contractAddress: string, fromCount?: number);
|
|
11
13
|
currentEventCount(): number;
|
|
12
14
|
private getEvents;
|
|
@@ -46,9 +46,11 @@ const utils_1 = require("../utils");
|
|
|
46
46
|
class EventSubscription extends utils_1.Subscription {
|
|
47
47
|
constructor(options, contractAddress, fromCount) {
|
|
48
48
|
super(options);
|
|
49
|
+
this.parallel = false;
|
|
49
50
|
this.contractAddress = contractAddress;
|
|
50
51
|
this.fromCount = typeof fromCount === 'undefined' ? 0 : fromCount;
|
|
51
52
|
this.onEventCountChanged = options.onEventCountChanged;
|
|
53
|
+
this.parallel = options.parallel ?? false;
|
|
52
54
|
}
|
|
53
55
|
currentEventCount() {
|
|
54
56
|
return this.fromCount;
|
|
@@ -72,8 +74,15 @@ class EventSubscription extends utils_1.Subscription {
|
|
|
72
74
|
if (this.fromCount === events.nextStart) {
|
|
73
75
|
return;
|
|
74
76
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
if (this.parallel) {
|
|
78
|
+
const promises = events.events.map((event) => this.messageCallback(event));
|
|
79
|
+
await Promise.all(promises);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
for (const event of events.events) {
|
|
83
|
+
await this.messageCallback(event);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
77
86
|
this.fromCount = events.nextStart;
|
|
78
87
|
if (this.onEventCountChanged !== undefined) {
|
|
79
88
|
await this.onEventCountChanged(this.fromCount);
|
|
@@ -29,7 +29,8 @@ class WebCrypto {
|
|
|
29
29
|
throw new TypeError("Failed to execute 'getRandomValues' on 'Crypto': parameter 1 is not of type 'ArrayBufferView'");
|
|
30
30
|
}
|
|
31
31
|
const bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
|
|
32
|
-
|
|
32
|
+
// Prefer global Web Crypto (e.g. react-native-get-random-values) for mobile compatibility
|
|
33
|
+
if (globalThis.crypto && typeof globalThis.crypto.getRandomValues === 'function') {
|
|
33
34
|
globalThis.crypto.getRandomValues(bytes);
|
|
34
35
|
}
|
|
35
36
|
else {
|
package/package.json
CHANGED
package/src/contract/contract.ts
CHANGED
|
@@ -59,7 +59,15 @@ import {
|
|
|
59
59
|
isHexString,
|
|
60
60
|
hexToString
|
|
61
61
|
} from '../utils'
|
|
62
|
-
import {
|
|
62
|
+
import {
|
|
63
|
+
contractIdFromAddress,
|
|
64
|
+
groupOfAddress,
|
|
65
|
+
addressFromContractId,
|
|
66
|
+
subContractId,
|
|
67
|
+
isGrouplessAddress,
|
|
68
|
+
isValidAddress,
|
|
69
|
+
addressWithoutExplicitGroupIndex
|
|
70
|
+
} from '../address'
|
|
63
71
|
import { getCurrentNodeProvider } from '../global'
|
|
64
72
|
import { EventSubscribeOptions, EventSubscription, subscribeToEvents } from './events'
|
|
65
73
|
import { MINIMAL_CONTRACT_DEPOSIT, ONE_ALPH, TOTAL_NUMBER_OF_GROUPS } from '../constants'
|
|
@@ -1334,7 +1342,8 @@ export function subscribeEventsFromContract<T extends Fields, M extends Contract
|
|
|
1334
1342
|
pollingInterval: options.pollingInterval,
|
|
1335
1343
|
messageCallback: messageCallback,
|
|
1336
1344
|
errorCallback: errorCallback,
|
|
1337
|
-
onEventCountChanged: options.onEventCountChanged
|
|
1345
|
+
onEventCountChanged: options.onEventCountChanged,
|
|
1346
|
+
parallel: options.parallel
|
|
1338
1347
|
}
|
|
1339
1348
|
return subscribeToEvents(opt, address, fromCount)
|
|
1340
1349
|
}
|
|
@@ -1907,7 +1916,8 @@ export function subscribeContractEvents(
|
|
|
1907
1916
|
pollingInterval: options.pollingInterval,
|
|
1908
1917
|
messageCallback: messageCallback,
|
|
1909
1918
|
errorCallback: errorCallback,
|
|
1910
|
-
onEventCountChanged: options.onEventCountChanged
|
|
1919
|
+
onEventCountChanged: options.onEventCountChanged,
|
|
1920
|
+
parallel: options.parallel
|
|
1911
1921
|
}
|
|
1912
1922
|
return subscribeToEvents(opt, instance.address, fromCount)
|
|
1913
1923
|
}
|
|
@@ -1921,8 +1931,9 @@ export async function callMethod<I extends ContractInstance, F extends Fields, A
|
|
|
1921
1931
|
): Promise<CallContractResult<R>> {
|
|
1922
1932
|
const methodIndex = contract.contract.getMethodIndex(methodName)
|
|
1923
1933
|
const txId = params?.txId ?? randomTxId()
|
|
1934
|
+
const callArgs = getCallMethodArgs(params.args ?? {}, instance.groupIndex)
|
|
1924
1935
|
const callParams = contract.contract.toApiCallContract(
|
|
1925
|
-
{ ...params, txId: txId, args:
|
|
1936
|
+
{ ...params, txId: txId, args: callArgs },
|
|
1926
1937
|
instance.groupIndex,
|
|
1927
1938
|
instance.address,
|
|
1928
1939
|
methodIndex
|
|
@@ -1933,6 +1944,33 @@ export async function callMethod<I extends ContractInstance, F extends Fields, A
|
|
|
1933
1944
|
return callResult as CallContractResult<R>
|
|
1934
1945
|
}
|
|
1935
1946
|
|
|
1947
|
+
function tryGetCallMethodAddressArg(maybeAddress: string, groupIndex: number): Val {
|
|
1948
|
+
if (!isValidAddress(maybeAddress)) return maybeAddress
|
|
1949
|
+
if (isGrouplessAddress(maybeAddress)) {
|
|
1950
|
+
const rawAddress = addressWithoutExplicitGroupIndex(maybeAddress)
|
|
1951
|
+
return `${rawAddress}:${groupIndex}`
|
|
1952
|
+
}
|
|
1953
|
+
return maybeAddress
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
function getCallMethodArgsFromVal(val: Val, groupIndex: number): Val {
|
|
1957
|
+
if (typeof val === 'string') {
|
|
1958
|
+
return tryGetCallMethodAddressArg(val, groupIndex)
|
|
1959
|
+
}
|
|
1960
|
+
if (Array.isArray(val)) {
|
|
1961
|
+
return val.map((v) => getCallMethodArgsFromVal(v, groupIndex))
|
|
1962
|
+
}
|
|
1963
|
+
return val
|
|
1964
|
+
}
|
|
1965
|
+
|
|
1966
|
+
function getCallMethodArgs<A extends Arguments>(args: A, groupIndex: number): A {
|
|
1967
|
+
const newArgs: Arguments = {}
|
|
1968
|
+
for (const [key, value] of Object.entries(args)) {
|
|
1969
|
+
newArgs[`${key}`] = getCallMethodArgsFromVal(value, groupIndex)
|
|
1970
|
+
}
|
|
1971
|
+
return newArgs as A
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1936
1974
|
export async function signExecuteMethod<I extends ContractInstance, F extends Fields, A extends Arguments, R>(
|
|
1937
1975
|
contract: ContractFactory<I, F>,
|
|
1938
1976
|
instance: ContractInstance,
|
|
@@ -2197,8 +2235,9 @@ export async function multicallMethods<I extends ContractInstance, F extends Fie
|
|
|
2197
2235
|
const [methodName, params] = entry
|
|
2198
2236
|
const methodIndex = contract.contract.getMethodIndex(methodName)
|
|
2199
2237
|
const txId = params?.txId ?? randomTxId()
|
|
2238
|
+
const callArgs = getCallMethodArgs(params.args ?? {}, instance.groupIndex)
|
|
2200
2239
|
return contract.contract.toApiCallContract(
|
|
2201
|
-
{ ...params, txId: txId, args:
|
|
2240
|
+
{ ...params, txId: txId, args: callArgs },
|
|
2202
2241
|
instance.groupIndex,
|
|
2203
2242
|
instance.address,
|
|
2204
2243
|
methodIndex
|
package/src/contract/events.ts
CHANGED
|
@@ -23,18 +23,21 @@ import { ContractEvents } from '../api/api-alephium'
|
|
|
23
23
|
|
|
24
24
|
export interface EventSubscribeOptions<Message> extends SubscribeOptions<Message> {
|
|
25
25
|
onEventCountChanged?: (eventCount: number) => Promise<void> | void
|
|
26
|
+
parallel?: boolean
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
export class EventSubscription extends Subscription<node.ContractEvent> {
|
|
29
30
|
readonly contractAddress: string
|
|
30
31
|
private fromCount: number
|
|
31
32
|
private onEventCountChanged?: (eventCount: number) => Promise<void> | void
|
|
33
|
+
private parallel = false
|
|
32
34
|
|
|
33
35
|
constructor(options: EventSubscribeOptions<node.ContractEvent>, contractAddress: string, fromCount?: number) {
|
|
34
36
|
super(options)
|
|
35
37
|
this.contractAddress = contractAddress
|
|
36
38
|
this.fromCount = typeof fromCount === 'undefined' ? 0 : fromCount
|
|
37
39
|
this.onEventCountChanged = options.onEventCountChanged
|
|
40
|
+
this.parallel = options.parallel ?? false
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
currentEventCount(): number {
|
|
@@ -61,8 +64,15 @@ export class EventSubscription extends Subscription<node.ContractEvent> {
|
|
|
61
64
|
return
|
|
62
65
|
}
|
|
63
66
|
|
|
64
|
-
|
|
65
|
-
|
|
67
|
+
if (this.parallel) {
|
|
68
|
+
const promises = events.events.map((event) => this.messageCallback(event))
|
|
69
|
+
await Promise.all(promises)
|
|
70
|
+
} else {
|
|
71
|
+
for (const event of events.events) {
|
|
72
|
+
await this.messageCallback(event)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
66
76
|
this.fromCount = events.nextStart
|
|
67
77
|
|
|
68
78
|
if (this.onEventCountChanged !== undefined) {
|
package/src/utils/webcrypto.ts
CHANGED
|
@@ -31,7 +31,8 @@ export class WebCrypto {
|
|
|
31
31
|
}
|
|
32
32
|
const bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength)
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
// Prefer global Web Crypto (e.g. react-native-get-random-values) for mobile compatibility
|
|
35
|
+
if (globalThis.crypto && typeof globalThis.crypto.getRandomValues === 'function') {
|
|
35
36
|
globalThis.crypto.getRandomValues(bytes)
|
|
36
37
|
} else {
|
|
37
38
|
randomFillSync(bytes)
|