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