Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Added state and expiry date to accounting transfer #3131

Merged
merged 8 commits into from
Dec 3, 2024
47 changes: 36 additions & 11 deletions packages/backend/src/accounting/tigerbeetle/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,7 @@ describe('TigerBeetle Accounting Service', (): void => {
transferRef: '00000000-0000-0000-0000-000000000000'
})
expect(transferCredit.timestamp).toBeGreaterThan(0)

const toleranceInMs = 10
const now = new Date().getTime()
const creditExpiresAt = transferCredit.expiresAt
? transferCredit.expiresAt.getTime()
: now
expect(Math.abs(now - creditExpiresAt)).toBeLessThanOrEqual(toleranceInMs)
expect(transferCredit.expiresAt).toBeUndefined()

const accTransfersDebit =
await accountingService.getAccountTransfers(ledger)
Expand All @@ -238,11 +232,42 @@ describe('TigerBeetle Accounting Service', (): void => {
transferRef: '00000000-0000-0000-0000-000000000000'
})
expect(transferDebit.timestamp).toBeGreaterThan(0)
expect(transferDebit.expiresAt).toBeUndefined()
})

const debitExpiresAt = transferDebit.expiresAt
? transferDebit.expiresAt.getTime()
: now
expect(Math.abs(now - debitExpiresAt)).toBeLessThanOrEqual(toleranceInMs)
test('Returns expiry date correctly', async (): Promise<void> => {
const receivingAccount = await accountFactory.build()
const balances = [BigInt(100), BigInt(100)]
const accounts = await Promise.all(
balances.map(async (balance) => {
return await accountFactory.build({
balance,
asset: receivingAccount.asset
})
})
)
const timeout = 10
await Promise.all(
accounts.map(async (account, i) => {
const transfer = await accountingService.createTransfer({
sourceAccount: account,
sourceAmount: BigInt(10 * (i + 1)),
destinationAccount: receivingAccount,
timeout: timeout
})
assert.ok(!isTransferError(transfer))
await transfer.post()
})
)
const transfer = await accountingService.getAccountTransfers(
receivingAccount.id
)
expect(transfer.credits).not.toHaveLength(0)
const timestamp = transfer.credits[0].timestamp
const expiresAtTime = transfer.credits[0].expiresAt?.getTime()
expect(expiresAtTime).toBe(
new Date(Number(timestamp) + timeout * 1000).getTime()
)
})

test('Returns undefined for nonexistent account', async (): Promise<void> => {
Expand Down
14 changes: 12 additions & 2 deletions packages/backend/src/accounting/tigerbeetle/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,18 @@ export function tbTransferToLedgerTransfer(
type: transferTypeFromCode(tbTransfer.code),
state: state,
ledger: tbTransfer.ledger,
expiresAt: new Date(
Number(tbTransfer.timestamp / 1_000_000n) + tbTransfer.timeout * 1000
expiresAt: expiresAtFromTimestampAndTimeout(
tbTransfer.timestamp,
tbTransfer.timeout
)
oana-lolea marked this conversation as resolved.
Show resolved Hide resolved
}
}

function expiresAtFromTimestampAndTimeout(
timestamp: bigint,
timeout: number
): Date | undefined {
return timeout
? new Date(Number(timestamp) / 1_000_000 + timeout * 1000)
oana-lolea marked this conversation as resolved.
Show resolved Hide resolved
: undefined
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('TigerBeetle: Accounting Transfer', (): void => {
transferType: TransferType.Deposit,
ledger,
state: TransferState.Posted,
expiresAt: response.debits[0].createdAt
expiresAt: null
})

// Credit:
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('TigerBeetle: Accounting Transfer', (): void => {
transferType: TransferType.Deposit,
ledger,
state: TransferState.Posted,
expiresAt: response.credits[0].createdAt
expiresAt: null
})
})
})
Loading