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