@alephium/web3 0.5.0-rc.13 → 0.5.0-rc.14
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.
|
@@ -257,6 +257,8 @@ export interface CallContractResult<R> {
|
|
|
257
257
|
txOutputs: Output[];
|
|
258
258
|
events: ContractEvent[];
|
|
259
259
|
}
|
|
260
|
+
export declare const CreateContractEventAddress: string;
|
|
261
|
+
export declare const DestroyContractEventAddress: string;
|
|
260
262
|
export interface SystemEventSig extends EventSig {
|
|
261
263
|
optionalFieldNames?: string[];
|
|
262
264
|
optionalFieldTypes?: string[];
|
|
@@ -279,10 +281,11 @@ export declare abstract class ContractInstance {
|
|
|
279
281
|
constructor(address: Address);
|
|
280
282
|
}
|
|
281
283
|
export declare function fetchContractState<F extends Fields, I extends ContractInstance>(contract: ContractFactory<I, F>, instance: ContractInstance): Promise<ContractState<F>>;
|
|
282
|
-
export declare function subscribeContractCreatedEvent(
|
|
283
|
-
export declare function subscribeContractDestroyedEvent(
|
|
284
|
+
export declare function subscribeContractCreatedEvent(options: SubscribeOptions<ContractCreatedEvent>, fromCount?: number): EventSubscription;
|
|
285
|
+
export declare function subscribeContractDestroyedEvent(options: SubscribeOptions<ContractDestroyedEvent>, fromCount?: number): EventSubscription;
|
|
284
286
|
export declare function decodeEvent<F extends Fields, M extends ContractEvent<F>>(contract: Contract, instance: ContractInstance, event: node.ContractEvent, targetEventIndex: number): M;
|
|
285
287
|
export declare function subscribeContractEvent<F extends Fields, M extends ContractEvent<F>>(contract: Contract, instance: ContractInstance, options: SubscribeOptions<M>, eventName: string, fromCount?: number): EventSubscription;
|
|
286
|
-
export declare function
|
|
288
|
+
export declare function subscribeContractEvents(contract: Contract, instance: ContractInstance, options: SubscribeOptions<ContractEvent<any>>, fromCount?: number): EventSubscription;
|
|
287
289
|
export declare function callMethod<I, F extends Fields, A extends Arguments, R>(contract: ContractFactory<I, F>, instance: ContractInstance, methodName: string, params: Optional<CallContractParams<A>, 'args'>): Promise<CallContractResult<R>>;
|
|
290
|
+
export declare function getContractEventsCurrentCount(contractAddress: Address): Promise<number>;
|
|
288
291
|
export {};
|
|
@@ -43,7 +43,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
43
43
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
44
44
|
};
|
|
45
45
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
-
exports.callMethod = exports.
|
|
46
|
+
exports.getContractEventsCurrentCount = exports.callMethod = exports.subscribeContractEvents = exports.subscribeContractEvent = exports.decodeEvent = exports.subscribeContractDestroyedEvent = exports.subscribeContractCreatedEvent = exports.fetchContractState = exports.ContractInstance = exports.testMethod = exports.subscribeEventsFromContract = exports.decodeContractDestroyedEvent = exports.decodeContractCreatedEvent = exports.DestroyContractEventAddress = exports.CreateContractEventAddress = exports.ContractFactory = exports.randomTxId = exports.toApiVals = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = void 0;
|
|
47
47
|
const buffer_1 = require("buffer/");
|
|
48
48
|
const crypto_1 = require("crypto");
|
|
49
49
|
const fs_1 = __importDefault(require("fs"));
|
|
@@ -886,6 +886,13 @@ class ContractFactory {
|
|
|
886
886
|
}
|
|
887
887
|
}
|
|
888
888
|
exports.ContractFactory = ContractFactory;
|
|
889
|
+
function specialContractAddress(n) {
|
|
890
|
+
const bytes = new Uint8Array(32).fill(0);
|
|
891
|
+
bytes[31] = n;
|
|
892
|
+
return (0, utils_1.addressFromContractId)((0, utils_1.binToHex)(bytes));
|
|
893
|
+
}
|
|
894
|
+
exports.CreateContractEventAddress = specialContractAddress(-1);
|
|
895
|
+
exports.DestroyContractEventAddress = specialContractAddress(-2);
|
|
889
896
|
function decodeSystemEvent(event, systemEventSig, eventIndex) {
|
|
890
897
|
if (event.eventIndex !== eventIndex) {
|
|
891
898
|
throw new Error(`Invalid event index: ${event.eventIndex}, expected: ${eventIndex}`);
|
|
@@ -968,20 +975,20 @@ async function fetchContractState(contract, instance) {
|
|
|
968
975
|
};
|
|
969
976
|
}
|
|
970
977
|
exports.fetchContractState = fetchContractState;
|
|
971
|
-
function subscribeContractCreatedEvent(
|
|
972
|
-
return subscribeEventsFromContract(options,
|
|
978
|
+
function subscribeContractCreatedEvent(options, fromCount) {
|
|
979
|
+
return subscribeEventsFromContract(options, exports.CreateContractEventAddress, Contract.ContractCreatedEventIndex, (event) => {
|
|
973
980
|
return {
|
|
974
981
|
...decodeContractCreatedEvent(event),
|
|
975
|
-
contractAddress:
|
|
982
|
+
contractAddress: exports.CreateContractEventAddress
|
|
976
983
|
};
|
|
977
984
|
}, fromCount);
|
|
978
985
|
}
|
|
979
986
|
exports.subscribeContractCreatedEvent = subscribeContractCreatedEvent;
|
|
980
|
-
function subscribeContractDestroyedEvent(
|
|
981
|
-
return subscribeEventsFromContract(options,
|
|
987
|
+
function subscribeContractDestroyedEvent(options, fromCount) {
|
|
988
|
+
return subscribeEventsFromContract(options, exports.DestroyContractEventAddress, Contract.ContractDestroyedEventIndex, (event) => {
|
|
982
989
|
return {
|
|
983
990
|
...decodeContractDestroyedEvent(event),
|
|
984
|
-
contractAddress:
|
|
991
|
+
contractAddress: exports.DestroyContractEventAddress
|
|
985
992
|
};
|
|
986
993
|
}, fromCount);
|
|
987
994
|
}
|
|
@@ -1010,27 +1017,12 @@ function subscribeContractEvent(contract, instance, options, eventName, fromCoun
|
|
|
1010
1017
|
return subscribeEventsFromContract(options, instance.address, eventIndex, (event) => decodeEvent(contract, instance, event, eventIndex), fromCount);
|
|
1011
1018
|
}
|
|
1012
1019
|
exports.subscribeContractEvent = subscribeContractEvent;
|
|
1013
|
-
function
|
|
1020
|
+
function subscribeContractEvents(contract, instance, options, fromCount) {
|
|
1014
1021
|
const messageCallback = (event) => {
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
contractAddress: instance.address
|
|
1020
|
-
});
|
|
1021
|
-
}
|
|
1022
|
-
case Contract.ContractDestroyedEventIndex: {
|
|
1023
|
-
return options.messageCallback({
|
|
1024
|
-
...decodeContractDestroyedEvent(event),
|
|
1025
|
-
contractAddress: instance.address
|
|
1026
|
-
});
|
|
1027
|
-
}
|
|
1028
|
-
default:
|
|
1029
|
-
return options.messageCallback({
|
|
1030
|
-
...decodeEvent(contract, instance, event, event.eventIndex),
|
|
1031
|
-
contractAddress: instance.address
|
|
1032
|
-
});
|
|
1033
|
-
}
|
|
1022
|
+
return options.messageCallback({
|
|
1023
|
+
...decodeEvent(contract, instance, event, event.eventIndex),
|
|
1024
|
+
contractAddress: instance.address
|
|
1025
|
+
});
|
|
1034
1026
|
};
|
|
1035
1027
|
const errorCallback = (err, subscription) => {
|
|
1036
1028
|
return options.errorCallback(err, subscription);
|
|
@@ -1042,7 +1034,7 @@ function subscribeAllEvents(contract, instance, options, fromCount) {
|
|
|
1042
1034
|
};
|
|
1043
1035
|
return (0, events_1.subscribeToEvents)(opt, instance.address, fromCount);
|
|
1044
1036
|
}
|
|
1045
|
-
exports.
|
|
1037
|
+
exports.subscribeContractEvents = subscribeContractEvents;
|
|
1046
1038
|
async function callMethod(contract, instance, methodName, params) {
|
|
1047
1039
|
const methodIndex = contract.contract.getMethodIndex(methodName);
|
|
1048
1040
|
const txId = params?.txId ?? randomTxId();
|
|
@@ -1052,3 +1044,14 @@ async function callMethod(contract, instance, methodName, params) {
|
|
|
1052
1044
|
return callResult;
|
|
1053
1045
|
}
|
|
1054
1046
|
exports.callMethod = callMethod;
|
|
1047
|
+
async function getContractEventsCurrentCount(contractAddress) {
|
|
1048
|
+
return (0, global_1.getCurrentNodeProvider)()
|
|
1049
|
+
.events.getEventsContractContractaddressCurrentCount(contractAddress)
|
|
1050
|
+
.catch((error) => {
|
|
1051
|
+
if (error instanceof Error && error.message.includes(`${contractAddress} not found`)) {
|
|
1052
|
+
return 0;
|
|
1053
|
+
}
|
|
1054
|
+
throw error;
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
exports.getContractEventsCurrentCount = getContractEventsCurrentCount;
|
package/package.json
CHANGED
package/src/contract/contract.ts
CHANGED
|
@@ -53,7 +53,8 @@ import {
|
|
|
53
53
|
assertType,
|
|
54
54
|
Eq,
|
|
55
55
|
Optional,
|
|
56
|
-
groupOfAddress
|
|
56
|
+
groupOfAddress,
|
|
57
|
+
addressFromContractId
|
|
57
58
|
} from '../utils'
|
|
58
59
|
import { getCurrentNodeProvider } from '../global'
|
|
59
60
|
import * as path from 'path'
|
|
@@ -1360,6 +1361,15 @@ export interface CallContractResult<R> {
|
|
|
1360
1361
|
events: ContractEvent[]
|
|
1361
1362
|
}
|
|
1362
1363
|
|
|
1364
|
+
function specialContractAddress(n: number): string {
|
|
1365
|
+
const bytes = new Uint8Array(32).fill(0)
|
|
1366
|
+
bytes[31] = n
|
|
1367
|
+
return addressFromContractId(binToHex(bytes))
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
export const CreateContractEventAddress = specialContractAddress(-1)
|
|
1371
|
+
export const DestroyContractEventAddress = specialContractAddress(-2)
|
|
1372
|
+
|
|
1363
1373
|
export interface SystemEventSig extends EventSig {
|
|
1364
1374
|
optionalFieldNames?: string[]
|
|
1365
1375
|
optionalFieldTypes?: string[]
|
|
@@ -1472,18 +1482,17 @@ export async function fetchContractState<F extends Fields, I extends ContractIns
|
|
|
1472
1482
|
}
|
|
1473
1483
|
|
|
1474
1484
|
export function subscribeContractCreatedEvent(
|
|
1475
|
-
instance: ContractInstance,
|
|
1476
1485
|
options: SubscribeOptions<ContractCreatedEvent>,
|
|
1477
1486
|
fromCount?: number
|
|
1478
1487
|
): EventSubscription {
|
|
1479
1488
|
return subscribeEventsFromContract(
|
|
1480
1489
|
options,
|
|
1481
|
-
|
|
1490
|
+
CreateContractEventAddress,
|
|
1482
1491
|
Contract.ContractCreatedEventIndex,
|
|
1483
1492
|
(event) => {
|
|
1484
1493
|
return {
|
|
1485
1494
|
...decodeContractCreatedEvent(event),
|
|
1486
|
-
contractAddress:
|
|
1495
|
+
contractAddress: CreateContractEventAddress
|
|
1487
1496
|
}
|
|
1488
1497
|
},
|
|
1489
1498
|
fromCount
|
|
@@ -1491,18 +1500,17 @@ export function subscribeContractCreatedEvent(
|
|
|
1491
1500
|
}
|
|
1492
1501
|
|
|
1493
1502
|
export function subscribeContractDestroyedEvent(
|
|
1494
|
-
instance: ContractInstance,
|
|
1495
1503
|
options: SubscribeOptions<ContractDestroyedEvent>,
|
|
1496
1504
|
fromCount?: number
|
|
1497
1505
|
): EventSubscription {
|
|
1498
1506
|
return subscribeEventsFromContract(
|
|
1499
1507
|
options,
|
|
1500
|
-
|
|
1508
|
+
DestroyContractEventAddress,
|
|
1501
1509
|
Contract.ContractDestroyedEventIndex,
|
|
1502
1510
|
(event) => {
|
|
1503
1511
|
return {
|
|
1504
1512
|
...decodeContractDestroyedEvent(event),
|
|
1505
|
-
contractAddress:
|
|
1513
|
+
contractAddress: DestroyContractEventAddress
|
|
1506
1514
|
}
|
|
1507
1515
|
},
|
|
1508
1516
|
fromCount
|
|
@@ -1552,34 +1560,17 @@ export function subscribeContractEvent<F extends Fields, M extends ContractEvent
|
|
|
1552
1560
|
)
|
|
1553
1561
|
}
|
|
1554
1562
|
|
|
1555
|
-
export function
|
|
1563
|
+
export function subscribeContractEvents(
|
|
1556
1564
|
contract: Contract,
|
|
1557
1565
|
instance: ContractInstance,
|
|
1558
1566
|
options: SubscribeOptions<ContractEvent<any>>,
|
|
1559
1567
|
fromCount?: number
|
|
1560
1568
|
): EventSubscription {
|
|
1561
1569
|
const messageCallback = (event: node.ContractEvent): Promise<void> => {
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
contractAddress: instance.address
|
|
1567
|
-
})
|
|
1568
|
-
}
|
|
1569
|
-
|
|
1570
|
-
case Contract.ContractDestroyedEventIndex: {
|
|
1571
|
-
return options.messageCallback({
|
|
1572
|
-
...decodeContractDestroyedEvent(event),
|
|
1573
|
-
contractAddress: instance.address
|
|
1574
|
-
})
|
|
1575
|
-
}
|
|
1576
|
-
|
|
1577
|
-
default:
|
|
1578
|
-
return options.messageCallback({
|
|
1579
|
-
...decodeEvent(contract, instance, event, event.eventIndex),
|
|
1580
|
-
contractAddress: instance.address
|
|
1581
|
-
})
|
|
1582
|
-
}
|
|
1570
|
+
return options.messageCallback({
|
|
1571
|
+
...decodeEvent(contract, instance, event, event.eventIndex),
|
|
1572
|
+
contractAddress: instance.address
|
|
1573
|
+
})
|
|
1583
1574
|
}
|
|
1584
1575
|
const errorCallback = (err: any, subscription: Subscription<node.ContractEvent>): Promise<void> => {
|
|
1585
1576
|
return options.errorCallback(err, subscription as unknown as Subscription<ContractEvent<any>>)
|
|
@@ -1610,3 +1601,14 @@ export async function callMethod<I, F extends Fields, A extends Arguments, R>(
|
|
|
1610
1601
|
const callResult = contract.contract.fromApiCallContractResult(result, txId, methodIndex)
|
|
1611
1602
|
return callResult as CallContractResult<R>
|
|
1612
1603
|
}
|
|
1604
|
+
|
|
1605
|
+
export async function getContractEventsCurrentCount(contractAddress: Address): Promise<number> {
|
|
1606
|
+
return getCurrentNodeProvider()
|
|
1607
|
+
.events.getEventsContractContractaddressCurrentCount(contractAddress)
|
|
1608
|
+
.catch((error) => {
|
|
1609
|
+
if (error instanceof Error && error.message.includes(`${contractAddress} not found`)) {
|
|
1610
|
+
return 0
|
|
1611
|
+
}
|
|
1612
|
+
throw error
|
|
1613
|
+
})
|
|
1614
|
+
}
|