Last active
June 25, 2021 14:39
-
-
Save ilyavf/37fe907615e9984c43a5fcf02b6d58ea to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import { createMachine, assign, interpret } from 'xstate'; | |
// const Machine = createMachine | |
const machine = Machine({ | |
id: 'lnd', | |
initial: 'idle', | |
context: { | |
isNewWallet: true, | |
fundingAddress: null, | |
balance: 0, | |
isConnected: false, | |
channels: { | |
active: [], | |
pending: [] | |
}, | |
password: null, | |
seed: null, | |
identity_pubkey: null, | |
latestError: '', | |
invoice: null | |
}, | |
states: { | |
idle: { | |
on: { | |
WW_LOADED_UTILS: 'loading' | |
} | |
}, | |
loading: { | |
initial: 'ww-utils', | |
states: { | |
'ww-utils': { on: { WW_LOADED_LND: 'lnd-files' } }, | |
'lnd-files': { on: { LND_UNLOCKER_READY: '#lnd.locked' } } | |
}, | |
on: { | |
LND_RUN_FINISHED: { | |
target: 'corrupted', | |
actions: ['setPeerConnection'] | |
} | |
} | |
}, | |
// startLnd: { | |
// invoke: { | |
// src: 'startLnd' | |
// }, | |
// on: { LND_UNLOCKER_READY: '#lnd.locked' } | |
// }, | |
locked: { | |
initial: 'unlockerReady', | |
states: { | |
unlockerReady: { | |
on: { | |
GEN_SEED: { actions: 'setPassword', target: 'pendingSeed' }, | |
INIT: { | |
target: 'pendingInit', | |
actions: 'setWalletNew' | |
}, | |
UNLOCK: { | |
target: 'pendingUnlock', | |
actions: 'setWalletExisting' | |
} | |
} | |
}, | |
pendingSeed: { | |
entry: ['resetLatestError'], | |
invoke: { | |
src: 'genSeed', | |
onDone: { | |
actions: 'setSeed', | |
target: 'pendingInit' | |
}, | |
onError: { | |
actions: 'setLatestError', | |
target: 'unlockerReady' | |
} | |
} | |
}, | |
pendingInit: { | |
entry: ['resetLatestError'], | |
invoke: { | |
src: 'initWallet', | |
onDone: '#lnd.unlocked', | |
onError: { | |
actions: 'setLatestError', | |
target: 'unlockerReady' | |
} | |
} | |
}, | |
pendingUnlock: { | |
entry: ['resetLatestError'], | |
invoke: { | |
src: 'unlockWallet', | |
onDone: '#lnd.unlocked', | |
onError: { | |
target: 'unlockerReady', | |
actions: ['setLatestError', 'setWalletNew'] | |
} | |
} | |
} | |
}, | |
on: { | |
LND_RUN_FINISHED: { | |
target: 'corrupted' | |
} | |
} | |
}, | |
corrupted: { | |
entry: ['logCorrupted'], | |
on: { | |
RESTORE_FROM_FOLDER: 'restoreFromFolder' | |
} | |
}, | |
restoreFromFolder: { | |
entry: ['restoreFromFolder'], | |
after: { | |
5000: 'reloadBrowser' | |
} | |
}, | |
reloadBrowser: { | |
// entry: ['reloadBrowser'], | |
type: 'final' | |
}, | |
unlocked: { | |
initial: 'initializing', | |
states: { | |
initializing: { | |
on: { | |
LND_REST_READY: [ | |
{ cond: 'isNewWallet', target: 'genNewAddress' }, | |
{ target: 'chainSyncing' } | |
] | |
} | |
}, | |
genNewAddress: { | |
invoke: { | |
src: 'generateNewAddress', | |
onDone: { | |
actions: 'setFundingAddress', | |
target: 'chainSyncing' | |
}, | |
onFailed: { actions: 'logError', target: 'chainSyncing' } | |
} | |
}, | |
chainSyncing: { | |
invoke: { src: 'checkChainSync' }, | |
on: { | |
LND_SYNCED: 'synced' | |
} | |
}, | |
synced: { | |
type: 'parallel', | |
states: { | |
subscribeToEvents: { | |
invoke: { src: 'subscribeLndEvents' } | |
}, | |
getBalance: { | |
initial: 'pending', | |
states: { | |
idle: { | |
on: { GET_BALANCE: 'pending' } | |
}, | |
pending: { | |
invoke: { | |
src: 'getBalance', | |
onDone: { actions: 'setBalance', target: 'idle' }, | |
onError: { actions: 'logError', target: 'idle' } | |
} | |
} | |
} | |
}, | |
getChannels: { | |
initial: 'pending', | |
states: { | |
idle: { | |
on: { GET_CHANNELS: 'pending' } | |
}, | |
pending: { | |
invoke: { | |
src: 'getChannels', | |
onDone: { actions: 'setChannels', target: 'idle' }, | |
onError: { actions: 'logError', target: 'idle' } | |
} | |
} | |
} | |
}, | |
getInvoices: { | |
initial: 'pending', | |
states: { | |
idle: { | |
on: { GET_INVOICES: 'pending' } | |
}, | |
pending: { | |
invoke: { | |
src: 'getInvoices', | |
onDone: { actions: 'setInvoices', target: 'idle' }, | |
onError: { actions: 'logError', target: 'idle' } | |
} | |
} | |
} | |
}, | |
getPayments: { | |
initial: 'pending', | |
states: { | |
idle: { | |
on: { GET_PAYMENTS: 'pending' } | |
}, | |
pending: { | |
invoke: { | |
src: 'getPayments', | |
onDone: { actions: 'setPayments', target: 'idle' }, | |
onError: { actions: 'logError', target: 'idle' } | |
} | |
} | |
} | |
}, | |
connectPeer: { | |
initial: 'pending', | |
states: { | |
idle: { | |
on: { | |
SET_PEER_CONN: { | |
cond: 'peerConnectionOff', | |
target: 'pending' | |
} | |
} | |
}, | |
pending: { | |
invoke: { | |
src: 'connectPeer', | |
onDone: { | |
actions: ['setPeerConnectionOn'], | |
target: 'idle' | |
}, | |
onError: { | |
actions: ['setPeerConnectionOff'], | |
target: 'failed' | |
} | |
} | |
}, | |
failed: { | |
after: { 15000: 'pending' } | |
} | |
} | |
}, | |
createInvoice: { | |
initial: 'idle', | |
states: { | |
idle: { | |
on: { CREATE_INVOICE: 'pending' } | |
}, | |
pending: { | |
entry: ['pendingInvoice'], | |
invoke: { | |
src: 'createInvoice', | |
onDone: { actions: 'setInvoice', target: 'checkChannels' }, | |
onError: { actions: ['logError', 'setInvoiceError'], target: 'failed' } | |
} | |
}, | |
checkChannels: { | |
invoke: { | |
src: 'checkChannelActive', | |
onDone: [{ | |
cond: 'isFalse', | |
target: 'reconnectPeer' | |
}, { | |
target: 'idle' | |
}], | |
onError: { target: 'idle' } | |
} | |
}, | |
reconnectPeer: { | |
invoke: { | |
src: 'reconnectPeer', | |
onDone: 'idle', | |
onError: 'idle' | |
} | |
}, | |
failed: { | |
on: { CLEAR_INVOICE: 'idle' } | |
} | |
}, | |
on: { | |
CREATE_INVOICE: { | |
actions: ['clearInvoice'], | |
target: '.pending' | |
}, | |
CLEAR_INVOICE: { actions: 'clearInvoice' }, | |
SET_INVOICE: { actions: 'setInvoice' } | |
} | |
} | |
}, | |
on: { | |
SET_PEER_CONN: [{ | |
actions: ['setPeerConnection'] | |
}], | |
SET_CHANNELS: { | |
cond: 'channelsChanged', | |
actions: ['setChannels'] | |
}, | |
SET_BALANCE: { | |
cond: 'balanceChanged', | |
actions: ['setBalance'] | |
}, | |
SHUTDOWN_AND_BACKUP: { | |
target: '#lnd.backup' | |
} | |
} | |
} | |
}, | |
on: { | |
LND_RUN_FINISHED: { | |
target: 'finished', | |
actions: ['setPeerConnection'] | |
}, | |
LND_SHUTDOWN_COMPLETE: { | |
target: 'finished', | |
actions: ['setPeerConnection'] | |
} | |
} | |
}, | |
backup: { | |
initial: 'shutdown', | |
states: { | |
shutdown: { | |
entry: 'shutdown', | |
on: { | |
LND_RUN_FINISHED: 'backupFolder' | |
} | |
}, | |
backupFolder: { | |
invoke: { | |
src: 'lndBackup', | |
onDone: { | |
target: '#lnd.finished' | |
} | |
} | |
} | |
} | |
}, | |
finished: { | |
type: 'final' | |
} | |
} | |
}, { | |
guards: { | |
peerConnectionOff: (context, event) => { | |
return !event.isConnected | |
}, | |
channelsChanged: (context, event) => { | |
return JSON.stringify(context.channels) !== JSON.stringify(event.channels) | |
}, | |
balanceChanged: (context, event) => { | |
return context.balance !== event.balance | |
}, | |
isNewWallet: (context, event) => { | |
return context.isNewWallet | |
}, | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment