diff --git a/packages/backend/src/accounting/tigerbeetle/accounts.ts b/packages/backend/src/accounting/tigerbeetle/accounts.ts index e1658821a1..39e6ca299f 100644 --- a/packages/backend/src/accounting/tigerbeetle/accounts.ts +++ b/packages/backend/src/accounting/tigerbeetle/accounts.ts @@ -6,7 +6,7 @@ import { import { ServiceDependencies, TigerBeetleAccountCode } from './service' import { TigerbeetleCreateAccountError } from './errors' -import { AccountId, toTigerbeetleId, AccountUserData128 } from './utils' +import { AccountId, toTigerBeetleId, AccountUserData128 } from './utils' export interface CreateAccountOptions { id: AccountId @@ -22,7 +22,7 @@ export async function createAccounts( ): Promise { const errors = await deps.tigerBeetle.createAccounts( accounts.map((account) => ({ - id: toTigerbeetleId(account.id), + id: toTigerBeetleId(account.id), user_data_32: 0, user_data_64: 0n, user_data_128: 0n, @@ -56,7 +56,7 @@ export function flagsBasedOnAccountOptions( options.code === TigerBeetleAccountCode.SETTLEMENT ? AccountFlags.credits_must_not_exceed_debits : AccountFlags.debits_must_not_exceed_credits - if (options.linked) returnVal = returnVal | AccountFlags.linked + if (options.linked) returnVal |= AccountFlags.linked return returnVal } @@ -65,7 +65,7 @@ export async function getAccounts( accountIds: AccountId[] ): Promise { return await deps.tigerBeetle.lookupAccounts( - accountIds.map((id) => toTigerbeetleId(id)) + accountIds.map((id) => toTigerBeetleId(id)) ) } diff --git a/packages/backend/src/accounting/tigerbeetle/service.ts b/packages/backend/src/accounting/tigerbeetle/service.ts index b7ba227a2a..b8c32ab3f2 100644 --- a/packages/backend/src/accounting/tigerbeetle/service.ts +++ b/packages/backend/src/accounting/tigerbeetle/service.ts @@ -25,7 +25,7 @@ import { areAllAccountExistsErrors } from './errors' import { NewTransferOptions, createTransfers } from './transfers' -import { AccountUserData128, toTigerbeetleId } from './utils' +import { AccountUserData128, toTigerBeetleId } from './utils' export enum TigerBeetleAccountCode { LIQUIDITY_WEB_MONETIZATION = 1, @@ -134,7 +134,7 @@ export async function createSettlementAccount( ledger, code: TigerBeetleAccountCode.SETTLEMENT, linked: false, - userData128: toTigerbeetleId(accountId) + userData128: toTigerBeetleId(accountId) } ]) } catch (err) { @@ -216,7 +216,7 @@ export async function getAccountsTotalSent( const accounts = await getAccounts(deps, ids) return ids.map( (id) => - accounts.find((account) => account.id === toTigerbeetleId(id)) + accounts.find((account) => account.id === toTigerBeetleId(id)) ?.debits_posted ) } else return [] @@ -240,7 +240,7 @@ export async function getAccountsTotalReceived( const accounts = await getAccounts(deps, ids) return ids.map( (id) => - accounts.find((account) => account.id === toTigerbeetleId(id)) + accounts.find((account) => account.id === toTigerBeetleId(id)) ?.credits_posted ) } else return [] diff --git a/packages/backend/src/accounting/tigerbeetle/transfers.ts b/packages/backend/src/accounting/tigerbeetle/transfers.ts index d8fb28582d..5109945f85 100644 --- a/packages/backend/src/accounting/tigerbeetle/transfers.ts +++ b/packages/backend/src/accounting/tigerbeetle/transfers.ts @@ -8,7 +8,7 @@ import { TransferError } from '../errors' import { TigerbeetleCreateTransferError } from './errors' import { ServiceDependencies } from './service' -import { AccountId, toTigerbeetleId } from './utils' +import { AccountId, toTigerBeetleId } from './utils' const ACCOUNT_TYPE = 1 @@ -17,7 +17,13 @@ type TransfersError = { error: TransferError } -export interface NewTransferOptions { +export type TransferUserData128 = string | number | bigint + +interface TransferOptions { + userData128?: TransferUserData128 +} + +export interface NewTransferOptions extends TransferOptions { id: string | bigint sourceAccountId: AccountId destinationAccountId: AccountId @@ -28,13 +34,13 @@ export interface NewTransferOptions { voidId?: never } -export interface PostTransferOptions { +export interface PostTransferOptions extends TransferOptions { id?: never postId: string | bigint voidId?: never } -export interface VoidTransferOptions { +export interface VoidTransferOptions extends TransferOptions { id?: never postId?: never voidId: string | bigint @@ -74,33 +80,31 @@ export async function createTransfers( timestamp: 0n } if (isNewTransferOptions(transfer)) { - if (transfer.amount <= BigInt(0)) { - return { index: i, error: TransferError.InvalidAmount } - } - tbTransfer.id = toTigerbeetleId(transfer.id) + if (transfer.amount <= 0n) return { index: i, error: TransferError.InvalidAmount } + + tbTransfer.id = toTigerBeetleId(transfer.id) tbTransfer.amount = transfer.amount tbTransfer.ledger = transfer.ledger - tbTransfer.debit_account_id = toTigerbeetleId(transfer.sourceAccountId) - tbTransfer.credit_account_id = toTigerbeetleId( - transfer.destinationAccountId - ) + tbTransfer.debit_account_id = toTigerBeetleId(transfer.sourceAccountId) + tbTransfer.credit_account_id = toTigerBeetleId(transfer.destinationAccountId) if (transfer.timeout) { tbTransfer.flags |= TransferFlags.pending tbTransfer.timeout = transfer.timeout } } else { - tbTransfer.id = toTigerbeetleId(uuid()) + tbTransfer.id = toTigerBeetleId(uuid()) if (transfer.postId) { tbTransfer.flags |= TransferFlags.post_pending_transfer - tbTransfer.pending_id = toTigerbeetleId(transfer.postId) + tbTransfer.pending_id = toTigerBeetleId(transfer.postId) } else if (transfer.voidId) { tbTransfer.flags |= TransferFlags.void_pending_transfer - tbTransfer.pending_id = toTigerbeetleId(transfer.voidId) + tbTransfer.pending_id = toTigerBeetleId(transfer.voidId) } } - if (i < transfers.length - 1) { - tbTransfer.flags |= TransferFlags.linked - } + + if (transfer.userData128) tbTransfer.user_data_128 = toTigerBeetleId(transfer.userData128) + + if (i < transfers.length - 1) tbTransfer.flags |= TransferFlags.linked tbTransfers.push(tbTransfer) } const res = await deps.tigerBeetle.createTransfers(tbTransfers) diff --git a/packages/backend/src/accounting/tigerbeetle/utils.ts b/packages/backend/src/accounting/tigerbeetle/utils.ts index c2904845f1..1f6b8d2c77 100644 --- a/packages/backend/src/accounting/tigerbeetle/utils.ts +++ b/packages/backend/src/accounting/tigerbeetle/utils.ts @@ -3,7 +3,7 @@ import { validateId } from '../../shared/utils' export type AccountId = string | number | bigint export type AccountUserData128 = AccountId -export function toTigerbeetleId(id: AccountId): bigint { +export function toTigerBeetleId(id: AccountId): bigint { if (typeof id === 'number') { return BigInt(id) }