@kasufinance/kasu-sdk 1.0.4 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/.claude/settings.local.json +15 -0
  2. package/CLAUDE.md +410 -0
  3. package/README.md +179 -12
  4. package/dist/bundle.cjs.js +616 -67
  5. package/dist/bundle.esm.js +612 -69
  6. package/dist/facade/chain-configs.d.ts +11 -0
  7. package/dist/facade/chain-configs.js +84 -0
  8. package/dist/facade/chain-configs.js.map +1 -0
  9. package/dist/facade/deposits.d.ts +56 -0
  10. package/dist/facade/deposits.js +87 -0
  11. package/dist/facade/deposits.js.map +1 -0
  12. package/dist/facade/index.d.ts +6 -0
  13. package/dist/facade/index.js +9 -0
  14. package/dist/facade/index.js.map +1 -0
  15. package/dist/facade/kasu.d.ts +71 -0
  16. package/dist/facade/kasu.js +101 -0
  17. package/dist/facade/kasu.js.map +1 -0
  18. package/dist/facade/strategies.d.ts +37 -0
  19. package/dist/facade/strategies.js +112 -0
  20. package/dist/facade/strategies.js.map +1 -0
  21. package/dist/facade/types.d.ts +127 -0
  22. package/dist/facade/types.js +2 -0
  23. package/dist/facade/types.js.map +1 -0
  24. package/dist/facade/user-portfolio.d.ts +35 -0
  25. package/dist/facade/user-portfolio.js +53 -0
  26. package/dist/facade/user-portfolio.js.map +1 -0
  27. package/dist/index.d.ts +9 -17
  28. package/dist/index.js +9 -16
  29. package/dist/index.js.map +1 -1
  30. package/dist/kasu-sdk.d.ts +17 -0
  31. package/dist/kasu-sdk.js +17 -0
  32. package/dist/kasu-sdk.js.map +1 -0
  33. package/dist/sdk-config.d.ts +28 -4
  34. package/dist/sdk-config.js +8 -6
  35. package/dist/sdk-config.js.map +1 -1
  36. package/dist/services/DataService/data-service.d.ts +9 -2
  37. package/dist/services/DataService/data-service.js +90 -52
  38. package/dist/services/DataService/data-service.js.map +1 -1
  39. package/dist/services/DataService/queries.d.ts +0 -1
  40. package/dist/services/DataService/queries.js +0 -8
  41. package/dist/services/DataService/queries.js.map +1 -1
  42. package/dist/services/DataService/subgraph-types.d.ts +0 -6
  43. package/dist/services/DataService/types.d.ts +0 -1
  44. package/dist/services/Locking/locking.d.ts +4 -2
  45. package/dist/services/Locking/locking.js +89 -7
  46. package/dist/services/Locking/locking.js.map +1 -1
  47. package/dist/services/Portfolio/portfolio.d.ts +1 -0
  48. package/dist/services/Portfolio/portfolio.js +9 -1
  49. package/dist/services/Portfolio/portfolio.js.map +1 -1
  50. package/dist/tests/facade.test.d.ts +1 -0
  51. package/dist/tests/facade.test.js +196 -0
  52. package/dist/tests/facade.test.js.map +1 -0
  53. package/dist/tests/sample.test.js +19 -14
  54. package/dist/tests/sample.test.js.map +1 -1
  55. package/dist/utils/deployment-mode.d.ts +6 -0
  56. package/dist/utils/deployment-mode.js +8 -0
  57. package/dist/utils/deployment-mode.js.map +1 -0
  58. package/package.json +2 -2
  59. package/src/facade/chain-configs.ts +93 -0
  60. package/src/facade/deposits.ts +102 -0
  61. package/src/facade/index.ts +26 -0
  62. package/src/facade/kasu.ts +144 -0
  63. package/src/facade/strategies.ts +119 -0
  64. package/src/facade/types.ts +168 -0
  65. package/src/facade/user-portfolio.ts +73 -0
  66. package/src/index.ts +59 -25
  67. package/src/kasu-sdk.ts +27 -0
  68. package/src/sdk-config.ts +36 -15
  69. package/src/services/DataService/data-service.ts +105 -65
  70. package/src/services/DataService/queries.ts +0 -9
  71. package/src/services/DataService/subgraph-types.ts +0 -7
  72. package/src/services/DataService/types.ts +0 -1
  73. package/src/services/Locking/locking.ts +113 -12
  74. package/src/services/Portfolio/portfolio.ts +15 -5
  75. package/src/tests/facade.test.ts +245 -0
  76. package/src/tests/sample.test.ts +7 -9
  77. package/src/utils/deployment-mode.ts +9 -0
