@alephium/web3 0.5.0-rc.0 → 0.5.0-rc.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.LICENSE.txt +2 -0
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +57 -20
- package/dist/src/api/api-alephium.js +57 -15
- package/dist/src/api/index.d.ts +1 -0
- package/dist/src/api/index.js +1 -0
- package/dist/src/api/types.d.ts +1 -1
- package/dist/src/api/types.js +6 -2
- package/dist/src/constants.d.ts +1 -0
- package/dist/src/constants.js +2 -1
- package/dist/src/contract/contract.d.ts +37 -17
- package/dist/src/contract/contract.js +167 -19
- package/dist/src/signer/signer.d.ts +29 -30
- package/dist/src/signer/signer.js +34 -25
- package/dist/src/signer/tx-builder.d.ts +2 -7
- package/dist/src/signer/tx-builder.js +10 -7
- package/dist/src/signer/types.d.ts +8 -0
- package/dist/src/transaction/sign-verify.d.ts +3 -2
- package/dist/src/transaction/sign-verify.js +4 -14
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/sign.d.ts +3 -0
- package/dist/src/utils/sign.js +89 -0
- package/dist/src/utils/utils.d.ts +5 -3
- package/dist/src/utils/utils.js +25 -10
- package/package.json +3 -2
- package/src/api/api-alephium.ts +88 -32
- package/src/api/index.ts +2 -0
- package/src/api/types.ts +12 -2
- package/src/constants.ts +1 -0
- package/src/contract/contract.ts +288 -38
- package/src/signer/signer.ts +69 -55
- package/src/signer/tx-builder.ts +13 -7
- package/src/signer/types.ts +10 -2
- package/src/transaction/sign-verify.ts +10 -15
- package/src/utils/index.ts +1 -0
- package/src/utils/sign.ts +66 -0
- package/src/utils/utils.ts +27 -10
package/src/contract/contract.ts
CHANGED
|
@@ -40,13 +40,25 @@ import {
|
|
|
40
40
|
SignDeployContractTxParams,
|
|
41
41
|
SignDeployContractTxResult,
|
|
42
42
|
SignExecuteScriptTxParams,
|
|
43
|
-
SignerProvider
|
|
43
|
+
SignerProvider,
|
|
44
|
+
Address
|
|
44
45
|
} from '../signer'
|
|
45
46
|
import * as ralph from './ralph'
|
|
46
|
-
import {
|
|
47
|
+
import {
|
|
48
|
+
bs58,
|
|
49
|
+
binToHex,
|
|
50
|
+
contractIdFromAddress,
|
|
51
|
+
SubscribeOptions,
|
|
52
|
+
Subscription,
|
|
53
|
+
assertType,
|
|
54
|
+
Eq,
|
|
55
|
+
Optional,
|
|
56
|
+
groupOfAddress
|
|
57
|
+
} from '../utils'
|
|
47
58
|
import { getCurrentNodeProvider } from '../global'
|
|
48
59
|
import * as path from 'path'
|
|
49
60
|
import { EventSubscription, subscribeToEvents } from './events'
|
|
61
|
+
import { ONE_ALPH } from '../constants'
|
|
50
62
|
|
|
51
63
|
export type FieldsSig = node.FieldsSig
|
|
52
64
|
export type EventSig = node.EventSig
|
|
@@ -836,49 +848,65 @@ export class Contract extends Artifact {
|
|
|
836
848
|
}
|
|
837
849
|
|
|
838
850
|
static ContractCreatedEventIndex = -1
|
|
839
|
-
static ContractCreatedEvent:
|
|
851
|
+
static ContractCreatedEvent: SystemEventSig = {
|
|
840
852
|
name: 'ContractCreated',
|
|
841
853
|
fieldNames: ['address'],
|
|
842
|
-
fieldTypes: ['Address']
|
|
854
|
+
fieldTypes: ['Address'],
|
|
855
|
+
optionalFieldNames: ['parentAddress'],
|
|
856
|
+
optionalFieldTypes: ['Address']
|
|
843
857
|
}
|
|
844
858
|
|
|
845
859
|
static ContractDestroyedEventIndex = -2
|
|
846
|
-
static ContractDestroyedEvent:
|
|
860
|
+
static ContractDestroyedEvent: SystemEventSig = {
|
|
847
861
|
name: 'ContractDestroyed',
|
|
848
862
|
fieldNames: ['address'],
|
|
849
863
|
fieldTypes: ['Address']
|
|
850
864
|
}
|
|
851
865
|
|
|
852
866
|
static fromApiEvent(event: node.ContractEventByTxId, codeHash: string | undefined, txId: string): ContractEvent {
|
|
853
|
-
let
|
|
867
|
+
let fields: Fields
|
|
868
|
+
let name: string
|
|
854
869
|
|
|
855
870
|
if (event.eventIndex == Contract.ContractCreatedEventIndex) {
|
|
856
|
-
|
|
871
|
+
fields = fromApiSystemEventFields(event.fields, Contract.ContractCreatedEvent)
|
|
872
|
+
name = Contract.ContractCreatedEvent.name
|
|
857
873
|
} else if (event.eventIndex == Contract.ContractDestroyedEventIndex) {
|
|
858
|
-
|
|
874
|
+
fields = fromApiSystemEventFields(event.fields, Contract.ContractDestroyedEvent)
|
|
875
|
+
name = Contract.ContractDestroyedEvent.name
|
|
859
876
|
} else {
|
|
860
877
|
const contract = Project.currentProject.contractByCodeHash(codeHash!)
|
|
861
|
-
eventSig = contract.eventsSig[event.eventIndex]
|
|
878
|
+
const eventSig = contract.eventsSig[event.eventIndex]
|
|
879
|
+
fields = fromApiEventFields(event.fields, eventSig)
|
|
880
|
+
name = eventSig.name
|
|
862
881
|
}
|
|
863
882
|
|
|
864
883
|
return {
|
|
865
884
|
txId: txId,
|
|
866
885
|
blockHash: event.blockHash,
|
|
867
886
|
contractAddress: event.contractAddress,
|
|
868
|
-
name:
|
|
887
|
+
name: name,
|
|
869
888
|
eventIndex: event.eventIndex,
|
|
870
|
-
fields:
|
|
889
|
+
fields: fields
|
|
871
890
|
}
|
|
872
891
|
}
|
|
873
892
|
|
|
874
|
-
fromApiTestContractResult(
|
|
893
|
+
fromApiTestContractResult(
|
|
894
|
+
methodName: string,
|
|
895
|
+
result: node.TestContractResult,
|
|
896
|
+
txId: string
|
|
897
|
+
): TestContractResult<unknown> {
|
|
898
|
+
const methodIndex = this.functions.findIndex((sig) => sig.name === methodName)
|
|
899
|
+
const returnTypes = this.functions[`${methodIndex}`].returnTypes
|
|
900
|
+
const rawReturn = fromApiArray(result.returns, returnTypes)
|
|
901
|
+
const returns = rawReturn.length === 0 ? null : rawReturn.length === 1 ? rawReturn[0] : rawReturn
|
|
902
|
+
|
|
875
903
|
const addressToCodeHash = new Map<string, string>()
|
|
876
904
|
addressToCodeHash.set(result.address, result.codeHash)
|
|
877
905
|
result.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash))
|
|
878
906
|
return {
|
|
879
907
|
contractId: binToHex(contractIdFromAddress(result.address)),
|
|
880
908
|
contractAddress: result.address,
|
|
881
|
-
returns:
|
|
909
|
+
returns: returns,
|
|
882
910
|
gasUsed: result.gasUsed,
|
|
883
911
|
contracts: result.contracts.map((contract) => Contract.fromApiContractState(contract)),
|
|
884
912
|
txOutputs: result.txOutputs.map(fromApiOutput),
|
|
@@ -892,8 +920,10 @@ export class Contract extends Artifact {
|
|
|
892
920
|
params: DeployContractParams<P>
|
|
893
921
|
): Promise<SignDeployContractTxParams> {
|
|
894
922
|
const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {})
|
|
923
|
+
const selectedAccount = await signer.getSelectedAccount()
|
|
895
924
|
const signerParams: SignDeployContractTxParams = {
|
|
896
|
-
signerAddress:
|
|
925
|
+
signerAddress: selectedAccount.address,
|
|
926
|
+
signerKeyType: selectedAccount.keyType,
|
|
897
927
|
bytecode: bytecode,
|
|
898
928
|
initialAttoAlphAmount: params?.initialAttoAlphAmount,
|
|
899
929
|
issueTokenAmount: params?.issueTokenAmount,
|
|
@@ -941,11 +971,17 @@ export class Contract extends Artifact {
|
|
|
941
971
|
}
|
|
942
972
|
}
|
|
943
973
|
|
|
944
|
-
fromApiCallContractResult(
|
|
974
|
+
fromApiCallContractResult(
|
|
975
|
+
result: node.CallContractResult,
|
|
976
|
+
txId: string,
|
|
977
|
+
methodIndex: number
|
|
978
|
+
): CallContractResult<unknown> {
|
|
979
|
+
const returnTypes = this.functions[`${methodIndex}`].returnTypes
|
|
980
|
+
const rawReturn = fromApiArray(result.returns, returnTypes)
|
|
981
|
+
const returns = rawReturn.length === 0 ? null : rawReturn.length === 1 ? rawReturn[0] : rawReturn
|
|
982
|
+
|
|
945
983
|
const addressToCodeHash = new Map<string, string>()
|
|
946
984
|
result.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash))
|
|
947
|
-
const functionSig = this.functions[`${methodIndex}`]
|
|
948
|
-
const returns = fromApiArray(result.returns, functionSig.returnTypes)
|
|
949
985
|
return {
|
|
950
986
|
returns: returns,
|
|
951
987
|
gasUsed: result.gasUsed,
|
|
@@ -1029,8 +1065,10 @@ export class Script extends Artifact {
|
|
|
1029
1065
|
signer: SignerProvider,
|
|
1030
1066
|
params: ExecuteScriptParams<P>
|
|
1031
1067
|
): Promise<SignExecuteScriptTxParams> {
|
|
1068
|
+
const selectedAccount = await signer.getSelectedAccount()
|
|
1032
1069
|
const signerParams: SignExecuteScriptTxParams = {
|
|
1033
|
-
signerAddress:
|
|
1070
|
+
signerAddress: selectedAccount.address,
|
|
1071
|
+
signerKeyType: selectedAccount.keyType,
|
|
1034
1072
|
bytecode: this.buildByteCodeToDeploy(params.initialFields ?? {}),
|
|
1035
1073
|
attoAlphAmount: params.attoAlphAmount,
|
|
1036
1074
|
tokens: params.tokens,
|
|
@@ -1068,6 +1106,16 @@ function fromApiEventFields(vals: node.Val[], eventSig: node.EventSig): Fields {
|
|
|
1068
1106
|
return fromApiVals(vals, eventSig.fieldNames, eventSig.fieldTypes)
|
|
1069
1107
|
}
|
|
1070
1108
|
|
|
1109
|
+
function fromApiSystemEventFields(vals: node.Val[], systemEventSig: SystemEventSig): Fields {
|
|
1110
|
+
return fromApiVals(
|
|
1111
|
+
vals,
|
|
1112
|
+
systemEventSig.fieldNames,
|
|
1113
|
+
systemEventSig.fieldTypes,
|
|
1114
|
+
systemEventSig.optionalFieldNames ?? [],
|
|
1115
|
+
systemEventSig.optionalFieldTypes ?? []
|
|
1116
|
+
)
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1071
1119
|
export interface Asset {
|
|
1072
1120
|
alphAmount: Number256
|
|
1073
1121
|
tokens?: Token[]
|
|
@@ -1180,10 +1228,10 @@ export interface ContractEvent<T extends Fields = Fields> {
|
|
|
1180
1228
|
|
|
1181
1229
|
export type DebugMessage = node.DebugMessage
|
|
1182
1230
|
|
|
1183
|
-
export interface TestContractResult {
|
|
1231
|
+
export interface TestContractResult<R> {
|
|
1184
1232
|
contractId: string
|
|
1185
1233
|
contractAddress: string
|
|
1186
|
-
returns:
|
|
1234
|
+
returns: R
|
|
1187
1235
|
gasUsed: number
|
|
1188
1236
|
contracts: ContractState[]
|
|
1189
1237
|
txOutputs: Output[]
|
|
@@ -1245,18 +1293,21 @@ export interface DeployContractParams<P extends Fields = Fields> {
|
|
|
1245
1293
|
assertType<
|
|
1246
1294
|
Eq<
|
|
1247
1295
|
Omit<DeployContractParams<undefined>, 'initialFields'>,
|
|
1248
|
-
Omit<SignDeployContractTxParams, 'signerAddress' | 'bytecode'>
|
|
1296
|
+
Omit<SignDeployContractTxParams, 'signerAddress' | 'signerKeyType' | 'bytecode'>
|
|
1249
1297
|
>
|
|
1250
1298
|
>
|
|
1251
1299
|
export type DeployContractResult<T> = SignDeployContractTxResult & { instance: T }
|
|
1252
1300
|
|
|
1253
|
-
export abstract class ContractFactory<
|
|
1301
|
+
export abstract class ContractFactory<I, F extends Fields = Fields> {
|
|
1254
1302
|
readonly contract: Contract
|
|
1303
|
+
|
|
1255
1304
|
constructor(contract: Contract) {
|
|
1256
1305
|
this.contract = contract
|
|
1257
1306
|
}
|
|
1258
1307
|
|
|
1259
|
-
|
|
1308
|
+
abstract at(address: string): I
|
|
1309
|
+
|
|
1310
|
+
async deploy(signer: SignerProvider, deployParams: DeployContractParams<F>): Promise<DeployContractResult<I>> {
|
|
1260
1311
|
const signerParams = await this.contract.txParamsForDeployment(signer, deployParams)
|
|
1261
1312
|
const result = await signer.signAndSubmitDeployContractTx(signerParams)
|
|
1262
1313
|
return {
|
|
@@ -1265,7 +1316,14 @@ export abstract class ContractFactory<T, P extends Fields = Fields> {
|
|
|
1265
1316
|
}
|
|
1266
1317
|
}
|
|
1267
1318
|
|
|
1268
|
-
|
|
1319
|
+
// This is used for testing contract functions
|
|
1320
|
+
stateForTest(initFields: F, asset?: Asset, address?: string): ContractState<F> {
|
|
1321
|
+
const newAsset = {
|
|
1322
|
+
alphAmount: asset?.alphAmount ?? ONE_ALPH,
|
|
1323
|
+
tokens: asset?.tokens
|
|
1324
|
+
}
|
|
1325
|
+
return this.contract.toState(initFields, newAsset, address)
|
|
1326
|
+
}
|
|
1269
1327
|
}
|
|
1270
1328
|
|
|
1271
1329
|
export interface ExecuteScriptParams<P extends Fields = Fields> {
|
|
@@ -1293,8 +1351,8 @@ export interface CallContractParams<T extends Arguments = Arguments> {
|
|
|
1293
1351
|
inputAssets?: node.TestInputAsset[]
|
|
1294
1352
|
}
|
|
1295
1353
|
|
|
1296
|
-
export interface CallContractResult {
|
|
1297
|
-
returns:
|
|
1354
|
+
export interface CallContractResult<R> {
|
|
1355
|
+
returns: R
|
|
1298
1356
|
gasUsed: number
|
|
1299
1357
|
contracts: ContractState[]
|
|
1300
1358
|
txInputs: string[]
|
|
@@ -1302,45 +1360,53 @@ export interface CallContractResult {
|
|
|
1302
1360
|
events: ContractEvent[]
|
|
1303
1361
|
}
|
|
1304
1362
|
|
|
1305
|
-
export
|
|
1306
|
-
|
|
1363
|
+
export interface SystemEventSig extends EventSig {
|
|
1364
|
+
optionalFieldNames?: string[]
|
|
1365
|
+
optionalFieldTypes?: string[]
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
export type ContractCreatedEvent = ContractEvent<{ address: Address; parentAddress?: Address }>
|
|
1369
|
+
export type ContractDestroyedEvent = ContractEvent<{ address: Address }>
|
|
1307
1370
|
|
|
1308
|
-
function
|
|
1371
|
+
function decodeSystemEvent(event: node.ContractEvent, systemEventSig: SystemEventSig, eventIndex: number): Fields {
|
|
1309
1372
|
if (event.eventIndex !== eventIndex) {
|
|
1310
1373
|
throw new Error(`Invalid event index: ${event.eventIndex}, expected: ${eventIndex}`)
|
|
1311
1374
|
}
|
|
1312
|
-
return
|
|
1375
|
+
return fromApiSystemEventFields(event.fields, systemEventSig)
|
|
1313
1376
|
}
|
|
1314
1377
|
|
|
1315
1378
|
export function decodeContractCreatedEvent(event: node.ContractEvent): Omit<ContractCreatedEvent, 'contractAddress'> {
|
|
1316
|
-
const fields =
|
|
1379
|
+
const fields = decodeSystemEvent(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex)
|
|
1317
1380
|
return {
|
|
1318
1381
|
blockHash: event.blockHash,
|
|
1319
1382
|
txId: event.txId,
|
|
1320
1383
|
eventIndex: event.eventIndex,
|
|
1321
1384
|
name: Contract.ContractCreatedEvent.name,
|
|
1322
|
-
fields: {
|
|
1385
|
+
fields: {
|
|
1386
|
+
address: fields['address'] as Address,
|
|
1387
|
+
parentAddress: fields['parentAddress'] === undefined ? undefined : (fields['parentAddress'] as Address)
|
|
1388
|
+
}
|
|
1323
1389
|
}
|
|
1324
1390
|
}
|
|
1325
1391
|
|
|
1326
1392
|
export function decodeContractDestroyedEvent(
|
|
1327
1393
|
event: node.ContractEvent
|
|
1328
1394
|
): Omit<ContractDestroyedEvent, 'contractAddress'> {
|
|
1329
|
-
const fields =
|
|
1395
|
+
const fields = decodeSystemEvent(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex)
|
|
1330
1396
|
return {
|
|
1331
1397
|
blockHash: event.blockHash,
|
|
1332
1398
|
txId: event.txId,
|
|
1333
1399
|
eventIndex: event.eventIndex,
|
|
1334
1400
|
name: Contract.ContractDestroyedEvent.name,
|
|
1335
|
-
fields: { address: fields['address'] as
|
|
1401
|
+
fields: { address: fields['address'] as Address }
|
|
1336
1402
|
}
|
|
1337
1403
|
}
|
|
1338
1404
|
|
|
1339
|
-
export function subscribeEventsFromContract<T extends Fields
|
|
1340
|
-
options: SubscribeOptions<
|
|
1405
|
+
export function subscribeEventsFromContract<T extends Fields, M extends ContractEvent<T>>(
|
|
1406
|
+
options: SubscribeOptions<M>,
|
|
1341
1407
|
address: string,
|
|
1342
1408
|
eventIndex: number,
|
|
1343
|
-
decodeFunc: (event: node.ContractEvent) =>
|
|
1409
|
+
decodeFunc: (event: node.ContractEvent) => M,
|
|
1344
1410
|
fromCount?: number
|
|
1345
1411
|
): EventSubscription {
|
|
1346
1412
|
const messageCallback = (event: node.ContractEvent): Promise<void> => {
|
|
@@ -1351,7 +1417,7 @@ export function subscribeEventsFromContract<T extends Fields>(
|
|
|
1351
1417
|
}
|
|
1352
1418
|
|
|
1353
1419
|
const errorCallback = (err: any, subscription: Subscription<node.ContractEvent>): Promise<void> => {
|
|
1354
|
-
return options.errorCallback(err, subscription as unknown as Subscription<
|
|
1420
|
+
return options.errorCallback(err, subscription as unknown as Subscription<M>)
|
|
1355
1421
|
}
|
|
1356
1422
|
const opt: SubscribeOptions<node.ContractEvent> = {
|
|
1357
1423
|
pollingInterval: options.pollingInterval,
|
|
@@ -1360,3 +1426,187 @@ export function subscribeEventsFromContract<T extends Fields>(
|
|
|
1360
1426
|
}
|
|
1361
1427
|
return subscribeToEvents(opt, address, fromCount)
|
|
1362
1428
|
}
|
|
1429
|
+
|
|
1430
|
+
export async function testMethod<I, F extends Fields, A extends Arguments, R>(
|
|
1431
|
+
contract: ContractFactory<I, F>,
|
|
1432
|
+
methodName: string,
|
|
1433
|
+
params: Optional<TestContractParams<F, A>, 'testArgs' | 'initialFields'>
|
|
1434
|
+
): Promise<TestContractResult<R>> {
|
|
1435
|
+
const txId = params?.txId ?? randomTxId()
|
|
1436
|
+
const apiParams = contract.contract.toApiTestContractParams(methodName, {
|
|
1437
|
+
...params,
|
|
1438
|
+
txId: txId,
|
|
1439
|
+
initialFields: params.initialFields === undefined ? {} : params.initialFields,
|
|
1440
|
+
testArgs: params.testArgs === undefined ? {} : params.testArgs
|
|
1441
|
+
})
|
|
1442
|
+
const apiResult = await getCurrentNodeProvider().contracts.postContractsTestContract(apiParams)
|
|
1443
|
+
const testResult = contract.contract.fromApiTestContractResult(methodName, apiResult, txId)
|
|
1444
|
+
contract.contract.printDebugMessages(methodName, testResult.debugMessages)
|
|
1445
|
+
return testResult as TestContractResult<R>
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
export abstract class ContractInstance {
|
|
1449
|
+
readonly address: Address
|
|
1450
|
+
readonly contractId: string
|
|
1451
|
+
readonly groupIndex: number
|
|
1452
|
+
|
|
1453
|
+
constructor(address: Address) {
|
|
1454
|
+
this.address = address
|
|
1455
|
+
this.contractId = binToHex(contractIdFromAddress(address))
|
|
1456
|
+
this.groupIndex = groupOfAddress(address)
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
export async function fetchContractState<F extends Fields, I extends ContractInstance>(
|
|
1461
|
+
contract: ContractFactory<I, F>,
|
|
1462
|
+
instance: ContractInstance
|
|
1463
|
+
): Promise<ContractState<F>> {
|
|
1464
|
+
const contractState = await getCurrentNodeProvider().contracts.getContractsAddressState(instance.address, {
|
|
1465
|
+
group: instance.groupIndex
|
|
1466
|
+
})
|
|
1467
|
+
const state = contract.contract.fromApiContractState(contractState)
|
|
1468
|
+
return {
|
|
1469
|
+
...state,
|
|
1470
|
+
fields: state.fields as F
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
export function subscribeContractCreatedEvent(
|
|
1475
|
+
instance: ContractInstance,
|
|
1476
|
+
options: SubscribeOptions<ContractCreatedEvent>,
|
|
1477
|
+
fromCount?: number
|
|
1478
|
+
): EventSubscription {
|
|
1479
|
+
return subscribeEventsFromContract(
|
|
1480
|
+
options,
|
|
1481
|
+
instance.address,
|
|
1482
|
+
Contract.ContractCreatedEventIndex,
|
|
1483
|
+
(event) => {
|
|
1484
|
+
return {
|
|
1485
|
+
...decodeContractCreatedEvent(event),
|
|
1486
|
+
contractAddress: instance.address
|
|
1487
|
+
}
|
|
1488
|
+
},
|
|
1489
|
+
fromCount
|
|
1490
|
+
)
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
export function subscribeContractDestroyedEvent(
|
|
1494
|
+
instance: ContractInstance,
|
|
1495
|
+
options: SubscribeOptions<ContractDestroyedEvent>,
|
|
1496
|
+
fromCount?: number
|
|
1497
|
+
): EventSubscription {
|
|
1498
|
+
return subscribeEventsFromContract(
|
|
1499
|
+
options,
|
|
1500
|
+
instance.address,
|
|
1501
|
+
Contract.ContractDestroyedEventIndex,
|
|
1502
|
+
(event) => {
|
|
1503
|
+
return {
|
|
1504
|
+
...decodeContractDestroyedEvent(event),
|
|
1505
|
+
contractAddress: instance.address
|
|
1506
|
+
}
|
|
1507
|
+
},
|
|
1508
|
+
fromCount
|
|
1509
|
+
)
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
export function decodeEvent<F extends Fields, M extends ContractEvent<F>>(
|
|
1513
|
+
contract: Contract,
|
|
1514
|
+
instance: ContractInstance,
|
|
1515
|
+
event: node.ContractEvent,
|
|
1516
|
+
targetEventIndex: number
|
|
1517
|
+
): M {
|
|
1518
|
+
if (
|
|
1519
|
+
event.eventIndex !== targetEventIndex &&
|
|
1520
|
+
!(targetEventIndex >= 0 && targetEventIndex < contract.eventsSig.length)
|
|
1521
|
+
) {
|
|
1522
|
+
throw new Error('Invalid event index: ' + event.eventIndex + ', expected: ' + targetEventIndex)
|
|
1523
|
+
}
|
|
1524
|
+
const eventSig = contract.eventsSig[`${targetEventIndex}`]
|
|
1525
|
+
const fieldNames = eventSig.fieldNames
|
|
1526
|
+
const fieldTypes = eventSig.fieldTypes
|
|
1527
|
+
const fields = fromApiVals(event.fields, fieldNames, fieldTypes)
|
|
1528
|
+
return {
|
|
1529
|
+
contractAddress: instance.address,
|
|
1530
|
+
blockHash: event.blockHash,
|
|
1531
|
+
txId: event.txId,
|
|
1532
|
+
eventIndex: event.eventIndex,
|
|
1533
|
+
name: eventSig.name,
|
|
1534
|
+
fields: fields
|
|
1535
|
+
} as M
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1538
|
+
export function subscribeContractEvent<F extends Fields, M extends ContractEvent<F>>(
|
|
1539
|
+
contract: Contract,
|
|
1540
|
+
instance: ContractInstance,
|
|
1541
|
+
options: SubscribeOptions<M>,
|
|
1542
|
+
eventName: string,
|
|
1543
|
+
fromCount?: number
|
|
1544
|
+
): EventSubscription {
|
|
1545
|
+
const eventIndex = contract.eventsSig.findIndex((sig) => sig.name === eventName)
|
|
1546
|
+
return subscribeEventsFromContract<F, M>(
|
|
1547
|
+
options,
|
|
1548
|
+
instance.address,
|
|
1549
|
+
eventIndex,
|
|
1550
|
+
(event) => decodeEvent(contract, instance, event, eventIndex),
|
|
1551
|
+
fromCount
|
|
1552
|
+
)
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
export function subscribeAllEvents(
|
|
1556
|
+
contract: Contract,
|
|
1557
|
+
instance: ContractInstance,
|
|
1558
|
+
options: SubscribeOptions<ContractEvent<any>>,
|
|
1559
|
+
fromCount?: number
|
|
1560
|
+
): EventSubscription {
|
|
1561
|
+
const messageCallback = (event: node.ContractEvent): Promise<void> => {
|
|
1562
|
+
switch (event.eventIndex) {
|
|
1563
|
+
case Contract.ContractCreatedEventIndex: {
|
|
1564
|
+
return options.messageCallback({
|
|
1565
|
+
...decodeContractCreatedEvent(event),
|
|
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
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
const errorCallback = (err: any, subscription: Subscription<node.ContractEvent>): Promise<void> => {
|
|
1585
|
+
return options.errorCallback(err, subscription as unknown as Subscription<ContractEvent<any>>)
|
|
1586
|
+
}
|
|
1587
|
+
const opt: SubscribeOptions<node.ContractEvent> = {
|
|
1588
|
+
pollingInterval: options.pollingInterval,
|
|
1589
|
+
messageCallback: messageCallback,
|
|
1590
|
+
errorCallback: errorCallback
|
|
1591
|
+
}
|
|
1592
|
+
return subscribeToEvents(opt, instance.address, fromCount)
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
export async function callMethod<I, F extends Fields, A extends Arguments, R>(
|
|
1596
|
+
contract: ContractFactory<I, F>,
|
|
1597
|
+
instance: ContractInstance,
|
|
1598
|
+
methodName: string,
|
|
1599
|
+
params: Optional<CallContractParams<A>, 'args'>
|
|
1600
|
+
): Promise<CallContractResult<R>> {
|
|
1601
|
+
const methodIndex = contract.contract.getMethodIndex(methodName)
|
|
1602
|
+
const txId = params?.txId ?? randomTxId()
|
|
1603
|
+
const callParams = contract.contract.toApiCallContract(
|
|
1604
|
+
{ ...params, txId: txId, args: params.args === undefined ? {} : params.args },
|
|
1605
|
+
instance.groupIndex,
|
|
1606
|
+
instance.address,
|
|
1607
|
+
methodIndex
|
|
1608
|
+
)
|
|
1609
|
+
const result = await getCurrentNodeProvider().contracts.postContractsCallContract(callParams)
|
|
1610
|
+
const callResult = contract.contract.fromApiCallContractResult(result, txId, methodIndex)
|
|
1611
|
+
return callResult as CallContractResult<R>
|
|
1612
|
+
}
|