Skip to content

Commit

Permalink
refactor(node): simplify code for prepare packet accounting
Browse files Browse the repository at this point in the history
  • Loading branch information
justmoon committed Nov 21, 2023
1 parent 314699b commit 12976cb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
import { IlpPreparePacket } from "../../ilp-connector/schemas/ilp-packet-codec"
import { CreateTransferParameters, Ledger } from "../stores/ledger"
import { CreateTransferParameters } from "../stores/ledger"
import { getLedgerIdFromPath } from "./get-ledger-id-from-path"

export const applyPacketPrepareToLedger = (
ledger: Ledger,
accountPath: string,
packet: IlpPreparePacket,
direction: "incoming" | "outgoing",
sourceAccountPath: string,
destinationAccountPath: string,
amount: bigint,
) => {
const ledgerId = getLedgerIdFromPath(accountPath)
const connectorPath = `${ledgerId}/internal/connector`
const transfers = []

const transfer: CreateTransferParameters = {
debitAccountPath: direction === "incoming" ? accountPath : connectorPath,
creditAccountPath: direction === "incoming" ? connectorPath : accountPath,
amount: packet.amount,
pending: true,
{
const ledgerId = getLedgerIdFromPath(sourceAccountPath)
const connectorPath = `${ledgerId}/internal/connector`

const transfer: CreateTransferParameters = {
debitAccountPath: sourceAccountPath,
creditAccountPath: connectorPath,
amount,
pending: true,
}

transfers.push(transfer)
}
{
const ledgerId = getLedgerIdFromPath(destinationAccountPath)
const connectorPath = `${ledgerId}/internal/connector`

const transfer: CreateTransferParameters = {
debitAccountPath: connectorPath,
creditAccountPath: destinationAccountPath,
amount,
pending: true,
}

transfers.push(transfer)
}

return ledger.createTransfer(transfer)
return transfers
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,82 +90,37 @@ export const ProcessPreparePacket = (reactor: Reactor) => {
return
}

{
const result = applyPacketPrepareToLedger(
ledgerStore,
sourceEndpointInfo.accountPath,
parsedPacket,
"incoming",
)
const transfers = applyPacketPrepareToLedger(
sourceEndpointInfo.accountPath,
destinationEndpointInfo.accountPath,
parsedPacket.amount,
)

for (const transfer of transfers) {
const result = ledgerStore.createTransfer(transfer)
if (isFailure(result)) {
switch (result.name) {
case "InvalidAccountFailure": {
triggerRejection({
requestId: outgoingRequestId,
errorCode: IlpErrorCode.T00_INTERNAL_ERROR,
message: "Missing internal ledger account for inbound transfer",
message: "Missing internal ledger account",
})
return
}
case "ExceedsCreditsFailure": {
triggerRejection({
requestId: outgoingRequestId,
errorCode: IlpErrorCode.T04_INSUFFICIENT_LIQUIDITY,
message:
"Insufficient liquidity (connector account credit limit exceeded)",
message: "Insufficient liquidity",
})
return
}
case "ExceedsDebitsFailure": {
triggerRejection({
requestId: outgoingRequestId,
errorCode: IlpErrorCode.T04_INSUFFICIENT_LIQUIDITY,
message:
"Insufficient liquidity (source account debit limit exceeded)",
})
return
}
default: {
throw new UnreachableCaseError(result)
}
}
}
pendingTransfers.push(result)
}

{
const result = applyPacketPrepareToLedger(
ledgerStore,
destinationEndpointInfo.accountPath,
parsedPacket,
"outgoing",
)
if (isFailure(result)) {
switch (result.name) {
case "InvalidAccountFailure": {
triggerRejection({
requestId: outgoingRequestId,
errorCode: IlpErrorCode.T00_INTERNAL_ERROR,
message:
"Missing internal ledger account for outbound transfer",
})
return
}
case "ExceedsCreditsFailure": {
triggerRejection({
requestId: outgoingRequestId,
errorCode: IlpErrorCode.T04_INSUFFICIENT_LIQUIDITY,
message:
"Insufficient liquidity (destination account credit limit exceeded)",
})
return
}
case "ExceedsDebitsFailure": {
triggerRejection({
requestId: outgoingRequestId,
errorCode: IlpErrorCode.T04_INSUFFICIENT_LIQUIDITY,
message:
"Insufficient liquidity (connector account debit limit exceeded)",
message: "Insufficient liquidity",
})
return
}
Expand Down

0 comments on commit 12976cb

Please sign in to comment.