type MemoApplicationFn = ( memoValue: string | number | undefined, memoType: string | undefined, currentTransaction: Record, ) => Record; const memoApplicationRegistry: Record = { solana: (memo, _type, transaction) => { const currentModel = (transaction.model as Record | undefined) || {}; const currentUiState = (currentModel.uiState as Record | undefined) || {}; return { model: { ...currentModel, uiState: { ...currentUiState, memo, }, }, }; }, casper: memo => ({ transferId: memo }), xrp: memo => { if (typeof memo === "number") return { tag: memo }; if (typeof memo === "string") return { tag: Number(memo) }; return { tag: undefined }; }, stellar: (memo, type) => ({ memoValue: memo, memoType: type }), ton: (memo, _type, transaction) => { const currentComment = (transaction.comment as Record | undefined) || {}; return { comment: { ...currentComment, text: memo, }, }; }, }; export function applyMemoToTransaction( family: string, memoValue: string | number | undefined, memoTypeOrTransaction?: string | Record | null, currentTransaction?: Record, ): Record { const isRecord = (value: unknown): value is Record => typeof value === "object" && value !== null; const memoType = memoTypeOrTransaction === undefined || typeof memoTypeOrTransaction === "string" ? memoTypeOrTransaction : undefined; const transaction = isRecord(memoTypeOrTransaction) ? memoTypeOrTransaction : currentTransaction ?? {}; const applyFn = memoApplicationRegistry[family]; if (!applyFn) { return { memo: memoValue }; } return applyFn(memoValue, memoType, transaction); }