raiden-ts
Version:
Raiden Light Client Typescript/Javascript SDK
128 lines • 7.07 kB
JavaScript
/* eslint-disable @typescript-eslint/no-namespace */
import * as t from 'io-ts';
import { BalanceProof } from '../channels/types';
import { LockedTransfer, LockExpired, Processed, SecretRequest, SecretReveal, Unlock, WithdrawConfirmation, WithdrawExpired, WithdrawRequest, } from '../messages/types';
import { Fee, InputPaths, PFS } from '../services/types';
import { Via } from '../transport/types';
import { createAction, createAsyncAction } from '../utils/actions';
import { Address, Hash, Secret, Signed, UInt } from '../utils/types';
import { DirectionC, TransferState } from './state';
const TransferId = t.type({
secrethash: Hash,
direction: DirectionC,
});
/**
* A transfer async action set
*
* A transfer is considered as having succeeded from the time the secret is revealed to the target,
* as from there, target and mediators can claim the payment down to us. But the full off-chain
* happy case completes only after partner/neighbor acknowledges receiving the Unlock.
* So, we usually only emits this action in the end of the happy case, and it'll then contain the
* unlock's balanceProof, which indicates the full off-chain path succeeded.
* It'll be emitted without a balanceProof if something forces the transfer to complete
* (e.g. channel closed), the secret was revealed (so target was paid) but for any reason the
* unlock didn't happen yet.
*
* transfer.failure is emitted as soon as we know the transfer failed definitely, like when a
* RefundTransfer is received or the lock expires before revealing the secret. It notifies the user
* (e.g. pending Promises) that the transfer failed and won't be paid (eventually, locked amount
* will be recovered by expiring the lock).
*/
export const transfer = createAsyncAction(TransferId, 'transfer', t.intersection([
t.type({
tokenNetwork: Address,
target: Address,
value: UInt(32),
paymentId: UInt(8),
}),
t.union([
t.intersection([
t.type({ resolved: t.literal(false) }),
t.partial({ paths: InputPaths, pfs: t.union([PFS, t.null]), encryptSecret: t.boolean }),
]),
t.intersection([
t.type({
resolved: t.literal(true),
metadata: t.unknown,
fee: Fee,
partner: Address,
}),
Via,
]),
]),
t.partial({ secret: Secret, initiator: Address }),
t.union([t.partial({ expiration: t.number }), t.partial({ lockTimeout: t.number })]),
]), t.partial({ balanceProof: Signed(BalanceProof) }));
/** A LockedTransfer was signed and should be sent to partner */
export const transferSigned = createAction('transfer/signed', t.intersection([t.type({ message: Signed(LockedTransfer), fee: Fee, partner: Address }), Via]), TransferId);
/** Partner acknowledge they received and processed our LockedTransfer */
export const transferProcessed = createAction('transfer/processed', t.intersection([t.type({ message: Signed(Processed) }), Via]), TransferId);
/** Register a secret */
export const transferSecret = createAction('transfer/secret', t.type({ secret: Secret }), TransferId);
export const transferSecretRegister = createAsyncAction(TransferId, 'transfer/secret/register', t.type({ secret: Secret }), t.type({
secret: Secret,
txTimestamp: t.number,
txHash: Hash,
txBlock: t.number,
// ConfirmableAction
confirmed: t.union([t.undefined, t.boolean]),
}));
/** A valid SecretRequest received from target */
export const transferSecretRequest = createAction('transfer/secret/request', t.intersection([t.type({ message: Signed(SecretRequest) }), Via]), TransferId);
/** A SecretReveal sent to target */
export const transferSecretReveal = createAction('transfer/secret/reveal', t.intersection([t.type({ message: Signed(SecretReveal) }), Via]), TransferId);
export const transferUnlock = createAsyncAction(TransferId, 'transfer/unlock', t.union([t.undefined, Via]), t.intersection([t.type({ message: Signed(Unlock), partner: Address }), Via]));
/** Partner acknowledge they received and processed our Unlock */
export const transferUnlockProcessed = createAction('transfer/unlock/processed', t.intersection([t.type({ message: Signed(Processed) }), Via]), TransferId);
/**
* A request to expire a given transfer
*
* A transfer expiration request may fail for any reason
* e.g. user rejected sign prompt. It should eventually get prompted again, on a future newBlock
* action which sees this transfer should be expired but sent.expired didn't get set yet.
*/
export const transferExpire = createAsyncAction(TransferId, 'transfer/expire', undefined, t.type({ message: Signed(LockExpired), partner: Address }));
/** Partner acknowledge they received and processed our LockExpired */
export const transferExpireProcessed = createAction('transfer/expire/processed', t.intersection([t.type({ message: Signed(Processed) }), Via]), TransferId);
export const transferClear = createAction('transfer/clear', t.undefined, TransferId);
export const transferLoad = createAction('transfer/load', TransferState, TransferId);
// Withdraw actions
const WithdrawId = t.type({
direction: DirectionC,
tokenNetwork: Address,
partner: Address,
totalWithdraw: UInt(32),
expiration: t.number,
});
/**
* Request withdrawResolveEpic to resolve and emit a withdraw.request
*/
export const withdrawResolve = createAction('withdraw/resolve', t.union([t.undefined, t.type({ coopSettle: t.literal(true) })]), WithdrawId);
/**
* Start a withdraw
* - request: request to start a withdraw
* - success: withdraw finished on-chain
* - failure: something went wrong generating or processing a request
*/
export const withdraw = createAsyncAction(WithdrawId, 'withdraw', t.union([t.undefined, t.type({ coopSettle: t.boolean })]), t.type({ txHash: Hash, txBlock: t.number, confirmed: t.union([t.undefined, t.boolean]) }));
/**
* Withdraw messages going through:
* - request: WithdrawRequest sent or received
* - success: WithdrawConfirmation sent or received
* - failure: something went wrong processing WithdrawRequest or WithdrawConfirmation messages
*/
export const withdrawMessage = createAsyncAction(WithdrawId, 'withdraw/message', t.type({ message: Signed(WithdrawRequest) }), t.type({ message: Signed(WithdrawConfirmation) }));
/**
* Signals this withdraw is "busy" performing contract's transaction (either setTotalWithdraw
* or cooperativeSettle). Is "unlocked" by the respective withdraw.success|failure
*/
export const withdrawBusy = createAction('withdraw/busy', t.undefined, WithdrawId);
/**
* Expires a withdraw
* - request: request to expire a past request
* - success: WithdrawExpired sent or received
* - failure: something went wrong generating or processing a request
*/
export const withdrawExpire = createAsyncAction(WithdrawId, 'withdraw/expire', t.undefined, t.type({ message: Signed(WithdrawExpired) }));
export const withdrawCompleted = createAction('withdraw/completed', t.undefined, WithdrawId);
//# sourceMappingURL=actions.js.map