Skip to content

Commit

Permalink
feat(2756): add support for history.
Browse files Browse the repository at this point in the history
  • Loading branch information
koekiebox committed Jun 19, 2024
1 parent 83db4ea commit 917aae8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
15 changes: 3 additions & 12 deletions packages/backend/src/accounting/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,7 @@ export async function createAccountToAccountTransfer(
) => {
if (account.onDebit) {
const balance = await getAccountBalance(account.id)

if (balance === undefined) {
throw new Error('undefined account balance')
}
if (balance === undefined) throw new Error('undefined account balance')

await account.onDebit({
balance
Expand All @@ -178,10 +175,7 @@ export async function createAccountToAccountTransfer(
return {
post: async (): Promise<void | TransferError> => {
const error = await postTransfers(pendingTransferIdsOrError)

if (error) {
return error
}
if (error) return error

await Promise.all([
onDebit(sourceAccount),
Expand All @@ -204,10 +198,7 @@ export async function createAccountToAccountTransfer(

void: async (): Promise<void | TransferError> => {
const error = await voidTransfers(pendingTransferIdsOrError)

if (error) {
return error
}
if (error) return error
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion packages/backend/src/accounting/tigerbeetle/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ export interface CreateAccountOptions {
id: AccountId
ledger: number
code: TigerBeetleAccountCode
linked: boolean
// This flag links the result of this account creation to the result of the next one in the request,
// such that they will either succeed or fail together.
// The last account in a chain of linked accounts does not have this flag set.
linked?: boolean
// When set, the account will retain the history of balances at each transfer.
history?: boolean
userData128: AccountUserData128
}

Expand Down Expand Up @@ -57,6 +62,7 @@ export function flagsBasedOnAccountOptions(
? AccountFlags.credits_must_not_exceed_debits
: AccountFlags.debits_must_not_exceed_credits
if (options.linked) returnVal |= AccountFlags.linked
if (options.history) returnVal |= AccountFlags.history
return returnVal
}

Expand Down
31 changes: 17 additions & 14 deletions packages/backend/src/accounting/tigerbeetle/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Client } from 'tigerbeetle-node'
import { v4 as uuid } from 'uuid'
import { createHash } from 'crypto'

import { BaseService } from '../../shared/baseService'
import { validateId } from '../../shared/utils'
Expand Down Expand Up @@ -106,7 +107,6 @@ export async function createLiquidityAccount(
id: account.id,
ledger: account.asset.ledger,
code: convertToTigerBeetleAccountCode[accountType],
linked: false,
userData128: account.asset.id
}
])
Expand All @@ -133,7 +133,6 @@ export async function createSettlementAccount(
id: ledger,
ledger,
code: TigerBeetleAccountCode.SETTLEMENT,
linked: false,
userData128: toTigerBeetleId(accountId)
}
])
Expand Down Expand Up @@ -166,13 +165,15 @@ export async function createLiquidityAndLinkedSettlementAccount(
ledger: account.asset.ledger,
code: convertToTigerBeetleAccountCode[accountType],
linked: true,
history: true,
userData128: account.asset.id
},
{
id: account.asset.ledger,
ledger: account.asset.ledger,
code: TigerBeetleAccountCode.SETTLEMENT,
linked: false,
linked: false, // Last account in the chain.
history: true,
userData128: account.asset.id
}
])
Expand Down Expand Up @@ -270,10 +271,7 @@ export async function createTransfer(
voidId: transferId
}))
)

if (error) {
return error.error
}
if (error) return error.error
},
postTransfers: async (transferIds) => {
const error = await createTransfers(
Expand All @@ -282,10 +280,7 @@ export async function createTransfer(
postId: transferId
}))
)

if (error) {
return error.error
}
if (error) return error.error
},
getAccountReceived: async (accountRef) =>
getAccountTotalReceived(deps, accountRef),
Expand All @@ -299,12 +294,14 @@ export async function createTransfer(
sourceAccountId: transfer.sourceAccountId,
destinationAccountId: transfer.destinationAccountId,
amount: transfer.amount,
timeout: args.timeout
timeout: args.timeout,
userData128: bilateralIdentification(
transfer.sourceAccountId,
transfer.destinationAccountId
)
})
)

const error = await createTransfers(deps, tbTransfers)

if (error) {
switch (error.error) {
case TransferError.UnknownSourceAccount:
Expand Down Expand Up @@ -332,6 +329,12 @@ export async function createTransfer(
})
}

function bilateralIdentification(source: string, destination: string): bigint {
const arr = [source, destination].sort()
const md5Hex = createHash('md5').update(`${arr[0]}${arr[1]}`).digest('hex')

Check failure

Code scanning / CodeQL

Use of a broken or weak cryptographic algorithm High

A broken or weak cryptographic algorithm
depends on
sensitive data from an access to sourceAccountId
.
A broken or weak cryptographic algorithm
depends on
sensitive data from an access to destinationAccountId
.
return BigInt(`0x${md5Hex}`)
}

async function createAccountDeposit(
deps: ServiceDependencies,
{ id, account, amount }: Deposit
Expand Down
10 changes: 7 additions & 3 deletions packages/backend/src/accounting/tigerbeetle/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,16 @@ export async function createTransfers(
timestamp: 0n
}
if (isNewTransferOptions(transfer)) {
if (transfer.amount <= 0n) return { index: i, error: TransferError.InvalidAmount }
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.credit_account_id = toTigerBeetleId(
transfer.destinationAccountId
)
if (transfer.timeout) {
tbTransfer.flags |= TransferFlags.pending
tbTransfer.timeout = transfer.timeout
Expand All @@ -102,7 +105,8 @@ export async function createTransfers(
}
}

if (transfer.userData128) tbTransfer.user_data_128 = toTigerBeetleId(transfer.userData128)
if (transfer.userData128)
tbTransfer.user_data_128 = toTigerBeetleId(transfer.userData128)

if (i < transfers.length - 1) tbTransfer.flags |= TransferFlags.linked
tbTransfers.push(tbTransfer)
Expand Down

0 comments on commit 917aae8

Please sign in to comment.