@@ -0,0 +1,168 @@
1
+ import { BigNumberish, BytesLike } from 'ethers';
2
+
3
+ import { ContractAddresses, SdkConfigOptions } from '../sdk-config';
4
+ import { LendingTotals, PoolOverview, TrancheData } from '../services/DataService/types';
5
+ import {
6
+ PortfolioLendingPool,
7
+ PortfolioSummary,
8
+ } from '../services/Portfolio/types';
9
+
10
+ // ---------------------------------------------------------------------------
11
+ // Chain & SDK Configuration
12
+ // ---------------------------------------------------------------------------
13
+
14
+ export type SupportedChain = 'base' | 'xdc' | 'plume';
15
+
16
+ export interface ChainConfigEntry {
17
+ chainId: number;
18
+ name: string;
19
+ isLiteDeployment: boolean;
20
+ contracts: ContractAddresses;
21
+ subgraphUrl: string;
22
+ directusUrl: string;
23
+ unusedPoolIds: string[];
24
+ poolMetadataMapping?: Record<string, string>;
25
+ }
26
+
27
+ /** Options passed to `Kasu.create()`. */
28
+ export interface KasuOptions {
29
+ /** A supported chain name or a custom `ChainConfigEntry`. */
30
+ chain: SupportedChain | ChainConfigEntry;
31
+ /** ethers Signer (for transactions) or Provider (read-only). */
32
+ signerOrProvider: import('ethers').Signer | import('@ethersproject/providers').Provider;
33
+ /** Override any default config value. */
34
+ configOverrides?: Partial<SdkConfigOptions>;
35
+ }
36
+
37
+ // ---------------------------------------------------------------------------
38
+ // Strategy (pool) types – integrator-friendly view of PoolOverview
39
+ // ---------------------------------------------------------------------------
40
+
41
+ export interface Strategy {
42
+ id: string;
43
+ name: string;
44
+ description: string;
45
+ isActive: boolean;
46
+ /** Weighted-average APY across tranches (decimal, e.g. 0.08 = 8 %). */
47
+ apy: number;
48
+ tvl: {
49
+ /** On-chain + off-chain total. */
50
+ total: string;
51
+ offchain: string;
52
+ };
53
+ /** Remaining capacity across all tranches. */
54
+ availableCapacity: string;
55
+ /** 0-1 utilisation ratio. */
56
+ capacityUtilisation: string;
57
+ tranches: StrategyTranche[];
58
+ /** Asset class label (e.g. "Tax Receivables"). */
59
+ assetClass: string;
60
+ /** Fixed or Variable. */
61
+ apyStructure: 'Variable' | 'Fixed';
62
+ /** Thumbnail image URL (empty string when Directus unavailable). */
63
+ thumbnailUrl: string;
64
+ /** Banner image URL (empty string when Directus unavailable). */
65
+ bannerUrl: string;
66
+ /** Raw `PoolOverview` for power-users who need all fields. */
67
+ _raw: PoolOverview;
68
+ }
69
+
70
+ export interface StrategyTranche {
71
+ id: string;
72
+ name: string;
73
+ /** Current base APY (decimal). */
74
+ apy: number;
75
+ minApy: number;
76
+ maxApy: number;
77
+ /** Minimum deposit in USDC (ether-formatted string). */
78
+ minimumDeposit: string;
79
+ /** Maximum deposit in USDC (ether-formatted string). */
80
+ maximumDeposit: string;
81
+ /** Remaining tranche capacity (ether-formatted string). */
82
+ availableCapacity: string;
83
+ /** Fixed-term deposit options on this tranche. */
84
+ fixedTermOptions: FixedTermOption[];
85
+ /** Raw `TrancheData` for power-users. */
86
+ _raw: TrancheData;
87
+ }
88
+
89
+ export interface FixedTermOption {
90
+ configId: string;
91
+ /** Annual APY (decimal). */
92
+ apy: number;
93
+ /** Lock duration in epochs (1 epoch ≈ 1 week). */
94
+ epochLockDuration: string;
95
+ }
96
+
97
+ // ---------------------------------------------------------------------------
98
+ // Deposit / Withdraw
99
+ // ---------------------------------------------------------------------------
100
+
101
+ export interface DepositParams {
102
+ poolId: string;
103
+ trancheId: string;
104
+ /** Amount in USDC base units (BigNumberish). */
105
+ amount: BigNumberish;
106
+ /** KYC signature obtained from the Nexera flow. */
107
+ kycSignature: {
108
+ blockExpiration: BigNumberish;
109
+ signature: BytesLike;
110
+ };
111
+ /** ABI-encoded deposit data (contract acceptance). Pass `'0x'` when not needed. */
112
+ depositData?: BytesLike;
113
+ /** Fixed-term config ID. Pass `0` for variable deposits. */
114
+ fixedTermConfigId?: BigNumberish;
115
+ /** Swap calldata. Pass `'0x'` when depositing USDC directly. */
116
+ swapData?: BytesLike;
117
+ /** Native token value to send (e.g. for gas on some chains). Defaults to `'0'`. */
118
+ ethValue?: string;
119
+ }
120
+
121
+ export interface WithdrawParams {
122
+ poolId: string;
123
+ trancheId: string;
124
+ /** USDC amount to withdraw, or `'max'` to withdraw entire balance. */
125
+ amount: BigNumberish;
126
+ /** Required when `amount` is `'max'`. */
127
+ userAddress?: string;
128
+ }
129
+
130
+ export interface DepositLimits {
131
+ /** Minimum deposit (ether-formatted USDC). */
132
+ min: string;
133
+ /** Maximum deposit (ether-formatted USDC). */
134
+ max: string;
135
+ /** Remaining tranche capacity (ether-formatted USDC). */
136
+ availableCapacity: string;
137
+ }
138
+
139
+ // ---------------------------------------------------------------------------
140
+ // KYC helpers
141
+ // ---------------------------------------------------------------------------
142
+
143
+ export interface KycParams {
144
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
145
+ contractAbi: any;
146
+ contractAddress: string;
147
+ functionName: string;
148
+ args: BytesLike[];
149
+ userAddress: string;
150
+ chainId: string;
151
+ }
152
+
153
+ // ---------------------------------------------------------------------------
154
+ // Portfolio
155
+ // ---------------------------------------------------------------------------
156
+
157
+ export interface UserPositions {
158
+ /** Per-pool breakdown. */
159
+ pools: PortfolioLendingPool[];
160
+ /** Aggregate summary (invested, yields, APY). */
161
+ summary: PortfolioSummary;
162
+ }
163
+
164
+ // ---------------------------------------------------------------------------
165
+ // Platform stats
166
+ // ---------------------------------------------------------------------------
167
+
168
+ export type PlatformStats = LendingTotals;
@@ -0,0 +1,73 @@
1
+ import { Provider } from '@ethersproject/providers';
2
+
3
+ import { DataService } from '../services/DataService/data-service';
4
+ import { Portfolio } from '../services/Portfolio/portfolio';
5
+ import { UserRequest } from '../services/UserLending/types';
6
+ import { UserLending } from '../services/UserLending/user-lending';
7
+
8
+ import { UserPositions } from './types';
9
+
10
+ /**
11
+ * High-level facade for querying a user's portfolio, positions, and history.
12
+ *
13
+ * Orchestrates DataService, UserLending, and Portfolio services so the caller
14
+ * only needs to provide a wallet address.
15
+ */
16
+ export class PortfolioFacade {
17
+ constructor(
18
+ private _dataService: DataService,
19
+ private _userLending: UserLending,
20
+ private _portfolio: Portfolio,
21
+ ) {}
22
+
23
+ /**
24
+ * Fetch a user's complete portfolio: per-pool positions and aggregate summary.
25
+ *
26
+ * ```ts
27
+ * const positions = await kasu.portfolio.getPositions('0xUser...');
28
+ * console.log(positions.summary.current.totalLendingPoolInvestments);
29
+ * ```
30
+ *
31
+ * @param userAddress - Wallet address.
32
+ * @param provider - Optional separate provider for balance queries (useful when the
33
+ * SDK was initialised with a Signer but you need a read-only provider).
34
+ */
35
+ async getPositions(
36
+ userAddress: string,
37
+ provider?: Provider,
38
+ ): Promise<UserPositions> {
39
+ const currentEpoch = await this._userLending.getCurrentEpoch();
40
+
41
+ const poolOverviews = await this._dataService.getPoolOverview(
42
+ currentEpoch,
43
+ );
44
+
45
+ const pools = await this._portfolio.getPortfolioLendingData(
46
+ userAddress,
47
+ poolOverviews,
48
+ currentEpoch,
49
+ provider,
50
+ );
51
+
52
+ const summary = await this._portfolio.getPortfolioSummary(
53
+ userAddress,
54
+ pools,
55
+ currentEpoch,
56
+ );
57
+
58
+ return { pools, summary };
59
+ }
60
+
61
+ /**
62
+ * Fetch a user's full transaction history (deposits, withdrawals, cancellations).
63
+ */
64
+ async getTransactionHistory(
65
+ userAddress: `0x${string}`,
66
+ ): Promise<UserRequest[]> {
67
+ const currentEpoch = await this._userLending.getCurrentEpoch();
68
+ return await this._userLending.getUserRequests(
69
+ userAddress,
70
+ currentEpoch,
71
+ );
72
+ }
73
+ }
package/src/index.ts CHANGED
@@ -1,27 +1,61 @@
1
- import { Provider } from '@ethersproject/providers';
2
- import { ethers, Signer } from 'ethers';
3
- import { GraphQLClient } from 'graphql-request';
1
+ // ---------------------------------------------------------------------------
2
+ // Core SDK class (low-level service access)
3
+ // ---------------------------------------------------------------------------
4
+ export { KasuSdk } from './kasu-sdk';
4
5
 
