Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | 4x 94x 95x 94x 4x 4x 8x 8x 8x 8x 8x 8x 8x 8x 8x 9x 9x 7x 8x 4x 10x 4x 4x 4x | import {
Book,
DLQStatus,
Metric,
SourceConstraint,
Tr8ASTType,
Tx,
} from "./ast.js";
import {
AbsoluteFixedValue,
Amount,
Fraction,
RelativeValue,
} from "./value.js";
interface LedgerEvent {
// TR8Script ID
scriptId: string;
// subId of the TR8Script after broken down per source book
subId: string;
type: Tr8ASTType;
semaphore: Record<string, number>;
metrics: Metric[];
txs: (LedgerEventAbsRefTx | LedgerEventRelRefTx)[];
typeTxs?: "abs" | "rel";
metadata: string;
partial_notif_id?: string;
}
interface LedgerEventDlq {
// TR8Script ID
scriptId: string;
// subId of the TR8Script after broken down per source book
subId: string;
type: Tr8ASTType;
semaphore: Record<string, number>;
metrics: Metric[];
txs: (LedgerEventAbsRefTx | LedgerEventRelRefTx)[];
typeTxs?: "abs" | "rel";
metadata: string;
error: string;
status?: DLQStatus;
nextRetryAt?: Date;
retries?: number;
partial_notif_id?: string;
}
interface LedgerEventAbsRefTxSource {
book: Book;
account: string;
constraints: SourceConstraint[];
extern: boolean;
allocationPolicy: {
amount: Amount;
policy: "max" | "split" | "remaining";
};
as?: string;
mark?: string;
}
interface LedgerEventAbsRefTxDestination {
book: Book;
account: string;
allocationPolicy: {
amount: Amount;
policy: "split" | "remaining";
};
as?: string;
mark?: string;
}
interface LedgerEventAbsRefTx {
id: string;
asset: string;
refValue: AbsoluteFixedValue;
sources: LedgerEventAbsRefTxSource[];
destinations: LedgerEventAbsRefTxDestination[];
}
interface LedgerEventRelRefTxSource {
book: Book;
account: string;
constraints: SourceConstraint[];
extern: boolean;
allocationPolicy: {
amount: Fraction;
policy: "max" | "split" | "remaining";
};
as?: string;
mark?: string;
}
interface LedgerEventRelRefTxDestination {
book: Book;
account: string;
allocationPolicy: {
amount: Fraction;
policy: "split" | "remaining";
};
as?: string;
mark?: string;
}
/**
* `Fraction` denotes a fraction i.e. numerator and denominator
*
* All the fractional amounts i.e. Fraction here are assumed to have the same denominator
*/
interface LedgerEventRelRefTx {
id: string;
asset: string;
refValue: RelativeValue;
use?: string;
sources: LedgerEventRelRefTxSource[];
destinations: LedgerEventRelRefTxDestination[];
}
const getSourceBookForTx = (
tx: Tx | LedgerEventAbsRefTx | LedgerEventRelRefTx,
): Book => {
for (const source of tx.sources) {
if (source.book.type === "normal" && !source.extern) {
return source.book;
}
}
return tx.sources[0]!.book;
};
const getSourceBookForTxToValidateTransactionTime = (
tx: Tx | LedgerEventAbsRefTx | LedgerEventRelRefTx,
): Book => {
for (const source of tx.sources) {
if (source.book.type === "normal") {
return source.book;
}
}
for (const source of tx.destinations) {
if (source.book.type === "normal") {
return source.book;
}
}
return tx.sources[0]!.book;
};
const extractAccountsFromLedgerEvent = (
event: LedgerEvent,
book: string,
): string[] => {
const accounts: Set<string> = new Set();
for (const tx of event.txs) {
for (const source of tx.sources) {
Iif (source.book.type === "world") {
continue;
}
const {
book: { book: b },
account,
} = source;
Eif (b === book) {
accounts.add(account);
}
for (const c of source.constraints) {
if (c.amount !== "unbounded" && c.amount.type === "relative") {
const {
book: { book: b },
account,
} = c.amount;
if (b === book) {
accounts.add(account);
}
}
}
}
for (const dest of tx.destinations) {
const {
book: { book: b },
account,
} = dest;
if (b === book) {
accounts.add(account);
}
}
}
return [...accounts];
};
const isRelativeTx = (
tx: LedgerEventAbsRefTx | LedgerEventRelRefTx,
): tx is LedgerEventRelRefTx => {
return tx.refValue.type === "relative";
};
const isAbsoluteTx = (
tx: LedgerEventAbsRefTx | LedgerEventRelRefTx,
): tx is LedgerEventAbsRefTx => {
return tx.refValue.type === "absolute";
};
/**
* Extracts the user book ID (non-world book) from LedgerEvent transactions.
* This is used as Kafka key to ensure all events for a transaction go to the same partition.
*/
const getUserBookIdForKafkaKey = (
txs: (LedgerEventAbsRefTx | LedgerEventRelRefTx)[],
): string => {
for (const tx of txs) {
// Check sources for user book
for (const source of tx.sources) {
if (source.book.type === "normal") {
return source.book.book;
}
}
// Check destinations for user book
for (const dest of tx.destinations) {
if (dest.book.type === "normal") {
return dest.book.book;
}
}
}
// Fallback to first source book if no user book found
return txs[0]?.sources[0]?.book.book ?? "unknown";
};
export type {
LedgerEvent,
LedgerEventDlq,
LedgerEventAbsRefTx,
LedgerEventRelRefTx,
LedgerEventRelRefTxSource,
LedgerEventRelRefTxDestination,
LedgerEventAbsRefTxDestination,
LedgerEventAbsRefTxSource,
};
export {
getSourceBookForTx,
getSourceBookForTxToValidateTransactionTime,
extractAccountsFromLedgerEvent,
isRelativeTx,
isAbsoluteTx,
getUserBookIdForKafkaKey,
};
|