import { providers } from "ethers" ; import { Address , Bytes32 } from "./basic" ; import { AppChallenge , ChallengeEventData , SignedCancelChallengeRequest , ChallengeEvents , } from "./contracts" ; import { IChannelSigner } from "./crypto" ; import { ILoggerService , ILogger } from "./logger" ; import { IOnchainTransactionService } from "./misc" ; import { ContractAddressBook } from "./node" ; import { IStoreService } from "./store" ; export type WatcherInitOptions = { signer : IChannelSigner | string ; providers : { [ chainId ] : providers . JsonRpcProvider | string ; } ; context : ContractAddressBook ; store : IStoreService ; logger ? : ILoggerService | ILogger ; transactionService ? : IOnchainTransactionService ; } ; type BaseChallengeTransactionCompletedEvent = { transaction : providers . TransactionReceipt ; appInstanceId : Bytes32 ; multisigAddress : Address ; } ; type BaseChallengeTransactionFailedEvent = { appInstanceId : Bytes32 ; error : string ; multisigAddress : Address ; challenge : StoredAppChallenge | undefined ; params : any ; } ; export const CHALLENGE_PROGRESSED_EVENT = "CHALLENGE_PROGRESSED_EVENT" ; export type ChallengeProgressedEventData = BaseChallengeTransactionCompletedEvent ; export const CHALLENGE_PROGRESSION_FAILED_EVENT = "CHALLENGE_PROGRESSION_FAILED_EVENT" ; export type ChallengeProgressionFailedEventData = BaseChallengeTransactionFailedEvent ; export const CHALLENGE_COMPLETED_EVENT = "CHALLENGE_COMPLETED_EVENT" ; export type ChallengeCompletedEventData = BaseChallengeTransactionCompletedEvent ; export const CHALLENGE_COMPLETION_FAILED_EVENT = "CHALLENGE_COMPLETION_FAILED_EVENT" ; export type ChallengeCompletionFailedEventData = BaseChallengeTransactionFailedEvent ; export const CHALLENGE_OUTCOME_SET_EVENT = "CHALLENGE_OUTCOME_SET_EVENT" ; export type ChallengeOutcomeSetEventData = BaseChallengeTransactionCompletedEvent ; export const CHALLENGE_OUTCOME_FAILED_EVENT = "CHALLENGE_OUTCOME_FAILED_EVENT" ; export type ChallengeOutcomeFailedEventData = BaseChallengeTransactionFailedEvent ; export const CHALLENGE_CANCELLED_EVENT = "CHALLENGE_CANCELLED_EVENT" ; export type ChallengeCancelledEventData = BaseChallengeTransactionCompletedEvent ; export const CHALLENGE_CANCELLATION_FAILED_EVENT = "CHALLENGE_CANCELLATION_FAILED_EVENT" ; export type ChallengeCancellationFailedEventData = BaseChallengeTransactionFailedEvent ; export const CHALLENGE_UPDATED_EVENT = "CHALLENGE_UPDATED_EVENT" ; export type ChallengeUpdatedEventData = ChallengeEventData [ typeof ChallengeEvents . ChallengeUpdated ] ; export const STATE_PROGRESSED_EVENT = "STATE_PROGRESSED_EVENT" ; export type StateProgressedEventData = ChallengeEventData [ typeof ChallengeEvents . StateProgressed ] ; export const WatcherEvents = { [ CHALLENGE_UPDATED_EVENT ] : CHALLENGE_UPDATED_EVENT , [ STATE_PROGRESSED_EVENT ] : STATE_PROGRESSED_EVENT , [ CHALLENGE_PROGRESSED_EVENT ] : CHALLENGE_PROGRESSED_EVENT , [ CHALLENGE_PROGRESSION_FAILED_EVENT ] : CHALLENGE_PROGRESSION_FAILED_EVENT , [ CHALLENGE_OUTCOME_SET_EVENT ] : CHALLENGE_OUTCOME_SET_EVENT , [ CHALLENGE_OUTCOME_FAILED_EVENT ] : CHALLENGE_OUTCOME_FAILED_EVENT , [ CHALLENGE_COMPLETED_EVENT ] : CHALLENGE_COMPLETED_EVENT , [ CHALLENGE_COMPLETION_FAILED_EVENT ] : CHALLENGE_COMPLETION_FAILED_EVENT , [ CHALLENGE_CANCELLED_EVENT ] : CHALLENGE_CANCELLED_EVENT , [ CHALLENGE_CANCELLATION_FAILED_EVENT ] : CHALLENGE_CANCELLATION_FAILED_EVENT , } as const ; export type WatcherEvent = keyof typeof WatcherEvents ; export interface WatcherEventDataMap { [ CHALLENGE_UPDATED_EVENT ] : ChallengeUpdatedEventData ; [ STATE_PROGRESSED_EVENT ] : StateProgressedEventData ; [ CHALLENGE_PROGRESSED_EVENT ] : ChallengeProgressedEventData ; [ CHALLENGE_PROGRESSION_FAILED_EVENT ] : ChallengeProgressionFailedEventData ; [ CHALLENGE_OUTCOME_FAILED_EVENT ] : ChallengeOutcomeFailedEventData ; [ CHALLENGE_OUTCOME_SET_EVENT ] : ChallengeOutcomeSetEventData ; [ CHALLENGE_COMPLETED_EVENT ] : ChallengeCompletedEventData ; [ CHALLENGE_COMPLETION_FAILED_EVENT ] : ChallengeCompletionFailedEventData ; [ CHALLENGE_CANCELLED_EVENT ] : ChallengeCancelledEventData ; [ CHALLENGE_CANCELLATION_FAILED_EVENT ] : ChallengeCancellationFailedEventData ; } export type WatcherEventData = { [ P in keyof WatcherEventDataMap ] : WatcherEventDataMap [ P ] ; } ; export type ChallengeInitiatedResponse = { freeBalanceChallenge : providers . TransactionResponse ; appChallenge : providers . TransactionResponse ; } ; export interface IWatcher { emit < T extends WatcherEvent > ( event , data ) : void ; on < T extends WatcherEvent > ( event , callback , filter ? ) : void ; once < T extends WatcherEvent > ( event , callback , filter ? ) : void ; waitFor < T extends WatcherEvent > ( event , timeout , filter ? ) : Promise < WatcherEventData [ T ] > ; off ( ) : void ; enable ( ) : Promise < void > ; disable ( ) : Promise < void > ; initiate ( appIdentityHash ) : Promise < ChallengeInitiatedResponse > ; cancel ( appIdentityHash , req ) : Promise < providers . TransactionResponse > ; } export enum StoredAppChallengeStatus { NO_CHALLENGE = 0 , IN_DISPUTE = 1 , IN_ONCHAIN_PROGRESSION = 2 , EXPLICITLY_FINALIZED = 3 , OUTCOME_SET = 4 , CONDITIONAL_SENT = 5 , PENDING_TRANSITION = 6 } export type StoredAppChallenge = Omit < AppChallenge , "status" > & { identityHash : Bytes32 ; status : StoredAppChallengeStatus ; chainId : number ; } ;