5
- import { SdkConfig } from './sdk-config';
6
- import { DataService } from './services/DataService/data-service';
7
- import { KSULocking } from './services/Locking/locking';
8
- import { Portfolio } from './services/Portfolio/portfolio';
9
- import { Swapper } from './services/Swapper/swapper';
10
- import { UserLending } from './services/UserLending/user-lending';
6
+ // ---------------------------------------------------------------------------
7
+ // Re-export config types
8
+ // ---------------------------------------------------------------------------
9
+ export type { ContractAddresses, SdkConfigOptions } from './sdk-config';
10
+ export { SdkConfig } from './sdk-config';
11
11
 
12
- export class KasuSdk {
13
- private readonly _graphClient: GraphQLClient;
14
- public readonly Locking: KSULocking;
15
- public readonly DataService: DataService;
16
- public readonly UserLending: UserLending;
17
- public readonly Portfolio: Portfolio;
18
- public readonly Swapper: Swapper;
19
- constructor(config: SdkConfig, signerOrProvider: Provider | Signer) {
20
- this._graphClient = new GraphQLClient(config.subgraphUrl);
21
- this.Locking = new KSULocking(config, signerOrProvider);
22
- this.UserLending = new UserLending(config, signerOrProvider);
23
- this.DataService = new DataService(config, signerOrProvider);
24
- this.Portfolio = new Portfolio(config, signerOrProvider);
25
- this.Swapper = new Swapper();
26
- }
27
- }
12
+ // ---------------------------------------------------------------------------
13
+ // Re-export service types so consumers don't need deep imports
14
+ // ---------------------------------------------------------------------------
15
+ export type {
16
+ PoolOverview,
17
+ TrancheData,
18
+ LendingTotals,
19
+ PoolRepayment,
20
+ RiskManagement,
21
+ PoolDelegateProfileAndHistory,
22
+ PoolTranche,
23
+ PoolCreditMetrics,
24
+ BadAndDoubtfulDebts,
25
+ FinancialReportingDocuments,
26
+ } from './services/DataService/types';
27
+
28
+ export type {
29
+ UserRequest,
30
+ UserRequestEvent,
31
+ UserTrancheBalance,
32
+ UserPoolBalance,
33
+ UserApyBonus,
34
+ PortfolioUserTrancheBalance,
35
+ } from './services/UserLending/types';
36
+
37
+ export type {
38
+ PortfolioSummary,
39
+ PortfolioLendingPool,
40
+ PortfolioTranche,
41
+ PortfolioRewards,
42
+ PortfolioTrancheDepositDetails,
43
+ } from './services/Portfolio/types';
44
+
45
+ export type {
46
+ UserLock,
47
+ UserBonusData,
48
+ LockPeriod,
49
+ GQLGetLockingPeriods,
50
+ SystemVariables,
51
+ } from './services/Locking/types';
52
+
53
+ export type {
54
+ PoolOverviewDirectus,
55
+ PlatformOverviewDirectus,
56
+ } from './services/DataService/directus-types';
57
+
58
+ // ---------------------------------------------------------------------------
59
+ // Re-export facade (high-level integrator API)
60
+ // ---------------------------------------------------------------------------
61
+ export * from './facade';
@@ -0,0 +1,27 @@
1
+ import { Provider } from '@ethersproject/providers';
2
+ import { Signer } from 'ethers';
3
+ import { GraphQLClient } from 'graphql-request';
4
+
5
+ import { SdkConfig } from './sdk-config';
6
+ import { DataService } from './services/DataService/data-service';
7
+ import { KSULocking } from './services/Locking/locking';
8
+ import { Portfolio } from './services/Portfolio/portfolio';
9
+ import { Swapper } from './services/Swapper/swapper';
10
+ import { UserLending } from './services/UserLending/user-lending';
11
+
12
+ export class KasuSdk {
13
+ private readonly _graphClient: GraphQLClient;
14
+ public readonly Locking: KSULocking;
15
+ public readonly DataService: DataService;
16
+ public readonly UserLending: UserLending;
17
+ public readonly Portfolio: Portfolio;
18
+ public readonly Swapper: Swapper;
19
+ constructor(config: SdkConfig, signerOrProvider: Provider | Signer) {
20
+ this._graphClient = new GraphQLClient(config.subgraphUrl);
21
+ this.Locking = new KSULocking(config, signerOrProvider);
22
+ this.UserLending = new UserLending(config, signerOrProvider);
23
+ this.DataService = new DataService(config, signerOrProvider);
24
+ this.Portfolio = new Portfolio(config, signerOrProvider);
25
+ this.Swapper = new Swapper();
26
+ }
27
+ }
package/src/sdk-config.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export interface ContractAddresses {
2
- KSUToken: string;
2
+ /** KSU Token address - only present on Full deployments (Base), undefined on Lite (XDC, Plume) */
3
+ KSUToken?: string;
3
4
  IKSULocking: string;
4
5
  IKSULockBonus: string;
5
6
  UserManager: string;
@@ -9,27 +10,47 @@ export interface ContractAddresses {
9
10
  UserLoyaltyRewards: string;
10
11
  KsuPrice: string;
11
12
  ClearingCoordinator: string;
12
- KasuNFTs: string;
13
+ /** KasuNFTs address - only present on Full deployments */
14
+ KasuNFTs?: string;
13
15
  ExternalTVL: string;
14
16
  }
17
+
18
+ export interface SdkConfigOptions {
19
+ subgraphUrl: string;
20
+ contracts: ContractAddresses;
21
+ /** Directus CMS URL. Optional – when omitted, pool descriptions/images will not be available but on-chain data still works. */
22
+ directusUrl?: string;
23
+ UNUSED_LENDING_POOL_IDS: string[];
24
+ /**
25
+ * Whether this is a Lite deployment (no KSU token, locking, or loyalty features).
26
+ * Set to true for XDC, Plume, and other chains without the full token system.
27
+ * @default false
28
+ */
29
+ isLiteDeployment?: boolean;
30
+ /**
31
+ * Maps pool addresses to their metadata source pool address in Directus.
32
+ * Used for chains (XDC, Plume) that share pool descriptions with Base.
33
+ * Key: pool address on this chain (lowercase)
34
+ * Value: pool address in Directus / Base pool (lowercase)
35
+ */
36
+ poolMetadataMapping?: Record<string, string>;
37
+ }
38
+
15
39
  export class SdkConfig {
16
40
  subgraphUrl: string;
17
- plumeSubgraphUrl: string;
18
41
  contracts: ContractAddresses;
42
+ /** Directus CMS URL. When empty, DataService skips CMS enrichment and returns on-chain data only. */
19
43
  directusUrl: string;
20
44
  UNUSED_LENDING_POOL_IDS: string[];
45
+ isLiteDeployment: boolean;
46
+ poolMetadataMapping: Record<string, string>;
21
47
 
22
- constructor(
23
- subgraphUrl: string,
24
- plumeSubgraphUrl: string,
25
- contracts: ContractAddresses,
26
- directusUrl: string,
27
- UNUSED_LENDING_POOL_IDS: string[],
28
- ) {
29
- this.subgraphUrl = subgraphUrl;
30
- this.plumeSubgraphUrl = plumeSubgraphUrl;
31
- this.contracts = contracts;
32
- this.directusUrl = directusUrl;
33
- this.UNUSED_LENDING_POOL_IDS = UNUSED_LENDING_POOL_IDS;
48
+ constructor(options: SdkConfigOptions) {
49
+ this.subgraphUrl = options.subgraphUrl;
50
+ this.contracts = options.contracts;
51
+ this.directusUrl = options.directusUrl ?? '';
52
+ this.UNUSED_LENDING_POOL_IDS = options.UNUSED_LENDING_POOL_IDS;
53
+ this.isLiteDeployment = options.isLiteDeployment ?? false;
54
+ this.poolMetadataMapping = options.poolMetadataMapping ?? {};
34
55
  }
35
56
  }