UNPKG

@erc7824/nitrolite

Version:

The Nitrolite SDK empowers developers to build high-performance, scalable web3 applications using state channels. It's designed to provide near-instant transactions and significantly improved user experiences by minimizing direct blockchain interactions.

115 lines (114 loc) 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ledgerParamsParsers = exports.TransactionSchema = exports.txTypeEnum = exports.ledgerAccountSchema = void 0; const zod_1 = require("zod"); const viem_1 = require("viem"); const types_1 = require("../types"); const common_1 = require("./common"); const BalanceObjectSchema = zod_1.z .object({ asset: zod_1.z.string(), amount: common_1.decimalSchema, }) .transform((b) => b); const GetLedgerBalancesParamsSchema = zod_1.z .object({ ledger_balances: zod_1.z.array(BalanceObjectSchema), }) .transform((raw) => ({ ledgerBalances: raw.ledger_balances, })); exports.ledgerAccountSchema = zod_1.z .string() .refine((val) => (0, viem_1.isAddress)(val) || ((0, viem_1.isHex)(val) && val.length === 66), { message: 'Must be a valid EVM address or a 0x-prefixed 64-char hex string', }) .transform((v) => v); const LedgerEntryObjectSchema = zod_1.z .object({ id: zod_1.z.number(), account_id: exports.ledgerAccountSchema, account_type: zod_1.z.number(), asset: zod_1.z.string(), participant: common_1.addressSchema, credit: common_1.decimalSchema, debit: common_1.decimalSchema, created_at: common_1.dateSchema, }) .transform((e) => ({ id: e.id, accountId: e.account_id, accountType: e.account_type, asset: e.asset, participant: e.participant, credit: e.credit, debit: e.debit, createdAt: e.created_at, })); const GetLedgerEntriesParamsSchema = zod_1.z .object({ ledger_entries: zod_1.z.array(LedgerEntryObjectSchema), }) .transform((raw) => ({ ledgerEntries: raw.ledger_entries, })); exports.txTypeEnum = zod_1.z.nativeEnum(types_1.RPCTxType); exports.TransactionSchema = zod_1.z .object({ id: zod_1.z.number(), tx_type: exports.txTypeEnum, from_account: exports.ledgerAccountSchema, from_account_tag: zod_1.z.string().optional(), to_account: exports.ledgerAccountSchema, to_account_tag: zod_1.z.string().optional(), asset: zod_1.z.string(), amount: zod_1.z.string(), created_at: common_1.dateSchema, }) .transform((raw) => ({ id: raw.id, txType: raw.tx_type, fromAccount: raw.from_account, fromAccountTag: raw.from_account_tag, toAccount: raw.to_account, toAccountTag: raw.to_account_tag, asset: raw.asset, amount: raw.amount, createdAt: raw.created_at, })); const GetLedgerTransactionsParamsSchema = zod_1.z .object({ ledger_transactions: zod_1.z.array(exports.TransactionSchema), }) .transform((raw) => ({ ledgerTransactions: raw.ledger_transactions, })); const BalanceUpdateParamsSchema = zod_1.z .object({ balance_updates: zod_1.z.array(BalanceObjectSchema), }) .transform((raw) => ({ balanceUpdates: raw.balance_updates, })); const TransferParamsSchema = zod_1.z .object({ transactions: zod_1.z.array(exports.TransactionSchema), }) .transform((raw) => ({ transactions: raw.transactions, })); const TransferNotificationParamsSchema = zod_1.z .object({ transactions: zod_1.z.array(exports.TransactionSchema), }) .transform((raw) => ({ transactions: raw.transactions, })); exports.ledgerParamsParsers = { [types_1.RPCMethod.GetLedgerBalances]: (params) => GetLedgerBalancesParamsSchema.parse(params), [types_1.RPCMethod.GetLedgerEntries]: (params) => GetLedgerEntriesParamsSchema.parse(params), [types_1.RPCMethod.GetLedgerTransactions]: (params) => GetLedgerTransactionsParamsSchema.parse(params), [types_1.RPCMethod.BalanceUpdate]: (params) => BalanceUpdateParamsSchema.parse(params), [types_1.RPCMethod.Transfer]: (params) => TransferParamsSchema.parse(params), [types_1.RPCMethod.TransferNotification]: (params) => TransferNotificationParamsSchema.parse(params), };