@liquidium/client 0.3.2 → 0.3.4
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/dist/index.cjs +268 -211
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +45 -9
- package/dist/index.d.ts +45 -9
- package/dist/index.js +266 -209
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -6,8 +6,7 @@ var agent = require('@icp-sdk/core/agent');
|
|
|
6
6
|
var principal = require('@icp-sdk/core/principal');
|
|
7
7
|
var base = require('@scure/base');
|
|
8
8
|
var candid = require('@icp-sdk/core/candid');
|
|
9
|
-
var
|
|
10
|
-
var principal$1 = require('@dfinity/principal');
|
|
9
|
+
var icrc = require('@icp-sdk/canisters/ledger/icrc');
|
|
11
10
|
var bitcoinAddressValidation = require('bitcoin-address-validation');
|
|
12
11
|
|
|
13
12
|
// src/client.ts
|
|
@@ -263,6 +262,7 @@ var idlFactory = ({ IDL }) => {
|
|
|
263
262
|
const Chains = IDL.Variant({
|
|
264
263
|
"BTC": IDL.Null,
|
|
265
264
|
"ETH": IDL.Null,
|
|
265
|
+
"ICP": IDL.Null,
|
|
266
266
|
"SOL": IDL.Null
|
|
267
267
|
});
|
|
268
268
|
const WalletType = IDL.Variant({ "Wallet": Chains });
|
|
@@ -320,8 +320,14 @@ var idlFactory = ({ IDL }) => {
|
|
|
320
320
|
"InsufficientFunds": IDL.Null
|
|
321
321
|
});
|
|
322
322
|
const Result = IDL.Variant({ "Ok": IDL.Null, "Err": ProtocolError });
|
|
323
|
+
const IcrcAccount = IDL.Record({
|
|
324
|
+
"owner": IDL.Principal,
|
|
325
|
+
"subaccount": IDL.Opt(IDL.Vec(IDL.Nat8))
|
|
326
|
+
});
|
|
323
327
|
const AccountType = IDL.Variant({
|
|
328
|
+
"Icrc": IcrcAccount,
|
|
324
329
|
"Native": IDL.Principal,
|
|
330
|
+
"AccountIdentifier": IDL.Text,
|
|
325
331
|
"External": IDL.Text
|
|
326
332
|
});
|
|
327
333
|
const BorrowAssetRequest = IDL.Record({
|
|
@@ -358,6 +364,7 @@ var idlFactory = ({ IDL }) => {
|
|
|
358
364
|
});
|
|
359
365
|
const Assets = IDL.Variant({
|
|
360
366
|
"BTC": IDL.Null,
|
|
367
|
+
"ICP": IDL.Null,
|
|
361
368
|
"SOL": IDL.Null,
|
|
362
369
|
"USDC": IDL.Null,
|
|
363
370
|
"USDT": IDL.Null
|
|
@@ -1388,8 +1395,14 @@ function extractVariantTag(variant, knownTags) {
|
|
|
1388
1395
|
|
|
1389
1396
|
// src/core/canisters/instant-loans/flexible-actor.ts
|
|
1390
1397
|
var flexibleInstantLoansIdlFactory = ({ IDL }) => {
|
|
1398
|
+
const IcrcAccount = IDL.Record({
|
|
1399
|
+
owner: IDL.Principal,
|
|
1400
|
+
subaccount: IDL.Opt(IDL.Vec(IDL.Nat8))
|
|
1401
|
+
});
|
|
1391
1402
|
const AccountType = IDL.Variant({
|
|
1403
|
+
Icrc: IcrcAccount,
|
|
1392
1404
|
Native: IDL.Principal,
|
|
1405
|
+
AccountIdentifier: IDL.Text,
|
|
1393
1406
|
External: IDL.Text
|
|
1394
1407
|
});
|
|
1395
1408
|
const SignatureVerificationError = IDL.Variant({
|
|
@@ -3038,6 +3051,189 @@ function createDepositAccountsActor(canisterContext) {
|
|
|
3038
3051
|
canisterId
|
|
3039
3052
|
});
|
|
3040
3053
|
}
|
|
3054
|
+
var flexibleLendingIdlFactory = ({ IDL }) => {
|
|
3055
|
+
const PoolRecord = IDL.Record({
|
|
3056
|
+
principal: IDL.Principal,
|
|
3057
|
+
asset: IDL.Unknown,
|
|
3058
|
+
chain: IDL.Unknown,
|
|
3059
|
+
total_supply_at_last_sync: IDL.Nat,
|
|
3060
|
+
total_debt_at_last_sync: IDL.Nat,
|
|
3061
|
+
supply_cap: IDL.Opt(IDL.Nat),
|
|
3062
|
+
borrow_cap: IDL.Opt(IDL.Nat),
|
|
3063
|
+
max_ltv: IDL.Nat64,
|
|
3064
|
+
liquidation_threshold: IDL.Nat64,
|
|
3065
|
+
liquidation_bonus: IDL.Nat64,
|
|
3066
|
+
protocol_liquidation_fee: IDL.Nat64,
|
|
3067
|
+
reserve_factor: IDL.Nat64,
|
|
3068
|
+
base_rate: IDL.Nat,
|
|
3069
|
+
optimal_utilization_rate: IDL.Nat,
|
|
3070
|
+
rate_slope_before: IDL.Nat,
|
|
3071
|
+
rate_slope_after: IDL.Nat,
|
|
3072
|
+
lending_index: IDL.Nat,
|
|
3073
|
+
borrow_index: IDL.Nat,
|
|
3074
|
+
same_asset_borrowing: IDL.Opt(IDL.Bool),
|
|
3075
|
+
frozen: IDL.Bool,
|
|
3076
|
+
last_updated: IDL.Opt(IDL.Nat64)
|
|
3077
|
+
});
|
|
3078
|
+
const BorrowingPowerRecord = IDL.Record({
|
|
3079
|
+
max_borrowable_usd: IDL.Nat,
|
|
3080
|
+
weighted_max_ltv: IDL.Nat
|
|
3081
|
+
});
|
|
3082
|
+
const PositionRecord = IDL.Record({
|
|
3083
|
+
asset: IDL.Unknown,
|
|
3084
|
+
total_debt_interest: IDL.Nat,
|
|
3085
|
+
borrow_index_snapshot: IDL.Nat,
|
|
3086
|
+
lending_index_snapshot: IDL.Nat,
|
|
3087
|
+
debt_scaled: IDL.Nat,
|
|
3088
|
+
total_earned_interest: IDL.Nat,
|
|
3089
|
+
deposit_scaled: IDL.Nat,
|
|
3090
|
+
pool_id: IDL.Principal,
|
|
3091
|
+
unpaid_debt_interest: IDL.Nat,
|
|
3092
|
+
last_update: IDL.Nat64,
|
|
3093
|
+
user_profile: IDL.Principal
|
|
3094
|
+
});
|
|
3095
|
+
const UserStatsRecord = IDL.Record({
|
|
3096
|
+
debt: IDL.Nat,
|
|
3097
|
+
collateral: IDL.Nat,
|
|
3098
|
+
acumulated_interest: IDL.Nat,
|
|
3099
|
+
borrowing_power: BorrowingPowerRecord,
|
|
3100
|
+
positions: IDL.Vec(PositionRecord),
|
|
3101
|
+
weighted_liquidation_threshold: IDL.Nat
|
|
3102
|
+
});
|
|
3103
|
+
const PositionViewRecord = IDL.Record({
|
|
3104
|
+
lending_index_now: IDL.Nat,
|
|
3105
|
+
interest_since_snapshot: IDL.Nat,
|
|
3106
|
+
asset: IDL.Unknown,
|
|
3107
|
+
total_debt_interest: IDL.Nat,
|
|
3108
|
+
borrow_index_snapshot: IDL.Nat,
|
|
3109
|
+
debt_native_now: IDL.Nat,
|
|
3110
|
+
borrow_index_now: IDL.Nat,
|
|
3111
|
+
lending_index_snapshot: IDL.Nat,
|
|
3112
|
+
debt_scaled: IDL.Nat,
|
|
3113
|
+
total_earned_interest: IDL.Nat,
|
|
3114
|
+
deposit_scaled: IDL.Nat,
|
|
3115
|
+
earned_since_snapshot: IDL.Nat,
|
|
3116
|
+
deposited_native_now: IDL.Nat,
|
|
3117
|
+
pool_id: IDL.Principal,
|
|
3118
|
+
last_update: IDL.Nat64,
|
|
3119
|
+
user_profile: IDL.Principal
|
|
3120
|
+
});
|
|
3121
|
+
return IDL.Service({
|
|
3122
|
+
list_pools: IDL.Func([], [IDL.Vec(PoolRecord)], ["query"]),
|
|
3123
|
+
get_pool_rate: IDL.Func(
|
|
3124
|
+
[IDL.Principal],
|
|
3125
|
+
[IDL.Opt(IDL.Tuple(IDL.Nat, IDL.Nat, IDL.Nat))],
|
|
3126
|
+
["query"]
|
|
3127
|
+
),
|
|
3128
|
+
get_health_factor: IDL.Func(
|
|
3129
|
+
[IDL.Principal],
|
|
3130
|
+
[IDL.Nat, UserStatsRecord],
|
|
3131
|
+
["query"]
|
|
3132
|
+
),
|
|
3133
|
+
get_profile_stats: IDL.Func([IDL.Principal], [UserStatsRecord], ["query"]),
|
|
3134
|
+
get_position: IDL.Func(
|
|
3135
|
+
[IDL.Principal, IDL.Principal],
|
|
3136
|
+
[IDL.Opt(PositionViewRecord)],
|
|
3137
|
+
["query"]
|
|
3138
|
+
)
|
|
3139
|
+
});
|
|
3140
|
+
};
|
|
3141
|
+
function createFlexibleLendingActor(canisterContext) {
|
|
3142
|
+
const canisterId = canisterContext.canisterIds.lending;
|
|
3143
|
+
if (!canisterId) {
|
|
3144
|
+
throw new LiquidiumError(
|
|
3145
|
+
LiquidiumErrorCode.SERVICE_UNAVAILABLE,
|
|
3146
|
+
"Lending canister ID is not configured"
|
|
3147
|
+
);
|
|
3148
|
+
}
|
|
3149
|
+
return agent.Actor.createActor(flexibleLendingIdlFactory, {
|
|
3150
|
+
agent: canisterContext.agent,
|
|
3151
|
+
canisterId
|
|
3152
|
+
});
|
|
3153
|
+
}
|
|
3154
|
+
function decodeFlexiblePool(pool) {
|
|
3155
|
+
const asset = extractVariantTag(pool.asset, KNOWN_ASSET_TAGS);
|
|
3156
|
+
const chain = extractVariantTag(pool.chain, KNOWN_CHAIN_TAGS);
|
|
3157
|
+
if (!asset || !chain) {
|
|
3158
|
+
return null;
|
|
3159
|
+
}
|
|
3160
|
+
return {
|
|
3161
|
+
principal: pool.principal,
|
|
3162
|
+
asset,
|
|
3163
|
+
chain,
|
|
3164
|
+
total_supply_at_last_sync: pool.total_supply_at_last_sync,
|
|
3165
|
+
total_debt_at_last_sync: pool.total_debt_at_last_sync,
|
|
3166
|
+
supply_cap: pool.supply_cap,
|
|
3167
|
+
borrow_cap: pool.borrow_cap,
|
|
3168
|
+
max_ltv: pool.max_ltv,
|
|
3169
|
+
liquidation_threshold: pool.liquidation_threshold,
|
|
3170
|
+
liquidation_bonus: pool.liquidation_bonus,
|
|
3171
|
+
protocol_liquidation_fee: pool.protocol_liquidation_fee,
|
|
3172
|
+
reserve_factor: pool.reserve_factor,
|
|
3173
|
+
base_rate: pool.base_rate,
|
|
3174
|
+
optimal_utilization_rate: pool.optimal_utilization_rate,
|
|
3175
|
+
rate_slope_before: pool.rate_slope_before,
|
|
3176
|
+
rate_slope_after: pool.rate_slope_after,
|
|
3177
|
+
lending_index: pool.lending_index,
|
|
3178
|
+
borrow_index: pool.borrow_index,
|
|
3179
|
+
same_asset_borrowing: pool.same_asset_borrowing,
|
|
3180
|
+
frozen: pool.frozen,
|
|
3181
|
+
last_updated: pool.last_updated
|
|
3182
|
+
};
|
|
3183
|
+
}
|
|
3184
|
+
function decodeFlexiblePosition(position) {
|
|
3185
|
+
const asset = extractVariantTag(position.asset, KNOWN_ASSET_TAGS);
|
|
3186
|
+
if (!asset) {
|
|
3187
|
+
return null;
|
|
3188
|
+
}
|
|
3189
|
+
return {
|
|
3190
|
+
asset,
|
|
3191
|
+
total_debt_interest: position.total_debt_interest,
|
|
3192
|
+
borrow_index_snapshot: position.borrow_index_snapshot,
|
|
3193
|
+
lending_index_snapshot: position.lending_index_snapshot,
|
|
3194
|
+
debt_scaled: position.debt_scaled,
|
|
3195
|
+
total_earned_interest: position.total_earned_interest,
|
|
3196
|
+
deposit_scaled: position.deposit_scaled,
|
|
3197
|
+
pool_id: position.pool_id,
|
|
3198
|
+
unpaid_debt_interest: position.unpaid_debt_interest,
|
|
3199
|
+
last_update: position.last_update,
|
|
3200
|
+
user_profile: position.user_profile
|
|
3201
|
+
};
|
|
3202
|
+
}
|
|
3203
|
+
function decodeFlexiblePositionView(view) {
|
|
3204
|
+
const asset = extractVariantTag(view.asset, KNOWN_ASSET_TAGS);
|
|
3205
|
+
if (!asset) {
|
|
3206
|
+
return null;
|
|
3207
|
+
}
|
|
3208
|
+
return {
|
|
3209
|
+
lending_index_now: view.lending_index_now,
|
|
3210
|
+
interest_since_snapshot: view.interest_since_snapshot,
|
|
3211
|
+
asset,
|
|
3212
|
+
total_debt_interest: view.total_debt_interest,
|
|
3213
|
+
borrow_index_snapshot: view.borrow_index_snapshot,
|
|
3214
|
+
debt_native_now: view.debt_native_now,
|
|
3215
|
+
borrow_index_now: view.borrow_index_now,
|
|
3216
|
+
lending_index_snapshot: view.lending_index_snapshot,
|
|
3217
|
+
debt_scaled: view.debt_scaled,
|
|
3218
|
+
total_earned_interest: view.total_earned_interest,
|
|
3219
|
+
deposit_scaled: view.deposit_scaled,
|
|
3220
|
+
earned_since_snapshot: view.earned_since_snapshot,
|
|
3221
|
+
deposited_native_now: view.deposited_native_now,
|
|
3222
|
+
pool_id: view.pool_id,
|
|
3223
|
+
last_update: view.last_update,
|
|
3224
|
+
user_profile: view.user_profile
|
|
3225
|
+
};
|
|
3226
|
+
}
|
|
3227
|
+
function decodeFlexibleUserStats(stats) {
|
|
3228
|
+
return {
|
|
3229
|
+
debt: stats.debt,
|
|
3230
|
+
collateral: stats.collateral,
|
|
3231
|
+
acumulated_interest: stats.acumulated_interest,
|
|
3232
|
+
borrowing_power: stats.borrowing_power,
|
|
3233
|
+
positions: stats.positions.map(decodeFlexiblePosition).filter((position) => position !== null),
|
|
3234
|
+
weighted_liquidation_threshold: stats.weighted_liquidation_threshold
|
|
3235
|
+
};
|
|
3236
|
+
}
|
|
3041
3237
|
|
|
3042
3238
|
// src/core/utils/inflow-subaccount.ts
|
|
3043
3239
|
var INFLOW_DEPOSIT_PREFIX = 1;
|
|
@@ -3075,8 +3271,8 @@ var EvmSupplyApprovalStrategy = {
|
|
|
3075
3271
|
// src/modules/lending/_internal/supply-targets.ts
|
|
3076
3272
|
async function resolveSupplyTarget(canisterContext, request) {
|
|
3077
3273
|
const selectedPool = await getPoolById(canisterContext, request.poolId);
|
|
3078
|
-
const asset =
|
|
3079
|
-
const chain =
|
|
3274
|
+
const asset = selectedPool.asset;
|
|
3275
|
+
const chain = selectedPool.chain;
|
|
3080
3276
|
const mechanism = resolveSupplyMechanism({
|
|
3081
3277
|
asset,
|
|
3082
3278
|
chain,
|
|
@@ -3182,15 +3378,16 @@ function mapDepositAccountErrorToLiquidiumError(error) {
|
|
|
3182
3378
|
);
|
|
3183
3379
|
}
|
|
3184
3380
|
async function getPoolById(canisterContext, poolId) {
|
|
3185
|
-
const pools = await
|
|
3381
|
+
const pools = await createFlexibleLendingActor(canisterContext).list_pools();
|
|
3186
3382
|
const selectedPool = pools.find((pool) => pool.principal.toText() === poolId);
|
|
3187
|
-
|
|
3383
|
+
const decodedPool = selectedPool ? decodeFlexiblePool(selectedPool) : null;
|
|
3384
|
+
if (!decodedPool) {
|
|
3188
3385
|
throw new LiquidiumError(
|
|
3189
3386
|
LiquidiumErrorCode.POOL_NOT_FOUND,
|
|
3190
3387
|
`Pool not found: ${poolId}`
|
|
3191
3388
|
);
|
|
3192
3389
|
}
|
|
3193
|
-
return
|
|
3390
|
+
return decodedPool;
|
|
3194
3391
|
}
|
|
3195
3392
|
async function getNativeAddressSupplyTarget(canisterContext, profileId, request) {
|
|
3196
3393
|
assertSupportsNativeAddressInflowTarget(request.asset, request.chain);
|
|
@@ -3262,8 +3459,8 @@ function getIcrcAccountSupplyTarget(profileId, request) {
|
|
|
3262
3459
|
action: request.action,
|
|
3263
3460
|
owner: poolPrincipal.toText(),
|
|
3264
3461
|
subaccount,
|
|
3265
|
-
account:
|
|
3266
|
-
owner:
|
|
3462
|
+
account: icrc.encodeIcrcAccount({
|
|
3463
|
+
owner: poolPrincipal,
|
|
3267
3464
|
subaccount
|
|
3268
3465
|
})
|
|
3269
3466
|
};
|
|
@@ -4337,8 +4534,32 @@ function accountFromCanister(account) {
|
|
|
4337
4534
|
if ("Native" in account) {
|
|
4338
4535
|
return { type: "Native", principal: account.Native.toText() };
|
|
4339
4536
|
}
|
|
4537
|
+
if ("AccountIdentifier" in account) {
|
|
4538
|
+
return {
|
|
4539
|
+
type: "AccountIdentifier",
|
|
4540
|
+
address: account.AccountIdentifier
|
|
4541
|
+
};
|
|
4542
|
+
}
|
|
4543
|
+
if ("Icrc" in account) {
|
|
4544
|
+
const subaccount = normalizeOptionalSubaccount(account.Icrc.subaccount[0]);
|
|
4545
|
+
return {
|
|
4546
|
+
type: "Icrc",
|
|
4547
|
+
owner: account.Icrc.owner.toText(),
|
|
4548
|
+
subaccount,
|
|
4549
|
+
address: icrc.encodeIcrcAccount({
|
|
4550
|
+
owner: account.Icrc.owner,
|
|
4551
|
+
subaccount
|
|
4552
|
+
})
|
|
4553
|
+
};
|
|
4554
|
+
}
|
|
4340
4555
|
return { type: "External", address: account.External };
|
|
4341
4556
|
}
|
|
4557
|
+
function normalizeOptionalSubaccount(subaccount) {
|
|
4558
|
+
if (!subaccount) {
|
|
4559
|
+
return void 0;
|
|
4560
|
+
}
|
|
4561
|
+
return subaccount instanceof Uint8Array ? subaccount : Uint8Array.from(subaccount);
|
|
4562
|
+
}
|
|
4342
4563
|
function mapInstantLoanEvent(event) {
|
|
4343
4564
|
return {
|
|
4344
4565
|
id: event.id,
|
|
@@ -4612,8 +4833,8 @@ function createTransferErc20Transaction(params) {
|
|
|
4612
4833
|
};
|
|
4613
4834
|
}
|
|
4614
4835
|
function createDepositErc20Transaction(params) {
|
|
4615
|
-
const expectedDestinationAccount =
|
|
4616
|
-
owner: principal
|
|
4836
|
+
const expectedDestinationAccount = icrc.encodeIcrcAccount({
|
|
4837
|
+
owner: principal.Principal.fromText(params.poolId),
|
|
4617
4838
|
subaccount: encodeInflowSubaccount({
|
|
4618
4839
|
action: params.action,
|
|
4619
4840
|
principal: principal.Principal.fromText(params.profileId)
|
|
@@ -4780,8 +5001,6 @@ function roundInflowFeeEstimate(request, totalFee) {
|
|
|
4780
5001
|
function roundUpToNearest(amount, roundingUnit) {
|
|
4781
5002
|
return ceilDivBigint(amount, roundingUnit) * roundingUnit;
|
|
4782
5003
|
}
|
|
4783
|
-
|
|
4784
|
-
// src/modules/lending/mappers.ts
|
|
4785
5004
|
function mapCanisterOutflowDetails(outflow) {
|
|
4786
5005
|
const rawOutflowType = getVariantKey(outflow.outflow_type);
|
|
4787
5006
|
return {
|
|
@@ -4800,11 +5019,35 @@ function mapCanisterAccountType(receiver) {
|
|
|
4800
5019
|
account: receiver.Native.toText()
|
|
4801
5020
|
};
|
|
4802
5021
|
}
|
|
5022
|
+
if ("AccountIdentifier" in receiver) {
|
|
5023
|
+
return {
|
|
5024
|
+
type: "AccountIdentifier",
|
|
5025
|
+
account: receiver.AccountIdentifier
|
|
5026
|
+
};
|
|
5027
|
+
}
|
|
5028
|
+
if ("Icrc" in receiver) {
|
|
5029
|
+
const subaccount = normalizeOptionalSubaccount2(receiver.Icrc.subaccount[0]);
|
|
5030
|
+
return {
|
|
5031
|
+
type: "Icrc",
|
|
5032
|
+
owner: receiver.Icrc.owner.toText(),
|
|
5033
|
+
subaccount,
|
|
5034
|
+
account: icrc.encodeIcrcAccount({
|
|
5035
|
+
owner: receiver.Icrc.owner,
|
|
5036
|
+
subaccount
|
|
5037
|
+
})
|
|
5038
|
+
};
|
|
5039
|
+
}
|
|
4803
5040
|
return {
|
|
4804
5041
|
type: "External",
|
|
4805
5042
|
account: receiver.External
|
|
4806
5043
|
};
|
|
4807
5044
|
}
|
|
5045
|
+
function normalizeOptionalSubaccount2(subaccount) {
|
|
5046
|
+
if (!subaccount) {
|
|
5047
|
+
return void 0;
|
|
5048
|
+
}
|
|
5049
|
+
return subaccount instanceof Uint8Array ? subaccount : Uint8Array.from(subaccount);
|
|
5050
|
+
}
|
|
4808
5051
|
function normalizeOutflowType(rawOutflowType) {
|
|
4809
5052
|
switch (rawOutflowType) {
|
|
4810
5053
|
case "Withdraw":
|
|
@@ -4998,7 +5241,7 @@ var LendingModule = class {
|
|
|
4998
5241
|
);
|
|
4999
5242
|
}
|
|
5000
5243
|
const selectedPool = await this.getPoolById(request.poolId);
|
|
5001
|
-
const selectedAsset =
|
|
5244
|
+
const selectedAsset = selectedPool.asset;
|
|
5002
5245
|
const minimumBorrowAmountError = getBorrowAmountMinimumValidationError({
|
|
5003
5246
|
amount: request.amount,
|
|
5004
5247
|
asset: selectedAsset
|
|
@@ -5012,7 +5255,7 @@ var LendingModule = class {
|
|
|
5012
5255
|
const receiverAddress = normalizeExternalAddress({
|
|
5013
5256
|
address: destinationAccount,
|
|
5014
5257
|
asset: selectedAsset,
|
|
5015
|
-
chain:
|
|
5258
|
+
chain: selectedPool.chain
|
|
5016
5259
|
});
|
|
5017
5260
|
const lendingActor = createLendingActor(this.canisterContext);
|
|
5018
5261
|
try {
|
|
@@ -5187,8 +5430,8 @@ var LendingModule = class {
|
|
|
5187
5430
|
const selectedPool = await this.getPoolById(request.poolId);
|
|
5188
5431
|
return await this.getEvmSupplyContextForPool({
|
|
5189
5432
|
request,
|
|
5190
|
-
asset:
|
|
5191
|
-
chain:
|
|
5433
|
+
asset: selectedPool.asset,
|
|
5434
|
+
chain: selectedPool.chain
|
|
5192
5435
|
});
|
|
5193
5436
|
}
|
|
5194
5437
|
async getEvmSupplyContextForPool(params) {
|
|
@@ -5603,24 +5846,27 @@ var LendingModule = class {
|
|
|
5603
5846
|
return this.evmReadClient;
|
|
5604
5847
|
}
|
|
5605
5848
|
async getPoolById(poolId) {
|
|
5606
|
-
const pools = await
|
|
5849
|
+
const pools = await createFlexibleLendingActor(
|
|
5850
|
+
this.canisterContext
|
|
5851
|
+
).list_pools();
|
|
5607
5852
|
const selectedPool = pools.find(
|
|
5608
5853
|
(pool) => pool.principal.toText() === poolId
|
|
5609
5854
|
);
|
|
5610
|
-
|
|
5855
|
+
const decodedPool = selectedPool ? decodeFlexiblePool(selectedPool) : null;
|
|
5856
|
+
if (!decodedPool) {
|
|
5611
5857
|
throw new LiquidiumError(
|
|
5612
5858
|
LiquidiumErrorCode.POOL_NOT_FOUND,
|
|
5613
5859
|
`Pool not found: ${poolId}`
|
|
5614
5860
|
);
|
|
5615
5861
|
}
|
|
5616
|
-
return
|
|
5862
|
+
return decodedPool;
|
|
5617
5863
|
}
|
|
5618
5864
|
async normalizeOutflowReceiverAddress(params) {
|
|
5619
5865
|
const selectedPool = await this.getPoolById(params.poolId);
|
|
5620
5866
|
return normalizeExternalAddress({
|
|
5621
5867
|
address: params.receiverAddress,
|
|
5622
|
-
asset:
|
|
5623
|
-
chain:
|
|
5868
|
+
asset: selectedPool.asset,
|
|
5869
|
+
chain: selectedPool.chain
|
|
5624
5870
|
});
|
|
5625
5871
|
}
|
|
5626
5872
|
async sendEthContractTransaction(walletAdapter, walletAddress, request, actionType) {
|
|
@@ -5704,12 +5950,6 @@ function mapExpectedOutflowDetails(details, expectedOutflowType, operation) {
|
|
|
5704
5950
|
`${operation} returned unexpected outflow type ${details.outflowType}`
|
|
5705
5951
|
);
|
|
5706
5952
|
}
|
|
5707
|
-
if (details.receiver.type !== "External") {
|
|
5708
|
-
throw new LiquidiumError(
|
|
5709
|
-
LiquidiumErrorCode.INTERNAL,
|
|
5710
|
-
`${operation} returned unexpected receiver type ${details.receiver.type}`
|
|
5711
|
-
);
|
|
5712
|
-
}
|
|
5713
5953
|
return details;
|
|
5714
5954
|
}
|
|
5715
5955
|
async function delay(timeoutMs) {
|
|
@@ -5745,189 +5985,6 @@ function shouldSubmitInflow(params) {
|
|
|
5745
5985
|
}
|
|
5746
5986
|
return !isEthStablecoin(params.instruction.asset, params.instruction.chain);
|
|
5747
5987
|
}
|
|
5748
|
-
var flexibleLendingIdlFactory = ({ IDL }) => {
|
|
5749
|
-
const PoolRecord = IDL.Record({
|
|
5750
|
-
principal: IDL.Principal,
|
|
5751
|
-
asset: IDL.Unknown,
|
|
5752
|
-
chain: IDL.Unknown,
|
|
5753
|
-
total_supply_at_last_sync: IDL.Nat,
|
|
5754
|
-
total_debt_at_last_sync: IDL.Nat,
|
|
5755
|
-
supply_cap: IDL.Opt(IDL.Nat),
|
|
5756
|
-
borrow_cap: IDL.Opt(IDL.Nat),
|
|
5757
|
-
max_ltv: IDL.Nat64,
|
|
5758
|
-
liquidation_threshold: IDL.Nat64,
|
|
5759
|
-
liquidation_bonus: IDL.Nat64,
|
|
5760
|
-
protocol_liquidation_fee: IDL.Nat64,
|
|
5761
|
-
reserve_factor: IDL.Nat64,
|
|
5762
|
-
base_rate: IDL.Nat,
|
|
5763
|
-
optimal_utilization_rate: IDL.Nat,
|
|
5764
|
-
rate_slope_before: IDL.Nat,
|
|
5765
|
-
rate_slope_after: IDL.Nat,
|
|
5766
|
-
lending_index: IDL.Nat,
|
|
5767
|
-
borrow_index: IDL.Nat,
|
|
5768
|
-
same_asset_borrowing: IDL.Opt(IDL.Bool),
|
|
5769
|
-
frozen: IDL.Bool,
|
|
5770
|
-
last_updated: IDL.Opt(IDL.Nat64)
|
|
5771
|
-
});
|
|
5772
|
-
const BorrowingPowerRecord = IDL.Record({
|
|
5773
|
-
max_borrowable_usd: IDL.Nat,
|
|
5774
|
-
weighted_max_ltv: IDL.Nat
|
|
5775
|
-
});
|
|
5776
|
-
const PositionRecord = IDL.Record({
|
|
5777
|
-
asset: IDL.Unknown,
|
|
5778
|
-
total_debt_interest: IDL.Nat,
|
|
5779
|
-
borrow_index_snapshot: IDL.Nat,
|
|
5780
|
-
lending_index_snapshot: IDL.Nat,
|
|
5781
|
-
debt_scaled: IDL.Nat,
|
|
5782
|
-
total_earned_interest: IDL.Nat,
|
|
5783
|
-
deposit_scaled: IDL.Nat,
|
|
5784
|
-
pool_id: IDL.Principal,
|
|
5785
|
-
unpaid_debt_interest: IDL.Nat,
|
|
5786
|
-
last_update: IDL.Nat64,
|
|
5787
|
-
user_profile: IDL.Principal
|
|
5788
|
-
});
|
|
5789
|
-
const UserStatsRecord = IDL.Record({
|
|
5790
|
-
debt: IDL.Nat,
|
|
5791
|
-
collateral: IDL.Nat,
|
|
5792
|
-
acumulated_interest: IDL.Nat,
|
|
5793
|
-
borrowing_power: BorrowingPowerRecord,
|
|
5794
|
-
positions: IDL.Vec(PositionRecord),
|
|
5795
|
-
weighted_liquidation_threshold: IDL.Nat
|
|
5796
|
-
});
|
|
5797
|
-
const PositionViewRecord = IDL.Record({
|
|
5798
|
-
lending_index_now: IDL.Nat,
|
|
5799
|
-
interest_since_snapshot: IDL.Nat,
|
|
5800
|
-
asset: IDL.Unknown,
|
|
5801
|
-
total_debt_interest: IDL.Nat,
|
|
5802
|
-
borrow_index_snapshot: IDL.Nat,
|
|
5803
|
-
debt_native_now: IDL.Nat,
|
|
5804
|
-
borrow_index_now: IDL.Nat,
|
|
5805
|
-
lending_index_snapshot: IDL.Nat,
|
|
5806
|
-
debt_scaled: IDL.Nat,
|
|
5807
|
-
total_earned_interest: IDL.Nat,
|
|
5808
|
-
deposit_scaled: IDL.Nat,
|
|
5809
|
-
earned_since_snapshot: IDL.Nat,
|
|
5810
|
-
deposited_native_now: IDL.Nat,
|
|
5811
|
-
pool_id: IDL.Principal,
|
|
5812
|
-
last_update: IDL.Nat64,
|
|
5813
|
-
user_profile: IDL.Principal
|
|
5814
|
-
});
|
|
5815
|
-
return IDL.Service({
|
|
5816
|
-
list_pools: IDL.Func([], [IDL.Vec(PoolRecord)], ["query"]),
|
|
5817
|
-
get_pool_rate: IDL.Func(
|
|
5818
|
-
[IDL.Principal],
|
|
5819
|
-
[IDL.Opt(IDL.Tuple(IDL.Nat, IDL.Nat, IDL.Nat))],
|
|
5820
|
-
["query"]
|
|
5821
|
-
),
|
|
5822
|
-
get_health_factor: IDL.Func(
|
|
5823
|
-
[IDL.Principal],
|
|
5824
|
-
[IDL.Nat, UserStatsRecord],
|
|
5825
|
-
["query"]
|
|
5826
|
-
),
|
|
5827
|
-
get_profile_stats: IDL.Func([IDL.Principal], [UserStatsRecord], ["query"]),
|
|
5828
|
-
get_position: IDL.Func(
|
|
5829
|
-
[IDL.Principal, IDL.Principal],
|
|
5830
|
-
[IDL.Opt(PositionViewRecord)],
|
|
5831
|
-
["query"]
|
|
5832
|
-
)
|
|
5833
|
-
});
|
|
5834
|
-
};
|
|
5835
|
-
function createFlexibleLendingActor(canisterContext) {
|
|
5836
|
-
const canisterId = canisterContext.canisterIds.lending;
|
|
5837
|
-
if (!canisterId) {
|
|
5838
|
-
throw new LiquidiumError(
|
|
5839
|
-
LiquidiumErrorCode.SERVICE_UNAVAILABLE,
|
|
5840
|
-
"Lending canister ID is not configured"
|
|
5841
|
-
);
|
|
5842
|
-
}
|
|
5843
|
-
return agent.Actor.createActor(flexibleLendingIdlFactory, {
|
|
5844
|
-
agent: canisterContext.agent,
|
|
5845
|
-
canisterId
|
|
5846
|
-
});
|
|
5847
|
-
}
|
|
5848
|
-
function decodeFlexiblePool(pool) {
|
|
5849
|
-
const asset = extractVariantTag(pool.asset, KNOWN_ASSET_TAGS);
|
|
5850
|
-
const chain = extractVariantTag(pool.chain, KNOWN_CHAIN_TAGS);
|
|
5851
|
-
if (!asset || !chain) {
|
|
5852
|
-
return null;
|
|
5853
|
-
}
|
|
5854
|
-
return {
|
|
5855
|
-
principal: pool.principal,
|
|
5856
|
-
asset,
|
|
5857
|
-
chain,
|
|
5858
|
-
total_supply_at_last_sync: pool.total_supply_at_last_sync,
|
|
5859
|
-
total_debt_at_last_sync: pool.total_debt_at_last_sync,
|
|
5860
|
-
supply_cap: pool.supply_cap,
|
|
5861
|
-
borrow_cap: pool.borrow_cap,
|
|
5862
|
-
max_ltv: pool.max_ltv,
|
|
5863
|
-
liquidation_threshold: pool.liquidation_threshold,
|
|
5864
|
-
liquidation_bonus: pool.liquidation_bonus,
|
|
5865
|
-
protocol_liquidation_fee: pool.protocol_liquidation_fee,
|
|
5866
|
-
reserve_factor: pool.reserve_factor,
|
|
5867
|
-
base_rate: pool.base_rate,
|
|
5868
|
-
optimal_utilization_rate: pool.optimal_utilization_rate,
|
|
5869
|
-
rate_slope_before: pool.rate_slope_before,
|
|
5870
|
-
rate_slope_after: pool.rate_slope_after,
|
|
5871
|
-
lending_index: pool.lending_index,
|
|
5872
|
-
borrow_index: pool.borrow_index,
|
|
5873
|
-
same_asset_borrowing: pool.same_asset_borrowing,
|
|
5874
|
-
frozen: pool.frozen,
|
|
5875
|
-
last_updated: pool.last_updated
|
|
5876
|
-
};
|
|
5877
|
-
}
|
|
5878
|
-
function decodeFlexiblePosition(position) {
|
|
5879
|
-
const asset = extractVariantTag(position.asset, KNOWN_ASSET_TAGS);
|
|
5880
|
-
if (!asset) {
|
|
5881
|
-
return null;
|
|
5882
|
-
}
|
|
5883
|
-
return {
|
|
5884
|
-
asset,
|
|
5885
|
-
total_debt_interest: position.total_debt_interest,
|
|
5886
|
-
borrow_index_snapshot: position.borrow_index_snapshot,
|
|
5887
|
-
lending_index_snapshot: position.lending_index_snapshot,
|
|
5888
|
-
debt_scaled: position.debt_scaled,
|
|
5889
|
-
total_earned_interest: position.total_earned_interest,
|
|
5890
|
-
deposit_scaled: position.deposit_scaled,
|
|
5891
|
-
pool_id: position.pool_id,
|
|
5892
|
-
unpaid_debt_interest: position.unpaid_debt_interest,
|
|
5893
|
-
last_update: position.last_update,
|
|
5894
|
-
user_profile: position.user_profile
|
|
5895
|
-
};
|
|
5896
|
-
}
|
|
5897
|
-
function decodeFlexiblePositionView(view) {
|
|
5898
|
-
const asset = extractVariantTag(view.asset, KNOWN_ASSET_TAGS);
|
|
5899
|
-
if (!asset) {
|
|
5900
|
-
return null;
|
|
5901
|
-
}
|
|
5902
|
-
return {
|
|
5903
|
-
lending_index_now: view.lending_index_now,
|
|
5904
|
-
interest_since_snapshot: view.interest_since_snapshot,
|
|
5905
|
-
asset,
|
|
5906
|
-
total_debt_interest: view.total_debt_interest,
|
|
5907
|
-
borrow_index_snapshot: view.borrow_index_snapshot,
|
|
5908
|
-
debt_native_now: view.debt_native_now,
|
|
5909
|
-
borrow_index_now: view.borrow_index_now,
|
|
5910
|
-
lending_index_snapshot: view.lending_index_snapshot,
|
|
5911
|
-
debt_scaled: view.debt_scaled,
|
|
5912
|
-
total_earned_interest: view.total_earned_interest,
|
|
5913
|
-
deposit_scaled: view.deposit_scaled,
|
|
5914
|
-
earned_since_snapshot: view.earned_since_snapshot,
|
|
5915
|
-
deposited_native_now: view.deposited_native_now,
|
|
5916
|
-
pool_id: view.pool_id,
|
|
5917
|
-
last_update: view.last_update,
|
|
5918
|
-
user_profile: view.user_profile
|
|
5919
|
-
};
|
|
5920
|
-
}
|
|
5921
|
-
function decodeFlexibleUserStats(stats) {
|
|
5922
|
-
return {
|
|
5923
|
-
debt: stats.debt,
|
|
5924
|
-
collateral: stats.collateral,
|
|
5925
|
-
acumulated_interest: stats.acumulated_interest,
|
|
5926
|
-
borrowing_power: stats.borrowing_power,
|
|
5927
|
-
positions: stats.positions.map(decodeFlexiblePosition).filter((position) => position !== null),
|
|
5928
|
-
weighted_liquidation_threshold: stats.weighted_liquidation_threshold
|
|
5929
|
-
};
|
|
5930
|
-
}
|
|
5931
5988
|
|
|
5932
5989
|
// src/modules/market/mappers.ts
|
|
5933
5990
|
var DECIMAL_BASE = 10;
|