raiden-ts
Version:
Raiden Light Client Typescript/Javascript SDK
67 lines • 2.73 kB
JavaScript
import * as t from 'io-ts';
import { WithdrawConfirmation, WithdrawExpired, WithdrawRequest } from '../messages';
import { Address, Signed, UInt } from '../utils/types';
import { BalanceProof, ChannelUniqueKey, Lock } from './types';
export var ChannelState;
(function (ChannelState) {
ChannelState["open"] = "open";
ChannelState["closing"] = "closing";
ChannelState["closed"] = "closed";
ChannelState["settleable"] = "settleable";
ChannelState["settling"] = "settling";
ChannelState["settled"] = "settled";
})(ChannelState || (ChannelState = {}));
/**
* Contains info of each side of a channel
*/
const _ChannelEnd = t.readonly(t.type({
address: Address,
deposit: UInt(32),
withdraw: UInt(32),
locks: t.readonlyArray(Lock),
balanceProof: Signed(BalanceProof),
pendingWithdraws: t.readonlyArray(t.union([Signed(WithdrawRequest), Signed(WithdrawConfirmation), Signed(WithdrawExpired)])),
nextNonce: UInt(8), // usually balanceProof.nonce+1, but withdraw messages also increment it
}), 'ChannelEnd');
export const ChannelEnd = _ChannelEnd;
export const Channel = t.intersection([
// readonly needs to be applied to the individual types to allow tagged union narrowing
t.readonly(t.type({
_id: ChannelUniqueKey,
id: t.number,
token: Address,
tokenNetwork: Address,
isFirstParticipant: t.boolean,
openBlock: t.number,
own: ChannelEnd,
partner: ChannelEnd,
}), 'Channel'),
t.union([
/* union of types with literals intersection allows narrowing other props presence. e.g.:
* if (channel.state === ChannelState.open) {
* id = channel.id; // <- id can't be undefined
* closeBlock = channel.closeBlock; // error: closeBlock only exist on states closed|settling
* }
*/
t.readonly(t.type({
state: t.union([t.literal(ChannelState.open), t.literal(ChannelState.closing)]),
}), 'state[open|closing]'),
t.intersection([
t.readonly(t.type({
closeBlock: t.number,
closeParticipant: Address,
}), 'ChannelClosed'),
t.union([
t.readonly(t.type({
state: t.union([
t.literal(ChannelState.closed),
t.literal(ChannelState.settleable),
t.literal(ChannelState.settling),
]),
}), 'state[closed|settleable|settling]'),
t.readonly(t.type({ state: t.literal(ChannelState.settled), settleBlock: t.number }), 'state[settled]'),
]),
]),
]),
]);
//# sourceMappingURL=state.js.map