UNPKG

@renproject/rentx

Version:

XState Statemachines for tracking RenVM transactions reactively

172 lines (171 loc) 6.92 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.mintMachine = void 0; const xstate_1 = require("xstate"); const utils_1 = require("@renproject/utils"); const actions_1 = require("xstate/lib/actions"); exports.mintMachine = xstate_1.Machine({ id: "RenVMGatewaySession", initial: "restoring", states: { restoring: { entry: [ xstate_1.send("RESTORE"), xstate_1.assign({ depositMachines: (_ctx, _evt) => ({}) }), ], meta: { test: () => __awaiter(void 0, void 0, void 0, function* () { }) }, on: { RESTORE: [ { target: "completed", cond: "isExpired", }, { target: "listening", actions: "depositMachineSpawner", cond: "isCreated", }, { target: "creating", }, ], }, }, creating: { meta: { test: (_, state) => __awaiter(void 0, void 0, void 0, function* () { utils_1.assert(!state.context.tx.gatewayAddress ? true : false, "Gateway address should not be initialized"); }), }, invoke: { src: "txCreator", onDone: { target: "listening", actions: xstate_1.assign({ tx: (_context, evt) => (Object.assign({}, evt.data)), }), }, onError: { target: "srcInitializeError", actions: [ xstate_1.assign({ tx: (context, evt) => { const newTx = Object.assign(Object.assign({}, context.tx), { error: evt.data || true }); return newTx; }, }), actions_1.log((_ctx, evt) => evt.data), ], }, }, }, srcInitializeError: { meta: { test: (_, state) => __awaiter(void 0, void 0, void 0, function* () { utils_1.assert(state.context.tx.error ? true : false, "Error must exist"); }), }, }, listening: { meta: { test: (_, state) => __awaiter(void 0, void 0, void 0, function* () { utils_1.assert(state.context.tx.gatewayAddress ? true : false, "GatewayAddress must exist"); }), }, invoke: { src: "depositListener", }, on: { EXPIRED: "completed", CLAIMABLE: { actions: xstate_1.assign({ signatureRequest: (_context, evt) => { var _a; return (_a = evt.data) === null || _a === void 0 ? void 0 : _a.sourceTxHash; }, tx: (context, evt) => { var _a; if ((_a = evt.data) === null || _a === void 0 ? void 0 : _a.sourceTxHash) { context.tx.transactions[evt.data.sourceTxHash] = evt.data; } return context.tx; }, }), target: "requestingSignature", }, DEPOSIT_COMPLETED: { target: "completed", cond: "isCompleted", }, DEPOSIT_UPDATE: [ { actions: xstate_1.assign({ tx: (context, evt) => { var _a; if ((_a = evt.data) === null || _a === void 0 ? void 0 : _a.sourceTxHash) { context.tx.transactions[evt.data.sourceTxHash] = evt.data; } return context.tx; }, }), }, ], DEPOSIT: { actions: [ xstate_1.assign({ tx: (context, evt) => { var _a; if ((_a = evt.data) === null || _a === void 0 ? void 0 : _a.sourceTxHash) { context.tx.transactions[evt.data.sourceTxHash] = evt.data; } return context.tx; }, }), "spawnDepositMachine", ], }, }, }, requestingSignature: { on: { SIGN: { target: "listening", actions: xstate_1.send("CLAIM", { to: (ctx) => { if (!ctx.depositMachines) return ""; return ctx.depositMachines[ctx.signatureRequest || ""]; }, }), }, }, meta: { test: (_, state) => { if (Object.keys(state.context.tx.transactions || {}) .length === 0) { throw Error("A deposit must exist for a signature to be requested"); } }, }, }, completed: { meta: { test: (_, state) => { if (state.context.depositListenerRef) { throw Error("Deposit listener has not been cleaned up"); } }, }, }, }, });