@kasufinance/kasu-sdk 1.0.4 → 2.2.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.
- package/CLAUDE.md +410 -0
- package/README.md +179 -12
- package/dist/bundle.cjs.js +658 -90
- package/dist/bundle.esm.js +654 -92
- package/dist/facade/chain-configs.d.ts +11 -0
- package/dist/facade/chain-configs.js +84 -0
- package/dist/facade/chain-configs.js.map +1 -0
- package/dist/facade/deposits.d.ts +56 -0
- package/dist/facade/deposits.js +87 -0
- package/dist/facade/deposits.js.map +1 -0
- package/dist/facade/index.d.ts +6 -0
- package/dist/facade/index.js +9 -0
- package/dist/facade/index.js.map +1 -0
- package/dist/facade/kasu.d.ts +71 -0
- package/dist/facade/kasu.js +101 -0
- package/dist/facade/kasu.js.map +1 -0
- package/dist/facade/strategies.d.ts +37 -0
- package/dist/facade/strategies.js +112 -0
- package/dist/facade/strategies.js.map +1 -0
- package/dist/facade/types.d.ts +127 -0
- package/dist/facade/types.js +2 -0
- package/dist/facade/types.js.map +1 -0
- package/dist/facade/user-portfolio.d.ts +35 -0
- package/dist/facade/user-portfolio.js +53 -0
- package/dist/facade/user-portfolio.js.map +1 -0
- package/dist/index.d.ts +9 -17
- package/dist/index.js +9 -16
- package/dist/index.js.map +1 -1
- package/dist/kasu-sdk.d.ts +17 -0
- package/dist/kasu-sdk.js +17 -0
- package/dist/kasu-sdk.js.map +1 -0
- package/dist/sdk-config.d.ts +34 -4
- package/dist/sdk-config.js +9 -6
- package/dist/sdk-config.js.map +1 -1
- package/dist/services/DataService/data-service.d.ts +9 -2
- package/dist/services/DataService/data-service.js +91 -53
- package/dist/services/DataService/data-service.js.map +1 -1
- package/dist/services/DataService/queries.d.ts +0 -1
- package/dist/services/DataService/queries.js +0 -8
- package/dist/services/DataService/queries.js.map +1 -1
- package/dist/services/DataService/subgraph-types.d.ts +0 -6
- package/dist/services/DataService/types.d.ts +0 -1
- package/dist/services/Locking/locking.d.ts +9 -3
- package/dist/services/Locking/locking.js +114 -20
- package/dist/services/Locking/locking.js.map +1 -1
- package/dist/services/Locking/types.d.ts +1 -1
- package/dist/services/Portfolio/portfolio.d.ts +3 -2
- package/dist/services/Portfolio/portfolio.js +15 -7
- package/dist/services/Portfolio/portfolio.js.map +1 -1
- package/dist/services/Portfolio/types.d.ts +2 -2
- package/dist/services/UserLending/user-lending.d.ts +3 -1
- package/dist/services/UserLending/user-lending.js +9 -3
- package/dist/services/UserLending/user-lending.js.map +1 -1
- package/dist/tests/facade.test.d.ts +1 -0
- package/dist/tests/facade.test.js +196 -0
- package/dist/tests/facade.test.js.map +1 -0
- package/dist/tests/sample.test.js +19 -14
- package/dist/tests/sample.test.js.map +1 -1
- package/dist/utils/deployment-mode.d.ts +6 -0
- package/dist/utils/deployment-mode.js +8 -0
- package/dist/utils/deployment-mode.js.map +1 -0
- package/package.json +2 -2
- package/src/facade/chain-configs.ts +93 -0
- package/src/facade/deposits.ts +102 -0
- package/src/facade/index.ts +26 -0
- package/src/facade/kasu.ts +144 -0
- package/src/facade/strategies.ts +119 -0
- package/src/facade/types.ts +168 -0
- package/src/facade/user-portfolio.ts +73 -0
- package/src/index.ts +59 -25
- package/src/kasu-sdk.ts +27 -0
- package/src/sdk-config.ts +43 -15
- package/src/services/DataService/data-service.ts +106 -66
- package/src/services/DataService/queries.ts +0 -9
- package/src/services/DataService/subgraph-types.ts +0 -7
- package/src/services/DataService/types.ts +0 -1
- package/src/services/Locking/locking.ts +141 -25
- package/src/services/Locking/types.ts +1 -1
- package/src/services/Portfolio/portfolio.ts +21 -11
- package/src/services/Portfolio/types.ts +2 -2
- package/src/services/UserLending/user-lending.ts +13 -4
- package/src/tests/facade.test.ts +245 -0
- package/src/tests/sample.test.ts +7 -9
- package/src/utils/deployment-mode.ts +9 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { KasuSdk } from '../kasu-sdk';
|
|
2
|
+
import { SdkConfig } from '../sdk-config';
|
|
3
|
+
|
|
4
|
+
import { CHAIN_CONFIGS } from './chain-configs';
|
|
5
|
+
import { DepositsFacade } from './deposits';
|
|
6
|
+
import { StrategiesFacade } from './strategies';
|
|
7
|
+
import { ChainConfigEntry, KasuOptions, SupportedChain } from './types';
|
|
8
|
+
import { PortfolioFacade } from './user-portfolio';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* High-level entry point for external integrators.
|
|
12
|
+
*
|
|
13
|
+
* Provides three domain facades — `strategies`, `deposits`, `portfolio` — and
|
|
14
|
+
* exposes the underlying `KasuSdk` services via `.services` for power-users.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { Kasu } from '@kasufinance/kasu-sdk';
|
|
18
|
+
*
|
|
19
|
+
* const kasu = Kasu.create({ chain: 'base', signerOrProvider: provider });
|
|
20
|
+
*
|
|
21
|
+
* // Browse strategies
|
|
22
|
+
* const strategies = await kasu.strategies.getAll();
|
|
23
|
+
*
|
|
24
|
+
* // Deposit
|
|
25
|
+
* const tx = await kasu.deposits.deposit({ poolId, trancheId, amount, kycSignature });
|
|
26
|
+
*
|
|
27
|
+
* // User positions
|
|
28
|
+
* const positions = await kasu.portfolio.getPositions(userAddress);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export class Kasu {
|
|
32
|
+
/** Browse lending strategies, APY, capacity. */
|
|
33
|
+
public readonly strategies: StrategiesFacade;
|
|
34
|
+
/** Deposit, withdraw, KYC helpers. */
|
|
35
|
+
public readonly deposits: DepositsFacade;
|
|
36
|
+
/** User positions, yields, transaction history. */
|
|
37
|
+
public readonly portfolio: PortfolioFacade;
|
|
38
|
+
|
|
39
|
+
private readonly _sdk: KasuSdk;
|
|
40
|
+
private readonly _chainConfig: ChainConfigEntry;
|
|
41
|
+
|
|
42
|
+
private constructor(
|
|
43
|
+
sdk: KasuSdk,
|
|
44
|
+
chainConfig: ChainConfigEntry,
|
|
45
|
+
) {
|
|
46
|
+
this._sdk = sdk;
|
|
47
|
+
this._chainConfig = chainConfig;
|
|
48
|
+
|
|
49
|
+
this.strategies = new StrategiesFacade(
|
|
50
|
+
sdk.DataService,
|
|
51
|
+
sdk.UserLending,
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
this.deposits = new DepositsFacade(
|
|
55
|
+
sdk.UserLending,
|
|
56
|
+
chainConfig.chainId.toString(),
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
this.portfolio = new PortfolioFacade(
|
|
60
|
+
sdk.DataService,
|
|
61
|
+
sdk.UserLending,
|
|
62
|
+
sdk.Portfolio,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Create a Kasu instance with built-in chain config.
|
|
68
|
+
*
|
|
69
|
+
* ```ts
|
|
70
|
+
* // Minimal setup — uses built-in Base mainnet config
|
|
71
|
+
* const kasu = Kasu.create({ chain: 'base', signerOrProvider: provider });
|
|
72
|
+
*
|
|
73
|
+
* // Custom config override
|
|
74
|
+
* const kasu = Kasu.create({
|
|
75
|
+
* chain: 'base',
|
|
76
|
+
* signerOrProvider: signer,
|
|
77
|
+
* configOverrides: { UNUSED_LENDING_POOL_IDS: ['0x...'] },
|
|
78
|
+
* });
|
|
79
|
+
*
|
|
80
|
+
* // Fully custom chain
|
|
81
|
+
* const kasu = Kasu.create({
|
|
82
|
+
* chain: { chainId: 123, name: 'MyChain', ... },
|
|
83
|
+
* signerOrProvider: provider,
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
static create(options: KasuOptions): Kasu {
|
|
88
|
+
const chainConfig = resolveChainConfig(options.chain);
|
|
89
|
+
const overrides = options.configOverrides ?? {};
|
|
90
|
+
|
|
91
|
+
const sdkConfig = new SdkConfig({
|
|
92
|
+
subgraphUrl: overrides.subgraphUrl ?? chainConfig.subgraphUrl,
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
94
|
+
contracts: overrides.contracts ?? chainConfig.contracts,
|
|
95
|
+
directusUrl:
|
|
96
|
+
overrides.directusUrl ?? chainConfig.directusUrl,
|
|
97
|
+
UNUSED_LENDING_POOL_IDS:
|
|
98
|
+
overrides.UNUSED_LENDING_POOL_IDS ??
|
|
99
|
+
chainConfig.unusedPoolIds,
|
|
100
|
+
isLiteDeployment:
|
|
101
|
+
overrides.isLiteDeployment ?? chainConfig.isLiteDeployment,
|
|
102
|
+
poolMetadataMapping:
|
|
103
|
+
overrides.poolMetadataMapping ??
|
|
104
|
+
chainConfig.poolMetadataMapping,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const sdk = new KasuSdk(sdkConfig, options.signerOrProvider);
|
|
108
|
+
return new Kasu(sdk, chainConfig);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Access the underlying `KasuSdk` services for advanced use cases.
|
|
113
|
+
*
|
|
114
|
+
* ```ts
|
|
115
|
+
* const locks = await kasu.services.Locking.getUserLocks(address);
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
get services(): KasuSdk {
|
|
119
|
+
return this._sdk;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The resolved chain configuration. */
|
|
123
|
+
get chainConfig(): ChainConfigEntry {
|
|
124
|
+
return this._chainConfig;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Whether this deployment has KSU token / locking features. */
|
|
128
|
+
get isLiteDeployment(): boolean {
|
|
129
|
+
return this._chainConfig.isLiteDeployment;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ---------------------------------------------------------------------------
|
|
134
|
+
// Helpers
|
|
135
|
+
// ---------------------------------------------------------------------------
|
|
136
|
+
|
|
137
|
+
function resolveChainConfig(
|
|
138
|
+
chain: SupportedChain | ChainConfigEntry,
|
|
139
|
+
): ChainConfigEntry {
|
|
140
|
+
if (typeof chain === 'string') {
|
|
141
|
+
return CHAIN_CONFIGS[chain];
|
|
142
|
+
}
|
|
143
|
+
return chain;
|
|
144
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { DataService } from '../services/DataService/data-service';
|
|
2
|
+
import { PoolOverview } from '../services/DataService/types';
|
|
3
|
+
import { UserLending } from '../services/UserLending/user-lending';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
DepositLimits,
|
|
7
|
+
FixedTermOption,
|
|
8
|
+
PlatformStats,
|
|
9
|
+
Strategy,
|
|
10
|
+
StrategyTranche,
|
|
11
|
+
} from './types';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* High-level facade for browsing Kasu lending strategies (pools).
|
|
15
|
+
*
|
|
16
|
+
* Handles epoch fetching internally so callers never need to know about epochs.
|
|
17
|
+
*/
|
|
18
|
+
export class StrategiesFacade {
|
|
19
|
+
constructor(
|
|
20
|
+
private _dataService: DataService,
|
|
21
|
+
private _userLending: UserLending,
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Fetch all active lending strategies.
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* const strategies = await kasu.strategies.getAll();
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
async getAll(poolIds?: string[]): Promise<Strategy[]> {
|
|
32
|
+
const epochId = await this._userLending.getCurrentEpoch();
|
|
33
|
+
const pools = await this._dataService.getPoolOverview(epochId, poolIds);
|
|
34
|
+
return pools.map((pool) => this.mapPoolToStrategy(pool));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Fetch a single strategy by pool ID. Returns `null` if not found.
|
|
39
|
+
*/
|
|
40
|
+
async getById(poolId: string): Promise<Strategy | null> {
|
|
41
|
+
const results = await this.getAll([poolId]);
|
|
42
|
+
return results[0] ?? null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Aggregate platform statistics (TVL, loans, yield, loss rate).
|
|
47
|
+
*/
|
|
48
|
+
async getPlatformStats(): Promise<PlatformStats> {
|
|
49
|
+
const epochId = await this._userLending.getCurrentEpoch();
|
|
50
|
+
const pools = await this._dataService.getPoolOverview(epochId);
|
|
51
|
+
return await this._dataService.getLendingTotals(pools);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Calculate deposit limits for a specific tranche.
|
|
56
|
+
*
|
|
57
|
+
* @param tranche - A `StrategyTranche` from `getAll()` or `getById()`.
|
|
58
|
+
* @returns min/max/capacity in ether-formatted USDC strings.
|
|
59
|
+
*/
|
|
60
|
+
calculateDepositLimits(tranche: StrategyTranche): DepositLimits {
|
|
61
|
+
const raw = tranche._raw;
|
|
62
|
+
const min =
|
|
63
|
+
parseFloat(raw.minimumDeposit) < 1
|
|
64
|
+
? '1'
|
|
65
|
+
: raw.minimumDeposit;
|
|
66
|
+
|
|
67
|
+
const max = parseFloat(raw.maximumDeposit) > parseFloat(raw.poolCapacity)
|
|
68
|
+
? raw.poolCapacity
|
|
69
|
+
: raw.maximumDeposit;
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
min,
|
|
73
|
+
max,
|
|
74
|
+
availableCapacity: raw.poolCapacity,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ------------------------------------------------------------------
|
|
79
|
+
// Private helpers
|
|
80
|
+
// ------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
private mapPoolToStrategy(pool: PoolOverview): Strategy {
|
|
83
|
+
const tranches: StrategyTranche[] = pool.tranches.map((t) => ({
|
|
84
|
+
id: t.id,
|
|
85
|
+
name: t.name,
|
|
86
|
+
apy: parseFloat(t.apy),
|
|
87
|
+
minApy: parseFloat(t.minApy),
|
|
88
|
+
maxApy: parseFloat(t.maxApy),
|
|
89
|
+
minimumDeposit: t.minimumDeposit,
|
|
90
|
+
maximumDeposit: t.maximumDeposit,
|
|
91
|
+
availableCapacity: t.poolCapacity,
|
|
92
|
+
fixedTermOptions: t.fixedTermConfig.map(
|
|
93
|
+
(ftd): FixedTermOption => ({
|
|
94
|
+
configId: ftd.configId,
|
|
95
|
+
apy: parseFloat(ftd.apy),
|
|
96
|
+
epochLockDuration: ftd.epochLockDuration,
|
|
97
|
+
}),
|
|
98
|
+
),
|
|
99
|
+
_raw: t,
|
|
100
|
+
}));
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
id: pool.id,
|
|
104
|
+
name: pool.poolName,
|
|
105
|
+
description: pool.description,
|
|
106
|
+
isActive: pool.isActive,
|
|
107
|
+
apy: pool.apy,
|
|
108
|
+
tvl: pool.totalValueLocked,
|
|
109
|
+
availableCapacity: pool.poolCapacity,
|
|
110
|
+
capacityUtilisation: pool.poolCapacityPercentage,
|
|
111
|
+
tranches,
|
|
112
|
+
assetClass: pool.assetClass,
|
|
113
|
+
apyStructure: pool.poolApyStructure,
|
|
114
|
+
thumbnailUrl: pool.thumbnailImageUrl,
|
|
115
|
+
bannerUrl: pool.bannerImageUrl,
|
|
116
|
+
_raw: pool,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -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 stable asset units (ether-formatted string). */
|
|
78
|
+
minimumDeposit: string;
|
|
79
|
+
/** Maximum deposit in stable asset units (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 stable asset 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 the stable asset 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
|
+
/** Stable asset 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 stable asset amount). */
|
|
132
|
+
min: string;
|
|
133
|
+
/** Maximum deposit (ether-formatted stable asset amount). */
|
|
134
|
+
max: string;
|
|
135
|
+
/** Remaining tranche capacity (ether-formatted stable asset amount). */
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Core SDK class (low-level service access)
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
export { KasuSdk } from './kasu-sdk';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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';
|
package/src/kasu-sdk.ts
ADDED
|
@@ -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
|
+
}
|