-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
237 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { CryptoAssetInfo } from './crypto-assets/crypto-asset-info.model'; | ||
import { Money } from './money.model'; | ||
|
||
export const ActivityTypes = { | ||
tokenTransfer: 'tokenTransfer', | ||
contractCall: 'contractCall', | ||
}; | ||
export type ActivityType = keyof typeof ActivityTypes; | ||
|
||
export interface BaseActivity { | ||
readonly type: ActivityType; | ||
readonly txid: string; | ||
readonly timestamp: number; | ||
readonly status: 'pending' | 'success' | 'failed'; | ||
} | ||
|
||
export const TokenTransferDirections = { | ||
outbound: 'outbound', | ||
inbound: 'inbound', | ||
}; | ||
export type TokenTransferDirection = keyof typeof TokenTransferDirections; | ||
|
||
export interface TokenTransferActivity extends BaseActivity { | ||
readonly type: 'tokenTransfer'; | ||
readonly asset: CryptoAssetInfo; | ||
readonly direction: TokenTransferDirection; | ||
readonly amount: { | ||
crypto: Money; | ||
fiat: Money; | ||
}; | ||
} | ||
|
||
export interface ContractCallActivity extends BaseActivity { | ||
readonly type: 'contractCall'; | ||
} | ||
|
||
export type Activity = TokenTransferActivity | ContractCallActivity; | ||
|
||
// Token | ||
// Collectible | ||
// Swap | ||
// Smart Contract Activity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Activity } from '@leather.io/models'; | ||
import { isDefined } from '@leather.io/utils'; | ||
|
||
import { UnifiedAccountIdentifier } from '../shared/bitcoin.types'; | ||
import { BitcoinTransactionsService } from '../transactions/bitcoin-transactions.service'; | ||
import { StacksTransactionsService } from '../transactions/stacks-transactions.service'; | ||
import { | ||
mapBitcoinTxToActivity, | ||
mapStacksTxToActivity, | ||
sortActivityByTimestampDesc, | ||
} from './activity.utils'; | ||
|
||
export interface ActivityService { | ||
getAccountActivity(account: UnifiedAccountIdentifier, signal?: AbortSignal): Promise<Activity[]>; | ||
} | ||
|
||
export function createActivityService( | ||
stacksTransactionsService: StacksTransactionsService, | ||
bitcoinTransactionsService: BitcoinTransactionsService | ||
): ActivityService { | ||
/* | ||
* Gets unified activity list sorted by timestamp | ||
*/ | ||
async function getAccountActivity(account: UnifiedAccountIdentifier, signal?: AbortSignal) { | ||
const [stacksTransactions, bitcoinTransactions] = await Promise.all([ | ||
stacksTransactionsService.getTotalTransactions(account.stxAddress, signal), | ||
bitcoinTransactionsService.getTotalTransactions(account, signal), | ||
]); | ||
return [ | ||
...stacksTransactions.map(mapStacksTxToActivity), | ||
...bitcoinTransactions.map(mapBitcoinTxToActivity), | ||
] | ||
.filter(isDefined) | ||
.sort(sortActivityByTimestampDesc); | ||
} | ||
|
||
return { | ||
getAccountActivity, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { btcCryptoAsset, stxCryptoAsset } from '@leather.io/constants'; | ||
import { Activity, StacksTx, TokenTransferActivity } from '@leather.io/models'; | ||
import { createMoney } from '@leather.io/utils'; | ||
|
||
import { LeatherApiBitcoinTransaction } from '../infrastructure/api/leather/leather-api.client'; | ||
|
||
export function mapStacksTxToActivity(stacksTx: StacksTx): Activity | undefined { | ||
switch (stacksTx.tx_type) { | ||
case 'token_transfer': | ||
return mapStacksTxToTransferActivity(stacksTx); | ||
default: | ||
return; | ||
} | ||
} | ||
|
||
function mapStacksTxToTransferActivity(stacksTx: StacksTx): TokenTransferActivity { | ||
const stxTransferActivity: TokenTransferActivity = { | ||
type: 'tokenTransfer', | ||
asset: stxCryptoAsset, | ||
direction: 'outbound', | ||
amount: { | ||
crypto: createMoney(0, 'STX'), | ||
fiat: createMoney(0, 'USD'), | ||
}, | ||
txid: stacksTx.tx_id, | ||
timestamp: 0, | ||
status: 'success', | ||
}; | ||
return stxTransferActivity; | ||
} | ||
|
||
export function mapBitcoinTxToActivity(bitcoinTx: LeatherApiBitcoinTransaction): Activity { | ||
const btcTransferActivity: TokenTransferActivity = { | ||
type: 'tokenTransfer', | ||
asset: btcCryptoAsset, | ||
txid: bitcoinTx.txid, | ||
direction: 'outbound', | ||
amount: { | ||
crypto: createMoney(0, 'BTC'), | ||
fiat: createMoney(0, 'USD'), | ||
}, | ||
timestamp: 0, | ||
status: 'success', | ||
}; | ||
return btcTransferActivity; | ||
} | ||
|
||
export function sortActivityByTimestampDesc(a: Activity, b: Activity) { | ||
return b.timestamp - a.timestamp; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 39 additions & 7 deletions
46
packages/services/src/transactions/bitcoin-transactions.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,48 @@ | ||
import { | ||
LeatherApiBitcoinTransaction, | ||
LeatherApiClient, | ||
} from '../infrastructure/api/leather/leather-api.client'; | ||
import { BitcoinAccountIdentifier } from '../shared/bitcoin.types'; | ||
|
||
export interface BitcoinTransactionsService { | ||
getOutboundTransactions(account: BitcoinAccountIdentifier, signal?: AbortSignal): Promise<any[]>; | ||
getTotalTransactions( | ||
account: BitcoinAccountIdentifier, | ||
signal?: AbortSignal | ||
): Promise<LeatherApiBitcoinTransaction[]>; | ||
getConfirmedTransactions( | ||
account: BitcoinAccountIdentifier, | ||
signal?: AbortSignal | ||
): Promise<LeatherApiBitcoinTransaction[]>; | ||
getPendingTransactions( | ||
account: BitcoinAccountIdentifier, | ||
signal?: AbortSignal | ||
): Promise<LeatherApiBitcoinTransaction[]>; | ||
} | ||
|
||
// TODO: WIP - Requires new Leather API Tx endpoint leveraging xpub lookups | ||
export function createBitcoinTransactionsService(): BitcoinTransactionsService { | ||
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars | ||
async function getOutboundTransactions(account: BitcoinAccountIdentifier, signal?: AbortSignal) { | ||
return []; | ||
export function createBitcoinTransactionsService( | ||
leatherApiClient: LeatherApiClient | ||
): BitcoinTransactionsService { | ||
async function getTotalTransactions(account: BitcoinAccountIdentifier, signal?: AbortSignal) { | ||
const [trTransactions, nsTransactions] = await Promise.all([ | ||
leatherApiClient.fetchBitcoinTransactions(account.taprootDescriptor, signal), | ||
leatherApiClient.fetchBitcoinTransactions(account.nativeSegwitDescriptor, signal), | ||
]); | ||
return [...trTransactions, ...nsTransactions]; | ||
} | ||
|
||
async function getConfirmedTransactions(account: BitcoinAccountIdentifier, signal?: AbortSignal) { | ||
const transactions = await getTotalTransactions(account, signal); | ||
return transactions.filter(tx => tx.confirmations > 0); | ||
} | ||
|
||
async function getPendingTransactions(account: BitcoinAccountIdentifier, signal?: AbortSignal) { | ||
const transactions = await getTotalTransactions(account, signal); | ||
return transactions.filter(tx => !tx.confirmations); | ||
} | ||
|
||
return { | ||
getOutboundTransactions, | ||
getTotalTransactions, | ||
getConfirmedTransactions, | ||
getPendingTransactions, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters