Skip to content

Commit

Permalink
add docs generation
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser committed Jan 13, 2025
1 parent 1e4b291 commit 486a30f
Show file tree
Hide file tree
Showing 16 changed files with 349 additions and 81 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"test:simple:single": "mocha --loader=ts-node/esm --exit --timeout 60000",
"test:single": "mocha --loader=ts-node/esm --require $(pwd)/src/tests/setup.ts --exit --timeout 60000",
"test:ci": "yarn test --reporter mocha-multi-reporters --reporter-options configFile=mocha-reporter-config.json",
"test:coverage": "c8 yarn test:ci"
"test:coverage": "c8 yarn test:ci",
"gen:docs": "typedoc --out docs --skipErrorChecking --excludeExternals src/index.ts"
},
"dependencies": {
"decimal.js-light": "^2.5.1",
Expand Down Expand Up @@ -61,6 +62,8 @@
"sinon-chai": "^4.0.0",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.2",
"typedoc": "^0.27.6",
"typedoc-plugin-markdown": "^4.4.1",
"typescript": "~5.6.3",
"typescript-eslint": "^8.8.1",
"viem": "^2.21.25"
Expand Down
27 changes: 5 additions & 22 deletions src/Centrifuge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
type Abi,
type Account as AccountType,
type Chain,
type PublicClient,
type WalletClient,
type WatchEventOnLogsParameter,
} from 'viem'
Expand All @@ -34,31 +33,14 @@ import { chains } from './config/chains.js'
import type { CurrencyMetadata } from './config/lp.js'
import { PERMIT_TYPEHASH } from './constants.js'
import { Pool } from './Pool.js'
import type { HexString } from './types/index.js'
import type { Client, DerivedConfig, EnvConfig, HexString, UserProvidedConfig } from './types/index.js'
import type { CentrifugeQueryOptions, Query } from './types/query.js'
import type { OperationStatus, Signer, Transaction, TransactionCallbackParams } from './types/transaction.js'
import { Currency } from './utils/BigInt.js'
import { hashKey } from './utils/query.js'
import { makeThenable, repeatOnEvents, shareReplayWithDelayedReset } from './utils/rx.js'
import { doTransaction, isLocalAccount } from './utils/transaction.js'

export type Config = {
environment: 'mainnet' | 'demo' | 'dev'
rpcUrls?: Record<number | string, string>
indexerUrl: string
ipfsUrl: string
}

export type UserProvidedConfig = Partial<Config>
type EnvConfig = {
indexerUrl: string
alchemyKey: string
infuraKey: string
defaultChain: number
ipfsUrl: string
}
type DerivedConfig = Config & EnvConfig

const envConfig = {
mainnet: {
indexerUrl: 'https://subql.embrio.tech/',
Expand Down Expand Up @@ -93,7 +75,7 @@ export class Centrifuge {
return this.#config
}

#clients = new Map<number, PublicClient<any, Chain>>()
#clients = new Map<number, Client>()
getClient(chainId?: number) {
return this.#clients.get(chainId ?? this.config.defaultChain)
}
Expand Down Expand Up @@ -134,8 +116,8 @@ export class Centrifuge {
})
}

pool(id: string, metadataHash?: string) {
return this._query(null, () => of(new Pool(this, id, metadataHash)))
pool(id: string | number, metadataHash?: string) {
return this._query(null, () => of(new Pool(this, String(id), metadataHash)))
}

account(address: string, chainId?: number) {
Expand Down Expand Up @@ -535,6 +517,7 @@ export class Centrifuge {
* // { type: 'SigningTransaction', title: 'Invest' }
* // { type: 'TransactionPending', title: 'Invest', hash: '0x123...abc' }
* // { type: 'TransactionConfirmed', title: 'Invest', hash: '0x123...abc', receipt: { ... } }
* ```
*
* @internal
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import type { CentrifugeQueryOptions } from './types/query.js'

export class Entity {
#baseKeys: (string | number)[]
/** @internal */
_transact: Centrifuge['_transact']
/** @internal */
_transactSequence: Centrifuge['_transactSequence']
constructor(
/** @internal */
protected _root: Centrifuge,
queryKeys: (string | number)[]
) {
Expand All @@ -15,6 +18,7 @@ export class Entity {
this._transactSequence = this._root._transactSequence.bind(this._root)
}

/** @internal */
protected _query<T>(
keys: (string | number | undefined)[] | null,
observableCallback: () => Observable<T>,
Expand Down
5 changes: 4 additions & 1 deletion src/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Reports } from './Reports/index.js'
import { PoolMetadata } from './types/poolMetadata.js'

export class Pool extends Entity {
/** @internal */
constructor(
_root: Centrifuge,
public id: string,
Expand All @@ -19,7 +20,9 @@ export class Pool extends Entity {
}

metadata() {
return this.metadataHash ? this._root._queryIPFS<PoolMetadata>(this.metadataHash) : of(undefined)
return this.metadataHash
? this._root._queryIPFS<PoolMetadata>(this.metadataHash)
: this._query(null, () => of(null))
}

trancheIds() {
Expand Down
4 changes: 3 additions & 1 deletion src/PoolNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Vault } from './Vault.js'
* Query and interact with a pool on a specific network.
*/
export class PoolNetwork extends Entity {
/** @internal */
constructor(
_root: Centrifuge,
public pool: Pool,
Expand Down Expand Up @@ -192,9 +193,10 @@ export class PoolNetwork extends Entity {

/**
* Get all Vaults for all tranches in the pool.
* @returns An object of tranche ID to Vault.
*/
vaultsByTranche() {
return this._query(null, () =>
return this._query<Record<string, Vault>>(null, () =>
this.pool.trancheIds().pipe(
switchMap((tranches) => {
return combineLatest(tranches.map((trancheId) => this.vaults(trancheId))).pipe(
Expand Down
47 changes: 24 additions & 23 deletions src/Reports/Processor.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import { AssetTransaction } from '../IndexerQueries/assetTransactions.js'
import { InvestorTransaction } from '../IndexerQueries/investorTransactions.js'
import { Currency, Price, Rate, Token } from '../utils/BigInt.js'
import { groupByPeriod } from '../utils/date.js'
import { PoolFeeTransaction } from '../IndexerQueries/poolFeeTransactions.js'
import {
AssetListData,
AssetListReport,
AssetListReportFilter,
AssetListReportPrivateCredit,
AssetListReportPublicCredit,
AssetTransactionReport,
AssetTransactionReportFilter,
AssetTransactionsData,
BalanceSheetData,
BalanceSheetReport,
CashflowData,
CashflowReport,
ProfitAndLossReport,
ProfitAndLossData,
ReportFilter,
FeeTransactionReport,
FeeTransactionReportFilter,
FeeTransactionsData,
InvestorListData,
InvestorListReport,
InvestorListReportFilter,
InvestorTransactionsData,
InvestorTransactionsReport,
InvestorTransactionsReportFilter,
AssetTransactionReport,
AssetTransactionsData,
AssetTransactionReportFilter,
FeeTransactionsData,
FeeTransactionReportFilter,
FeeTransactionReport,
ProfitAndLossData,
ProfitAndLossReport,
ReportFilter,
TokenPriceData,
TokenPriceReport,
TokenPriceReportFilter,
TokenPriceData,
AssetListReport,
AssetListReportFilter,
AssetListData,
AssetListReportPublicCredit,
AssetListReportPrivateCredit,
InvestorListData,
InvestorListReportFilter,
InvestorListReport,
} from '../types/reports.js'
import { PoolFeeTransaction } from '../IndexerQueries/poolFeeTransactions.js'
import { Currency, Price, Rate, Token } from '../utils/BigInt.js'
import { groupByPeriod } from '../utils/date.js'

export class Processor {
/**
Expand Down Expand Up @@ -299,7 +299,7 @@ export class Processor {
if (Object.values(data.trancheSnapshots).length === 0) return []
const items = Object.entries(data.trancheSnapshots).map(([timestamp, snapshots]) => ({
type: 'tokenPrice' as const,
timestamp: timestamp,
timestamp,
tranches: snapshots.map((snapshot) => ({
timestamp: snapshot.timestamp,
id: snapshot.trancheId,
Expand Down Expand Up @@ -327,7 +327,8 @@ export class Processor {
return isMaturityDatePassed && isDebtZero
} else if (filter?.status === 'overdue') {
return isMaturityDatePassed && !isDebtZero
} else return true
}
return true
})
.sort((a, b) => {
// Sort by actualMaturityDate in descending order
Expand Down
41 changes: 21 additions & 20 deletions src/Reports/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
import { Entity } from '../Entity.js'
import { combineLatest, map } from 'rxjs'
import { Centrifuge } from '../Centrifuge.js'
import { combineLatest } from 'rxjs'
import { processor } from './Processor.js'

import { map } from 'rxjs'
import { Entity } from '../Entity.js'
import { IndexerQueries } from '../IndexerQueries/index.js'
import { Pool } from '../Pool.js'
import { Query } from '../types/query.js'
import {
AssetListReport,
AssetListReportFilter,
AssetTransactionReport,
AssetTransactionReportFilter,
BalanceSheetReport,
CashflowReport,
InvestorTransactionsReport,
ProfitAndLossReport,
ReportFilter,
Report,
DataReport,
DataReportFilter,
InvestorTransactionsReportFilter,
AssetTransactionReport,
AssetTransactionReportFilter,
TokenPriceReport,
TokenPriceReportFilter,
FeeTransactionReport,
FeeTransactionReportFilter,
AssetListReportFilter,
AssetListReport,
InvestorListReportFilter,
InvestorListReport,
InvestorListReportFilter,
InvestorTransactionsReport,
InvestorTransactionsReportFilter,
ProfitAndLossReport,
Report,
ReportFilter,
TokenPriceReport,
TokenPriceReportFilter,
} from '../types/reports.js'
import { Query } from '../types/query.js'
import { Pool } from '../Pool.js'
import { IndexerQueries } from '../IndexerQueries/index.js'
import { processor } from './Processor.js'

const DEFAULT_FILTER: ReportFilter = {
from: '2024-01-01T00:00:00.000Z',
to: new Date().toISOString(),
}
export class Reports extends Entity {
private queries: IndexerQueries
/** @internal */
constructor(
centrifuge: Centrifuge,
public pool: Pool
Expand Down Expand Up @@ -83,6 +82,8 @@ export class Reports extends Entity {
* Reports are split into two types:
* - A `Report` is a standard report: balanceSheet, cashflow, profitAndLoss
* - A `DataReport` is a custom report: investorTransactions, assetTransactions, feeTransactions, tokenPrice, assetList, investorList
*
* @internal
*/
_generateReport<T>(type: Report, filter?: ReportFilter): Query<T[]>
_generateReport<T>(type: DataReport, filter?: DataReportFilter): Query<T[]>
Expand Down
1 change: 1 addition & 0 deletions src/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class Vault extends Entity {
* The contract address of the vault.
*/
address: HexString
/** @internal */
constructor(
_root: Centrifuge,
public network: PoolNetwork,
Expand Down
50 changes: 47 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,53 @@
import { Centrifuge } from './Centrifuge.js'
export type { CurrencyMetadata } from './config/lp.js'
export * from './Pool.js'
export * from './PoolNetwork.js'
export * from './types/index.js'
export * from './types/query.js'
export * from './types/transaction.js'
export * from './Reports/index.js'
export type { Client, Config, HexString } from './types/index.js'
export type { Query } from './types/query.js'
export type {
AssetListReport,
AssetListReportBase,
AssetListReportFilter,
AssetListReportPrivateCredit,
AssetListReportPublicCredit,
AssetTransactionReport,
AssetTransactionReportFilter,
BalanceSheetReport,
CashflowReport,
CashflowReportBase,
CashflowReportPrivateCredit,
CashflowReportPublicCredit,
FeeTransactionReport,
FeeTransactionReportFilter,
InvestorListReport,
InvestorListReportFilter,
InvestorTransactionsReport,
InvestorTransactionsReportFilter,
ProfitAndLossReport,
ProfitAndLossReportBase,
ProfitAndLossReportPrivateCredit,
ProfitAndLossReportPublicCredit,
ReportFilter,
TokenPriceReport,
TokenPriceReportFilter,
} from './types/reports.js'
export type {
EIP1193ProviderLike,
OperationConfirmedStatus,
OperationPendingStatus,
OperationSignedMessageStatus,
OperationSigningMessageStatus,
OperationSigningStatus,
OperationStatus,
OperationStatusType,
OperationSwitchChainStatus,
Signer,
Transaction,
} from './types/transaction.js'
export { Currency, Perquintill, Price, Rate } from './utils/BigInt.js'
export type { GroupBy } from './utils/date.js'
export * from './Vault.js'

export { Centrifuge }
export default Centrifuge
19 changes: 19 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
import { Chain, PublicClient } from 'viem'

export type Config = {
environment: 'mainnet' | 'demo' | 'dev'
rpcUrls?: Record<number | string, string>
indexerUrl: string
ipfsUrl: string
}

export type UserProvidedConfig = Partial<Config>
export type EnvConfig = {
indexerUrl: string
alchemyKey: string
infuraKey: string
defaultChain: number
ipfsUrl: string
}
export type DerivedConfig = Config & EnvConfig
export type Client = PublicClient<any, Chain>
export type HexString = `0x${string}`
Loading

0 comments on commit 486a30f

Please sign in to comment.