Skip to content

Commit

Permalink
chore: Added state and expiry date to accounting transfer (#3131)
Browse files Browse the repository at this point in the history
* Added state and expiry date to accounting transfer

* Added expiresAt to TigerBeetle transfer

* Converted timeout from seconds to milliseconds

* Updated test to check if expiresAt is set
  • Loading branch information
oana-lolea authored Dec 3, 2024
1 parent 8c8a9b9 commit 497d160
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ body:graphql {
transferType
ledger
createdAt
state
expiresAt
}
credits {
id
Expand All @@ -30,6 +32,8 @@ body:graphql {
transferType
ledger
createdAt
state
expiresAt
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions localenv/mock-account-servicing-entity/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions packages/backend/src/accounting/tigerbeetle/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,41 @@ describe('TigerBeetle Accounting Service', (): void => {
expect(transferDebit.expiresAt).toBeUndefined()
})

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> => {
await expect(
accountingService.getTotalReceived(uuid())
Expand Down
15 changes: 14 additions & 1 deletion packages/backend/src/accounting/tigerbeetle/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ export function tbTransferToLedgerTransfer(
transferRef: fromTigerBeetleId(tbTransfer.user_data_128),
type: transferTypeFromCode(tbTransfer.code),
state: state,
ledger: tbTransfer.ledger
ledger: tbTransfer.ledger,
expiresAt: expiresAtFromTimestampAndTimeout(
tbTransfer.timestamp,
tbTransfer.timeout
)
}
}

function expiresAtFromTimestampAndTimeout(
timestamp: bigint,
timeout: number
): Date | undefined {
return timeout
? new Date(Number(timestamp / 1_000_000n) + timeout * 1000)
: undefined
}
57 changes: 57 additions & 0 deletions packages/backend/src/graphql/generated/graphql.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions packages/backend/src/graphql/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Config } from '../../config/app'
import { truncateTables } from '../../tests/tableManager'
import {
AccountingTransferConnection,
TransferState,
TransferType
} from '../generated/graphql'
import { createAsset } from '../../tests/asset'
Expand Down Expand Up @@ -99,6 +100,7 @@ describe('Accounting Transfer', (): void => {
// Top up debit account first:
const transferAmount = 123
const ledger = 1
const tomorrow = new Date(new Date().getDate() + 1)
const queryLimit = 20

const insertedTransfer = await createLedgerTransfer(
Expand All @@ -109,7 +111,8 @@ describe('Accounting Transfer', (): void => {
ledger: ledger,
state: LedgerTransferState.POSTED,
transferRef: uuid(),
type: LedgerTransferType.DEPOSIT
type: LedgerTransferType.DEPOSIT,
expiresAt: tomorrow
},
appContainer.knex
)
Expand All @@ -127,6 +130,8 @@ describe('Accounting Transfer', (): void => {
transferType
ledger
createdAt
state
expiresAt
}
credits {
id
Expand All @@ -136,6 +141,8 @@ describe('Accounting Transfer', (): void => {
transferType
ledger
createdAt
state
expiresAt
}
}
}
Expand All @@ -161,7 +168,9 @@ describe('Accounting Transfer', (): void => {
debitAccountId: accountDebitId,
amount: `${transferAmount}`,
transferType: TransferType.Deposit,
ledger
ledger,
state: TransferState.Posted,
expiresAt: tomorrow.toISOString()
})

// Credit:
Expand All @@ -179,6 +188,8 @@ describe('Accounting Transfer', (): void => {
transferType
ledger
createdAt
state
expiresAt
}
credits {
id
Expand All @@ -188,6 +199,8 @@ describe('Accounting Transfer', (): void => {
transferType
ledger
createdAt
state
expiresAt
}
}
}
Expand All @@ -214,7 +227,9 @@ describe('Accounting Transfer', (): void => {
creditAccountId: accountCreditId,
amount: `${transferAmount}`,
transferType: TransferType.Deposit,
ledger
ledger,
state: TransferState.Posted,
expiresAt: tomorrow.toISOString()
})
})
})
Loading

0 comments on commit 497d160

Please sign in to comment.