Skip to content

Commit

Permalink
feat(2756): add test cases for liquidity and settlement account creat…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
koekiebox committed Jun 18, 2024
1 parent 87f39a2 commit 8572518
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 33 deletions.
15 changes: 4 additions & 11 deletions packages/backend/src/accounting/psql/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
voidTransfers
} from './ledger-transfer'
import { LedgerTransfer, LedgerTransferType } from './ledger-transfer/model'
import { AccountId } from '../tigerbeetle/utils'
import { AccountId, AccountUserData128 } from '../tigerbeetle/utils'

export interface ServiceDependencies extends BaseService {
knex: TransactionOrKnex
Expand All @@ -52,17 +52,11 @@ export function createAccountingService(
createLiquidityAccount(deps, options, accTypeCode, trx),
createSettlementAccount: (ledger, accountId, trx) =>
createSettlementAccount(deps, ledger, accountId, trx),
createLiquidityAndLinkedSettlementAccount: (
options,
accTypeCode,
ledger,
trx
) =>
createLiquidityAndLinkedSettlementAccount: (options, accTypeCode, trx) =>
createLiquidityAndLinkedSettlementAccount(
deps,
options,
accTypeCode,
ledger,
trx
),
getBalance: (accountRef) => getLiquidityAccountBalance(deps, accountRef),
Expand Down Expand Up @@ -102,7 +96,7 @@ export async function createLiquidityAccount(
export async function createSettlementAccount(
deps: ServiceDependencies,
ledger: number,
accountId: AccountId,
accountId: AccountUserData128,
trx?: TransactionOrKnex
): Promise<void> {
const asset = await Asset.query(trx || deps.knex).findOne({ ledger })
Expand All @@ -125,11 +119,10 @@ export async function createLiquidityAndLinkedSettlementAccount(
deps: ServiceDependencies,
account: LiquidityAccount,
accountType: LiquidityAccountType,
ledger: number,
trx?: TransactionOrKnex
): Promise<LiquidityAccount> {
await createLiquidityAccount(deps, account, accountType, trx)
await createSettlementAccount(deps, ledger, account.id, trx)
await createSettlementAccount(deps, account.asset.ledger, account.id, trx)
return account
}

Expand Down
5 changes: 2 additions & 3 deletions packages/backend/src/accounting/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TransactionOrKnex } from 'objection'
import { BaseService } from '../shared/baseService'
import { TransferError, isTransferError } from './errors'
import { AccountId } from './tigerbeetle/utils'
import { AccountUserData128 } from './tigerbeetle/utils'

export enum LiquidityAccountType {
ASSET = 'ASSET',
Expand Down Expand Up @@ -66,13 +66,12 @@ export interface AccountingService {
): Promise<LiquidityAccount>
createSettlementAccount(
ledger: number,
accountId: AccountId,
accountId: AccountUserData128,
trx?: TransactionOrKnex
): Promise<void>
createLiquidityAndLinkedSettlementAccount(
account: LiquidityAccount,
accountType: LiquidityAccountType,
ledger: number,
trx?: TransactionOrKnex
): Promise<LiquidityAccount>
getBalance(id: string): Promise<bigint | undefined>
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/accounting/tigerbeetle/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {

import { ServiceDependencies, TigerBeetleAccountCode } from './service'
import { TigerbeetleCreateAccountError } from './errors'
import { AccountId, toTigerbeetleId, UserData128 } from './utils'
import { AccountId, toTigerbeetleId, AccountUserData128 } from './utils'

export interface CreateAccountOptions {
id: AccountId
ledger: number
code: TigerBeetleAccountCode
linked: boolean
userData128: UserData128
userData128: AccountUserData128
}

export async function createAccounts(
Expand Down
24 changes: 24 additions & 0 deletions packages/backend/src/accounting/tigerbeetle/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ describe('TigerBeetle Accounting Service', (): void => {
})
})

describe('Create Liquidity and Settlement Account - Linked', (): void => {
test('Can create a liquidity and settlement account', async (): Promise<void> => {
const account: LiquidityAccount = {
id: uuid(),
asset: {
id: uuid(),
ledger: newLedger()
}
}
await expect(
accountingService.createLiquidityAndLinkedSettlementAccount(
account,
LiquidityAccountType.ASSET
)
).resolves.toEqual(account)
await expect(accountingService.getBalance(account.id)).resolves.toEqual(
BigInt(0)
)
await expect(
accountingService.getSettlementBalance(account.asset.ledger)
).resolves.toEqual(BigInt(0))
})
})

describe('Get Account Balance', (): void => {
test("Can retrieve an account's balance", async (): Promise<void> => {
const { id } = await accountFactory.build()
Expand Down
20 changes: 7 additions & 13 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 { AccountId, toTigerbeetleId } from './utils'
import { AccountUserData128, toTigerbeetleId } from './utils'

export enum TigerBeetleAccountCode {
LIQUIDITY_WEB_MONETIZATION = 1,
Expand Down Expand Up @@ -76,13 +76,8 @@ export function createAccountingService(
createLiquidityAccount(deps, options, accountType),
createSettlementAccount: (ledger, accountId) =>
createSettlementAccount(deps, ledger, accountId),
createLiquidityAndLinkedSettlementAccount: (options, accTypeCode, ledger) =>
createLiquidityAndLinkedSettlementAccount(
deps,
options,
accTypeCode,
ledger
),
createLiquidityAndLinkedSettlementAccount: (options, accTypeCode) =>
createLiquidityAndLinkedSettlementAccount(deps, options, accTypeCode),
getBalance: (id) => getAccountBalance(deps, id),
getTotalSent: (id) => getAccountTotalSent(deps, id),
getAccountsTotalSent: (ids) => getAccountsTotalSent(deps, ids),
Expand Down Expand Up @@ -130,7 +125,7 @@ export async function createLiquidityAccount(
export async function createSettlementAccount(
deps: ServiceDependencies,
ledger: number,
accountId: AccountId
accountId: AccountUserData128
): Promise<void> {
try {
await createAccounts(deps, [
Expand Down Expand Up @@ -158,8 +153,7 @@ export async function createSettlementAccount(
export async function createLiquidityAndLinkedSettlementAccount(
deps: ServiceDependencies,
account: LiquidityAccount,
accountType: LiquidityAccountType,
ledger: number
accountType: LiquidityAccountType
): Promise<LiquidityAccount> {
if (!validateId(account.id)) {
throw new Error('unable to create account, invalid id')
Expand All @@ -175,8 +169,8 @@ export async function createLiquidityAndLinkedSettlementAccount(
userData128: account.asset.id
},
{
id: ledger,
ledger,
id: account.asset.ledger,
ledger: account.asset.ledger,
code: TigerBeetleAccountCode.SETTLEMENT,
linked: false,
userData128: account.asset.id
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
@@ -1,7 +1,7 @@
import { validateId } from '../../shared/utils'

export type AccountId = string | number | bigint
export type UserData128 = AccountId
export type AccountUserData128 = AccountId

export function toTigerbeetleId(id: AccountId): bigint {
if (typeof id === 'number') {
Expand Down
1 change: 0 additions & 1 deletion packages/backend/src/asset/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ async function createAsset(
await deps.accountingService.createLiquidityAndLinkedSettlementAccount(
asset,
LiquidityAccountType.ASSET,
asset.ledger,
trx
)
return asset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../../../../../../accounting/errors'
import { CreateAccountError as CreateAccountErrorCode } from 'tigerbeetle-node'
import { TransactionOrKnex } from 'objection'
import { AccountId } from '../../../../../../accounting/tigerbeetle/utils'
import { AccountUserData128 } from '../../../../../../accounting/tigerbeetle/utils'

interface MockAccount {
id: string
Expand Down Expand Up @@ -150,7 +150,7 @@ export class MockAccountingService implements AccountingService {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ledger: number,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
accountId: AccountId,
accountId: AccountUserData128,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
trx?: TransactionOrKnex
): Promise<void> {
Expand Down

0 comments on commit 8572518

Please sign in to comment.