@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.
90 lines (89 loc) • 3.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ledgerParamsParsers = exports.TransactionSchema = exports.txTypeEnum = void 0;
const zod_1 = require("zod");
const types_1 = require("../types");
const common_1 = require("./common");
const GetLedgerBalancesParamsSchema = zod_1.z
.array(zod_1.z.array(zod_1.z.object({
asset: zod_1.z.string(),
amount: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).transform((a) => a.toString()),
})))
.refine((arr) => arr.length === 1)
.transform((arr) => arr[0])
.transform((arr) => arr);
const GetLedgerEntriesParamsSchema = zod_1.z
.array(zod_1.z.array(zod_1.z
.object({
id: zod_1.z.number(),
account_id: zod_1.z.string(),
account_type: zod_1.z.number(),
asset: zod_1.z.string(),
participant: common_1.addressSchema,
credit: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).transform((v) => v.toString()),
debit: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).transform((v) => v.toString()),
created_at: zod_1.z.union([zod_1.z.string(), zod_1.z.date()]).transform((v) => new Date(v)),
})
.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,
}))))
.refine((arr) => arr.length === 1)
.transform((arr) => arr[0])
.transform((arr) => arr);
exports.txTypeEnum = zod_1.z.nativeEnum(types_1.TxType);
exports.TransactionSchema = zod_1.z
.object({
id: zod_1.z.number(),
tx_type: exports.txTypeEnum,
from_account: common_1.addressSchema,
from_account_tag: zod_1.z.string().optional(),
to_account: common_1.addressSchema,
to_account_tag: zod_1.z.string().optional(),
asset: zod_1.z.string(),
amount: zod_1.z.string(),
created_at: zod_1.z.union([zod_1.z.string(), zod_1.z.date()]).transform((v) => new Date(v)),
})
.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
.array(zod_1.z.array(exports.TransactionSchema))
.refine((arr) => arr.length === 1)
.transform((arr) => arr[0]);
const BalanceUpdateParamsSchema = zod_1.z
.array(zod_1.z.array(zod_1.z
.object({ asset: zod_1.z.string(), amount: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).transform((a) => a.toString()) })
.transform((b) => ({ asset: b.asset, amount: b.amount }))))
.refine((arr) => arr.length === 1)
.transform((arr) => arr[0]);
const TransferParamsSchema = zod_1.z
.array(zod_1.z.array(exports.TransactionSchema))
.refine((arr) => arr.length === 1)
.transform((arr) => arr[0]);
const TransferNotificationParamsSchema = zod_1.z
.array(zod_1.z.array(exports.TransactionSchema))
.refine((arr) => arr.length === 1)
.transform((arr) => arr[0]);
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),
};