@paraswap/dex-lib 4.1.7-pub-sub → 4.1.7-pub-sub.3

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.
Files changed (69) hide show
  1. package/build/dex/angle-staked-stable/angle-staked-stable.js +2 -1
  2. package/build/dex/angle-staked-stable/angle-staked-stable.js.map +1 -1
  3. package/build/dex/angle-transmuter/angle-transmuter.js.map +1 -1
  4. package/build/dex/balancer-v1/config.js +2 -1
  5. package/build/dex/balancer-v1/config.js.map +1 -1
  6. package/build/dex/weth/weth.js +2 -1
  7. package/build/dex/weth/weth.js.map +1 -1
  8. package/build/lib/pub-sub.js +5 -5
  9. package/build/lib/pub-sub.js.map +1 -1
  10. package/build/pricing-helper.d.ts +1 -1
  11. package/build/pricing-helper.js +13 -9
  12. package/build/pricing-helper.js.map +1 -1
  13. package/build/types.d.ts +1 -0
  14. package/package.json +1 -1
  15. package/src/dex/aave-gsm/aave-gsm-gas-estimation.test.ts +32 -0
  16. package/src/dex/aave-v2/aave-v2-gas-estimation.test.ts +28 -0
  17. package/src/dex/aave-v3/aave-v3-gas-estimation.test.ts +66 -0
  18. package/src/dex/aave-v3-stata/aave-v3-stata-gas-estimation.test.ts +41 -0
  19. package/src/dex/aave-v3-stata-v2/aave-v3-stata-v2-gas-estimation.test.ts +41 -0
  20. package/src/dex/angle-staked-stable/angle-staked-stable-gas-estimation.test.ts +29 -0
  21. package/src/dex/angle-staked-stable/angle-staked-stable.ts +2 -1
  22. package/src/dex/angle-transmuter/angle-transmuter-gas-estimation.test.ts +41 -0
  23. package/src/dex/angle-transmuter/angle-transmuter.ts +0 -1
  24. package/src/dex/balancer-v1/balancer-v1-gas-estimation.test.ts +29 -0
  25. package/src/dex/balancer-v1/config.ts +2 -1
  26. package/src/dex/balancer-v2/balancer-v2-gas-estimation.test.ts +98 -0
  27. package/src/dex/balancer-v3/balancer-v3-gas-estimation.test.ts +30 -0
  28. package/src/dex/bancor/bancor-gas-estimation.test.ts +30 -0
  29. package/src/dex/bebop/bebop-gas-estimation.test.ts +30 -0
  30. package/src/dex/concentrator-arusd/concentrator-arusd-gas-estimation.test.ts +29 -0
  31. package/src/dex/curve-v1/curve-v1-gas-estimation.test.ts +29 -0
  32. package/src/dex/curve-v2/curve-v2-gas-estimation.test.ts +29 -0
  33. package/src/dex/fluid-dex/fluid-dex-gas-estimation.test.ts +29 -0
  34. package/src/dex/fx-protocol-rusd/fx-protocol-rusd-gas-estimation.test.ts +29 -0
  35. package/src/dex/hashflow/hashflow-gas-estimation.test.ts +30 -0
  36. package/src/dex/idle-dao/idle-dao--gas-estimation.test.ts +30 -0
  37. package/src/dex/lido/lido-gas-estimation.test.ts +29 -0
  38. package/src/dex/lite-psm/lite-psm-gas-estimation.test.ts +34 -0
  39. package/src/dex/maker-psm/maker-psm-gas-estimation.test.ts +34 -0
  40. package/src/dex/maverick-v1/maverick-v1-gas-estimation.test.ts +29 -0
  41. package/src/dex/maverick-v2/maverick-v2-gas-estimation.test.ts +29 -0
  42. package/src/dex/pancakeswap-v3/pancakeswap-v3-gas-estimation.test.ts +29 -0
  43. package/src/dex/polygon-migrator/polygon-migrator-gas-estimation.test.ts +41 -0
  44. package/src/dex/sky-converter/sky-converter-gas-estimation.test.ts +78 -0
  45. package/src/dex/solidly/solidly-gas-estimation.test.ts +29 -0
  46. package/src/dex/solidly-v3/solidly-v3-gas-estimation.test.ts +29 -0
  47. package/src/dex/spark/spark-gas-estimation.test.ts +29 -0
  48. package/src/dex/stader/stader-gas-estimation.test.ts +29 -0
  49. package/src/dex/stkgho/stkgho-gas-estimation.test.ts +29 -0
  50. package/src/dex/swaap-v2/swaap-v2-estimation.test.ts +29 -0
  51. package/src/dex/swell/swell-estimation.test.ts +29 -0
  52. package/src/dex/trader-joe-v2.1/trader-joe-v2.1-gas-estimation.test.ts +29 -0
  53. package/src/dex/uniswap-v2/uniswap-v2-gas-estimation.test.ts +98 -0
  54. package/src/dex/uniswap-v3/uniswap-v3-gas-estimation.test.ts +98 -0
  55. package/src/dex/usual/usual-gas-estimation.test.ts +54 -0
  56. package/src/dex/usual-pp/usual-pp-gas-estimation.test.ts +29 -0
  57. package/src/dex/weth/weth-gas-estimation.test.ts +42 -0
  58. package/src/dex/weth/weth.ts +3 -1
  59. package/src/dex/wombat/wombat-gas-estimation.test.ts +29 -0
  60. package/src/dex/wsteth/wsteth-gas-estimation.test.ts +41 -0
  61. package/src/dex/wusdm/wusdm-gas-estimation.test.ts +41 -0
  62. package/src/lib/pub-sub.ts +9 -5
  63. package/src/pricing-helper.ts +19 -11
  64. package/src/types.ts +1 -0
  65. package/tests/constants-e2e.ts +8 -0
  66. package/tests/tenderly-simulation-new.test.ts +151 -0
  67. package/tests/tenderly-simulation-new.ts +698 -0
  68. package/tests/token-storage-slots.json +532 -0
  69. package/tests/utils-e2e.ts +137 -1
@@ -14,6 +14,7 @@ import {
14
14
  TenderlySimulation,
15
15
  TransactionSimulator,
16
16
  } from './tenderly-simulation';
17
+ import { TenderlySimulatorNew, StateOverride } from './tenderly-simulation-new';
17
18
  import {
18
19
  SwapSide,
19
20
  ETHER_ADDRESS,
@@ -124,7 +125,7 @@ class APIParaswapSDK implements IParaSwapSDK {
124
125
  constructor(
125
126
  protected network: number,
126
127
  dexKeys: string | string[],
127
- rpcUrl: string,
128
+ rpcUrl?: string,
128
129
  ) {
129
130
  this.dexKeys = Array.isArray(dexKeys) ? dexKeys : [dexKeys];
130
131
  this.paraSwap = constructSimpleSDK({
@@ -224,6 +225,10 @@ class APIParaswapSDK implements IParaSwapSDK {
224
225
  uuid: uuid(),
225
226
  })) as TxObject;
226
227
  }
228
+
229
+ async releaseResources(): Promise<void> {
230
+ await this.pricingHelper.releaseResources(this.dexKeys);
231
+ }
227
232
  }
228
233
 
229
234
  function send1WeiTo(token: Address, to: Address, network: Network) {
@@ -1028,3 +1033,134 @@ export const constructE2ETests = (
1028
1033
  });
1029
1034
  });
1030
1035
  };
1036
+
1037
+ export const testGasEstimation = async (
1038
+ network: Network,
1039
+ srcToken: Token,
1040
+ destToken: Token,
1041
+ amount: bigint,
1042
+ swapSide: SwapSide,
1043
+ dexKeys: string | string[],
1044
+ contractMethod: ContractMethod,
1045
+ route?: string[],
1046
+ targetDifference?: number,
1047
+ ) => {
1048
+ assert(
1049
+ testingEndpoint,
1050
+ 'Estimation can only be tested with testing endpoint',
1051
+ );
1052
+ // initialize pricing
1053
+ const sdk = new APIParaswapSDK(network, dexKeys);
1054
+ await sdk.initializePricing();
1055
+ // fetch the route
1056
+ const priceRoute = await sdk.getPrices(
1057
+ srcToken,
1058
+ destToken,
1059
+ amount,
1060
+ swapSide,
1061
+ contractMethod,
1062
+ undefined,
1063
+ undefined,
1064
+ route,
1065
+ );
1066
+ // make sure fetched route uses correct `contractMethod`
1067
+ assert(
1068
+ priceRoute.contractMethod === contractMethod,
1069
+ 'Price route has incorrect contract method!',
1070
+ );
1071
+ // log the route for visibility
1072
+ console.log({ priceRoute: JSON.stringify(priceRoute, null, 2) });
1073
+ // prepare state overrides
1074
+ const tenderlySimulator = TenderlySimulatorNew.getInstance();
1075
+ // any address works
1076
+ const userAddress = TenderlySimulatorNew.DEFAULT_OWNER;
1077
+ // init `StateOverride` object
1078
+ const stateOverride: StateOverride = {};
1079
+ // fund x2 just in case
1080
+ const amountToFund = amount * 2n;
1081
+ // add overrides for src token
1082
+ if (srcToken.address.toLowerCase() === ETHER_ADDRESS) {
1083
+ // add eth balance to user
1084
+ tenderlySimulator.addBalanceOverride(
1085
+ stateOverride,
1086
+ userAddress,
1087
+ amountToFund,
1088
+ );
1089
+ } else {
1090
+ // add token balance and allowance to Augustus
1091
+ await tenderlySimulator.addTokenBalanceOverride(
1092
+ stateOverride,
1093
+ network,
1094
+ srcToken.address,
1095
+ userAddress,
1096
+ amountToFund,
1097
+ );
1098
+ await tenderlySimulator.addAllowanceOverride(
1099
+ stateOverride,
1100
+ network,
1101
+ srcToken.address,
1102
+ userAddress,
1103
+ priceRoute.tokenTransferProxy,
1104
+ amountToFund,
1105
+ );
1106
+ }
1107
+ // add overrides for dest token (dust balance)
1108
+ if (destToken.address.toLowerCase() === ETHER_ADDRESS) {
1109
+ // add eth dust
1110
+ tenderlySimulator.addBalanceOverride(stateOverride, userAddress, 1n);
1111
+ } else {
1112
+ // add token dust
1113
+ await tenderlySimulator.addTokenBalanceOverride(
1114
+ stateOverride,
1115
+ network,
1116
+ destToken.address,
1117
+ userAddress,
1118
+ 1n,
1119
+ );
1120
+ }
1121
+ // build swap transaction
1122
+ const slippage = 100n;
1123
+ const minMaxAmount =
1124
+ (swapSide === SwapSide.SELL
1125
+ ? BigInt(priceRoute.destAmount) * (10000n - slippage)
1126
+ : BigInt(priceRoute.srcAmount) * (10000n + slippage)) / 10000n;
1127
+ const swapParams = await sdk.buildTransaction(
1128
+ priceRoute,
1129
+ minMaxAmount,
1130
+ userAddress,
1131
+ );
1132
+ assert(
1133
+ swapParams.to !== undefined,
1134
+ 'Transaction params missing `to` property',
1135
+ );
1136
+ // assemble `SimulationRequest`
1137
+ const { from, to, data, value } = swapParams;
1138
+ const simulationRequest = {
1139
+ chainId: network,
1140
+ from,
1141
+ to,
1142
+ data,
1143
+ value,
1144
+ blockNumber: priceRoute.blockNumber,
1145
+ stateOverride,
1146
+ };
1147
+ // simulate the transaction with overrides
1148
+ const simulation = await tenderlySimulator.simulateTransaction(
1149
+ simulationRequest,
1150
+ );
1151
+ // compare and assert
1152
+ const estimatedGas = Number(priceRoute.gasCost);
1153
+ const actualGas = simulation.gas_used;
1154
+ const diffPercent = ((estimatedGas - actualGas) / actualGas) * 100;
1155
+ console.log(
1156
+ `Estimated gas cost: ${estimatedGas}, actual gas cost: ${actualGas}, diff: ${diffPercent}%`,
1157
+ );
1158
+ if (targetDifference !== undefined) {
1159
+ assert(
1160
+ targetDifference <= Math.abs(diffPercent),
1161
+ `Deviation is higher than target ${targetDifference}%`,
1162
+ );
1163
+ }
1164
+ // release
1165
+ await sdk.releaseResources();
1166
+ };