Skip to content

Commit

Permalink
feat(2756): add support for user_data_128.
Browse files Browse the repository at this point in the history
  • Loading branch information
koekiebox committed Jun 19, 2024
1 parent 35149c1 commit 83db4ea
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
8 changes: 4 additions & 4 deletions packages/backend/src/accounting/tigerbeetle/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +22,7 @@ export async function createAccounts(
): Promise<void> {
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,
Expand Down Expand Up @@ -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
}

Expand All @@ -65,7 +65,7 @@ export async function getAccounts(
accountIds: AccountId[]
): Promise<Account[]> {
return await deps.tigerBeetle.lookupAccounts(
accountIds.map((id) => toTigerbeetleId(id))
accountIds.map((id) => toTigerBeetleId(id))
)
}

Expand Down
8 changes: 4 additions & 4 deletions packages/backend/src/accounting/tigerbeetle/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -134,7 +134,7 @@ export async function createSettlementAccount(
ledger,
code: TigerBeetleAccountCode.SETTLEMENT,
linked: false,
userData128: toTigerbeetleId(accountId)
userData128: toTigerBeetleId(accountId)
}
])
} catch (err) {
Expand Down Expand Up @@ -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 []
Expand All @@ -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 []
Expand Down
40 changes: 22 additions & 18 deletions packages/backend/src/accounting/tigerbeetle/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/accounting/tigerbeetle/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 83db4ea

Please sign in to comment.