diff --git a/packages/backend/src/accounting/psql/service.ts b/packages/backend/src/accounting/psql/service.ts index 68d3294fe0..81d4709cb0 100644 --- a/packages/backend/src/accounting/psql/service.ts +++ b/packages/backend/src/accounting/psql/service.ts @@ -270,9 +270,8 @@ export async function createTransfer( ) ) - if (isTransferError(pendingTransfersOrError)) { + if (isTransferError(pendingTransfersOrError)) return pendingTransfersOrError - } return pendingTransfersOrError.map( (pendingTransfer) => pendingTransfer.transferRef diff --git a/packages/backend/src/accounting/service.ts b/packages/backend/src/accounting/service.ts index eb71b3328a..f2c2f38c09 100644 --- a/packages/backend/src/accounting/service.ts +++ b/packages/backend/src/accounting/service.ts @@ -2,6 +2,7 @@ import { TransactionOrKnex } from 'objection' import { BaseService } from '../shared/baseService' import { TransferError, isTransferError } from './errors' import { AccountUserData128 } from './tigerbeetle/utils' +import { convertToTigerBeetleTransferCode } from './tigerbeetle/service' export enum LiquidityAccountType { ASSET = 'ASSET', @@ -11,6 +12,12 @@ export enum LiquidityAccountType { WEB_MONETIZATION = 'WEB_MONETIZATION' } +export enum TransferType { + DEPOSIT = 'DEPOSIT', + WITHDRAWAL = 'WITHDRAWAL', + TRANSFER = 'TRANSFER' +} + export interface LiquidityAccountAsset { id: string code?: string @@ -35,7 +42,11 @@ export interface OnDebitOptions { balance: bigint } -export interface Deposit { +export interface BaseTransfer { + transferType?: TransferType +} + +export interface Deposit extends BaseTransfer { id: string account: LiquidityAccount amount: bigint @@ -45,7 +56,7 @@ export interface Withdrawal extends Deposit { timeout?: number } -export interface TransferOptions { +export interface TransferOptions extends BaseTransfer { sourceAccount: LiquidityAccount destinationAccount: LiquidityAccount sourceAmount: bigint @@ -95,6 +106,7 @@ export interface TransferToCreate { destinationAccountId: string amount: bigint ledger: number + transferCode: number } export interface BaseAccountingServiceDependencies extends BaseService { @@ -117,12 +129,12 @@ export async function createAccountToAccountTransfer( args: CreateAccountToAccountTransferArgs ): Promise { const { + transferArgs, voidTransfers, postTransfers, createPendingTransfers, getAccountReceived, - getAccountBalance, - transferArgs + getAccountBalance } = args const { withdrawalThrottleDelay } = deps @@ -206,20 +218,30 @@ export async function createAccountToAccountTransfer( export function buildCrossAssetTransfers( args: TransferOptions ): TransferToCreate[] | TransferError { - const { sourceAccount, destinationAccount, sourceAmount, destinationAmount } = - args + const { + sourceAccount, + destinationAccount, + sourceAmount, + destinationAmount, + transferType + } = args if (!destinationAmount) { return TransferError.InvalidDestinationAmount } + let transferCode = 0 + if (transferType) + transferCode = convertToTigerBeetleTransferCode[transferType] + const transfers: TransferToCreate[] = [] // Send to source liquidity account transfers.push({ sourceAccountId: sourceAccount.id, destinationAccountId: sourceAccount.asset.id, amount: sourceAmount, - ledger: sourceAccount.asset.ledger + ledger: sourceAccount.asset.ledger, + transferCode }) // Deliver from destination liquidity account @@ -227,7 +249,8 @@ export function buildCrossAssetTransfers( sourceAccountId: destinationAccount.asset.id, destinationAccountId: destinationAccount.id, amount: destinationAmount, - ledger: destinationAccount.asset.ledger + ledger: destinationAccount.asset.ledger, + transferCode }) return transfers @@ -236,10 +259,19 @@ export function buildCrossAssetTransfers( export function buildSameAssetTransfers( args: TransferOptions ): TransferToCreate[] { - const { sourceAccount, destinationAccount, sourceAmount, destinationAmount } = - args + const { + sourceAccount, + destinationAccount, + sourceAmount, + destinationAmount, + transferType + } = args const transfers: TransferToCreate[] = [] + let transferCode = 0 + if (transferType) + transferCode = convertToTigerBeetleTransferCode[transferType] + transfers.push({ sourceAccountId: sourceAccount.id, destinationAccountId: destinationAccount.id, @@ -247,7 +279,8 @@ export function buildSameAssetTransfers( destinationAmount && destinationAmount < sourceAmount ? destinationAmount : sourceAmount, - ledger: sourceAccount.asset.ledger + ledger: sourceAccount.asset.ledger, + transferCode }) if (destinationAmount && sourceAmount !== destinationAmount) { @@ -257,7 +290,8 @@ export function buildSameAssetTransfers( sourceAccountId: sourceAccount.id, destinationAccountId: sourceAccount.asset.id, amount: sourceAmount - destinationAmount, - ledger: sourceAccount.asset.ledger + ledger: sourceAccount.asset.ledger, + transferCode }) } else { // Deliver excess destination amount from liquidity account @@ -265,7 +299,8 @@ export function buildSameAssetTransfers( sourceAccountId: destinationAccount.asset.id, destinationAccountId: destinationAccount.id, amount: destinationAmount - sourceAmount, - ledger: sourceAccount.asset.ledger + ledger: sourceAccount.asset.ledger, + transferCode }) } } diff --git a/packages/backend/src/accounting/tigerbeetle/service.ts b/packages/backend/src/accounting/tigerbeetle/service.ts index 833e19c5b5..eaba09412c 100644 --- a/packages/backend/src/accounting/tigerbeetle/service.ts +++ b/packages/backend/src/accounting/tigerbeetle/service.ts @@ -17,7 +17,8 @@ import { Transaction, TransferOptions, Withdrawal, - createAccountToAccountTransfer + createAccountToAccountTransfer, + TransferType } from '../service' import { calculateBalance, createAccounts, getAccounts } from './accounts' import { @@ -37,6 +38,12 @@ export enum TigerBeetleAccountCode { SETTLEMENT = 101 } +export enum TigerBeetleTransferCode { + TRANSFER = 1, + DEPOSIT = 2, + WITHDRAWAL = 3 +} + export const convertToTigerBeetleAccountCode: { [key in LiquidityAccountType]: TigerBeetleAccountCode } = { @@ -48,6 +55,14 @@ export const convertToTigerBeetleAccountCode: { [LiquidityAccountType.OUTGOING]: TigerBeetleAccountCode.LIQUIDITY_OUTGOING } +export const convertToTigerBeetleTransferCode: { + [key in TransferType]: TigerBeetleTransferCode +} = { + [TransferType.TRANSFER]: TigerBeetleTransferCode.TRANSFER, + [TransferType.DEPOSIT]: TigerBeetleTransferCode.DEPOSIT, + [TransferType.WITHDRAWAL]: TigerBeetleTransferCode.WITHDRAWAL +} + export interface ServiceDependencies extends BaseService { tigerBeetle: Client withdrawalThrottleDelay?: number diff --git a/packages/backend/src/accounting/tigerbeetle/transfers.ts b/packages/backend/src/accounting/tigerbeetle/transfers.ts index e4bd2f7f36..cfe5d7fb76 100644 --- a/packages/backend/src/accounting/tigerbeetle/transfers.ts +++ b/packages/backend/src/accounting/tigerbeetle/transfers.ts @@ -7,11 +7,9 @@ import { v4 as uuid } from 'uuid' import { TransferError } from '../errors' import { TigerbeetleCreateTransferError } from './errors' -import { ServiceDependencies } from './service' +import { ServiceDependencies, TigerBeetleTransferCode } from './service' import { AccountId, toTigerBeetleId } from './utils' -const ACCOUNT_TYPE = 1 - type TransfersError = { index: number error: TransferError @@ -21,6 +19,7 @@ export type TransferUserData128 = string | number | bigint interface TransferOptions { userData128?: TransferUserData128 + code?: TigerBeetleTransferCode } export interface NewTransferOptions extends TransferOptions { @@ -74,7 +73,7 @@ export async function createTransfers( pending_id: 0n, timeout: 0, ledger: 0, - code: ACCOUNT_TYPE, + code: 0, flags: 0, amount: 0n, timestamp: 0n @@ -94,7 +93,9 @@ export async function createTransfers( tbTransfer.flags |= TransferFlags.pending tbTransfer.timeout = transfer.timeout } + if (transfer.code) tbTransfer.code = transfer.code } else { + tbTransfer.code = 0 tbTransfer.id = toTigerBeetleId(uuid()) if (transfer.postId) { tbTransfer.flags |= TransferFlags.post_pending_transfer