@strkfarm/sdk 1.1.67 → 1.1.69

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.
@@ -388,6 +388,150 @@ var PricerBase = class {
388
388
  }
389
389
  };
390
390
 
391
+ // src/modules/avnu.ts
392
+ import { uint256 } from "starknet";
393
+ import { fetchBuildExecuteTransaction, fetchQuotes } from "@avnu/avnu-sdk";
394
+
395
+ // src/utils/oz-merkle.ts
396
+ import { toHex } from "@ericnordelo/strk-merkle-tree/dist/bytes";
397
+ import { processMultiProof, processProof } from "@ericnordelo/strk-merkle-tree/dist/core";
398
+ import { standardLeafHash } from "@ericnordelo/strk-merkle-tree/dist/hashes";
399
+ import { MerkleTreeImpl } from "@ericnordelo/strk-merkle-tree/dist/merkletree";
400
+ import { num as num2 } from "starknet";
401
+ import { pedersen } from "@scure/starknet";
402
+ function hash_leaf(leaf) {
403
+ if (leaf.data.length < 1) {
404
+ throw new Error("Invalid leaf data");
405
+ }
406
+ let firstElement = leaf.data[0];
407
+ let value = firstElement;
408
+ for (let i = 1; i < leaf.data.length; i++) {
409
+ value = pedersen_hash(value, leaf.data[i]);
410
+ }
411
+ return `0x${num2.toHexString(value).replace(/^0x/, "").padStart(64, "0")}`;
412
+ }
413
+ function pedersen_hash(a, b) {
414
+ return BigInt(pedersen(a, b).toString());
415
+ }
416
+ var StandardMerkleTree = class _StandardMerkleTree extends MerkleTreeImpl {
417
+ constructor(tree, values, leafEncoding) {
418
+ super(tree, values, (leaf) => {
419
+ return hash_leaf(leaf);
420
+ });
421
+ this.tree = tree;
422
+ this.values = values;
423
+ this.leafEncoding = leafEncoding;
424
+ }
425
+ static of(values, leafEncoding = [], options = {}) {
426
+ const [tree, indexedValues] = MerkleTreeImpl.prepare(values, options, (leaf) => {
427
+ return hash_leaf(leaf);
428
+ });
429
+ return new _StandardMerkleTree(tree, indexedValues, leafEncoding);
430
+ }
431
+ static verify(root, leafEncoding, leaf, proof) {
432
+ return toHex(root) === processProof(standardLeafHash(leafEncoding, leaf), proof);
433
+ }
434
+ static verifyMultiProof(root, leafEncoding, multiproof) {
435
+ return toHex(root) === processMultiProof({
436
+ leaves: multiproof.leaves.map((leaf) => standardLeafHash(leafEncoding, leaf)),
437
+ proof: multiproof.proof,
438
+ proofFlags: multiproof.proofFlags
439
+ });
440
+ }
441
+ dump() {
442
+ return {
443
+ format: "standard-v1",
444
+ leafEncoding: this.leafEncoding,
445
+ tree: this.tree,
446
+ values: this.values
447
+ };
448
+ }
449
+ };
450
+
451
+ // src/utils/index.ts
452
+ function assert(condition, message) {
453
+ if (!condition) {
454
+ throw new Error(message);
455
+ }
456
+ }
457
+ function getTrovesEndpoint() {
458
+ return process.env.TROVES_ENDPOINT || "https://app.troves.fi";
459
+ }
460
+
461
+ // src/modules/avnu.ts
462
+ var AvnuWrapper = class {
463
+ async getQuotes(fromToken, toToken, amountWei, taker, retry = 0, excludeSources = ["Haiko(Solvers)"]) {
464
+ const MAX_RETRY = 5;
465
+ const params = {
466
+ sellTokenAddress: fromToken,
467
+ buyTokenAddress: toToken,
468
+ sellAmount: amountWei,
469
+ takerAddress: taker,
470
+ // excludeSources: ['Nostra', 'Haiko(Solvers)']
471
+ excludeSources
472
+ // excludeSources: ['Haiko(Solvers)'] // to resolve InvalidOraclePrice error
473
+ };
474
+ assert(fromToken != toToken, "From and to tokens are the same");
475
+ const quotes = await fetchQuotes(params);
476
+ const filteredQuotes = quotes.filter((q) => q.sellAmount.toString() == amountWei);
477
+ if (filteredQuotes.length == 0) {
478
+ if (retry < MAX_RETRY) {
479
+ await new Promise((res) => setTimeout(res, 3e3));
480
+ return await this.getQuotes(fromToken, toToken, amountWei, taker, retry + 1);
481
+ }
482
+ throw new Error("no quotes found");
483
+ }
484
+ return filteredQuotes[0];
485
+ }
486
+ async getSwapInfo(quote, taker, integratorFeeBps, integratorFeeRecipient, minAmount, options) {
487
+ const calldata = await fetchBuildExecuteTransaction(quote.quoteId, taker, void 0, void 0, options);
488
+ const call = calldata.calls[1];
489
+ const callData = call.calldata;
490
+ const routesLen = Number(callData[11]);
491
+ assert(routesLen > 0, "No routes found");
492
+ let startIndex = 12;
493
+ const routes = [];
494
+ for (let i = 0; i < routesLen; ++i) {
495
+ const swap_params_len = Number(callData[startIndex + 4]);
496
+ const route = {
497
+ token_from: callData[startIndex],
498
+ token_to: callData[startIndex + 1],
499
+ exchange_address: callData[startIndex + 2],
500
+ percent: Number(callData[startIndex + 3]),
501
+ additional_swap_params: swap_params_len > 0 ? callData.slice(startIndex + 5, startIndex + 5 + swap_params_len) : []
502
+ };
503
+ routes.push(route);
504
+ startIndex += 5 + swap_params_len;
505
+ }
506
+ const _minAmount = minAmount || (quote.buyAmount * 95n / 100n).toString();
507
+ const swapInfo = {
508
+ token_from_address: quote.sellTokenAddress,
509
+ token_from_amount: uint256.bnToUint256(quote.sellAmount),
510
+ token_to_address: quote.buyTokenAddress,
511
+ token_to_amount: uint256.bnToUint256(_minAmount),
512
+ token_to_min_amount: uint256.bnToUint256(_minAmount),
513
+ beneficiary: taker,
514
+ integrator_fee_amount_bps: integratorFeeBps,
515
+ integrator_fee_recipient: integratorFeeRecipient,
516
+ routes
517
+ };
518
+ return swapInfo;
519
+ }
520
+ static buildZeroSwap(tokenToSell, beneficiary, tokenToBuy = tokenToSell) {
521
+ return {
522
+ token_from_address: tokenToSell.address,
523
+ token_from_amount: uint256.bnToUint256(0),
524
+ token_to_address: tokenToBuy.address,
525
+ token_to_amount: uint256.bnToUint256(0),
526
+ token_to_min_amount: uint256.bnToUint256(0),
527
+ beneficiary,
528
+ integrator_fee_amount_bps: 0,
529
+ integrator_fee_recipient: beneficiary,
530
+ routes: []
531
+ };
532
+ }
533
+ };
534
+
391
535
  // src/modules/pricer.ts
392
536
  var Pricer = class extends PricerBase {
393
537
  // e.g. ETH/USDC
@@ -404,7 +548,7 @@ var Pricer = class extends PricerBase {
404
548
  */
405
549
  // ! switch to USDC (new) later
406
550
  this.PRICE_API = `https://api.coinbase.com/v2/prices/{{PRICER_KEY}}/buy`;
407
- this.EKUBO_API = "https://quoter-mainnet-api.ekubo.org/{{AMOUNT}}/{{TOKEN_ADDRESS}}/0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8";
551
+ this.EKUBO_API = "https://prod-api-quoter.ekubo.org/23448594291968334/{{AMOUNT}}/{{TOKEN_ADDRESS}}/0x033068F6539f8e6e6b131e6B2B814e6c34A5224bC66947c47DaB9dFeE93b35fb";
408
552
  this.refreshInterval = refreshInterval;
409
553
  this.staleTime = staleTime;
410
554
  }
@@ -538,6 +682,15 @@ var Pricer = class extends PricerBase {
538
682
  console.warn(`Ekubo: price err [${token.symbol}]: `, error.message);
539
683
  console.warn(`Ekubo: price err [${token.symbol}]: `, Object.keys(error));
540
684
  }
685
+ case "Avnu":
686
+ try {
687
+ const result = await this._getAvnuPrice(token, new Web3Number(token.priceCheckAmount ? token.priceCheckAmount : 1, token.decimals));
688
+ this.methodToUse[token.symbol] = "Avnu";
689
+ return result;
690
+ } catch (error) {
691
+ console.warn(`Avnu: price err [${token.symbol}]: `, error.message);
692
+ console.warn(`Avnu: price err [${token.symbol}]: `, Object.keys(error));
693
+ }
541
694
  }
