@xyo-network/xl1-protocol-sdk 1.26.12 → 1.26.13
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/neutral/simple/accountBalance/SimpleAccountBalanceViewer.d.ts +2 -2
- package/dist/neutral/simple/block/SimpleBlockViewer.d.ts +2 -2
- package/dist/neutral/simple/datalake/SimpleDataLakeRunner.d.ts +1 -1
- package/dist/neutral/simple/finalization/SimpleFinalizationViewer.d.ts +1 -1
- package/dist/neutral/simple/windowedBlock/SimpleWindowedBlockViewer.d.ts +10 -10
- package/dist/neutral/transaction/TransactionBuilder.d.ts +4 -4
- package/dist/neutral/transaction/signTransaction.d.ts +1 -1
- package/package.json +24 -14
- package/src/ChainContextHelpers.ts +0 -50
- package/src/CreatableProvider/AbstractCreatableProvider.ts +0 -97
- package/src/CreatableProvider/CreatableProvider.ts +0 -102
- package/src/CreatableProvider/CreatableProviderRegistry.ts +0 -94
- package/src/CreatableProvider/GetInstanceOptions.ts +0 -11
- package/src/CreatableProvider/LabeledCreatableProviderFactory.ts +0 -24
- package/src/CreatableProvider/ProviderFactory.ts +0 -138
- package/src/CreatableProvider/ProviderFactoryLocator.ts +0 -170
- package/src/CreatableProvider/ProviderFactoryLocatorInstance.ts +0 -70
- package/src/CreatableProvider/index.ts +0 -8
- package/src/SignedBigInt.ts +0 -59
- package/src/_internal/context/getTestProviderContext.ts +0 -13
- package/src/_internal/context/index.ts +0 -1
- package/src/_internal/index.ts +0 -2
- package/src/actor/ActorV3.ts +0 -177
- package/src/actor/index.ts +0 -1
- package/src/amount/ShiftedBigInt.ts +0 -73
- package/src/amount/XL1Amount.ts +0 -103
- package/src/amount/index.ts +0 -4
- package/src/amount/splitOnDecimal.ts +0 -6
- package/src/amount/splitOnDecimalToString.ts +0 -40
- package/src/block/hydrate/allHashesPresent.ts +0 -8
- package/src/block/hydrate/blockPayloadsFromHydratedBlock.ts +0 -12
- package/src/block/hydrate/flattenHydratedBlock.ts +0 -28
- package/src/block/hydrate/flattenHydratedBlocks.ts +0 -7
- package/src/block/hydrate/hydrateBlock.ts +0 -43
- package/src/block/hydrate/index.ts +0 -7
- package/src/block/hydrate/transactionsFromHydratedBlock.ts +0 -7
- package/src/block/hydrate/tryHydrateBlock.ts +0 -26
- package/src/block/index.ts +0 -2
- package/src/block/primitives/blockFromBlockNumber.ts +0 -69
- package/src/block/primitives/frames/calculateFramesFromRange.ts +0 -30
- package/src/block/primitives/frames/deepCalculateFramesFromRange.ts +0 -28
- package/src/block/primitives/frames/index.ts +0 -2
- package/src/block/primitives/hashFromBlockNumber.ts +0 -9
- package/src/block/primitives/index.ts +0 -7
- package/src/block/primitives/model.ts +0 -18
- package/src/block/primitives/payloads/ChainWindow.ts +0 -7
- package/src/block/primitives/payloads/index.ts +0 -1
- package/src/block/primitives/toStepIdentityString.ts +0 -6
- package/src/block/primitives/validateTransactionOpcodes.ts +0 -45
- package/src/config/Actor.ts +0 -32
- package/src/config/Actors.ts +0 -9
- package/src/config/Base.ts +0 -27
- package/src/config/Chain.ts +0 -23
- package/src/config/Config.ts +0 -24
- package/src/config/DataLake/DataLake.ts +0 -12
- package/src/config/DataLake/DataLakeRemoteConfig.ts +0 -11
- package/src/config/DataLake/RestDataLakeConfig.ts +0 -18
- package/src/config/DataLake/RouterDataLakeConfig.ts +0 -22
- package/src/config/DataLake/index.ts +0 -4
- package/src/config/DeepPartial.ts +0 -4
- package/src/config/Evm.ts +0 -38
- package/src/config/HostActor.ts +0 -32
- package/src/config/Log.ts +0 -25
- package/src/config/Provider.ts +0 -20
- package/src/config/Providers.ts +0 -9
- package/src/config/Remote.ts +0 -59
- package/src/config/Telemetry.ts +0 -49
- package/src/config/UsageMeta.ts +0 -46
- package/src/config/Validation.ts +0 -29
- package/src/config/index.ts +0 -12
- package/src/config/storage/Storage.ts +0 -16
- package/src/config/storage/driver/Mongo.ts +0 -53
- package/src/config/storage/driver/index.ts +0 -1
- package/src/config/storage/index.ts +0 -2
- package/src/constants.ts +0 -4
- package/src/context/Actor.ts +0 -20
- package/src/context/HostActor.ts +0 -20
- package/src/context/getEmptyProviderContext.ts +0 -19
- package/src/context/index.ts +0 -3
- package/src/createDeclarationPayload.ts +0 -15
- package/src/createTransferPayload.ts +0 -16
- package/src/driver/cache/LruCacheMap.ts +0 -48
- package/src/driver/cache/index.ts +0 -1
- package/src/driver/index.ts +0 -2
- package/src/driver/memory/MemoryMap.ts +0 -48
- package/src/driver/memory/index.ts +0 -1
- package/src/eip-712/Payloads/EIP712Data.ts +0 -35
- package/src/eip-712/Payloads/EIP712Signature.ts +0 -32
- package/src/eip-712/Payloads/index.ts +0 -2
- package/src/eip-712/Types.ts +0 -41
- package/src/eip-712/index.ts +0 -4
- package/src/eip-712/sign.ts +0 -19
- package/src/eip-712/spec/fixtures.ts +0 -27
- package/src/eip-712/verify.ts +0 -20
- package/src/getFileConfig.ts +0 -36
- package/src/index.ts +0 -27
- package/src/instances/Address.ts +0 -16
- package/src/instances/BoundWitness.ts +0 -13
- package/src/instances/Data.ts +0 -4
- package/src/instances/Fees.ts +0 -9
- package/src/instances/HydratedBoundWitness.ts +0 -16
- package/src/instances/Object.ts +0 -6
- package/src/instances/Payload.ts +0 -7
- package/src/instances/Signature.ts +0 -12
- package/src/instances/block/BlockFields.ts +0 -18
- package/src/instances/block/HydratedBlock.ts +0 -10
- package/src/instances/block/index.ts +0 -1
- package/src/instances/index.ts +0 -10
- package/src/instances/modifiers/BlockWindowInstance.ts +0 -8
- package/src/instances/modifiers/BlockWindowState.ts +0 -6
- package/src/instances/modifiers/Signed.ts +0 -9
- package/src/instances/modifiers/Validatable.ts +0 -6
- package/src/instances/modifiers/index.ts +0 -4
- package/src/instances/transaction/HydratedTransaction.ts +0 -9
- package/src/instances/transaction/TransactionFields.ts +0 -22
- package/src/instances/transaction/index.ts +0 -1
- package/src/isInternetAvailable.ts +0 -11
- package/src/model/ChainFork/ChainForkStatic.ts +0 -9
- package/src/model/ChainFork/index.ts +0 -1
- package/src/model/ChainIdentity.ts +0 -6
- package/src/model/ChainStore.ts +0 -16
- package/src/model/CreatableProviderContext.ts +0 -6
- package/src/model/CreatableProviderContext.zod.ts +0 -55
- package/src/model/PayloadBundle/bundledPayloadToHydratedBlock.ts +0 -15
- package/src/model/PayloadBundle/bundledPayloadToHydratedTransaction.ts +0 -15
- package/src/model/PayloadBundle/hydratedBlockToPayloadBundle.ts +0 -19
- package/src/model/PayloadBundle/hydratedTransactionToPayloadBundle.ts +0 -19
- package/src/model/PayloadBundle/index.ts +0 -4
- package/src/model/index.ts +0 -6
- package/src/multipliers.ts +0 -18
- package/src/payloads/AddressPairPayload.ts +0 -27
- package/src/payloads/index.ts +0 -4
- package/src/payloads/netBalancesForPayloads.ts +0 -24
- package/src/payloads/netSchemasForPayloads.ts +0 -24
- package/src/payloads/netTransfersForPayloads.ts +0 -27
- package/src/primitives/block/index.ts +0 -2
- package/src/primitives/block/range/blockRangeSteps.ts +0 -23
- package/src/primitives/block/range/index.ts +0 -1
- package/src/primitives/block/rate/blockRate.ts +0 -77
- package/src/primitives/block/rate/index.ts +0 -4
- package/src/primitives/block/rate/stepRate.ts +0 -37
- package/src/primitives/block/rate/timeHelpers.ts +0 -61
- package/src/primitives/block/rate/timeRate.ts +0 -121
- package/src/primitives/chain/getWindowedChain.ts +0 -50
- package/src/primitives/chain/index.ts +0 -3
- package/src/primitives/chain/step/chainStepRewardAddress.ts +0 -30
- package/src/primitives/chain/step/index.ts +0 -4
- package/src/primitives/chain/step/stepRewardBlock.ts +0 -13
- package/src/primitives/chain/step/stepRewardTotal.ts +0 -46
- package/src/primitives/chain/step/stepsRewardTotal.ts +0 -37
- package/src/primitives/chain/time/externalBlockNumberFromXL1BlockNumber.ts +0 -32
- package/src/primitives/chain/time/externalBlockRangeFromStep.ts +0 -21
- package/src/primitives/chain/time/externalBlockRangeFromXL1BlockRange.ts +0 -17
- package/src/primitives/chain/time/index.ts +0 -3
- package/src/primitives/datalake/addDataLakePayloads.ts +0 -18
- package/src/primitives/datalake/addDataLakePayloadsToPayloads.ts +0 -24
- package/src/primitives/datalake/index.ts +0 -2
- package/src/primitives/index.ts +0 -13
- package/src/primitives/mapToMapType.ts +0 -35
- package/src/primitives/payload/index.ts +0 -1
- package/src/primitives/payload/mergeTransfers.ts +0 -16
- package/src/primitives/readPayloadMapFromStore.ts +0 -58
- package/src/primitives/rewardFromBlockNumber.ts +0 -19
- package/src/primitives/rewards/index.ts +0 -1
- package/src/primitives/rewards/networkStakeStepRewardPositionWeight.ts +0 -26
- package/src/primitives/stake/activeStakeAtTimeByAddress.ts +0 -27
- package/src/primitives/stake/activeStakeAtTimeByPosition.ts +0 -25
- package/src/primitives/stake/allStakersForRange.ts +0 -32
- package/src/primitives/stake/allStakersForStep.ts +0 -24
- package/src/primitives/stake/index.ts +0 -6
- package/src/primitives/stake/mergedAddRemoveStakeEventsByPosition.ts +0 -15
- package/src/primitives/stake/mergedAddRemoveStakeEventsByStaker.ts +0 -17
- package/src/primitives/stake/weightedStakeForRangeByPosition.ts +0 -69
- package/src/primitives/state/findMostRecentBlock.ts +0 -44
- package/src/primitives/state/hydratedBlockByNumber.ts +0 -25
- package/src/primitives/state/index.ts +0 -2
- package/src/primitives/step/completedStepRewardAddress.ts +0 -10
- package/src/primitives/step/derivedReceiveAddress.ts +0 -9
- package/src/primitives/step/index.ts +0 -4
- package/src/primitives/step/stepBlockRange.ts +0 -9
- package/src/primitives/step/stepTransferIndex.ts +0 -19
- package/src/primitives/transaction/elevatedPayloads.ts +0 -24
- package/src/primitives/transaction/index.ts +0 -1
- package/src/primitives/uncle/findBestUncle.ts +0 -9
- package/src/primitives/uncle/findUncles.ts +0 -75
- package/src/primitives/uncle/index.ts +0 -3
- package/src/primitives/uncle/scoreUncle.ts +0 -7
- package/src/services/AccountTransfersService.ts +0 -13
- package/src/services/BlockProducerService.ts +0 -11
- package/src/services/Chain/BaseChainService.ts +0 -4
- package/src/services/Chain/ChainHeadService.ts +0 -8
- package/src/services/Chain/ChainService.ts +0 -6
- package/src/services/Chain/index.ts +0 -3
- package/src/services/Election.ts +0 -14
- package/src/services/NetworkStakeService.ts +0 -4
- package/src/services/Params.ts +0 -13
- package/src/services/PendingTransactionsService.ts +0 -7
- package/src/services/SchemasService.ts +0 -11
- package/src/services/StakeIntentService/ChainIndexingServiceStateSchema.ts +0 -50
- package/src/services/StakeIntentService/StakeIntentService.ts +0 -35
- package/src/services/StakeIntentService/index.ts +0 -2
- package/src/services/index.ts +0 -8
- package/src/simple/StakeEventsViewer/SimpleStakeEventsViewer.ts +0 -93
- package/src/simple/StakeEventsViewer/index.ts +0 -1
- package/src/simple/StakeTotalsViewer/SimpleStakeTotalsViewer.ts +0 -116
- package/src/simple/StakeTotalsViewer/index.ts +0 -1
- package/src/simple/StakeViewer/SimpleStakeViewer.ts +0 -164
- package/src/simple/StakeViewer/index.ts +0 -1
- package/src/simple/TransactionViewer/SimpleTransactionViewer.ts +0 -100
- package/src/simple/TransactionViewer/index.ts +0 -1
- package/src/simple/accountBalance/SimpleAccountBalanceViewer.ts +0 -271
- package/src/simple/accountBalance/index.ts +0 -1
- package/src/simple/block/SimpleBlockViewer.ts +0 -231
- package/src/simple/block/index.ts +0 -1
- package/src/simple/blockInvalidation/SimpleBlockInvalidationViewer.ts +0 -197
- package/src/simple/blockInvalidation/index.ts +0 -1
- package/src/simple/blockReward/SimpleBlockRewardViewer.ts +0 -27
- package/src/simple/blockReward/index.ts +0 -1
- package/src/simple/blockValidation/SimpleBlockValidationViewer.ts +0 -194
- package/src/simple/blockValidation/index.ts +0 -1
- package/src/simple/chainContractViewer/SimpleChainContractViewer.ts +0 -92
- package/src/simple/chainContractViewer/index.ts +0 -1
- package/src/simple/client/SimpleXyoClient.ts +0 -14
- package/src/simple/client/index.ts +0 -1
- package/src/simple/datalake/AbstractRestDataLake.ts +0 -70
- package/src/simple/datalake/AbstractSimpleDataLake.ts +0 -62
- package/src/simple/datalake/RestDataLakeRunner.ts +0 -54
- package/src/simple/datalake/RestDataLakeViewer.ts +0 -22
- package/src/simple/datalake/SimpleDataLakeRunner.ts +0 -44
- package/src/simple/datalake/SimpleDataLakeViewer.ts +0 -24
- package/src/simple/datalake/index.ts +0 -4
- package/src/simple/finalization/SimpleFinalizationRunner.ts +0 -45
- package/src/simple/finalization/SimpleFinalizationViewer.ts +0 -123
- package/src/simple/finalization/index.ts +0 -2
- package/src/simple/gateway/SimpleXyoGateway.ts +0 -30
- package/src/simple/gateway/SimpleXyoGatewayRunner.ts +0 -137
- package/src/simple/gateway/index.ts +0 -2
- package/src/simple/index.ts +0 -22
- package/src/simple/mempool/SimpleMempoolRunner.ts +0 -390
- package/src/simple/mempool/SimpleMempoolViewer.ts +0 -215
- package/src/simple/mempool/index.ts +0 -2
- package/src/simple/network/SimpleXyoNetwork.ts +0 -48
- package/src/simple/network/index.ts +0 -1
- package/src/simple/network/lib/FailedNetworkStatusPayloads.ts +0 -16
- package/src/simple/network/lib/StatusNetworks.ts +0 -32
- package/src/simple/network/lib/index.ts +0 -2
- package/src/simple/permissions/SimpleXyoPermissions.ts +0 -82
- package/src/simple/permissions/index.ts +0 -2
- package/src/simple/permissions/store/MemoryPermissions.ts +0 -32
- package/src/simple/permissions/store/PermissionsStore.ts +0 -17
- package/src/simple/permissions/store/index.ts +0 -2
- package/src/simple/runner/SimpleXyoRunner.ts +0 -41
- package/src/simple/runner/index.ts +0 -1
- package/src/simple/signer/SimpleXyoSigner.ts +0 -78
- package/src/simple/signer/index.ts +0 -1
- package/src/simple/timeSync2/SimpleTimeSyncViewer.ts +0 -132
- package/src/simple/timeSync2/index.ts +0 -1
- package/src/simple/transactionInvalidation/SimpleTransactionInvalidationViewer.ts +0 -197
- package/src/simple/transactionInvalidation/index.ts +0 -1
- package/src/simple/transactionValidation/SimpleTransactionValidationViewer.ts +0 -173
- package/src/simple/transactionValidation/index.ts +0 -1
- package/src/simple/windowedBlock/SimpleWindowedBlockViewer.ts +0 -207
- package/src/simple/windowedBlock/index.ts +0 -1
- package/src/status/RuntimeStatusMonitor.ts +0 -120
- package/src/status/ServiceStatus.ts +0 -21
- package/src/status/index.ts +0 -2
- package/src/summary/index.ts +0 -2
- package/src/summary/model/BalancesStepSummary.ts +0 -35
- package/src/summary/model/SchemasStepSummary.ts +0 -33
- package/src/summary/model/StepSummary.ts +0 -20
- package/src/summary/model/TransfersSummary.ts +0 -36
- package/src/summary/model/index.ts +0 -4
- package/src/summary/primitives/balances/balancesStepSummaryFromRange.ts +0 -94
- package/src/summary/primitives/balances/balancesSummary.ts +0 -48
- package/src/summary/primitives/balances/index.ts +0 -2
- package/src/summary/primitives/index.ts +0 -3
- package/src/summary/primitives/schemas/index.ts +0 -2
- package/src/summary/primitives/schemas/schemasStepSummaryFromRange.ts +0 -86
- package/src/summary/primitives/schemas/schemasSummary.ts +0 -44
- package/src/summary/primitives/transfers/index.ts +0 -2
- package/src/summary/primitives/transfers/transfersStepSummaryFromRange.ts +0 -109
- package/src/summary/primitives/transfers/transfersSummary.ts +0 -52
- package/src/test/BuildBlockOptions.ts +0 -81
- package/src/test/buildBlock.ts +0 -105
- package/src/test/buildGenesisBlock.ts +0 -29
- package/src/test/buildNextBlock.ts +0 -37
- package/src/test/buildRandomChain.ts +0 -92
- package/src/test/buildRandomGenesisBlock.ts +0 -47
- package/src/test/createGenesisBlock.ts +0 -41
- package/src/test/createProducerChainStakeIntentTransaction.ts +0 -48
- package/src/test/getSimpleBlockViewerLocator.ts +0 -41
- package/src/test/getTestProviderContext.ts +0 -8
- package/src/test/index.ts +0 -2
- package/src/time/index.ts +0 -1
- package/src/time/primitives/index.ts +0 -1
- package/src/time/primitives/xl1BlockNumberToEthBlockNumber.ts +0 -13
- package/src/transaction/TransactionBuilder.ts +0 -164
- package/src/transaction/buildRandomTransaction.ts +0 -38
- package/src/transaction/buildTransaction.ts +0 -68
- package/src/transaction/buildUnsignedTransaction.ts +0 -56
- package/src/transaction/confirmSubmittedTransaction.ts +0 -42
- package/src/transaction/hydrateTransaction.ts +0 -117
- package/src/transaction/index.ts +0 -9
- package/src/transaction/primitives/index.ts +0 -4
- package/src/transaction/primitives/transactionBlockByteCount.ts +0 -10
- package/src/transaction/primitives/transactionElevatedPayloads.ts +0 -18
- package/src/transaction/primitives/transactionOperations.ts +0 -13
- package/src/transaction/primitives/transactionRequiredGas.ts +0 -22
- package/src/transaction/script.ts +0 -48
- package/src/transaction/signTransaction.ts +0 -32
- package/src/utils/HydratedCache.ts +0 -39
- package/src/utils/index.ts +0 -2
- package/src/utils/isZodError.ts +0 -11
- package/src/validation/index.ts +0 -2
- package/src/validation/lib/getUrl.ts +0 -7
- package/src/validation/lib/index.ts +0 -2
- package/src/validation/lib/isLocalhost.ts +0 -9
- package/src/validation/schema/Address.ts +0 -2
- package/src/validation/schema/Mnemonic.ts +0 -21
- package/src/validation/schema/index.ts +0 -2
- package/src/wallet/generateXyoBaseWalletFromPhrase.ts +0 -13
- package/src/wallet/index.ts +0 -2
- package/src/wallet/paths.ts +0 -17
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import type { Labels, WithOptionalLabels } from '@xylabs/sdk-js'
|
|
2
|
-
import { assertEx } from '@xylabs/sdk-js'
|
|
3
|
-
import type { ProviderMoniker } from '@xyo-network/xl1-protocol-lib'
|
|
4
|
-
|
|
5
|
-
import type {
|
|
6
|
-
CreatableProvider, CreatableProviderFactory, CreatableProviderInstance,
|
|
7
|
-
ProviderFactoryScope,
|
|
8
|
-
} from './CreatableProvider.ts'
|
|
9
|
-
import type { GetInstanceOptions } from './GetInstanceOptions.ts'
|
|
10
|
-
|
|
11
|
-
declare global {
|
|
12
|
-
var xyoServiceSingletons: Record<string, unknown>
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** ProviderFactoryDescription helper function. */
|
|
16
|
-
export function providerFactoryDescription(factory: CreatableProviderFactory, labels?: Labels) {
|
|
17
|
-
return `${factory.providerName}:${factory.defaultMoniker}:${JSON.stringify(labels ?? (factory as WithOptionalLabels).labels ?? {})}`
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** ProviderFactory implementation. */
|
|
21
|
-
export class ProviderFactory<TProvider extends CreatableProviderInstance,
|
|
22
|
-
TDependencies extends ProviderMoniker[]> implements CreatableProviderFactory<TProvider> {
|
|
23
|
-
creatableProvider: CreatableProvider<TProvider>
|
|
24
|
-
|
|
25
|
-
defaultMoniker: CreatableProvider<TProvider>['monikers'][number]
|
|
26
|
-
|
|
27
|
-
defaultParams: Omit<TProvider['params'], 'context'>
|
|
28
|
-
|
|
29
|
-
dependencies: TDependencies
|
|
30
|
-
|
|
31
|
-
labels?: Labels
|
|
32
|
-
|
|
33
|
-
monikers: CreatableProvider<TProvider>['monikers']
|
|
34
|
-
|
|
35
|
-
providerName: string
|
|
36
|
-
|
|
37
|
-
scope: ProviderFactoryScope
|
|
38
|
-
|
|
39
|
-
private _uniqueId: symbol
|
|
40
|
-
|
|
41
|
-
constructor(
|
|
42
|
-
creatableProvider: CreatableProvider<TProvider>,
|
|
43
|
-
dependencies: TDependencies,
|
|
44
|
-
params: Omit<TProvider['params'], 'context'>,
|
|
45
|
-
labels: Labels = {},
|
|
46
|
-
scope: ProviderFactoryScope = 'context',
|
|
47
|
-
) {
|
|
48
|
-
this.creatableProvider = creatableProvider
|
|
49
|
-
this.defaultParams = params
|
|
50
|
-
this.defaultMoniker = creatableProvider.defaultMoniker
|
|
51
|
-
this.dependencies = dependencies
|
|
52
|
-
this.monikers = creatableProvider.monikers
|
|
53
|
-
this.scope = scope
|
|
54
|
-
assertEx(this.monikers.includes(this.defaultMoniker), () => 'defaultMoniker must be in monikers')
|
|
55
|
-
this.labels = Object.assign({}, (creatableProvider as WithOptionalLabels).labels ?? {}, labels ?? {})
|
|
56
|
-
this.providerName = creatableProvider.name
|
|
57
|
-
this._uniqueId = Symbol(providerFactoryDescription(this))
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
get resolvedMoniker() {
|
|
61
|
-
const labels: Labels = this.labels ?? {}
|
|
62
|
-
const labelString = Object.entries(labels).map(([key, value]) => `${key}=${value}`).join(',')
|
|
63
|
-
return labelString.length === 0 ? `${this.defaultMoniker}` : `${this.defaultMoniker}|${labelString}`
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
get uniqueId(): symbol {
|
|
67
|
-
return this._uniqueId
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
static withParams<TInstance extends CreatableProviderInstance, TDependencies extends ProviderMoniker[]>(
|
|
71
|
-
creatableProvider: CreatableProvider<TInstance>,
|
|
72
|
-
dependencies: TDependencies,
|
|
73
|
-
params: Omit<TInstance['params'], 'context'>,
|
|
74
|
-
labels: Labels = {},
|
|
75
|
-
) {
|
|
76
|
-
return new ProviderFactory<TInstance, TDependencies>(creatableProvider, dependencies, params, labels)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
factory<TInstance extends CreatableProviderInstance, TDependencies extends ProviderMoniker[]>(
|
|
80
|
-
this: CreatableProviderFactory<TInstance, TDependencies>,
|
|
81
|
-
dependencies: TDependencies,
|
|
82
|
-
params: Omit<TInstance['params'], 'context'>,
|
|
83
|
-
labels: Labels = {},
|
|
84
|
-
) {
|
|
85
|
-
return new ProviderFactory<TInstance, TDependencies>(this.creatableProvider, dependencies, params, labels)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async getInstance(this: CreatableProviderFactory<TProvider>, params: TProvider['params'], { start = true }: GetInstanceOptions): Promise<TProvider> {
|
|
89
|
-
let scopeObject: Record<string, unknown> = {}
|
|
90
|
-
switch (this.scope) {
|
|
91
|
-
case 'global': {
|
|
92
|
-
if (globalThis.xyoServiceSingletons === undefined) {
|
|
93
|
-
globalThis.xyoServiceSingletons = {}
|
|
94
|
-
}
|
|
95
|
-
scopeObject = globalThis.xyoServiceSingletons
|
|
96
|
-
break
|
|
97
|
-
}
|
|
98
|
-
case 'context': {
|
|
99
|
-
const context = assertEx(
|
|
100
|
-
params?.context,
|
|
101
|
-
() => 'Context is required for context-scoped providers',
|
|
102
|
-
)
|
|
103
|
-
if (context.singletons === undefined) {
|
|
104
|
-
context.singletons = {}
|
|
105
|
-
}
|
|
106
|
-
scopeObject = context.singletons
|
|
107
|
-
break
|
|
108
|
-
}
|
|
109
|
-
default: {
|
|
110
|
-
scopeObject = {}
|
|
111
|
-
break
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
const mergedParams: TProvider['params'] = {
|
|
115
|
-
...this.defaultParams,
|
|
116
|
-
...params,
|
|
117
|
-
} as TProvider['params']
|
|
118
|
-
const resultPromise = scopeObject[this.resolvedMoniker] as Promise<TProvider> ?? this.creatableProvider.create<TProvider>(mergedParams)
|
|
119
|
-
scopeObject[this.resolvedMoniker] = resultPromise
|
|
120
|
-
const result = await resultPromise
|
|
121
|
-
if (start) {
|
|
122
|
-
assertEx(await result.start(), () => `Failed to start provider instance [${this.resolvedMoniker}]`)
|
|
123
|
-
}
|
|
124
|
-
return result
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
async tryGetInstance(
|
|
128
|
-
this: CreatableProviderFactory<TProvider>,
|
|
129
|
-
params: TProvider['params'],
|
|
130
|
-
options?: GetInstanceOptions,
|
|
131
|
-
): Promise<TProvider | undefined> {
|
|
132
|
-
try {
|
|
133
|
-
return await this.getInstance(params, options)
|
|
134
|
-
} catch {
|
|
135
|
-
return
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import { hasAllLabels, type Labels } from '@xylabs/sdk-js'
|
|
2
|
-
import { assertEx } from '@xylabs/sdk-js'
|
|
3
|
-
import type { Provider, ProviderMoniker } from '@xyo-network/xl1-protocol-lib'
|
|
4
|
-
|
|
5
|
-
import type { CreatableProviderContext } from '../model/index.ts'
|
|
6
|
-
import type { CreatableProviderFactory, CreatableProviderInstance } from './CreatableProvider.ts'
|
|
7
|
-
import { type CreatableProviderRegistry, registerCreatableProviderFactory } from './CreatableProviderRegistry.ts'
|
|
8
|
-
import type { ProviderFactoryGetInstanceOptions } from './GetInstanceOptions.ts'
|
|
9
|
-
import { hasLabels, type LabeledCreatableProviderFactory } from './LabeledCreatableProviderFactory.ts'
|
|
10
|
-
import type { ProviderFactoryLocatorInstance } from './ProviderFactoryLocatorInstance.ts'
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* A class which encapsulates the Service Locator Pattern for Provider Factories
|
|
14
|
-
*/
|
|
15
|
-
export class ProviderFactoryLocator<TContext extends CreatableProviderContext = CreatableProviderContext,
|
|
16
|
-
TMonikers extends ProviderMoniker[] = ProviderMoniker[]> implements ProviderFactoryLocatorInstance<TContext> {
|
|
17
|
-
protected readonly _context: TContext
|
|
18
|
-
protected readonly _registry: CreatableProviderRegistry<TMonikers>
|
|
19
|
-
|
|
20
|
-
private _frozen = false
|
|
21
|
-
private _parent?: ProviderFactoryLocatorInstance<TContext>
|
|
22
|
-
private _validateDepsOnRegister: boolean
|
|
23
|
-
|
|
24
|
-
constructor(context: (Omit<TContext, 'locator'> & { locator?: TContext['locator'] }), registry: CreatableProviderRegistry = {}, validateDepsOnRegister = false) {
|
|
25
|
-
this._registry = registry
|
|
26
|
-
this._context = { ...context, locator: this } as unknown as TContext
|
|
27
|
-
this._parent = context.locator as ProviderFactoryLocatorInstance<TContext>
|
|
28
|
-
this._validateDepsOnRegister = validateDepsOnRegister
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
get context() {
|
|
32
|
-
return this._context
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
get logger() {
|
|
36
|
-
return this.context.logger
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The current registry for the module factory
|
|
41
|
-
*/
|
|
42
|
-
get registry(): Readonly<CreatableProviderRegistry> {
|
|
43
|
-
return this._registry
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
protected get validateDepsOnRegister() {
|
|
47
|
-
return this._validateDepsOnRegister
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
freeze() {
|
|
51
|
-
this._frozen = true
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async getInstance<TProvider extends Provider<ProviderMoniker>>(
|
|
55
|
-
moniker: TProvider['moniker'],
|
|
56
|
-
{ start = true, labels }: ProviderFactoryGetInstanceOptions = {},
|
|
57
|
-
) {
|
|
58
|
-
return assertEx(
|
|
59
|
-
await this.tryGetInstance<TProvider>(moniker, { start, labels }),
|
|
60
|
-
() => `No provider instance for the supplied config moniker [${moniker}]${labels ? ` & labels [${JSON.stringify(labels)}]` : ''} could be created`,
|
|
61
|
-
)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
has(moniker: TMonikers[number]): boolean {
|
|
65
|
-
return !!this._registry[moniker]
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Locates a provider factory that matches the supplied moniker and labels
|
|
70
|
-
* @param moniker The config moniker for the provider
|
|
71
|
-
* @param labels The labels for the provider factory
|
|
72
|
-
* @returns A provider factory that matches the supplied moniker and labels or throws if one is not found
|
|
73
|
-
*/
|
|
74
|
-
locate<TProvider extends Provider<ProviderMoniker>>(moniker: TProvider['moniker'], labels?: Labels):
|
|
75
|
-
CreatableProviderFactory<CreatableProviderInstance<TProvider>> | LabeledCreatableProviderFactory<CreatableProviderInstance<TProvider>> {
|
|
76
|
-
return assertEx(
|
|
77
|
-
this.tryLocate<TProvider>(moniker, labels),
|
|
78
|
-
|
|
79
|
-
() => `No module factory for the supplied config moniker [${moniker}]${labels ? ` & labels [${JSON.stringify(labels)}]` : ''} registered`,
|
|
80
|
-
)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
merge(locator: ProviderFactoryLocatorInstance): ProviderFactoryLocatorInstance {
|
|
84
|
-
const registry = { ...this.registry }
|
|
85
|
-
for (const moniker in locator.registry) {
|
|
86
|
-
if (registry[moniker]) {
|
|
87
|
-
registry[moniker].push(...(locator.registry[moniker] ?? []))
|
|
88
|
-
} else {
|
|
89
|
-
registry[moniker] = locator.registry[moniker]
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return new ProviderFactoryLocator(this.context, registry)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Registers a single module factory (with optional tags) with the locator
|
|
97
|
-
* @param factory The factory to register
|
|
98
|
-
* @param labels The labels for the module factory
|
|
99
|
-
*/
|
|
100
|
-
register(factory: CreatableProviderFactory, labels?: Labels, primary: boolean | ProviderMoniker | ProviderMoniker[] = false): this {
|
|
101
|
-
assertEx(!this._frozen, () => 'Cannot register a module factory after the locator has been frozen')
|
|
102
|
-
if (this.validateDepsOnRegister) {
|
|
103
|
-
const missingDeps = factory.dependencies.filter(dep => !this.registered(dep))
|
|
104
|
-
assertEx(missingDeps.length === 0, () => `Cannot register module factory [${factory.uniqueId.description}] due to missing dependencies: ${missingDeps.join(', ')}`)
|
|
105
|
-
}
|
|
106
|
-
registerCreatableProviderFactory(this._registry, factory, labels, primary)
|
|
107
|
-
return this
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Registers multiple module factories with the locator
|
|
112
|
-
* @param factories The factories to register
|
|
113
|
-
*/
|
|
114
|
-
registerMany(factories: CreatableProviderFactory[]): this {
|
|
115
|
-
for (const factory of factories) {
|
|
116
|
-
this.register(factory)
|
|
117
|
-
}
|
|
118
|
-
return this
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
registered(moniker: ProviderMoniker) {
|
|
122
|
-
return !!this.registry[moniker] || (this._parent?.registered(moniker) ?? false)
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
async tryGetInstance<TProvider extends Provider<ProviderMoniker>>(
|
|
126
|
-
moniker: TProvider['moniker'],
|
|
127
|
-
{ start = true, labels }: ProviderFactoryGetInstanceOptions = {},
|
|
128
|
-
) {
|
|
129
|
-
const resolvedParams = { context: this.context } as CreatableProviderInstance<TProvider>['params']
|
|
130
|
-
const factory = this.tryLocate<TProvider>(moniker, labels)
|
|
131
|
-
if (factory) {
|
|
132
|
-
if (this.context.singletons[factory.uniqueId]) {
|
|
133
|
-
return this.context.singletons[factory.uniqueId] as CreatableProviderInstance<TProvider>
|
|
134
|
-
}
|
|
135
|
-
this.logger?.info(`Creating provider instance for moniker [${moniker}]${labels ? ` with labels [${JSON.stringify(labels)}]` : ''} using factory [${factory.uniqueId.description}]`)
|
|
136
|
-
const result = await factory.getInstance(resolvedParams, { start })
|
|
137
|
-
this.context.singletons[factory.uniqueId] = result
|
|
138
|
-
return result
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Tries to locate a module factory that matches the supplied moniker and labels
|
|
144
|
-
* @param moniker The config moniker for the module
|
|
145
|
-
* @param labels The labels for the module factory
|
|
146
|
-
* @returns A module factory that matches the supplied moniker and labels or undefined
|
|
147
|
-
*/
|
|
148
|
-
tryLocate<TProvider extends Provider<ProviderMoniker>>(moniker: TProvider['moniker'], labels?: Labels) {
|
|
149
|
-
const result = (labels
|
|
150
|
-
// Find the first factory that has labels and has all the labels provided
|
|
151
|
-
? (this._registry[moniker]?.filter(hasLabels).find(factory => hasAllLabels(factory?.labels, labels)) ?? this._registry[moniker]?.[0])
|
|
152
|
-
// Otherwise, return the first factory
|
|
153
|
-
: this._registry[moniker]?.[0]) ?? this._parent?.tryLocate<TProvider>(moniker, labels)
|
|
154
|
-
|
|
155
|
-
return result as CreatableProviderFactory<CreatableProviderInstance<TProvider>>
|
|
156
|
-
| LabeledCreatableProviderFactory<CreatableProviderInstance<TProvider>> | undefined
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
validateDependencies(recursive = false) {
|
|
160
|
-
if (recursive) {
|
|
161
|
-
this._parent?.validateDependencies()
|
|
162
|
-
}
|
|
163
|
-
for (const moniker in this.registry) {
|
|
164
|
-
for (const factory of this.registry[moniker] ?? []) {
|
|
165
|
-
const missingDeps = factory.dependencies.filter(dep => !this.registered(dep))
|
|
166
|
-
assertEx(missingDeps.length === 0, () => `Module factory [${factory.uniqueId.description}] is missing dependencies: ${missingDeps.join(', ')}`)
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import type { Labels } from '@xylabs/sdk-js'
|
|
2
|
-
import type { Provider, ProviderMoniker } from '@xyo-network/xl1-protocol-lib'
|
|
3
|
-
|
|
4
|
-
import type { CreatableProviderContext } from '../model/index.ts'
|
|
5
|
-
import type { CreatableProviderFactory, CreatableProviderInstance } from './CreatableProvider.ts'
|
|
6
|
-
import type { CreatableProviderRegistry } from './CreatableProviderRegistry.ts'
|
|
7
|
-
import type { ProviderFactoryGetInstanceOptions } from './GetInstanceOptions.ts'
|
|
8
|
-
import type { LabeledCreatableProviderFactory } from './LabeledCreatableProviderFactory.ts'
|
|
9
|
-
|
|
10
|
-
/** Interface for ProviderFactoryLocator instances. */
|
|
11
|
-
export interface ProviderFactoryLocatorInstance<T extends CreatableProviderContext = CreatableProviderContext> {
|
|
12
|
-
|
|
13
|
-
context: T
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* The current registry for the module factory
|
|
17
|
-
*/
|
|
18
|
-
registry: Readonly<CreatableProviderRegistry>
|
|
19
|
-
|
|
20
|
-
freeze(): void
|
|
21
|
-
|
|
22
|
-
getInstance<TProvider extends Provider<ProviderMoniker>>(
|
|
23
|
-
moniker: TProvider['moniker'],
|
|
24
|
-
options?: ProviderFactoryGetInstanceOptions,
|
|
25
|
-
): Promise<CreatableProviderInstance<TProvider>>
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Locates a module factory that matches the supplied moniker and labels
|
|
29
|
-
* @param moniker The config moniker for the module
|
|
30
|
-
* @param labels The labels for the module factory
|
|
31
|
-
* @returns A module factory that matches the supplied moniker and labels or throws if one is not found
|
|
32
|
-
*/
|
|
33
|
-
locate<TProvider extends Provider<ProviderMoniker>>(moniker: ProviderMoniker, labels?: Labels):
|
|
34
|
-
CreatableProviderFactory<CreatableProviderInstance<TProvider>> | LabeledCreatableProviderFactory<CreatableProviderInstance<TProvider>> | undefined
|
|
35
|
-
|
|
36
|
-
merge(locator: ProviderFactoryLocatorInstance): ProviderFactoryLocatorInstance
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Registers a single module factory (with optional tags) with the locator
|
|
40
|
-
* @param factory The factory to register
|
|
41
|
-
* @param labels The labels for the module factory
|
|
42
|
-
*/
|
|
43
|
-
register(factory: CreatableProviderFactory, labels?: Labels, primary?: boolean | ProviderMoniker | ProviderMoniker[]): ProviderFactoryLocatorInstance
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Registers multiple module factories with the locator
|
|
47
|
-
* @param factories The factories to register
|
|
48
|
-
*/
|
|
49
|
-
registerMany(factories: CreatableProviderFactory[]): ProviderFactoryLocatorInstance
|
|
50
|
-
|
|
51
|
-
registered(moniker: ProviderMoniker): boolean
|
|
52
|
-
|
|
53
|
-
tryGetInstance<TProvider extends Provider<ProviderMoniker>>(
|
|
54
|
-
moniker: TProvider['moniker'],
|
|
55
|
-
options?: ProviderFactoryGetInstanceOptions,
|
|
56
|
-
): Promise<CreatableProviderInstance<TProvider> | undefined>
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Tries to locate a module factory that matches the supplied moniker and labels
|
|
60
|
-
* @param moniker The config moniker for the module
|
|
61
|
-
* @param labels The labels for the module factory
|
|
62
|
-
* @returns A module factory that matches the supplied moniker and labels or undefined
|
|
63
|
-
*/
|
|
64
|
-
tryLocate<TProvider extends Provider<ProviderMoniker>>(
|
|
65
|
-
moniker: ProviderMoniker,
|
|
66
|
-
labels?: Labels
|
|
67
|
-
): CreatableProviderFactory<CreatableProviderInstance<TProvider>> | LabeledCreatableProviderFactory<CreatableProviderInstance<TProvider>> | undefined
|
|
68
|
-
|
|
69
|
-
validateDependencies(): void
|
|
70
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export * from './AbstractCreatableProvider.ts'
|
|
2
|
-
export * from './CreatableProvider.ts'
|
|
3
|
-
export * from './CreatableProviderRegistry.ts'
|
|
4
|
-
export * from './GetInstanceOptions.ts'
|
|
5
|
-
export * from './LabeledCreatableProviderFactory.ts'
|
|
6
|
-
export * from './ProviderFactory.ts'
|
|
7
|
-
export * from './ProviderFactoryLocator.ts'
|
|
8
|
-
export * from './ProviderFactoryLocatorInstance.ts'
|
package/src/SignedBigInt.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Hex,
|
|
3
|
-
hexToBigInt,
|
|
4
|
-
toHex,
|
|
5
|
-
} from '@xylabs/sdk-js'
|
|
6
|
-
import { isObject } from '@xylabs/sdk-js'
|
|
7
|
-
|
|
8
|
-
/** Represents a negative bigint value stored as a hex string */
|
|
9
|
-
export interface NegativeBigInt {
|
|
10
|
-
negative: Hex
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/** Represents a positive bigint value stored as a hex string */
|
|
14
|
-
export interface PositiveBigInt {
|
|
15
|
-
positive: Hex
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/** A signed bigint value represented as either a negative or positive hex string */
|
|
19
|
-
export type SignedBigInt = NegativeBigInt | PositiveBigInt
|
|
20
|
-
|
|
21
|
-
/** Type guard that checks if a value is a NegativeBigInt */
|
|
22
|
-
export const isNegativeBigInt = (value: unknown): value is NegativeBigInt => {
|
|
23
|
-
return isObject(value) && 'negative' in value && typeof value.negative === 'string'
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Type guard that checks if a value is a PositiveBigInt */
|
|
27
|
-
export const isPositiveBigInt = (value: unknown): value is PositiveBigInt => {
|
|
28
|
-
return isObject(value) && 'positive' in value && typeof value.positive === 'string'
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** parseSignedBigInt constant. */
|
|
32
|
-
export const parseSignedBigInt = (value: SignedBigInt): bigint => {
|
|
33
|
-
if (isNegativeBigInt(value)) {
|
|
34
|
-
return -hexToBigInt(value.negative)
|
|
35
|
-
} else if (isPositiveBigInt(value)) {
|
|
36
|
-
return hexToBigInt(value.positive)
|
|
37
|
-
} else {
|
|
38
|
-
throw new Error('Invalid balance type')
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** toSignedBigInt constant. */
|
|
43
|
-
export const toSignedBigInt = (value: bigint): SignedBigInt => {
|
|
44
|
-
return value < 0n ? { negative: toHex(-value) } : { positive: toHex(value) }
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** toPositiveBigInt constant. */
|
|
48
|
-
export const toPositiveBigInt = (value: unknown): PositiveBigInt => {
|
|
49
|
-
if (isNegativeBigInt(value)) {
|
|
50
|
-
return { positive: toHex(0n) }
|
|
51
|
-
}
|
|
52
|
-
if (isPositiveBigInt(value)) {
|
|
53
|
-
return { positive: value.positive }
|
|
54
|
-
}
|
|
55
|
-
if (typeof value === 'bigint') {
|
|
56
|
-
return { positive: toHex(value) }
|
|
57
|
-
}
|
|
58
|
-
throw new Error('Invalid value for positive big int')
|
|
59
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Config } from '../../config/index.ts'
|
|
2
|
-
import { ProviderFactoryLocator } from '../../CreatableProvider/index.ts'
|
|
3
|
-
import type { CreatableProviderContext } from '../../model/index.ts'
|
|
4
|
-
|
|
5
|
-
// This file should not be exported from the package
|
|
6
|
-
export function getTestProviderContext(config: Config): CreatableProviderContext {
|
|
7
|
-
const singletons = {}
|
|
8
|
-
const caches = {}
|
|
9
|
-
const locator = new ProviderFactoryLocator({
|
|
10
|
-
config, singletons, caches, logger: console,
|
|
11
|
-
})
|
|
12
|
-
return locator.context
|
|
13
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './getTestProviderContext.ts'
|
package/src/_internal/index.ts
DELETED
package/src/actor/ActorV3.ts
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CreatableInstance, CreatableName, CreatableParams, CreatableStatusReporter, EmptyObject,
|
|
3
|
-
Logger,
|
|
4
|
-
} from '@xylabs/sdk-js'
|
|
5
|
-
import {
|
|
6
|
-
AbstractCreatable,
|
|
7
|
-
assertEx,
|
|
8
|
-
delay, IdLogger,
|
|
9
|
-
} from '@xylabs/sdk-js'
|
|
10
|
-
import type { AccountInstance } from '@xyo-network/sdk-js'
|
|
11
|
-
import { Semaphore } from 'async-mutex'
|
|
12
|
-
import z from 'zod'
|
|
13
|
-
|
|
14
|
-
import type { ProviderFactoryLocatorInstance } from '../CreatableProvider/index.ts'
|
|
15
|
-
|
|
16
|
-
const CreatableNameZod = z.custom<CreatableName>(val => typeof val === 'string' && val.length > 0)
|
|
17
|
-
const StatusReporterInstanceZod = z.custom<CreatableStatusReporter>(val => val && typeof val === 'object' && 'report' in val)
|
|
18
|
-
const AccountInstanceZod = z.custom<AccountInstance>(val => val && typeof val === 'object' && 'address' in val)
|
|
19
|
-
|
|
20
|
-
/** ActorParamsV3Zod constant. */
|
|
21
|
-
export const ActorParamsV3Zod = z.object({
|
|
22
|
-
account: AccountInstanceZod,
|
|
23
|
-
locator: z.unknown(),
|
|
24
|
-
name: CreatableNameZod,
|
|
25
|
-
statusReporter: StatusReporterInstanceZod.optional(),
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
/** ActorParamsV3Zod type. */
|
|
29
|
-
export type ActorParamsV3Zod = z.infer<typeof ActorParamsV3Zod>
|
|
30
|
-
|
|
31
|
-
/** ActorParamsV3 type. */
|
|
32
|
-
export type ActorParamsV3<T extends EmptyObject | void = void> = CreatableParams & {
|
|
33
|
-
account: AccountInstance
|
|
34
|
-
locator: ProviderFactoryLocatorInstance
|
|
35
|
-
} & (T extends void ? EmptyObject : T)
|
|
36
|
-
|
|
37
|
-
/** ActorInstanceV3 type. */
|
|
38
|
-
export type ActorInstanceV3<T extends ActorParamsV3 = ActorParamsV3> = CreatableInstance<T>
|
|
39
|
-
|
|
40
|
-
/** ActorV3 implementation. */
|
|
41
|
-
export abstract class ActorV3<TParams extends ActorParamsV3 = ActorParamsV3> extends AbstractCreatable<TParams> {
|
|
42
|
-
protected readonly _intervals: Map<string, ReturnType<typeof setInterval>> = new Map()
|
|
43
|
-
protected readonly _semaphores: Map<string, Semaphore> = new Map()
|
|
44
|
-
protected readonly _timeouts: Map<string, ReturnType<typeof setTimeout>> = new Map()
|
|
45
|
-
|
|
46
|
-
private _logger?: Logger
|
|
47
|
-
|
|
48
|
-
override get logger() {
|
|
49
|
-
this._logger = new IdLogger(assertEx(this.context.logger, () => `Logger is required in context for actor ${this.name}.`), () => this.name)
|
|
50
|
-
return this._logger
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
protected get account() {
|
|
54
|
-
return this.params.account
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
protected get context() {
|
|
58
|
-
return this.locator.context
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
protected get locator() {
|
|
62
|
-
return this.params.locator
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
static override async paramsHandler<T extends ActorInstanceV3>(params: Partial<T['params']>) {
|
|
66
|
-
const baseParams = await super.paramsHandler({ ...params, name: params.name ?? 'UnknownActor' })
|
|
67
|
-
const account = assertEx(params.account, () => `params.account is required for actor ${baseParams.name}.`)
|
|
68
|
-
const locator = assertEx(params.locator, () => `params.locator is required for actor ${baseParams.name}.`)
|
|
69
|
-
return {
|
|
70
|
-
...baseParams, account, locator,
|
|
71
|
-
} satisfies ActorParamsV3
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* The timer runs until the actor is deactivated (or you manually stop it).
|
|
76
|
-
*/
|
|
77
|
-
registerTimer(timerName: string, callback: () => Promise<void>, dueTimeMs: number, periodMs: number) {
|
|
78
|
-
if (this.status !== 'starting') {
|
|
79
|
-
this.logger?.warn(
|
|
80
|
-
`Cannot register timer '${timerName}' because actor is not starting.`,
|
|
81
|
-
)
|
|
82
|
-
return
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
let running = false
|
|
86
|
-
|
|
87
|
-
this._semaphores.set(timerName, new Semaphore(1))
|
|
88
|
-
|
|
89
|
-
const timeoutId = setTimeout(() => {
|
|
90
|
-
const intervalId = setInterval(() => {
|
|
91
|
-
const semaphore = this._semaphores.get(timerName)
|
|
92
|
-
if (this.status !== 'started' || !this._intervals.has(timerName) || !semaphore || running) return
|
|
93
|
-
if (semaphore.isLocked()) {
|
|
94
|
-
this.logger?.warn(
|
|
95
|
-
`Skipping timer '${this.name}:${timerName}' execution because previous execution is still running.`,
|
|
96
|
-
)
|
|
97
|
-
return
|
|
98
|
-
}
|
|
99
|
-
semaphore.acquire().then(([, release]) => {
|
|
100
|
-
const startTime = Date.now()
|
|
101
|
-
running = true
|
|
102
|
-
callback()
|
|
103
|
-
.then(() => {
|
|
104
|
-
const duration = Date.now() - startTime
|
|
105
|
-
if (duration > periodMs) {
|
|
106
|
-
this.logger?.warn(
|
|
107
|
-
`Timer '${this.name}:${timerName}' execution took longer (${duration}ms) than the period (${periodMs}ms).`,
|
|
108
|
-
)
|
|
109
|
-
} else if (duration > 5000) {
|
|
110
|
-
this.logger?.warn(
|
|
111
|
-
`Timer '${this.name}:${timerName}' execution took longer (${duration}ms) than 5000ms.`,
|
|
112
|
-
)
|
|
113
|
-
}
|
|
114
|
-
})
|
|
115
|
-
.catch((error) => {
|
|
116
|
-
this.logger?.error(`Error in timer '${this.name}:${timerName}': ${error}`)
|
|
117
|
-
this.logger?.error(error.stack)
|
|
118
|
-
})
|
|
119
|
-
.finally(() => {
|
|
120
|
-
release()
|
|
121
|
-
running = false
|
|
122
|
-
})
|
|
123
|
-
}).catch((error) => {
|
|
124
|
-
this.logger?.error(`Error acquiring semaphore for timer '${this.name}:${timerName}': ${error}`)
|
|
125
|
-
})
|
|
126
|
-
}, periodMs)
|
|
127
|
-
|
|
128
|
-
// store interval so we can clear it on stop()
|
|
129
|
-
this._intervals.set(timerName, intervalId)
|
|
130
|
-
}, dueTimeMs)
|
|
131
|
-
|
|
132
|
-
// store timeout so we can clear it on stop() if interval hasn't started yet
|
|
133
|
-
this._timeouts.set(timerName, timeoutId)
|
|
134
|
-
|
|
135
|
-
this.logger?.log(
|
|
136
|
-
`Timer '${this.name}:${timerName}' registered: first call after ${dueTimeMs}ms, recurring every ${periodMs}ms.`,
|
|
137
|
-
)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Called by the Orchestrator when the actor is deactivated.
|
|
142
|
-
* Stop all running timers.
|
|
143
|
-
*/
|
|
144
|
-
override async stopHandler() {
|
|
145
|
-
await super.stopHandler()
|
|
146
|
-
this.logger?.log('Stopping all timers...')
|
|
147
|
-
|
|
148
|
-
// wait for all semaphores to be free and acquire them to prevent new tasks from starting
|
|
149
|
-
await Promise.all(
|
|
150
|
-
[...this._semaphores.values()].map(async (semaphore) => {
|
|
151
|
-
// Wait for any running tasks to complete
|
|
152
|
-
while (semaphore.isLocked()) {
|
|
153
|
-
this.logger?.log('Waiting for running timer task to complete...')
|
|
154
|
-
await delay(500)
|
|
155
|
-
}
|
|
156
|
-
await semaphore.acquire()
|
|
157
|
-
}),
|
|
158
|
-
)
|
|
159
|
-
|
|
160
|
-
this._semaphores.clear()
|
|
161
|
-
|
|
162
|
-
for (const [, timeoutRef] of this._timeouts.entries()) {
|
|
163
|
-
clearTimeout(timeoutRef)
|
|
164
|
-
}
|
|
165
|
-
this._timeouts.clear()
|
|
166
|
-
|
|
167
|
-
for (const [, intervalRef] of this._intervals.entries()) {
|
|
168
|
-
clearInterval(intervalRef)
|
|
169
|
-
}
|
|
170
|
-
this._intervals.clear()
|
|
171
|
-
|
|
172
|
-
this.logger?.log('Stopped.')
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/** Actor implementation. */
|
|
177
|
-
export abstract class Actor<TParams extends ActorParamsV3 = ActorParamsV3> extends ActorV3<TParams> {}
|
package/src/actor/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './ActorV3.ts'
|