@morpho-dev/router 0.1.17 → 0.1.18

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.
@@ -623,14 +623,35 @@ declare function fromSnakeCase$2(input: Snake<Omit<Offer, "hash">>): Offer;
623
623
  * @returns The converted offer.
624
624
  */
625
625
  declare function toSnakeCase(offer: Offer): Snake<Offer>;
626
+ type RandomConfig = {
627
+ chains?: Chain[];
628
+ loanTokens?: Address[];
629
+ collateralTokens?: Address[];
630
+ assetsDecimals?: Record<Address, number>;
631
+ buy?: boolean;
632
+ assets?: bigint;
633
+ consumed?: bigint;
634
+ offering?: Address;
635
+ maturity?: Maturity;
636
+ start?: number;
637
+ expiry?: number;
638
+ nonce?: bigint;
639
+ rate?: bigint;
640
+ callback?: {
641
+ address: Address;
642
+ data: Hex;
643
+ gasLimit: bigint;
644
+ };
645
+ collaterals?: readonly Collateral[];
646
+ signature?: Hex;
647
+ };
626
648
  /**
627
649
  * Generates a random Offer.
628
- * The returned Offer contains randomly generated values for most fields, such as assets, rate, nonce, and buy flag.
629
- * Some fields use fixed or deterministic values (e.g., chainId, collaterals, callback).
650
+ * The returned Offer contains randomly generated values.
630
651
  * @warning The generated Offer should not be used for production usage.
631
652
  * @returns {Offer} A randomly generated Offer object.
632
653
  */
633
- declare function random$2(): Offer;
654
+ declare function random$2(config?: RandomConfig): Offer;
634
655
  /**
635
656
  * Creates an EIP-712 domain object.
636
657
  * @param chainId - The chain ID.
@@ -781,6 +802,7 @@ type Offer$1_Offer = Offer;
781
802
  type Offer$1_OfferConsumed = OfferConsumed;
782
803
  declare const Offer$1_OfferHashSchema: typeof OfferHashSchema;
783
804
  declare const Offer$1_OfferSchema: typeof OfferSchema;
805
+ type Offer$1_RandomConfig = RandomConfig;
784
806
  declare const Offer$1_consumedEvent: typeof consumedEvent;
785
807
  declare const Offer$1_domain: typeof domain;
786
808
  declare const Offer$1_fromConsumedLog: typeof fromConsumedLog;
@@ -790,7 +812,7 @@ declare const Offer$1_sign: typeof sign;
790
812
  declare const Offer$1_toSnakeCase: typeof toSnakeCase;
791
813
  declare const Offer$1_types: typeof types;
792
814
  declare namespace Offer$1 {
793
- export { Offer$1_AccountNotSetError as AccountNotSetError, Offer$1_InvalidOfferError as InvalidOfferError, type Offer$1_Offer as Offer, type Offer$1_OfferConsumed as OfferConsumed, Offer$1_OfferHashSchema as OfferHashSchema, Offer$1_OfferSchema as OfferSchema, Offer$1_consumedEvent as consumedEvent, decode$1 as decode, Offer$1_domain as domain, encode$1 as encode, from$5 as from, Offer$1_fromConsumedLog as fromConsumedLog, fromSnakeCase$2 as fromSnakeCase, Offer$1_hash as hash, Offer$1_obligationId as obligationId, random$2 as random, Offer$1_sign as sign, Offer$1_toSnakeCase as toSnakeCase, Offer$1_types as types };
815
+ export { Offer$1_AccountNotSetError as AccountNotSetError, Offer$1_InvalidOfferError as InvalidOfferError, type Offer$1_Offer as Offer, type Offer$1_OfferConsumed as OfferConsumed, Offer$1_OfferHashSchema as OfferHashSchema, Offer$1_OfferSchema as OfferSchema, type Offer$1_RandomConfig as RandomConfig, Offer$1_consumedEvent as consumedEvent, decode$1 as decode, Offer$1_domain as domain, encode$1 as encode, from$5 as from, Offer$1_fromConsumedLog as fromConsumedLog, fromSnakeCase$2 as fromSnakeCase, Offer$1_hash as hash, Offer$1_obligationId as obligationId, random$2 as random, Offer$1_sign as sign, Offer$1_toSnakeCase as toSnakeCase, Offer$1_types as types };
794
816
  }
795
817
 
796
818
  /**
@@ -623,14 +623,35 @@ declare function fromSnakeCase$2(input: Snake<Omit<Offer, "hash">>): Offer;
623
623
  * @returns The converted offer.
624
624
  */
625
625
  declare function toSnakeCase(offer: Offer): Snake<Offer>;
626
+ type RandomConfig = {
627
+ chains?: Chain[];
628
+ loanTokens?: Address[];
629
+ collateralTokens?: Address[];
630
+ assetsDecimals?: Record<Address, number>;
631
+ buy?: boolean;
632
+ assets?: bigint;
633
+ consumed?: bigint;
634
+ offering?: Address;
635
+ maturity?: Maturity;
636
+ start?: number;
637
+ expiry?: number;
638
+ nonce?: bigint;
639
+ rate?: bigint;
640
+ callback?: {
641
+ address: Address;
642
+ data: Hex;
643
+ gasLimit: bigint;
644
+ };
645
+ collaterals?: readonly Collateral[];
646
+ signature?: Hex;
647
+ };
626
648
  /**
627
649
  * Generates a random Offer.
628
- * The returned Offer contains randomly generated values for most fields, such as assets, rate, nonce, and buy flag.
629
- * Some fields use fixed or deterministic values (e.g., chainId, collaterals, callback).
650
+ * The returned Offer contains randomly generated values.
630
651
  * @warning The generated Offer should not be used for production usage.
631
652
  * @returns {Offer} A randomly generated Offer object.
632
653
  */
633
- declare function random$2(): Offer;
654
+ declare function random$2(config?: RandomConfig): Offer;
634
655
  /**
635
656
  * Creates an EIP-712 domain object.
636
657
  * @param chainId - The chain ID.
@@ -781,6 +802,7 @@ type Offer$1_Offer = Offer;
781
802
  type Offer$1_OfferConsumed = OfferConsumed;
782
803
  declare const Offer$1_OfferHashSchema: typeof OfferHashSchema;
783
804
  declare const Offer$1_OfferSchema: typeof OfferSchema;
805
+ type Offer$1_RandomConfig = RandomConfig;
784
806
  declare const Offer$1_consumedEvent: typeof consumedEvent;
785
807
  declare const Offer$1_domain: typeof domain;
786
808
  declare const Offer$1_fromConsumedLog: typeof fromConsumedLog;
@@ -790,7 +812,7 @@ declare const Offer$1_sign: typeof sign;
790
812
  declare const Offer$1_toSnakeCase: typeof toSnakeCase;
791
813
  declare const Offer$1_types: typeof types;
792
814
  declare namespace Offer$1 {
793
- export { Offer$1_AccountNotSetError as AccountNotSetError, Offer$1_InvalidOfferError as InvalidOfferError, type Offer$1_Offer as Offer, type Offer$1_OfferConsumed as OfferConsumed, Offer$1_OfferHashSchema as OfferHashSchema, Offer$1_OfferSchema as OfferSchema, Offer$1_consumedEvent as consumedEvent, decode$1 as decode, Offer$1_domain as domain, encode$1 as encode, from$5 as from, Offer$1_fromConsumedLog as fromConsumedLog, fromSnakeCase$2 as fromSnakeCase, Offer$1_hash as hash, Offer$1_obligationId as obligationId, random$2 as random, Offer$1_sign as sign, Offer$1_toSnakeCase as toSnakeCase, Offer$1_types as types };
815
+ export { Offer$1_AccountNotSetError as AccountNotSetError, Offer$1_InvalidOfferError as InvalidOfferError, type Offer$1_Offer as Offer, type Offer$1_OfferConsumed as OfferConsumed, Offer$1_OfferHashSchema as OfferHashSchema, Offer$1_OfferSchema as OfferSchema, type Offer$1_RandomConfig as RandomConfig, Offer$1_consumedEvent as consumedEvent, decode$1 as decode, Offer$1_domain as domain, encode$1 as encode, from$5 as from, Offer$1_fromConsumedLog as fromConsumedLog, fromSnakeCase$2 as fromSnakeCase, Offer$1_hash as hash, Offer$1_obligationId as obligationId, random$2 as random, Offer$1_sign as sign, Offer$1_toSnakeCase as toSnakeCase, Offer$1_types as types };
794
816
  }
795
817
 
796
818
  /**
@@ -509,8 +509,12 @@ var chains = {
509
509
  [
510
510
  "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
511
511
  // USDC
512
- "0x6B175474E89094C44Da98b954EedeAC495271d0F"
512
+ "0x6B175474E89094C44Da98b954EedeAC495271d0F",
513
513
  // DAI
514
+ "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
515
+ // WETH
516
+ "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599"
517
+ // WBTC
514
518
  ].map((address) => address.toLowerCase())
515
519
  ),
516
520
  morpho: "0x0000000000000000000000000000000000000000",
@@ -533,8 +537,12 @@ var chains = {
533
537
  [
534
538
  "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
535
539
  // USDC
536
- "0x50c5725949A6F0c72E6C4a641F24049A917DB0Cb"
540
+ "0x50c5725949A6F0c72E6C4a641F24049A917DB0Cb",
537
541
  // DAI
542
+ "0x4200000000000000000000000000000000000006",
543
+ // WETH
544
+ "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599"
545
+ // WBTC
538
546
  ].map((address) => address.toLowerCase())
539
547
  ),
540
548
  morpho: "0x0000000000000000000000000000000000000000",
@@ -557,8 +565,12 @@ var chains = {
557
565
  [
558
566
  "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
559
567
  // USDC
560
- "0x6B175474E89094C44Da98b954EedeAC495271d0F"
568
+ "0x6B175474E89094C44Da98b954EedeAC495271d0F",
561
569
  // DAI
570
+ "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
571
+ // WETH
572
+ "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599"
573
+ // WBTC
562
574
  ].map((address) => address.toLowerCase())
563
575
  ),
564
576
  morpho: "0x11a002d45db720ed47a80d2f3489cba5b833eaf5",
@@ -582,8 +594,12 @@ var chains = {
582
594
  [
583
595
  "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
584
596
  // USDC
585
- "0x6B175474E89094C44Da98b954EedeAC495271d0F"
597
+ "0x6B175474E89094C44Da98b954EedeAC495271d0F",
586
598
  // DAI
599
+ "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
600
+ // WETH
601
+ "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599"
602
+ // WBTC
587
603
  ].map((address) => address.toLowerCase())
588
604
  ),
589
605
  morpho: "0x23DFBc4B8B80C14CC5e25011B8491f268395BAd6",
@@ -1292,39 +1308,92 @@ function fromSnakeCase3(input) {
1292
1308
  function toSnakeCase2(offer) {
1293
1309
  return toSnakeCase(offer);
1294
1310
  }
1295
- function random2() {
1296
- const loanToken = accounts.privateKeyToAccount(accounts.generatePrivateKey()).address;
1297
- const maturity = from5("end_of_month");
1298
- const expiry = from5("end_of_week") - 1;
1299
- const lltv = from3(0.965);
1311
+ function random2(config) {
1312
+ const chain = config?.chains ? config.chains[Math.floor(Math.random() * config.chains.length)] : chains.ethereum;
1313
+ const loanToken = config?.loanTokens ? config.loanTokens[Math.floor(Math.random() * config.loanTokens.length)] : accounts.privateKeyToAccount(accounts.generatePrivateKey()).address;
1314
+ const collateralCandidates = config?.collateralTokens ? config.collateralTokens.filter((a) => a !== loanToken) : [accounts.privateKeyToAccount(accounts.generatePrivateKey()).address];
1315
+ const collateralAsset = collateralCandidates[Math.floor(Math.random() * collateralCandidates.length)];
1316
+ const maturityOption = weightedChoice([
1317
+ ["end_of_month", 1],
1318
+ ["end_of_next_month", 1]
1319
+ ]);
1320
+ const maturity = config?.maturity ?? from5(maturityOption);
1321
+ const lltv = from3(
1322
+ weightedChoice([
1323
+ [0.385, 1],
1324
+ [0.5, 1],
1325
+ [0.625, 2],
1326
+ [0.77, 8],
1327
+ [0.86, 10],
1328
+ [0.915, 8],
1329
+ [0.945, 6],
1330
+ [0.965, 4],
1331
+ [0.98, 2]
1332
+ ])
1333
+ );
1334
+ const buy = config?.buy !== void 0 ? config.buy : Math.random() > 0.5;
1335
+ const ONE = 1000000000000000000n;
1336
+ const qMin = buy ? 16 : 4;
1337
+ const qMax = buy ? 32 : 16;
1338
+ const len = qMax - qMin + 1;
1339
+ const ratePairs = Array.from(
1340
+ { length: len },
1341
+ (_, idx) => {
1342
+ const q = qMin + idx;
1343
+ const scaledRate = BigInt(q) * (ONE / 4n);
1344
+ const weight = buy ? 1 + idx : 1 + (len - 1 - idx);
1345
+ return [scaledRate, weight];
1346
+ }
1347
+ );
1348
+ const rate = config?.rate ?? weightedChoice(ratePairs);
1349
+ const loanTokenDecimals = config?.assetsDecimals?.[loanToken] ?? 18;
1350
+ const unit = BigInt(10) ** BigInt(loanTokenDecimals);
1351
+ const amountBase = BigInt(100 + Math.floor(Math.random() * (1e6 - 100 + 1)));
1352
+ const assetsScaled = config?.assets ?? amountBase * unit;
1353
+ const consumed = config?.consumed !== void 0 ? config.consumed : Math.random() < 0.8 ? 0n : assetsScaled * BigInt(1 + Math.floor(Math.random() * 900)) / 1000n;
1354
+ const callbackBySide = (() => {
1355
+ if (buy) return { address: viem.zeroAddress, data: "0x", gasLimit: 0n };
1356
+ const sellCallbackAddress = WhitelistedCallbackAddresses["sell_erc20_callback" /* SellERC20Callback */][0].toLowerCase();
1357
+ const amount = assetsScaled * 1000000000000000000000n;
1358
+ const data = encodeSellERC20Callback({
1359
+ collaterals: [collateralAsset],
1360
+ amounts: [amount]
1361
+ });
1362
+ return { address: sellCallbackAddress, data, gasLimit: 500000n };
1363
+ })();
1300
1364
  const offer = from7({
1301
- offering: accounts.privateKeyToAccount(accounts.generatePrivateKey()).address,
1302
- assets: BigInt(Math.floor(Math.random() * 1e6)),
1303
- rate: BigInt(Math.floor(Math.random() * 1e6)),
1365
+ offering: config?.offering ?? accounts.privateKeyToAccount(accounts.generatePrivateKey()).address,
1366
+ assets: assetsScaled,
1367
+ rate,
1304
1368
  maturity,
1305
- expiry,
1306
- start: expiry - 10,
1369
+ expiry: config?.expiry ?? maturity - 1,
1370
+ start: config?.start ?? maturity - 10,
1307
1371
  nonce: BigInt(Math.floor(Math.random() * 1e6)),
1308
- buy: Math.random() > 0.5,
1309
- chainId: 1n,
1372
+ buy,
1373
+ chainId: chain.id,
1310
1374
  loanToken,
1311
- collaterals: [
1375
+ collaterals: config?.collaterals ?? [
1312
1376
  from4({
1313
- asset: viem.zeroAddress,
1377
+ asset: collateralAsset,
1314
1378
  oracle: viem.zeroAddress,
1315
1379
  lltv
1316
1380
  })
1317
1381
  ],
1318
- callback: {
1319
- address: viem.zeroAddress,
1320
- data: "0x",
1321
- gasLimit: 0n
1322
- },
1323
- consumed: 0n,
1382
+ callback: config?.callback ?? callbackBySide,
1383
+ consumed,
1324
1384
  blockNumber: Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)
1325
1385
  });
1326
1386
  return offer;
1327
1387
  }
1388
+ var weightedChoice = (pairs) => {
1389
+ const total = pairs.reduce((sum, [, weight]) => sum + weight, 0);
1390
+ let roll = Math.random() * total;
1391
+ for (const [value, weight] of pairs) {
1392
+ roll -= weight;
1393
+ if (roll < 0) return value;
1394
+ }
1395
+ return pairs[0][0];
1396
+ };
1328
1397
  var domain = (chainId) => ({
1329
1398
  chainId,
1330
1399
  verifyingContract: viem.zeroAddress