@liquidium/client 0.3.0 → 0.3.1

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 CHANGED
@@ -4,6 +4,7 @@ var viem = require('viem');
4
4
  var chains = require('viem/chains');
5
5
  var agent = require('@icp-sdk/core/agent');
6
6
  var principal = require('@icp-sdk/core/principal');
7
+ var candid = require('@icp-sdk/core/candid');
7
8
  var ledgerIcrc = require('@dfinity/ledger-icrc');
8
9
  var principal$1 = require('@dfinity/principal');
9
10
  var bitcoinAddressValidation = require('bitcoin-address-validation');
@@ -1307,17 +1308,35 @@ function mapCanisterWalletToWallet(canisterWallet) {
1307
1308
  );
1308
1309
  }
1309
1310
  }
1310
- var idlFactory2 = ({ IDL }) => {
1311
+ var KNOWN_ASSET_TAGS = ["BTC", "SOL", "USDC", "USDT"];
1312
+ var KNOWN_CHAIN_TAGS = ["BTC", "ETH", "SOL"];
1313
+ function extractVariantTag(variant, knownTags) {
1314
+ const [key] = Object.keys(variant);
1315
+ if (!key) {
1316
+ return null;
1317
+ }
1318
+ if (knownTags.includes(key)) {
1319
+ return key;
1320
+ }
1321
+ const hashMatch = /^_(\d+)_$/.exec(key);
1322
+ if (!hashMatch) {
1323
+ return null;
1324
+ }
1325
+ const hash = Number(hashMatch[1]);
1326
+ for (const tag of knownTags) {
1327
+ if (candid.idlLabelToId(tag) === hash) {
1328
+ return tag;
1329
+ }
1330
+ }
1331
+ return null;
1332
+ }
1333
+
1334
+ // src/core/canisters/instant-loans/flexible-actor.ts
1335
+ var flexibleInstantLoansIdlFactory = ({ IDL }) => {
1311
1336
  const AccountType = IDL.Variant({
1312
1337
  Native: IDL.Principal,
1313
1338
  External: IDL.Text
1314
1339
  });
1315
- const Assets = IDL.Variant({
1316
- BTC: IDL.Null,
1317
- SOL: IDL.Null,
1318
- USDC: IDL.Null,
1319
- USDT: IDL.Null
1320
- });
1321
1340
  const SignatureVerificationError = IDL.Variant({
1322
1341
  InvalidEthSignature: IDL.Null,
1323
1342
  UnsupportedChain: IDL.Null,
@@ -1388,13 +1407,13 @@ var idlFactory2 = ({ IDL }) => {
1388
1407
  });
1389
1408
  const CreateLoanRequest = IDL.Record({
1390
1409
  borrow_destination: AccountType,
1391
- lend_asset: Assets,
1410
+ lend_asset: IDL.Unknown,
1392
1411
  borrow_amount: IDL.Nat,
1393
1412
  lend_pool_id: IDL.Principal,
1394
1413
  refund_destination: AccountType,
1395
1414
  ltv_max_bps: IDL.Nat64,
1396
1415
  borrow_pool_id: IDL.Principal,
1397
- borrow_asset: Assets,
1416
+ borrow_asset: IDL.Unknown,
1398
1417
  ltv_timer_s: IDL.Nat64
1399
1418
  });
1400
1419
  const CreateLoanResponse = IDL.Record({
@@ -1410,7 +1429,7 @@ var idlFactory2 = ({ IDL }) => {
1410
1429
  LoanCreated: IDL.Record({
1411
1430
  loan_id: IDL.Nat,
1412
1431
  borrow_destination: AccountType,
1413
- lend_asset: Assets,
1432
+ lend_asset: IDL.Unknown,
1414
1433
  borrow_amount: IDL.Nat,
1415
1434
  lend_pool_id: IDL.Principal,
1416
1435
  refund_destination: AccountType,
@@ -1418,7 +1437,7 @@ var idlFactory2 = ({ IDL }) => {
1418
1437
  ltv_timer_s: IDL.Nat64,
1419
1438
  lending_profile: IDL.Principal,
1420
1439
  borrow_pool_id: IDL.Principal,
1421
- borrow_asset: Assets
1440
+ borrow_asset: IDL.Unknown
1422
1441
  }),
1423
1442
  FullLendWithdrawalRequested: IDL.Record({
1424
1443
  loan_id: IDL.Nat,
@@ -1469,7 +1488,7 @@ var idlFactory2 = ({ IDL }) => {
1469
1488
  authorisation: AuthorisationType,
1470
1489
  borrow_destination: AccountType,
1471
1490
  started: IDL.Bool,
1472
- lend_asset: Assets,
1491
+ lend_asset: IDL.Unknown,
1473
1492
  created_at: IDL.Nat64,
1474
1493
  schema_version: IDL.Nat16,
1475
1494
  borrow_amount: IDL.Nat,
@@ -1480,7 +1499,7 @@ var idlFactory2 = ({ IDL }) => {
1480
1499
  lending_profile: IDL.Principal,
1481
1500
  expires_at: IDL.Opt(IDL.Nat64),
1482
1501
  borrow_pool_id: IDL.Principal,
1483
- borrow_asset: Assets,
1502
+ borrow_asset: IDL.Unknown,
1484
1503
  deposit_detected_ts: IDL.Opt(IDL.Nat64)
1485
1504
  });
1486
1505
  const LoanResult = IDL.Variant({ Ok: Loan, Err: InstantLoansError });
@@ -1508,7 +1527,7 @@ var idlFactory2 = ({ IDL }) => {
1508
1527
  list_warmed_profiles: IDL.Func([], [IDL.Vec(WarmedProfile)], ["query"])
1509
1528
  });
1510
1529
  };
1511
- function createInstantLoansActor(canisterContext) {
1530
+ function createFlexibleInstantLoansActor(canisterContext) {
1512
1531
  const canisterId = canisterContext.canisterIds.instantLoans;
1513
1532
  if (!canisterId) {
1514
1533
  throw new LiquidiumError(
@@ -1516,10 +1535,84 @@ function createInstantLoansActor(canisterContext) {
1516
1535
  "Instant loans canister ID is not configured"
1517
1536
  );
1518
1537
  }
1519
- return agent.Actor.createActor(idlFactory2, {
1520
- agent: canisterContext.agent,
1521
- canisterId
1522
- });
1538
+ return agent.Actor.createActor(
1539
+ flexibleInstantLoansIdlFactory,
1540
+ {
1541
+ agent: canisterContext.agent,
1542
+ canisterId
1543
+ }
1544
+ );
1545
+ }
1546
+ function decodeFlexibleInstantLoanRecord(record) {
1547
+ const lend_asset = extractVariantTag(record.lend_asset, KNOWN_ASSET_TAGS);
1548
+ const borrow_asset = extractVariantTag(record.borrow_asset, KNOWN_ASSET_TAGS);
1549
+ if (!lend_asset || !borrow_asset) {
1550
+ return null;
1551
+ }
1552
+ return {
1553
+ id: record.id,
1554
+ authorisation: record.authorisation,
1555
+ borrow_destination: record.borrow_destination,
1556
+ started: record.started,
1557
+ lend_asset,
1558
+ created_at: record.created_at,
1559
+ schema_version: record.schema_version,
1560
+ borrow_amount: record.borrow_amount,
1561
+ lend_pool_id: record.lend_pool_id,
1562
+ refund_destination: record.refund_destination,
1563
+ ltv_max_bps: record.ltv_max_bps,
1564
+ ltv_timer_s: record.ltv_timer_s,
1565
+ lending_profile: record.lending_profile,
1566
+ borrow_pool_id: record.borrow_pool_id,
1567
+ borrow_asset,
1568
+ expires_at: record.expires_at,
1569
+ deposit_detected_ts: record.deposit_detected_ts
1570
+ };
1571
+ }
1572
+ function decodeFlexibleHeadlessLoanEvent(event) {
1573
+ if ("LoanCreated" in event.event_type) {
1574
+ const decoded = decodeLoanCreatedEvent(event.event_type.LoanCreated);
1575
+ if (!decoded) {
1576
+ return null;
1577
+ }
1578
+ return {
1579
+ id: event.id,
1580
+ schema_version: event.schema_version,
1581
+ timestamp: event.timestamp,
1582
+ event_type: decoded
1583
+ };
1584
+ }
1585
+ return {
1586
+ id: event.id,
1587
+ schema_version: event.schema_version,
1588
+ timestamp: event.timestamp,
1589
+ event_type: event.event_type
1590
+ };
1591
+ }
1592
+ function decodeLoanCreatedEvent(payload) {
1593
+ const lend_asset = extractVariantTag(payload.lend_asset, KNOWN_ASSET_TAGS);
1594
+ const borrow_asset = extractVariantTag(
1595
+ payload.borrow_asset,
1596
+ KNOWN_ASSET_TAGS
1597
+ );
1598
+ if (!lend_asset || !borrow_asset) {
1599
+ return null;
1600
+ }
1601
+ return {
1602
+ LoanCreated: {
1603
+ loan_id: payload.loan_id,
1604
+ borrow_destination: payload.borrow_destination,
1605
+ lend_asset,
1606
+ borrow_amount: payload.borrow_amount,
1607
+ lend_pool_id: payload.lend_pool_id,
1608
+ refund_destination: payload.refund_destination,
1609
+ ltv_max_bps: payload.ltv_max_bps,
1610
+ ltv_timer_s: payload.ltv_timer_s,
1611
+ lending_profile: payload.lending_profile,
1612
+ borrow_pool_id: payload.borrow_pool_id,
1613
+ borrow_asset
1614
+ }
1615
+ };
1523
1616
  }
1524
1617
 
1525
1618
  // src/core/sdk-api-paths.ts
@@ -1809,7 +1902,7 @@ var ActivitiesModule = class {
1809
1902
  );
1810
1903
  }
1811
1904
  try {
1812
- const result = await createInstantLoansActor(
1905
+ const result = await createFlexibleInstantLoansActor(
1813
1906
  this.canisterContext
1814
1907
  ).get_loan(loanId);
1815
1908
  if ("Err" in result) {
@@ -2278,7 +2371,7 @@ function getAssetNativeDecimals(asset) {
2278
2371
  }
2279
2372
 
2280
2373
  // src/generated/canisters/ck-btc-minter/declaration.js
2281
- var idlFactory3 = ({ IDL }) => {
2374
+ var idlFactory2 = ({ IDL }) => {
2282
2375
  const Mode = IDL.Variant({
2283
2376
  "RestrictedTo": IDL.Vec(IDL.Principal),
2284
2377
  "DepositsRestrictedTo": IDL.Vec(IDL.Principal),
@@ -2658,14 +2751,14 @@ var idlFactory3 = ({ IDL }) => {
2658
2751
  // src/core/canisters/ckbtc/minter.ts
2659
2752
  function createCkBtcMinterActor(canisterContext) {
2660
2753
  const canisterId = CK_CANISTER_IDS.btcMinter;
2661
- return agent.Actor.createActor(idlFactory3, {
2754
+ return agent.Actor.createActor(idlFactory2, {
2662
2755
  agent: canisterContext.agent,
2663
2756
  canisterId
2664
2757
  });
2665
2758
  }
2666
2759
 
2667
2760
  // src/generated/canisters/deposit-accounts/deposit_accounts.did.js
2668
- var idlFactory4 = ({ IDL }) => {
2761
+ var idlFactory3 = ({ IDL }) => {
2669
2762
  const DepositAccountErrors = IDL.Variant({
2670
2763
  "Busy": IDL.Null,
2671
2764
  "NotFound": IDL.Null,
@@ -2885,7 +2978,7 @@ function createDepositAccountsActor(canisterContext) {
2885
2978
  "ETH deposit canister ID is not configured"
2886
2979
  );
2887
2980
  }
2888
- return agent.Actor.createActor(idlFactory4, {
2981
+ return agent.Actor.createActor(idlFactory3, {
2889
2982
  agent: canisterContext.agent,
2890
2983
  canisterId
2891
2984
  });
@@ -3771,7 +3864,7 @@ var InstantLoansModule = class {
3771
3864
  */
3772
3865
  async getConfig() {
3773
3866
  try {
3774
- const config = await createInstantLoansActor(
3867
+ const config = await createFlexibleInstantLoansActor(
3775
3868
  this.canisterContext
3776
3869
  ).get_config();
3777
3870
  return {
@@ -3789,10 +3882,11 @@ var InstantLoansModule = class {
3789
3882
  */
3790
3883
  async getEvent(eventId) {
3791
3884
  try {
3792
- const event = await createInstantLoansActor(
3885
+ const event = await createFlexibleInstantLoansActor(
3793
3886
  this.canisterContext
3794
3887
  ).get_event(eventId);
3795
- return event[0] ? mapInstantLoanEvent(event[0]) : null;
3888
+ const decoded = event[0] ? decodeFlexibleHeadlessLoanEvent(event[0]) : null;
3889
+ return decoded ? mapInstantLoanEvent(decoded) : null;
3796
3890
  } catch (error) {
3797
3891
  throw mapCanisterCallErrorToLiquidiumError("get_event", error);
3798
3892
  }
@@ -3805,10 +3899,10 @@ var InstantLoansModule = class {
3805
3899
  */
3806
3900
  async listEvents(request) {
3807
3901
  try {
3808
- const events = await createInstantLoansActor(
3902
+ const events = await createFlexibleInstantLoansActor(
3809
3903
  this.canisterContext
3810
3904
  ).list_events(request.start, request.limit);
3811
- return events.map(([, event]) => mapInstantLoanEvent(event));
3905
+ return events.map(([, event]) => decodeFlexibleHeadlessLoanEvent(event)).filter((event) => event !== null).map((event) => mapInstantLoanEvent(event));
3812
3906
  } catch (error) {
3813
3907
  throw mapCanisterCallErrorToLiquidiumError("list_events", error);
3814
3908
  }
@@ -3820,7 +3914,7 @@ var InstantLoansModule = class {
3820
3914
  */
3821
3915
  async listAccessList() {
3822
3916
  try {
3823
- const principals = await createInstantLoansActor(
3917
+ const principals = await createFlexibleInstantLoansActor(
3824
3918
  this.canisterContext
3825
3919
  ).list_access_list();
3826
3920
  return principals.map((principal) => principal.toText());
@@ -3835,7 +3929,7 @@ var InstantLoansModule = class {
3835
3929
  */
3836
3930
  async countWarmedProfiles() {
3837
3931
  try {
3838
- return await createInstantLoansActor(
3932
+ return await createFlexibleInstantLoansActor(
3839
3933
  this.canisterContext
3840
3934
  ).count_warmed_profiles();
3841
3935
  } catch (error) {
@@ -3852,7 +3946,7 @@ var InstantLoansModule = class {
3852
3946
  */
3853
3947
  async listWarmedProfiles() {
3854
3948
  try {
3855
- const profiles = await createInstantLoansActor(
3949
+ const profiles = await createFlexibleInstantLoansActor(
3856
3950
  this.canisterContext
3857
3951
  ).list_warmed_profiles();
3858
3952
  return profiles.map(mapWarmedProfile);
@@ -3869,13 +3963,20 @@ var InstantLoansModule = class {
3869
3963
  }
3870
3964
  async getLoanRecord(loanId) {
3871
3965
  try {
3872
- const result = await createInstantLoansActor(
3966
+ const result = await createFlexibleInstantLoansActor(
3873
3967
  this.canisterContext
3874
3968
  ).get_loan(loanId);
3875
3969
  if ("Err" in result) {
3876
3970
  throw mapInstantLoansErrorToLiquidiumError(result.Err);
3877
3971
  }
3878
- return result.Ok;
3972
+ const decoded = decodeFlexibleInstantLoanRecord(result.Ok);
3973
+ if (!decoded) {
3974
+ throw new LiquidiumError(
3975
+ LiquidiumErrorCode.POOL_NOT_FOUND,
3976
+ `Instant loan ${loanId.toString()} uses an unsupported asset`
3977
+ );
3978
+ }
3979
+ return decoded;
3879
3980
  } catch (error) {
3880
3981
  if (error instanceof LiquidiumError) {
3881
3982
  throw error;
@@ -3892,8 +3993,8 @@ var InstantLoansModule = class {
3892
3993
  collateralPoolId: record.lend_pool_id.toText(),
3893
3994
  collateralAmount,
3894
3995
  borrowPoolId: record.borrow_pool_id.toText(),
3895
- collateralAsset: getVariantKey(record.lend_asset),
3896
- borrowAsset: getVariantKey(record.borrow_asset),
3996
+ collateralAsset: record.lend_asset,
3997
+ borrowAsset: record.borrow_asset,
3897
3998
  borrowAmount: record.borrow_amount,
3898
3999
  borrowDestination: accountFromCanister(record.borrow_destination),
3899
4000
  refundDestination: accountFromCanister(record.refund_destination),
@@ -4198,7 +4299,7 @@ function mapInstantLoanEventType(eventType) {
4198
4299
  type: "LoanCreated",
4199
4300
  loanId: event.loan_id,
4200
4301
  borrowDestination: accountFromCanister(event.borrow_destination),
4201
- collateralAsset: assetFromCanister(event.lend_asset),
4302
+ collateralAsset: event.lend_asset,
4202
4303
  borrowAmount: event.borrow_amount,
4203
4304
  collateralPoolId: event.lend_pool_id.toText(),
4204
4305
  refundDestination: accountFromCanister(event.refund_destination),
@@ -4206,7 +4307,7 @@ function mapInstantLoanEventType(eventType) {
4206
4307
  depositWindowSeconds: event.ltv_timer_s,
4207
4308
  profileId: event.lending_profile.toText(),
4208
4309
  borrowPoolId: event.borrow_pool_id.toText(),
4209
- borrowAsset: assetFromCanister(event.borrow_asset)
4310
+ borrowAsset: event.borrow_asset
4210
4311
  };
4211
4312
  }
4212
4313
  if ("FullLendWithdrawalRequested" in eventType) {
@@ -4284,9 +4385,6 @@ function authorizationFromCanister(authorization) {
4284
4385
  address: authorization.EthSignature.address
4285
4386
  };
4286
4387
  }
4287
- function assetFromCanister(asset) {
4288
- return getVariantKey(asset);
4289
- }
4290
4388
  function legFromCanister(leg) {
4291
4389
  return getVariantKey(leg);
4292
4390
  }
@@ -4514,12 +4612,12 @@ function encodeBytes32Hex(bytes) {
4514
4612
  (byte) => byte.toString(16).padStart(2, "0")
4515
4613
  ).join("")}`;
4516
4614
  }
4517
- var idlFactory5 = ({ IDL }) => IDL.Service({
4615
+ var idlFactory4 = ({ IDL }) => IDL.Service({
4518
4616
  icrc1_fee: IDL.Func([], [IDL.Nat], ["query"])
4519
4617
  });
4520
4618
  function createCkBtcLedgerActor(canisterContext) {
4521
4619
  const canisterId = CK_CANISTER_IDS.btcLedger;
4522
- return agent.Actor.createActor(idlFactory5, {
4620
+ return agent.Actor.createActor(idlFactory4, {
4523
4621
  agent: canisterContext.agent,
4524
4622
  canisterId
4525
4623
  });
@@ -5586,21 +5684,203 @@ function shouldSubmitInflow(params) {
5586
5684
  }
5587
5685
  return !isEthStablecoin(params.instruction.asset, params.instruction.chain);
5588
5686
  }
5687
+ var flexibleLendingIdlFactory = ({ IDL }) => {
5688
+ const PoolRecord = IDL.Record({
5689
+ principal: IDL.Principal,
5690
+ asset: IDL.Unknown,
5691
+ chain: IDL.Unknown,
5692
+ total_supply_at_last_sync: IDL.Nat,
5693
+ total_debt_at_last_sync: IDL.Nat,
5694
+ supply_cap: IDL.Opt(IDL.Nat),
5695
+ borrow_cap: IDL.Opt(IDL.Nat),
5696
+ max_ltv: IDL.Nat64,
5697
+ liquidation_threshold: IDL.Nat64,
5698
+ liquidation_bonus: IDL.Nat64,
5699
+ protocol_liquidation_fee: IDL.Nat64,
5700
+ reserve_factor: IDL.Nat64,
5701
+ base_rate: IDL.Nat,
5702
+ optimal_utilization_rate: IDL.Nat,
5703
+ rate_slope_before: IDL.Nat,
5704
+ rate_slope_after: IDL.Nat,
5705
+ lending_index: IDL.Nat,
5706
+ borrow_index: IDL.Nat,
5707
+ same_asset_borrowing: IDL.Opt(IDL.Bool),
5708
+ frozen: IDL.Bool,
5709
+ last_updated: IDL.Opt(IDL.Nat64)
5710
+ });
5711
+ const BorrowingPowerRecord = IDL.Record({
5712
+ max_borrowable_usd: IDL.Nat,
5713
+ weighted_max_ltv: IDL.Nat
5714
+ });
5715
+ const PositionRecord = IDL.Record({
5716
+ asset: IDL.Unknown,
5717
+ total_debt_interest: IDL.Nat,
5718
+ borrow_index_snapshot: IDL.Nat,
5719
+ lending_index_snapshot: IDL.Nat,
5720
+ debt_scaled: IDL.Nat,
5721
+ total_earned_interest: IDL.Nat,
5722
+ deposit_scaled: IDL.Nat,
5723
+ pool_id: IDL.Principal,
5724
+ unpaid_debt_interest: IDL.Nat,
5725
+ last_update: IDL.Nat64,
5726
+ user_profile: IDL.Principal
5727
+ });
5728
+ const UserStatsRecord = IDL.Record({
5729
+ debt: IDL.Nat,
5730
+ collateral: IDL.Nat,
5731
+ acumulated_interest: IDL.Nat,
5732
+ borrowing_power: BorrowingPowerRecord,
5733
+ positions: IDL.Vec(PositionRecord),
5734
+ weighted_liquidation_threshold: IDL.Nat
5735
+ });
5736
+ const PositionViewRecord = IDL.Record({
5737
+ lending_index_now: IDL.Nat,
5738
+ interest_since_snapshot: IDL.Nat,
5739
+ asset: IDL.Unknown,
5740
+ total_debt_interest: IDL.Nat,
5741
+ borrow_index_snapshot: IDL.Nat,
5742
+ debt_native_now: IDL.Nat,
5743
+ borrow_index_now: IDL.Nat,
5744
+ lending_index_snapshot: IDL.Nat,
5745
+ debt_scaled: IDL.Nat,
5746
+ total_earned_interest: IDL.Nat,
5747
+ deposit_scaled: IDL.Nat,
5748
+ earned_since_snapshot: IDL.Nat,
5749
+ deposited_native_now: IDL.Nat,
5750
+ pool_id: IDL.Principal,
5751
+ last_update: IDL.Nat64,
5752
+ user_profile: IDL.Principal
5753
+ });
5754
+ return IDL.Service({
5755
+ list_pools: IDL.Func([], [IDL.Vec(PoolRecord)], ["query"]),
5756
+ get_pool_rate: IDL.Func(
5757
+ [IDL.Principal],
5758
+ [IDL.Opt(IDL.Tuple(IDL.Nat, IDL.Nat, IDL.Nat))],
5759
+ ["query"]
5760
+ ),
5761
+ get_health_factor: IDL.Func(
5762
+ [IDL.Principal],
5763
+ [IDL.Nat, UserStatsRecord],
5764
+ ["query"]
5765
+ ),
5766
+ get_profile_stats: IDL.Func([IDL.Principal], [UserStatsRecord], ["query"]),
5767
+ get_position: IDL.Func(
5768
+ [IDL.Principal, IDL.Principal],
5769
+ [IDL.Opt(PositionViewRecord)],
5770
+ ["query"]
5771
+ )
5772
+ });
5773
+ };
5774
+ function createFlexibleLendingActor(canisterContext) {
5775
+ const canisterId = canisterContext.canisterIds.lending;
5776
+ if (!canisterId) {
5777
+ throw new LiquidiumError(
5778
+ LiquidiumErrorCode.SERVICE_UNAVAILABLE,
5779
+ "Lending canister ID is not configured"
5780
+ );
5781
+ }
5782
+ return agent.Actor.createActor(flexibleLendingIdlFactory, {
5783
+ agent: canisterContext.agent,
5784
+ canisterId
5785
+ });
5786
+ }
5787
+ function decodeFlexiblePool(pool) {
5788
+ const asset = extractVariantTag(pool.asset, KNOWN_ASSET_TAGS);
5789
+ const chain = extractVariantTag(pool.chain, KNOWN_CHAIN_TAGS);
5790
+ if (!asset || !chain) {
5791
+ return null;
5792
+ }
5793
+ return {
5794
+ principal: pool.principal,
5795
+ asset,
5796
+ chain,
5797
+ total_supply_at_last_sync: pool.total_supply_at_last_sync,
5798
+ total_debt_at_last_sync: pool.total_debt_at_last_sync,
5799
+ supply_cap: pool.supply_cap,
5800
+ borrow_cap: pool.borrow_cap,
5801
+ max_ltv: pool.max_ltv,
5802
+ liquidation_threshold: pool.liquidation_threshold,
5803
+ liquidation_bonus: pool.liquidation_bonus,
5804
+ protocol_liquidation_fee: pool.protocol_liquidation_fee,
5805
+ reserve_factor: pool.reserve_factor,
5806
+ base_rate: pool.base_rate,
5807
+ optimal_utilization_rate: pool.optimal_utilization_rate,
5808
+ rate_slope_before: pool.rate_slope_before,
5809
+ rate_slope_after: pool.rate_slope_after,
5810
+ lending_index: pool.lending_index,
5811
+ borrow_index: pool.borrow_index,
5812
+ same_asset_borrowing: pool.same_asset_borrowing,
5813
+ frozen: pool.frozen,
5814
+ last_updated: pool.last_updated
5815
+ };
5816
+ }
5817
+ function decodeFlexiblePosition(position) {
5818
+ const asset = extractVariantTag(position.asset, KNOWN_ASSET_TAGS);
5819
+ if (!asset) {
5820
+ return null;
5821
+ }
5822
+ return {
5823
+ asset,
5824
+ total_debt_interest: position.total_debt_interest,
5825
+ borrow_index_snapshot: position.borrow_index_snapshot,
5826
+ lending_index_snapshot: position.lending_index_snapshot,
5827
+ debt_scaled: position.debt_scaled,
5828
+ total_earned_interest: position.total_earned_interest,
5829
+ deposit_scaled: position.deposit_scaled,
5830
+ pool_id: position.pool_id,
5831
+ unpaid_debt_interest: position.unpaid_debt_interest,
5832
+ last_update: position.last_update,
5833
+ user_profile: position.user_profile
5834
+ };
5835
+ }
5836
+ function decodeFlexiblePositionView(view) {
5837
+ const asset = extractVariantTag(view.asset, KNOWN_ASSET_TAGS);
5838
+ if (!asset) {
5839
+ return null;
5840
+ }
5841
+ return {
5842
+ lending_index_now: view.lending_index_now,
5843
+ interest_since_snapshot: view.interest_since_snapshot,
5844
+ asset,
5845
+ total_debt_interest: view.total_debt_interest,
5846
+ borrow_index_snapshot: view.borrow_index_snapshot,
5847
+ debt_native_now: view.debt_native_now,
5848
+ borrow_index_now: view.borrow_index_now,
5849
+ lending_index_snapshot: view.lending_index_snapshot,
5850
+ debt_scaled: view.debt_scaled,
5851
+ total_earned_interest: view.total_earned_interest,
5852
+ deposit_scaled: view.deposit_scaled,
5853
+ earned_since_snapshot: view.earned_since_snapshot,
5854
+ deposited_native_now: view.deposited_native_now,
5855
+ pool_id: view.pool_id,
5856
+ last_update: view.last_update,
5857
+ user_profile: view.user_profile
5858
+ };
5859
+ }
5860
+ function decodeFlexibleUserStats(stats) {
5861
+ return {
5862
+ debt: stats.debt,
5863
+ collateral: stats.collateral,
5864
+ acumulated_interest: stats.acumulated_interest,
5865
+ borrowing_power: stats.borrowing_power,
5866
+ positions: stats.positions.map(decodeFlexiblePosition).filter((position) => position !== null),
5867
+ weighted_liquidation_threshold: stats.weighted_liquidation_threshold
5868
+ };
5869
+ }
5589
5870
 
5590
5871
  // src/modules/market/mappers.ts
5591
5872
  var DECIMAL_BASE = 10;
5592
5873
  var PAIR_SEPARATOR = "_";
5593
5874
  var USDT_SYMBOL = "USDT";
5594
- function mapLendingPoolRecordToPool(pool, rate) {
5595
- const asset = getVariantKey(pool.asset);
5875
+ function mapDecodedPoolToPool(pool, rate) {
5596
5876
  const totalSupply = pool.total_supply_at_last_sync;
5597
5877
  const totalDebt = pool.total_debt_at_last_sync;
5598
5878
  const availableLiquidity = totalSupply > totalDebt ? totalSupply - totalDebt : 0n;
5599
5879
  return {
5600
5880
  id: pool.principal.toString(),
5601
- asset,
5602
- chain: getVariantKey(pool.chain),
5603
- decimals: getAssetNativeDecimals(asset),
5881
+ asset: pool.asset,
5882
+ chain: pool.chain,
5883
+ decimals: getAssetNativeDecimals(pool.asset),
5604
5884
  frozen: pool.frozen,
5605
5885
  totalSupply,
5606
5886
  totalDebt,
@@ -5673,13 +5953,14 @@ var MarketModule = class {
5673
5953
  async listPools() {
5674
5954
  void this.apiClient;
5675
5955
  try {
5676
- const lendingActor = createLendingActor(this.canisterContext);
5677
- const lendingPools = await lendingActor.list_pools();
5956
+ const flexibleActor = createFlexibleLendingActor(this.canisterContext);
5957
+ const rawPools = await flexibleActor.list_pools();
5958
+ const decodedPools = rawPools.map(decodeFlexiblePool).filter((pool) => pool !== null);
5678
5959
  return await Promise.all(
5679
- lendingPools.map(async (pool) => {
5680
- const poolRate = await lendingActor.get_pool_rate(pool.principal);
5960
+ decodedPools.map(async (pool) => {
5961
+ const poolRate = await flexibleActor.get_pool_rate(pool.principal);
5681
5962
  const resolvedPoolRate = poolRate[0] ?? ZERO_POOL_RATE;
5682
- return mapLendingPoolRecordToPool(pool, resolvedPoolRate);
5963
+ return mapDecodedPoolToPool(pool, resolvedPoolRate);
5683
5964
  })
5684
5965
  );
5685
5966
  } catch (error) {
@@ -5784,12 +6065,11 @@ var MarketModule = class {
5784
6065
 
5785
6066
  // src/modules/positions/mappers.ts
5786
6067
  var USD_VALUE_SCALE_DECIMALS = 27n;
5787
- function mapPositionViewToPosition(view) {
5788
- const asset = getVariantKey(view.asset);
5789
- const nativeDecimals = getAssetNativeDecimals(asset);
6068
+ function mapDecodedPositionViewToPosition(view) {
6069
+ const nativeDecimals = getAssetNativeDecimals(view.asset);
5790
6070
  return {
5791
6071
  poolId: view.pool_id.toText(),
5792
- asset,
6072
+ asset: view.asset,
5793
6073
  deposited: view.deposited_native_now,
5794
6074
  depositedDecimals: nativeDecimals,
5795
6075
  borrowed: view.debt_native_now,
@@ -5806,15 +6086,18 @@ function mapBorrowingPowerRecordToBorrowingPower(record) {
5806
6086
  maxBorrowableUsdDecimals: USD_VALUE_SCALE_DECIMALS
5807
6087
  };
5808
6088
  }
5809
- function mapUserStatsRecordToUserStats(record) {
6089
+ function mapDecodedUserStatsToUserStats(stats) {
6090
+ return mapUserStatsLikeToUserStats(stats);
6091
+ }
6092
+ function mapUserStatsLikeToUserStats(stats) {
5810
6093
  return {
5811
- debt: record.debt,
6094
+ debt: stats.debt,
5812
6095
  debtDecimals: USD_VALUE_SCALE_DECIMALS,
5813
- collateral: record.collateral,
6096
+ collateral: stats.collateral,
5814
6097
  collateralDecimals: USD_VALUE_SCALE_DECIMALS,
5815
- weightedLiquidationThreshold: record.weighted_liquidation_threshold,
6098
+ weightedLiquidationThreshold: stats.weighted_liquidation_threshold,
5816
6099
  borrowingPower: mapBorrowingPowerRecordToBorrowingPower(
5817
- record.borrowing_power
6100
+ stats.borrowing_power
5818
6101
  )
5819
6102
  };
5820
6103
  }
@@ -5838,14 +6121,18 @@ var PositionsModule = class {
5838
6121
  */
5839
6122
  async getPosition(profileId, poolId) {
5840
6123
  try {
5841
- const result = await createLendingActor(
6124
+ const result = await createFlexibleLendingActor(
5842
6125
  this.canisterContext
5843
6126
  ).get_position(principal.Principal.fromText(profileId), principal.Principal.fromText(poolId));
5844
6127
  const view = result[0];
5845
6128
  if (!view) {
5846
6129
  return null;
5847
6130
  }
5848
- return mapPositionViewToPosition(view);
6131
+ const decodedView = decodeFlexiblePositionView(view);
6132
+ if (!decodedView) {
6133
+ return null;
6134
+ }
6135
+ return mapDecodedPositionViewToPosition(decodedView);
5849
6136
  } catch (error) {
5850
6137
  if (error instanceof LiquidiumError) {
5851
6138
  throw error;
@@ -5861,15 +6148,16 @@ var PositionsModule = class {
5861
6148
  */
5862
6149
  async listPositions(profileId) {
5863
6150
  try {
5864
- const actor = createLendingActor(this.canisterContext);
6151
+ const actor = createFlexibleLendingActor(this.canisterContext);
5865
6152
  const profilePrincipal = principal.Principal.fromText(profileId);
5866
6153
  const stats = await actor.get_profile_stats(profilePrincipal);
6154
+ const decodedStats = decodeFlexibleUserStats(stats);
5867
6155
  const positionViews = await Promise.all(
5868
- stats.positions.map(
6156
+ decodedStats.positions.map(
5869
6157
  (position) => actor.get_position(profilePrincipal, position.pool_id)
5870
6158
  )
5871
6159
  );
5872
- return positionViews.map((result) => result[0]).filter((view) => view !== void 0).map(mapPositionViewToPosition);
6160
+ return positionViews.map((result) => result[0]).filter((view) => view !== void 0).map(decodeFlexiblePositionView).filter((view) => view !== null).map(mapDecodedPositionViewToPosition);
5873
6161
  } catch (error) {
5874
6162
  if (error instanceof LiquidiumError) {
5875
6163
  throw error;
@@ -5885,12 +6173,14 @@ var PositionsModule = class {
5885
6173
  */
5886
6174
  async getHealthFactor(profileId) {
5887
6175
  try {
5888
- const [healthFactor, userStatsRecord] = await createLendingActor(
6176
+ const [healthFactor, userStatsRecord] = await createFlexibleLendingActor(
5889
6177
  this.canisterContext
5890
6178
  ).get_health_factor(principal.Principal.fromText(profileId));
5891
6179
  return {
5892
6180
  healthFactor,
5893
- userStats: mapUserStatsRecordToUserStats(userStatsRecord)
6181
+ userStats: mapDecodedUserStatsToUserStats(
6182
+ decodeFlexibleUserStats(userStatsRecord)
6183
+ )
5894
6184
  };
5895
6185
  } catch (error) {
5896
6186
  if (error instanceof LiquidiumError) {
@@ -5907,10 +6197,10 @@ var PositionsModule = class {
5907
6197
  */
5908
6198
  async getUserStats(profileId) {
5909
6199
  try {
5910
- const result = await createLendingActor(
6200
+ const result = await createFlexibleLendingActor(
5911
6201
  this.canisterContext
5912
6202
  ).get_profile_stats(principal.Principal.fromText(profileId));
5913
- return mapUserStatsRecordToUserStats(result);
6203
+ return mapDecodedUserStatsToUserStats(decodeFlexibleUserStats(result));
5914
6204
  } catch (error) {
5915
6205
  if (error instanceof LiquidiumError) {
5916
6206
  throw error;