Skip to content

Instantly share code, notes, and snippets.

process.env.NODE_ENV = 'development';
require('../env');
import {
StateVariablesWithHash,
StateVariables,
} from '@statechannels/wallet-core';
import {
Machine,
MachineConfig,
send,
import { State, StateVariables } from '@statechannels/wallet-core';
import { Channel } from '../models/channel';
type PresentStage = 'PrefundSetup' | 'PostfundSetup' | 'Running' | 'Final';
type Stage = PresentStage | 'Missing';
export type ChannelState = Channel & { peer: string };
// stage is a property with a finite number of values, so it is easy to partition
const stage = (state?: State): Stage =>
const wireMachine = Machine({
initial: 'setup',
invoke: {src: 'watcher'},
states: {
setup: {invoke: {src: 'createPaymentChannel', onDone: 'uploading'}, exit: 'sendSpacerState'},
uploading: {
initial: 'unblocked',
on: {DONE: 'refunding'},
states: {
unblocked: {
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
const taskHandler = () => new Promise(resolve => setTimeout(() => {
console.log("Done task")
resolve()
}, 1000))
let taskCounter = 0
const taskManager = Machine({
context: {tasks: {}},
on: {
RUN_TASK: {
const guards = {};
const customActions = {};
const config = {
key: 'create-and-fund',
initial: 'preFundSetup',
on: {'xstate.error': {target: 'failure'}},
states: {
preFundSetup: {
initial: 'getPreFundSetup',
states: {
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
const guards = {
channelOpen: {name: 'channelOpen', predicate: () => true, type: 'xstate.guard'},
channelClosing: {name: 'channelClosing', predicate: () => true, type: 'xstate.guard'},
channelClosed: {name: 'channelClosed', predicate: () => true, type: 'xstate.guard'},
isDirectFunding: {name: 'isDirectFunding', predicate: () => true, type: 'xstate.guard'},
isLedgerFunding: {name: 'isLedgerFunding', predicate: () => true, type: 'xstate.guard'},
isVirtualFunding: {name: 'isVirtualFunding', predicate: () => true, type: 'xstate.guard'},
amCreator: {name: 'amCreator', predicate: () => true, type: 'xstate.guard'},
amJoiner: {name: 'amJoiner', predicate: () => true, type: 'xstate.guard'}
};
const guards = {
channelOpen: {name: 'channelOpen', predicate: () => true, type: 'xstate.guard'},
channelClosing: {name: 'channelClosing', predicate: () => true, type: 'xstate.guard'},
channelClosed: {name: 'channelClosed', predicate: () => true, type: 'xstate.guard'},
isDirectFunding: {name: 'isDirectFunding', predicate: () => true, type: 'xstate.guard'},
isLedgerFunding: {name: 'isLedgerFunding', predicate: () => true, type: 'xstate.guard'},
isVirtualFunding: {name: 'isVirtualFunding', predicate: () => true, type: 'xstate.guard'},
amCreator: {name: 'amCreator', predicate: () => true, type: 'xstate.guard'},
amJoiner: {name: 'amJoiner', predicate: () => true, type: 'xstate.guard'}
};