Skip to content

Commit

Permalink
feat(2756): transfer code.
Browse files Browse the repository at this point in the history
  • Loading branch information
koekiebox committed Jun 19, 2024
1 parent bf8d72c commit 84dff35
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 20 deletions.
3 changes: 1 addition & 2 deletions packages/backend/src/accounting/psql/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,8 @@ export async function createTransfer(
)
)

if (isTransferError(pendingTransfersOrError)) {
if (isTransferError(pendingTransfersOrError))
return pendingTransfersOrError
}

return pendingTransfersOrError.map(
(pendingTransfer) => pendingTransfer.transferRef
Expand Down
61 changes: 48 additions & 13 deletions packages/backend/src/accounting/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -95,6 +106,7 @@ export interface TransferToCreate {
destinationAccountId: string
amount: bigint
ledger: number
transferCode: number
}

export interface BaseAccountingServiceDependencies extends BaseService {
Expand All @@ -117,12 +129,12 @@ export async function createAccountToAccountTransfer(
args: CreateAccountToAccountTransferArgs
): Promise<Transaction | TransferError> {
const {
transferArgs,
voidTransfers,
postTransfers,
createPendingTransfers,
getAccountReceived,
getAccountBalance,
transferArgs
getAccountBalance
} = args

const { withdrawalThrottleDelay } = deps
Expand Down Expand Up @@ -206,28 +218,39 @@ 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
transfers.push({
sourceAccountId: destinationAccount.asset.id,
destinationAccountId: destinationAccount.id,
amount: destinationAmount,
ledger: destinationAccount.asset.ledger
ledger: destinationAccount.asset.ledger,
transferCode
})

return transfers
Expand All @@ -236,18 +259,28 @@ 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,
amount:
destinationAmount && destinationAmount < sourceAmount
? destinationAmount
: sourceAmount,
ledger: sourceAccount.asset.ledger
ledger: sourceAccount.asset.ledger,
transferCode
})

if (destinationAmount && sourceAmount !== destinationAmount) {
Expand All @@ -257,15 +290,17 @@ 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
transfers.push({
sourceAccountId: destinationAccount.asset.id,
destinationAccountId: destinationAccount.id,
amount: destinationAmount - sourceAmount,
ledger: sourceAccount.asset.ledger
ledger: sourceAccount.asset.ledger,
transferCode
})
}
}
Expand Down
17 changes: 16 additions & 1 deletion packages/backend/src/accounting/tigerbeetle/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
Transaction,
TransferOptions,
Withdrawal,
createAccountToAccountTransfer
createAccountToAccountTransfer,
TransferType
} from '../service'
import { calculateBalance, createAccounts, getAccounts } from './accounts'
import {
Expand All @@ -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
} = {
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions packages/backend/src/accounting/tigerbeetle/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,6 +19,7 @@ export type TransferUserData128 = string | number | bigint

interface TransferOptions {
userData128?: TransferUserData128
code?: TigerBeetleTransferCode
}

export interface NewTransferOptions extends TransferOptions {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 84dff35

Please sign in to comment.