542
695
  if (defaultMethod == "all") {
543
696
  return await this._getPrice(token, "Coinbase");
@@ -553,6 +706,22 @@ var Pricer = class extends PricerBase {
553
706
  async _getPriceCoinMarketCap(token) {
554
707
  throw new Error("Not implemented");
555
708
  }
709
+ async _getAvnuPrice(token, amountIn = new Web3Number(1, token.decimals), retry = 0) {
710
+ logger.verbose(`Getting price of ${token.symbol} using Ekubo, amountIn: ${amountIn.toWei()}`);
711
+ const avnuWrapper = new AvnuWrapper();
712
+ const usdcAddress = "0x033068F6539f8e6e6b131e6B2B814e6c34A5224bC66947c47DaB9dFeE93b35fb";
713
+ const quote = await avnuWrapper.getQuotes(token.address.toString(), usdcAddress, amountIn.toWei(), "0x1");
714
+ const multiplier = 1 / amountIn.toNumber();
715
+ const outputUSDC = Number(Web3Number.fromWei(quote.buyAmount.toString(), 6).toFixed(6)) * multiplier;
716
+ logger.verbose(`Avnu: ${token.symbol} -> USDC: ${outputUSDC}, retry: ${retry}`);
717
+ if (outputUSDC === 0 && retry < 3) {
718
+ const amountIn2 = new Web3Number(100, token.decimals);
719
+ return await this._getAvnuPrice(token, amountIn2, retry + 1);
720
+ }
721
+ const usdcPrice = 1;
722
+ logger.verbose(`USDC Price: ${usdcPrice}`);
723
+ return outputUSDC * usdcPrice;
724
+ }
556
725
  async _getPriceEkubo(token, amountIn = new Web3Number(1, token.decimals), retry = 0) {
557
726
  logger.verbose(`Getting price of ${token.symbol} using Ekubo, amountIn: ${amountIn.toWei()}`);
558
727
  const url = this.EKUBO_API.replace("{{TOKEN_ADDRESS}}", token.address.toString()).replace("{{AMOUNT}}", amountIn.toWei());
@@ -2058,150 +2227,6 @@ var ERC20 = class {
2058
2227
  }
2059
2228
  };
2060
2229
 
2061
- // src/modules/avnu.ts
2062
- import { uint256 } from "starknet";
2063
- import { fetchBuildExecuteTransaction, fetchQuotes } from "@avnu/avnu-sdk";
2064
-
2065
- // src/utils/oz-merkle.ts
2066
- import { toHex } from "@ericnordelo/strk-merkle-tree/dist/bytes";
2067
- import { processMultiProof, processProof } from "@ericnordelo/strk-merkle-tree/dist/core";
2068
- import { standardLeafHash } from "@ericnordelo/strk-merkle-tree/dist/hashes";
2069
- import { MerkleTreeImpl } from "@ericnordelo/strk-merkle-tree/dist/merkletree";
2070
- import { num as num2 } from "starknet";
2071
- import { pedersen } from "@scure/starknet";
2072
- function hash_leaf(leaf) {
2073
- if (leaf.data.length < 1) {
2074
- throw new Error("Invalid leaf data");
2075
- }
2076
- let firstElement = leaf.data[0];
2077
- let value = firstElement;
2078
- for (let i = 1; i < leaf.data.length; i++) {
2079
- value = pedersen_hash(value, leaf.data[i]);
2080
- }
2081
- return `0x${num2.toHexString(value).replace(/^0x/, "").padStart(64, "0")}`;
2082
- }
2083
- function pedersen_hash(a, b) {
2084
- return BigInt(pedersen(a, b).toString());
2085
- }
2086
- var StandardMerkleTree = class _StandardMerkleTree extends MerkleTreeImpl {
2087
- constructor(tree, values, leafEncoding) {
2088
- super(tree, values, (leaf) => {
2089
- return hash_leaf(leaf);
2090
- });
2091
- this.tree = tree;
2092
- this.values = values;
2093
- this.leafEncoding = leafEncoding;
2094
- }
2095
- static of(values, leafEncoding = [], options = {}) {
2096
- const [tree, indexedValues] = MerkleTreeImpl.prepare(values, options, (leaf) => {
2097
- return hash_leaf(leaf);
2098
- });
2099
- return new _StandardMerkleTree(tree, indexedValues, leafEncoding);
2100
- }
2101
- static verify(root, leafEncoding, leaf, proof) {
2102
- return toHex(root) === processProof(standardLeafHash(leafEncoding, leaf), proof);
2103
- }
2104
- static verifyMultiProof(root, leafEncoding, multiproof) {
2105
- return toHex(root) === processMultiProof({
2106
- leaves: multiproof.leaves.map((leaf) => standardLeafHash(leafEncoding, leaf)),
2107
- proof: multiproof.proof,
2108
- proofFlags: multiproof.proofFlags
2109
- });
2110
- }
2111
- dump() {
2112
- return {
2113
- format: "standard-v1",
2114
- leafEncoding: this.leafEncoding,
2115
- tree: this.tree,
2116
- values: this.values
2117
- };
2118
- }
2119
- };
2120
-
2121
- // src/utils/index.ts
2122
- function assert(condition, message) {
2123
- if (!condition) {
2124
- throw new Error(message);
2125
- }
2126
- }
2127
- function getTrovesEndpoint() {
2128
- return process.env.TROVES_ENDPOINT || "https://app.troves.fi";
2129
- }
2130
-
2131
- // src/modules/avnu.ts
2132
- var AvnuWrapper = class {
2133
- async getQuotes(fromToken, toToken, amountWei, taker, retry = 0, excludeSources = ["Haiko(Solvers)"]) {
2134
- const MAX_RETRY = 5;
2135
- const params = {
2136
- sellTokenAddress: fromToken,
2137
- buyTokenAddress: toToken,
2138
- sellAmount: amountWei,
2139
- takerAddress: taker,
2140
- // excludeSources: ['Nostra', 'Haiko(Solvers)']
2141
- excludeSources
2142
- // excludeSources: ['Haiko(Solvers)'] // to resolve InvalidOraclePrice error
2143
- };
2144
- assert(fromToken != toToken, "From and to tokens are the same");
2145
- const quotes = await fetchQuotes(params);
2146
- const filteredQuotes = quotes.filter((q) => q.sellAmount.toString() == amountWei);
2147
- if (filteredQuotes.length == 0) {
2148
- if (retry < MAX_RETRY) {
2149
- await new Promise((res) => setTimeout(res, 3e3));
2150
- return await this.getQuotes(fromToken, toToken, amountWei, taker, retry + 1);
2151
- }
2152
- throw new Error("no quotes found");
2153
- }
2154
- return filteredQuotes[0];
2155
- }
2156
- async getSwapInfo(quote, taker, integratorFeeBps, integratorFeeRecipient, minAmount, options) {
2157
- const calldata = await fetchBuildExecuteTransaction(quote.quoteId, taker, void 0, void 0, options);
2158
- const call = calldata.calls[1];
2159
- const callData = call.calldata;
2160
- const routesLen = Number(callData[11]);
2161
- assert(routesLen > 0, "No routes found");
2162
- let startIndex = 12;
2163
- const routes = [];
2164
- for (let i = 0; i < routesLen; ++i) {
2165
- const swap_params_len = Number(callData[startIndex + 4]);
2166
- const route = {
2167
- token_from: callData[startIndex],
2168
- token_to: callData[startIndex + 1],
2169
- exchange_address: callData[startIndex + 2],
2170
- percent: Number(callData[startIndex + 3]),
2171
- additional_swap_params: swap_params_len > 0 ? callData.slice(startIndex + 5, startIndex + 5 + swap_params_len) : []
2172
- };
2173
- routes.push(route);
2174
- startIndex += 5 + swap_params_len;
2175
- }
2176
- const _minAmount = minAmount || (quote.buyAmount * 95n / 100n).toString();
2177
- const swapInfo = {
2178
- token_from_address: quote.sellTokenAddress,
2179
- token_from_amount: uint256.bnToUint256(quote.sellAmount),
2180
- token_to_address: quote.buyTokenAddress,
2181
- token_to_amount: uint256.bnToUint256(_minAmount),
2182
- token_to_min_amount: uint256.bnToUint256(_minAmount),
2183
- beneficiary: taker,
2184
- integrator_fee_amount_bps: integratorFeeBps,
2185
- integrator_fee_recipient: integratorFeeRecipient,
2186
- routes
2187
- };
2188
- return swapInfo;
2189
- }
2190
- static buildZeroSwap(tokenToSell, beneficiary, tokenToBuy = tokenToSell) {
2191
- return {
2192
- token_from_address: tokenToSell.address,
2193
- token_from_amount: uint256.bnToUint256(0),
2194
- token_to_address: tokenToBuy.address,
2195
- token_to_amount: uint256.bnToUint256(0),
2196
- token_to_min_amount: uint256.bnToUint256(0),
2197
- beneficiary,
2198
- integrator_fee_amount_bps: 0,
2199
- integrator_fee_recipient: beneficiary,
2200
- routes: []
2201
- };
2202
- }
2203
- };
2204
-
2205
2230
  // src/modules/ekubo-quoter.ts
2206
2231
  import axios5 from "axios";
2207
2232
  var EkuboQuoter = class {
@@ -30552,8 +30577,8 @@ var usdcVaultSettings = {
30552
30577
  aumOracle: ContractAddr.from("0x6faf45ed185dec13ef723c9ead4266cab98d06f2cb237e331b1fa5c2aa79afe"),
30553
30578
  leafAdapters: [],
30554
30579
  adapters: [],
30555
- targetHealthFactor: 1.3,
30556
- minHealthFactor: 1.25
30580
+ targetHealthFactor: 1.25,
30581
+ minHealthFactor: 1.15
30557
30582
  };
30558
30583
  var wbtcVaultSettings = {
30559
30584
  vaultAddress: ContractAddr.from("0x5a4c1651b913aa2ea7afd9024911603152a19058624c3e425405370d62bf80c"),
@@ -30563,8 +30588,8 @@ var wbtcVaultSettings = {
30563
30588
  aumOracle: ContractAddr.from("0x2edf4edbed3f839e7f07dcd913e92299898ff4cf0ba532f8c572c66c5b331b2"),
30564
30589
  leafAdapters: [],
30565
30590
  adapters: [],
30566
- targetHealthFactor: 1.3,
30567
- minHealthFactor: 1.25
30591
+ targetHealthFactor: 1.2,
30592
+ minHealthFactor: 1.15
30568
30593
  };
30569
30594
  var ethVaultSettings = {
30570
30595
  vaultAddress: ContractAddr.from("0x446c22d4d3f5cb52b4950ba832ba1df99464c6673a37c092b1d9622650dbd8"),
@@ -30574,8 +30599,8 @@ var ethVaultSettings = {
30574
30599
  aumOracle: ContractAddr.from("0x4b747f2e75c057bed9aa2ce46fbdc2159dc684c15bd32d4f95983a6ecf39a05"),
30575
30600
  leafAdapters: [],
30576
30601
  adapters: [],
30577
- targetHealthFactor: 1.3,
30578
- minHealthFactor: 1.25
30602
+ targetHealthFactor: 1.25,
30603
+ minHealthFactor: 1.21
30579
30604
  };
30580
30605
  var strkVaultSettings = {
30581
30606
  vaultAddress: ContractAddr.from("0x55d012f57e58c96e0a5c7ebbe55853989d01e6538b15a95e7178aca4af05c21"),
@@ -30585,8 +30610,8 @@ var strkVaultSettings = {
30585
30610
  aumOracle: ContractAddr.from("0x6d7dbfad4bb51715da211468389a623da00c0625f8f6efbea822ee5ac5231f4"),
30586
30611
  leafAdapters: [],
30587
30612
  adapters: [],
30588
- targetHealthFactor: 1.3,
30589
- minHealthFactor: 1.25
30613
+ targetHealthFactor: 1.2,
30614
+ minHealthFactor: 1.15
30590
30615
  };
30591
30616
  var usdtVaultSettings = {
30592
30617
  vaultAddress: ContractAddr.from("0x1c4933d1880c6778585e597154eaca7b428579d72f3aae425ad2e4d26c6bb3"),
@@ -30596,8 +30621,8 @@ var usdtVaultSettings = {
30596
30621
  aumOracle: ContractAddr.from("0x7018f8040c8066a4ab929e6760ae52dd43b6a3a289172f514750a61fcc565cc"),
30597
30622
  leafAdapters: [],
30598
30623
  adapters: [],
30599
- targetHealthFactor: 1.3,
30600
- minHealthFactor: 1.25
30624
+ targetHealthFactor: 1.25,
30625
+ minHealthFactor: 1.15
30601
30626
  };
30602
30627
  function MetaVaultDescription(allowedSources) {
30603
30628
  const logos = {
package/dist/index.d.ts CHANGED
@@ -246,7 +246,7 @@ declare class Pricer extends PricerBase {
246
246
  refreshInterval: number;
247
247
  staleTime: number;
248
248
  protected methodToUse: {
249
- [tokenSymbol: string]: 'Ekubo' | 'Coinbase' | 'Coinmarketcap';
249
+ [tokenSymbol: string]: 'Ekubo' | 'Coinbase' | 'Coinmarketcap' | 'Avnu';
250
250
  };
251
251
  /**
252
252
  * TOKENA and TOKENB are the two token names to get price of TokenA in terms of TokenB
@@ -264,6 +264,7 @@ declare class Pricer extends PricerBase {
264
264
  _getPrice(token: TokenInfo, defaultMethod?: string): Promise<number>;
265
265
  _getPriceCoinbase(token: TokenInfo): Promise<number>;
266
266
  _getPriceCoinMarketCap(token: TokenInfo): Promise<number>;
267
+ _getAvnuPrice(token: TokenInfo, amountIn?: Web3Number, retry?: number): Promise<number>;
267
268
  _getPriceEkubo(token: TokenInfo, amountIn?: Web3Number, retry?: number): Promise<number>;
268
269
  }
269
270