@gmx-io/sdk 2.9.3 → 3.0.0

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 (163) hide show
  1. package/build/cjs/src/abis/RatioVesterReader.js +45 -0
  2. package/build/cjs/src/abis/index.js +2 -0
  3. package/build/cjs/src/clients/v1/modules/oracle.js +15 -0
  4. package/build/cjs/src/clients/v1/modules/oracle.spec.js +20 -0
  5. package/build/cjs/src/clients/v1/modules/orders/helpers.js +16 -12
  6. package/build/cjs/src/clients/v1/modules/orders/helpers.spec.js +21 -0
  7. package/build/cjs/src/clients/v1/modules/orders/orders.js +4 -0
  8. package/build/cjs/src/clients/v1/modules/orders/utils.js +1 -0
  9. package/build/cjs/src/clients/v2/__tests__/subaccount.spec.js +74 -17
  10. package/build/cjs/src/clients/v2/__tests__/subaccountLifecycle.spec.js +1 -1
  11. package/build/cjs/src/clients/v2/__tests__/testUtil.js +14 -2
  12. package/build/cjs/src/clients/v2/index.js +28 -7
  13. package/build/cjs/src/clients/v2/solana.spec.js +81 -4
  14. package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +48 -0
  15. package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +782 -0
  16. package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +90 -0
  17. package/build/cjs/src/configs/contracts.js +10 -0
  18. package/build/cjs/src/configs/dataStore.js +22 -4
  19. package/build/cjs/src/configs/express.js +1 -2
  20. package/build/cjs/src/configs/markets.js +42 -0
  21. package/build/cjs/src/configs/tokens.js +2 -0
  22. package/build/cjs/src/test/mockCrossFetch.js +1 -0
  23. package/build/cjs/src/utils/__tests__/fees.spec.js +52 -0
  24. package/build/cjs/src/utils/__tests__/gmsolUnits.spec.js +101 -0
  25. package/build/cjs/src/utils/__tests__/orderTransactions/batchUtils.spec.js +66 -0
  26. package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +6 -6
  27. package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +40 -0
  28. package/build/cjs/src/utils/__tests__/orderTransactions/updateOrder.spec.js +4 -2
  29. package/build/cjs/src/utils/__tests__/tokens.spec.js +63 -0
  30. package/build/cjs/src/utils/errors/transactionsErrors.js +6 -0
  31. package/build/cjs/src/utils/fees/index.js +5 -0
  32. package/build/cjs/src/utils/fees/priceImpact.js +39 -6
  33. package/build/cjs/src/utils/gmsol/index.js +15 -0
  34. package/build/cjs/src/utils/gmsol/units.js +106 -0
  35. package/build/cjs/src/utils/http/http.js +21 -25
  36. package/build/cjs/src/utils/http/httpFallback.js +3 -3
  37. package/build/cjs/src/utils/http/validation.js +9 -1
  38. package/build/cjs/src/utils/markets/hashKeys.js +8 -0
  39. package/build/cjs/src/utils/markets/multicall.js +10 -0
  40. package/build/cjs/src/utils/markets/utils.js +2 -0
  41. package/build/cjs/src/utils/multichain/__tests__/api.spec.js +49 -0
  42. package/build/cjs/src/utils/multichain/api.js +29 -2
  43. package/build/cjs/src/utils/orderTransactions/utils.js +63 -5
  44. package/build/cjs/src/utils/orders/api.js +66 -3
  45. package/build/cjs/src/utils/positions/__tests__/liquidationImpact.spec.js +44 -0
  46. package/build/cjs/src/utils/positions/__tests__/liquidationPrice.spec.js +159 -2
  47. package/build/cjs/src/utils/positions/__tests__/trackingCollateral.spec.js +395 -0
  48. package/build/cjs/src/utils/positions/api.js +4 -0
  49. package/build/cjs/src/utils/positions/utils.js +119 -44
  50. package/build/cjs/src/utils/prices/api.js +2 -2
  51. package/build/cjs/src/utils/referrals/api.js +5 -4
  52. package/build/cjs/src/utils/subaccount/generateSubaccount.js +14 -14
  53. package/build/cjs/src/utils/subaccount/sdkClient.js +40 -19
  54. package/build/cjs/src/utils/tokens/utils.js +23 -7
  55. package/build/cjs/src/utils/trade/__tests__/decrease.spec.js +152 -0
  56. package/build/cjs/src/utils/trade/__tests__/gmsolTradeMath.spec.js +43 -0
  57. package/build/cjs/src/utils/trade/decrease.js +113 -33
  58. package/build/cjs/src/utils/trade/decreaseMarginCheck.js +38 -2
  59. package/build/cjs/src/utils/trade/decreaseOutputs.js +20 -5
  60. package/build/cjs/src/utils/trade/increase.js +30 -11
  61. package/build/cjs/src/utils/trade/increaseMarginCheck.js +14 -35
  62. package/build/esm/src/abis/RatioVesterReader.js +43 -0
  63. package/build/esm/src/abis/index.js +2 -0
  64. package/build/esm/src/clients/v1/modules/oracle.js +15 -0
  65. package/build/esm/src/clients/v1/modules/oracle.spec.js +18 -0
  66. package/build/esm/src/clients/v1/modules/orders/helpers.js +17 -13
  67. package/build/esm/src/clients/v1/modules/orders/helpers.spec.js +21 -0
  68. package/build/esm/src/clients/v1/modules/orders/orders.js +4 -0
  69. package/build/esm/src/clients/v1/modules/orders/utils.js +1 -0
  70. package/build/esm/src/clients/v2/__tests__/subaccount.spec.js +74 -17
  71. package/build/esm/src/clients/v2/__tests__/subaccountLifecycle.spec.js +2 -2
  72. package/build/esm/src/clients/v2/__tests__/testUtil.js +13 -2
  73. package/build/esm/src/clients/v2/index.js +29 -8
  74. package/build/esm/src/clients/v2/solana.spec.js +79 -2
  75. package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +48 -0
  76. package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +782 -0
  77. package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +90 -0
  78. package/build/esm/src/configs/contracts.js +10 -0
  79. package/build/esm/src/configs/dataStore.js +14 -0
  80. package/build/esm/src/configs/express.js +0 -1
  81. package/build/esm/src/configs/markets.js +42 -0
  82. package/build/esm/src/configs/tokens.js +2 -0
  83. package/build/esm/src/test/mockCrossFetch.js +1 -0
  84. package/build/esm/src/utils/__tests__/fees.spec.js +53 -1
  85. package/build/esm/src/utils/__tests__/gmsolUnits.spec.js +99 -0
  86. package/build/esm/src/utils/__tests__/orderTransactions/batchUtils.spec.js +68 -2
  87. package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +6 -6
  88. package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +40 -0
  89. package/build/esm/src/utils/__tests__/orderTransactions/updateOrder.spec.js +5 -3
  90. package/build/esm/src/utils/__tests__/tokens.spec.js +64 -1
  91. package/build/esm/src/utils/errors/transactionsErrors.js +6 -0
  92. package/build/esm/src/utils/fees/index.js +5 -0
  93. package/build/esm/src/utils/fees/priceImpact.js +36 -5
  94. package/build/esm/src/utils/gmsol/index.js +1 -0
  95. package/build/esm/src/utils/gmsol/units.js +97 -0
  96. package/build/esm/src/utils/http/http.js +21 -25
  97. package/build/esm/src/utils/http/httpFallback.js +3 -3
  98. package/build/esm/src/utils/http/validation.js +7 -0
  99. package/build/esm/src/utils/markets/hashKeys.js +9 -1
  100. package/build/esm/src/utils/markets/multicall.js +10 -0
  101. package/build/esm/src/utils/markets/utils.js +2 -0
  102. package/build/esm/src/utils/multichain/__tests__/api.spec.js +46 -0
  103. package/build/esm/src/utils/multichain/api.js +29 -2
  104. package/build/esm/src/utils/orderTransactions/utils.js +59 -5
  105. package/build/esm/src/utils/orders/api.js +64 -2
  106. package/build/esm/src/utils/positions/__tests__/liquidationImpact.spec.js +42 -0
  107. package/build/esm/src/utils/positions/__tests__/liquidationPrice.spec.js +161 -4
  108. package/build/esm/src/utils/positions/__tests__/trackingCollateral.spec.js +393 -0
  109. package/build/esm/src/utils/positions/api.js +4 -0
  110. package/build/esm/src/utils/positions/utils.js +115 -44
  111. package/build/esm/src/utils/prices/api.js +2 -2
  112. package/build/esm/src/utils/referrals/api.js +6 -5
  113. package/build/esm/src/utils/subaccount/generateSubaccount.js +13 -14
  114. package/build/esm/src/utils/subaccount/sdkClient.js +38 -19
  115. package/build/esm/src/utils/tokens/utils.js +20 -6
  116. package/build/esm/src/utils/trade/__tests__/decrease.spec.js +150 -0
  117. package/build/esm/src/utils/trade/__tests__/gmsolTradeMath.spec.js +43 -0
  118. package/build/esm/src/utils/trade/decrease.js +115 -35
  119. package/build/esm/src/utils/trade/decreaseMarginCheck.js +37 -2
  120. package/build/esm/src/utils/trade/decreaseOutputs.js +19 -5
  121. package/build/esm/src/utils/trade/increase.js +32 -13
  122. package/build/esm/src/utils/trade/increaseMarginCheck.js +18 -39
  123. package/build/tsconfig.tsbuildinfo +1 -1
  124. package/build/types/src/abis/RatioVesterReader.d.ts +74 -0
  125. package/build/types/src/abis/index.d.ts +73 -0
  126. package/build/types/src/clients/v1/modules/oracle.d.ts +6 -1
  127. package/build/types/src/clients/v1/modules/oracle.spec.d.ts +1 -0
  128. package/build/types/src/clients/v2/__tests__/testUtil.d.ts +1 -0
  129. package/build/types/src/clients/v2/index.d.ts +26 -5
  130. package/build/types/src/codegen/prebuilt/index.d.ts +1045 -125
  131. package/build/types/src/configs/contracts.d.ts +10 -0
  132. package/build/types/src/configs/dataStore.d.ts +8 -0
  133. package/build/types/src/configs/express.d.ts +0 -1
  134. package/build/types/src/utils/__tests__/gmsolUnits.spec.d.ts +1 -0
  135. package/build/types/src/utils/errors/parseError.d.ts +1 -1
  136. package/build/types/src/utils/errors/transactionsErrors.d.ts +6 -0
  137. package/build/types/src/utils/fees/priceImpact.d.ts +10 -0
  138. package/build/types/src/utils/gmsol/index.d.ts +1 -0
  139. package/build/types/src/utils/gmsol/units.d.ts +53 -0
  140. package/build/types/src/utils/http/types.d.ts +1 -0
  141. package/build/types/src/utils/http/validation.d.ts +1 -0
  142. package/build/types/src/utils/markets/types.d.ts +5 -2
  143. package/build/types/src/utils/multichain/api.d.ts +4 -1
  144. package/build/types/src/utils/orderTransactions/utils.d.ts +19 -4
  145. package/build/types/src/utils/orders/api.d.ts +2 -1
  146. package/build/types/src/utils/orders/types.d.ts +24 -0
  147. package/build/types/src/utils/positions/__tests__/liquidationImpact.spec.d.ts +1 -0
  148. package/build/types/src/utils/positions/__tests__/trackingCollateral.spec.d.ts +1 -0
  149. package/build/types/src/utils/positions/types.d.ts +2 -0
  150. package/build/types/src/utils/positions/utils.d.ts +30 -0
  151. package/build/types/src/utils/prices/api.d.ts +2 -0
  152. package/build/types/src/utils/referrals/types.d.ts +4 -0
  153. package/build/types/src/utils/subaccount/generateSubaccount.d.ts +9 -4
  154. package/build/types/src/utils/subaccount/sdkClient.d.ts +7 -1
  155. package/build/types/src/utils/subaccount/types.d.ts +2 -0
  156. package/build/types/src/utils/tokens/types.d.ts +1 -0
  157. package/build/types/src/utils/tokens/utils.d.ts +6 -0
  158. package/build/types/src/utils/trade/__tests__/decrease.spec.d.ts +1 -0
  159. package/build/types/src/utils/trade/decrease.d.ts +3 -0
  160. package/build/types/src/utils/trade/decreaseMarginCheck.d.ts +11 -0
  161. package/build/types/src/utils/trade/decreaseOutputs.d.ts +7 -2
  162. package/build/types/src/utils/trade/increaseMarginCheck.d.ts +1 -0
  163. package/package.json +10 -1
@@ -2011,6 +2011,96 @@
2011
2011
  "longInterestInTokensUsingShortToken": "0x0be7c3fb85d2667f04730e3e00c1d76a02c6551187763671de6f340e54253765",
2012
2012
  "shortInterestInTokensUsingLongToken": "0x5b6f9198409137fb78d86f451b96d2e90f1838104150f722d67a5e04d5fc1f6f",
2013
2013
  "shortInterestInTokensUsingShortToken": "0x5b6f9198409137fb78d86f451b96d2e90f1838104150f722d67a5e04d5fc1f6f"
2014
+ },
2015
+ "0x71Dd0524F8e0aab8FE59E6B3888145A8ac117Aa3": {
2016
+ "longPoolAmount": "0x9286c5da2fa3651cb28106c21cd8039da9f3151ed7bab4389b11faf22a0d79e1",
2017
+ "shortPoolAmount": "0x9286c5da2fa3651cb28106c21cd8039da9f3151ed7bab4389b11faf22a0d79e1",
2018
+ "positionImpactPoolAmount": "0xa1a502eee053d43852c6db46aa6b3b4b0ca117c557897b0d512454c4a6bff89d",
2019
+ "swapImpactPoolAmountLong": "0xb53c8bfdc409eac690d733d6e70bf57d0dbd54750476cc9c688333ee079d3711",
2020
+ "swapImpactPoolAmountShort": "0xb53c8bfdc409eac690d733d6e70bf57d0dbd54750476cc9c688333ee079d3711",
2021
+ "longInterestUsingLongToken": "0x1b7274fa8d7e4cbb03554d1f9b2f0e3d61da628fba36750bea245551405916cb",
2022
+ "longInterestUsingShortToken": "0x1b7274fa8d7e4cbb03554d1f9b2f0e3d61da628fba36750bea245551405916cb",
2023
+ "shortInterestUsingLongToken": "0x77ee3963ac76d940de65b80c13a79f2fe13c0615d73fa178d34e0f89ae78460a",
2024
+ "shortInterestUsingShortToken": "0x77ee3963ac76d940de65b80c13a79f2fe13c0615d73fa178d34e0f89ae78460a",
2025
+ "longInterestInTokensUsingLongToken": "0x52042389bce6a82e5a842771f1d6476da104ad9d7cbad1bfc7525e3b66c0086c",
2026
+ "longInterestInTokensUsingShortToken": "0x52042389bce6a82e5a842771f1d6476da104ad9d7cbad1bfc7525e3b66c0086c",
2027
+ "shortInterestInTokensUsingLongToken": "0x4c3ecbccd4889edb438db4665e4469e58e04f34d80f920fe6aaff21f2c9ab941",
2028
+ "shortInterestInTokensUsingShortToken": "0x4c3ecbccd4889edb438db4665e4469e58e04f34d80f920fe6aaff21f2c9ab941"
2029
+ },
2030
+ "0x1e5bf5390a75a6ab085bd4e7E63A00074C43B858": {
2031
+ "longPoolAmount": "0x2084219e5e561cfc73d07f538adf223db309599f1cac0121e74fca82433071a9",
2032
+ "shortPoolAmount": "0x2084219e5e561cfc73d07f538adf223db309599f1cac0121e74fca82433071a9",
2033
+ "positionImpactPoolAmount": "0x546dfbaf1a45a0c9550e49887f22a3cd8f7228c857a15561a65d7fc92418592e",
2034
+ "swapImpactPoolAmountLong": "0xeddd9a2a42ded7615891f67d25d30b77efd4cf5bc6a2811719674a1300ab2a4e",
2035
+ "swapImpactPoolAmountShort": "0xeddd9a2a42ded7615891f67d25d30b77efd4cf5bc6a2811719674a1300ab2a4e",
2036
+ "longInterestUsingLongToken": "0xa931ae9669a709872e0bda24764dba8fa7e97fd601b06f0a8e8e2221a7c136c0",
2037
+ "longInterestUsingShortToken": "0xa931ae9669a709872e0bda24764dba8fa7e97fd601b06f0a8e8e2221a7c136c0",
2038
+ "shortInterestUsingLongToken": "0xe35433bfd0f2a02981f23577cb07cd6aad7b0c6fb856c951018d57e6bf0bb169",
2039
+ "shortInterestUsingShortToken": "0xe35433bfd0f2a02981f23577cb07cd6aad7b0c6fb856c951018d57e6bf0bb169",
2040
+ "longInterestInTokensUsingLongToken": "0x39770b981ca73b938fa5e7dd504a70dda5ce2323a896e86487f28cc5f28b1bcc",
2041
+ "longInterestInTokensUsingShortToken": "0x39770b981ca73b938fa5e7dd504a70dda5ce2323a896e86487f28cc5f28b1bcc",
2042
+ "shortInterestInTokensUsingLongToken": "0xa43697010da6a02ae5c417c365379774ea5b65b3b87eb6e2181d800d860d93cc",
2043
+ "shortInterestInTokensUsingShortToken": "0xa43697010da6a02ae5c417c365379774ea5b65b3b87eb6e2181d800d860d93cc"
2044
+ },
2045
+ "0x2bDCC67868abA103406dCeb4F853B6BF2227AB14": {
2046
+ "longPoolAmount": "0x99e5ca17fd7c5c067d609272d77c0d52c91740aaf5ac72171c307b352a2e4f78",
2047
+ "shortPoolAmount": "0x99e5ca17fd7c5c067d609272d77c0d52c91740aaf5ac72171c307b352a2e4f78",
2048
+ "positionImpactPoolAmount": "0x0a36d347fa7b4056fb469f85a92f57edd0702571aa3dce5af0741782434b0450",
2049
+ "swapImpactPoolAmountLong": "0xde0927cbb886d32e0f175c13cc8b11020c84f2c122309cab258ca2ef7a32096c",
2050
+ "swapImpactPoolAmountShort": "0xde0927cbb886d32e0f175c13cc8b11020c84f2c122309cab258ca2ef7a32096c",
2051
+ "longInterestUsingLongToken": "0xeadf853697eb15357d851f5274af5de3b74b401b9bf352cde4d13ec57c80157c",
2052
+ "longInterestUsingShortToken": "0xeadf853697eb15357d851f5274af5de3b74b401b9bf352cde4d13ec57c80157c",
2053
+ "shortInterestUsingLongToken": "0x171ddb73d9512cb2983913a9d046c1377841205989354f525365de27c015ceb8",
2054
+ "shortInterestUsingShortToken": "0x171ddb73d9512cb2983913a9d046c1377841205989354f525365de27c015ceb8",
2055
+ "longInterestInTokensUsingLongToken": "0x5b587aa069de38c4f8769041cdf59fbb3d26047fc7c0fb4ed283d148a71c7c24",
2056
+ "longInterestInTokensUsingShortToken": "0x5b587aa069de38c4f8769041cdf59fbb3d26047fc7c0fb4ed283d148a71c7c24",
2057
+ "shortInterestInTokensUsingLongToken": "0x97d8b7d86da9282bf51c3c3b6ba7e27441d780dd7b84a0531978f946fe282673",
2058
+ "shortInterestInTokensUsingShortToken": "0x97d8b7d86da9282bf51c3c3b6ba7e27441d780dd7b84a0531978f946fe282673"
2059
+ },
2060
+ "0x55556D4734DFA693F554985818dBcA07a0225986": {
2061
+ "longPoolAmount": "0xa40cff54c51965564374dfe8be8b9647af8bc16990fde2b0633cf992a72934e4",
2062
+ "shortPoolAmount": "0xa40cff54c51965564374dfe8be8b9647af8bc16990fde2b0633cf992a72934e4",
2063
+ "positionImpactPoolAmount": "0x245dc4a3d59c1a351510c814679ef039341a679496c7ddd527c17833e12d8641",
2064
+ "swapImpactPoolAmountLong": "0x20cc023ac95e9c54b698abb765d527455678789e48d35cb54c861065f814bf14",
2065
+ "swapImpactPoolAmountShort": "0x20cc023ac95e9c54b698abb765d527455678789e48d35cb54c861065f814bf14",
2066
+ "longInterestUsingLongToken": "0x02fc75fb4039cbde29e8b863a6b58980389ee85315924cf8e98eea34cd4833b8",
2067
+ "longInterestUsingShortToken": "0x02fc75fb4039cbde29e8b863a6b58980389ee85315924cf8e98eea34cd4833b8",
2068
+ "shortInterestUsingLongToken": "0xda52605ed97d56c97910fb48a2b841e7069e455c04cd34e29d636a3924b83a9e",
2069
+ "shortInterestUsingShortToken": "0xda52605ed97d56c97910fb48a2b841e7069e455c04cd34e29d636a3924b83a9e",
2070
+ "longInterestInTokensUsingLongToken": "0xc23fc02aa6bac55af294d733e2faebeadc46434203e12a44fdc0a9055c430172",
2071
+ "longInterestInTokensUsingShortToken": "0xc23fc02aa6bac55af294d733e2faebeadc46434203e12a44fdc0a9055c430172",
2072
+ "shortInterestInTokensUsingLongToken": "0xe7ecec90ceea6f7da2ef4ea9bcc8d616738b1e166ca37ea59d7c5c57c087f4cd",
2073
+ "shortInterestInTokensUsingShortToken": "0xe7ecec90ceea6f7da2ef4ea9bcc8d616738b1e166ca37ea59d7c5c57c087f4cd"
2074
+ },
2075
+ "0x460E90d682920E0Fe500aa180a45326d2089f18f": {
2076
+ "longPoolAmount": "0xb87e56496185fab08153d58f21d6658afef2f0becedd182f32bb104cbebeba7d",
2077
+ "shortPoolAmount": "0xb87e56496185fab08153d58f21d6658afef2f0becedd182f32bb104cbebeba7d",
2078
+ "positionImpactPoolAmount": "0x9273126381f81d6e8c6c6e87202f587ff1f12f4b809fa1a48737e00af1d318c4",
2079
+ "swapImpactPoolAmountLong": "0x159f4d5e29cebdf48f642f80550d8468fe0f9420f8bcfce0b1cb9d0bf0f4f680",
2080
+ "swapImpactPoolAmountShort": "0x159f4d5e29cebdf48f642f80550d8468fe0f9420f8bcfce0b1cb9d0bf0f4f680",
2081
+ "longInterestUsingLongToken": "0x92f543609dec2fd3a4ea1637d442240bb19df79b8d6d65666cc4519d19eddbba",
2082
+ "longInterestUsingShortToken": "0x92f543609dec2fd3a4ea1637d442240bb19df79b8d6d65666cc4519d19eddbba",
2083
+ "shortInterestUsingLongToken": "0x61f65513eaa88ddd63b9bf916fb03c2b5c861692d7558ac666e2366397222776",
2084
+ "shortInterestUsingShortToken": "0x61f65513eaa88ddd63b9bf916fb03c2b5c861692d7558ac666e2366397222776",
2085
+ "longInterestInTokensUsingLongToken": "0xc2842f8addfe9b47d55f4226b29b183eae13f6c6838491b299c485275eb52217",
2086
+ "longInterestInTokensUsingShortToken": "0xc2842f8addfe9b47d55f4226b29b183eae13f6c6838491b299c485275eb52217",
2087
+ "shortInterestInTokensUsingLongToken": "0xca5fcd2297e40ac25679cace8656f0d70913ac256cb18bfff5a32957804bb924",
2088
+ "shortInterestInTokensUsingShortToken": "0xca5fcd2297e40ac25679cace8656f0d70913ac256cb18bfff5a32957804bb924"
2089
+ },
2090
+ "0x51C4C72263D705Ab2F78531Bd3c8a43D362CCB53": {
2091
+ "longPoolAmount": "0x2713f228dc9be7d2be3937c045ea44cdfbc197d2c23a4961bc0b8e5349ac9a88",
2092
+ "shortPoolAmount": "0x2713f228dc9be7d2be3937c045ea44cdfbc197d2c23a4961bc0b8e5349ac9a88",
2093
+ "positionImpactPoolAmount": "0x455f16cac6d7ce33f1be131b12f67a6b99e11b1a5e31b4398b76fe2fc46df6aa",
2094
+ "swapImpactPoolAmountLong": "0x2b9b58bfc45f60a315231fd019948454c6e109b633e9b9cf4053e7961efc2642",
2095
+ "swapImpactPoolAmountShort": "0x2b9b58bfc45f60a315231fd019948454c6e109b633e9b9cf4053e7961efc2642",
2096
+ "longInterestUsingLongToken": "0x4e54b077d5ee982d1e10c3d1d5ee7808ae1db5b98a87e92b89820bd9bb93f196",
2097
+ "longInterestUsingShortToken": "0x4e54b077d5ee982d1e10c3d1d5ee7808ae1db5b98a87e92b89820bd9bb93f196",
2098
+ "shortInterestUsingLongToken": "0xe88c86573f1f471407b3d2631cf0a43c679125616bdc5de29912bfe2a16544f3",
2099
+ "shortInterestUsingShortToken": "0xe88c86573f1f471407b3d2631cf0a43c679125616bdc5de29912bfe2a16544f3",
2100
+ "longInterestInTokensUsingLongToken": "0xa57dcdeedd7d1b4d80c5e1b149532177e077427c90a06432010a6b314d5b5535",
2101
+ "longInterestInTokensUsingShortToken": "0xa57dcdeedd7d1b4d80c5e1b149532177e077427c90a06432010a6b314d5b5535",
2102
+ "shortInterestInTokensUsingLongToken": "0x25469ae6ae291a30d02961edf65f0a77bceb384f04575e6a486875fdace504fd",
2103
+ "shortInterestInTokensUsingShortToken": "0x25469ae6ae291a30d02961edf65f0a77bceb384f04575e6a486875fdace504fd"
2014
2104
  }
2015
2105
  },
2016
2106
  "43113": {
@@ -30,6 +30,8 @@ export const CONTRACTS = {
30
30
  GmxVester: "0x199070DDfd1CFb69173aa2F7e20906F26B363004",
31
31
  GlpVester: "0xA75287d2f8b217273E7FCD7E86eF07D33972042E",
32
32
  AffiliateVester: "0x7c100c0F55A15221A4c1C5a25Db8C98A81df49B2",
33
+ OrphanRatioVester: "0x0ccF3836DAd54eD9abcc6C5D5dB0Ccd19291B4F6",
34
+ RatioVesterReader: "0x04a6502933c8ac23ffE73B566970309040074CF0",
33
35
  PositionRouter: "0xb87a436B93fFE9D75c5cFA7bAcFff96430b09868",
34
36
  UniswapGmxEthPool: "0x80A9ae39310abf666A87C743d6ebBD0E8C42158E",
35
37
  ReferralStorage: "0xe6fab3F0c7199b0d34d7FbE83394fc0e0D06e99d",
@@ -97,6 +99,8 @@ export const CONTRACTS = {
97
99
  GmxVester: "0x472361d3cA5F49c8E633FB50385BfaD1e018b445",
98
100
  GlpVester: "0x62331A7Bd1dfB3A7642B7db50B5509E57CA3154A",
99
101
  AffiliateVester: "0x754eC029EF9926184b4CFDeA7756FbBAE7f326f7",
102
+ OrphanRatioVester: "0x3A664088D3db8Cc13d5201cB28E70176D4DDd0f1",
103
+ RatioVesterReader: "0x7915aF2a4771B98dd7E6f2336960D8A956DFf188",
100
104
  PositionRouter: "0xffF6D276Bc37c61A23f06410Dce4A400f66420f8",
101
105
  TraderJoeGmxAvaxPool: "0x0c91a070f862666bbcce281346be45766d874d98",
102
106
  ReferralStorage: "0x827ed045002ecdabeb6e2b0d1604cf5fc3d322f8",
@@ -194,6 +198,8 @@ export const CONTRACTS = {
194
198
  GmxVester: zeroAddress,
195
199
  GlpVester: zeroAddress,
196
200
  AffiliateVester: zeroAddress,
201
+ OrphanRatioVester: zeroAddress,
202
+ RatioVesterReader: zeroAddress,
197
203
  Router: zeroAddress,
198
204
  GovToken: zeroAddress,
199
205
  ES_GMX_IOU: zeroAddress,
@@ -231,6 +237,8 @@ export const CONTRACTS = {
231
237
  GmxVester: zeroAddress,
232
238
  GlpVester: zeroAddress,
233
239
  AffiliateVester: zeroAddress,
240
+ OrphanRatioVester: zeroAddress,
241
+ RatioVesterReader: zeroAddress,
234
242
  PositionRouter: zeroAddress,
235
243
  TraderJoeGmxAvaxPool: zeroAddress,
236
244
  ReferralStorage: "0x192e82A18a4ab446dD9968f055431b60640B155D",
@@ -325,6 +333,8 @@ export const CONTRACTS = {
325
333
  GmxVester: zeroAddress,
326
334
  GlpVester: zeroAddress,
327
335
  AffiliateVester: zeroAddress,
336
+ OrphanRatioVester: zeroAddress,
337
+ RatioVesterReader: zeroAddress,
328
338
  Router: zeroAddress,
329
339
  VaultReader: zeroAddress,
330
340
  Reader: zeroAddress,
@@ -8,6 +8,8 @@ export const SWAP_IMPACT_EXPONENT_FACTOR_KEY = hashString("SWAP_IMPACT_EXPONENT_
8
8
  export const SWAP_FEE_FACTOR_KEY = hashString("SWAP_FEE_FACTOR");
9
9
  export const ATOMIC_SWAP_FEE_FACTOR_KEY = hashString("ATOMIC_SWAP_FEE_FACTOR");
10
10
  export const WITHDRAWAL_FEE_FACTOR_KEY = hashString("WITHDRAWAL_FEE_FACTOR");
11
+ export const DEPOSIT_FEE_FACTOR_KEY = hashString("DEPOSIT_FEE_FACTOR");
12
+ export const SWAP_FEE_RECEIVER_FACTOR_KEY = hashString("SWAP_FEE_RECEIVER_FACTOR");
11
13
  export const FEE_RECEIVER_DEPOSIT_FACTOR_KEY = hashString("FEE_RECEIVER_DEPOSIT_FACTOR");
12
14
  export const BORROWING_FEE_RECEIVER_FACTOR_KEY = hashString("BORROWING_FEE_RECEIVER_FACTOR");
13
15
  export const FEE_RECEIVER_WITHDRAWAL_FACTOR_KEY = hashString("FEE_RECEIVER_WITHDRAWAL_FACTOR");
@@ -106,9 +108,12 @@ export const BASE_BORROWING_FACTOR = hashString("BASE_BORROWING_FACTOR");
106
108
  export const ABOVE_OPTIMAL_USAGE_BORROWING_FACTOR = hashString("ABOVE_OPTIMAL_USAGE_BORROWING_FACTOR");
107
109
  export const SUBACCOUNT_EXPIRES_AT = hashString("SUBACCOUNT_EXPIRES_AT");
108
110
  export const MULTICHAIN_BALANCE = hashString("MULTICHAIN_BALANCE");
111
+ export const POSITION_LAST_SRC_CHAIN_ID = hashString("POSITION_LAST_SRC_CHAIN_ID");
109
112
  export const PRICE_FEED_KEY = hashString("PRICE_FEED");
110
113
  export const GASLESS_FEATURE_DISABLED_KEY = hashString("GASLESS_FEATURE_DISABLED");
111
114
  export const SIGNAL_UNSTAKE_FEATURE_DISABLED_KEY = hashString("SIGNAL_UNSTAKE_FEATURE_DISABLED");
115
+ export const INCENTIVE_VESTER_DEPOSIT_FEATURE_DISABLED_KEY = hashString("INCENTIVE_VESTER_DEPOSIT_FEATURE_DISABLED");
116
+ export const INCENTIVE_VESTER_CLAIM_FEATURE_DISABLED_KEY = hashString("INCENTIVE_VESTER_CLAIM_FEATURE_DISABLED");
112
117
  export const GELATO_RELAY_FEE_MULTIPLIER_FACTOR_KEY = hashString("GELATO_RELAY_FEE_MULTIPLIER_FACTOR");
113
118
  export const REQUEST_EXPIRATION_TIME_KEY = hashString("REQUEST_EXPIRATION_TIME");
114
119
  export const GMX_SIMULATION_ORIGIN = "0x" + keccakString("GMX SIMULATION ORIGIN").slice(-40);
@@ -346,6 +351,9 @@ export function subaccountAutoTopUpAmountKey(account, subaccount) {
346
351
  export function multichainBalanceKey(account, token) {
347
352
  return hashData(["bytes32", "address", "address"], [MULTICHAIN_BALANCE, account, token]);
348
353
  }
354
+ export function positionLastSrcChainIdKey(positionKey) {
355
+ return hashData(["bytes32", "bytes32"], [POSITION_LAST_SRC_CHAIN_ID, positionKey]);
356
+ }
349
357
  export function priceFeedKey(token) {
350
358
  return hashData(["bytes32", "address"], [PRICE_FEED_KEY, token]);
351
359
  }
@@ -355,6 +363,12 @@ export function gaslessFeatureDisabledKey(module) {
355
363
  export function signalUnstakeFeatureDisabledKey(rewardRouter) {
356
364
  return hashData(["bytes32", "address"], [SIGNAL_UNSTAKE_FEATURE_DISABLED_KEY, rewardRouter]);
357
365
  }
366
+ export function incentiveVesterDepositFeatureDisabledKey(vester) {
367
+ return hashData(["bytes32", "address"], [INCENTIVE_VESTER_DEPOSIT_FEATURE_DISABLED_KEY, vester]);
368
+ }
369
+ export function incentiveVesterClaimFeatureDisabledKey(vester) {
370
+ return hashData(["bytes32", "address"], [INCENTIVE_VESTER_CLAIM_FEATURE_DISABLED_KEY, vester]);
371
+ }
358
372
  export function claimTermsKey(distributionId) {
359
373
  return hashData(["bytes32", "uint256"], [CLAIM_TERMS_KEY, distributionId]);
360
374
  }
@@ -1,7 +1,6 @@
1
1
  import { periodToSeconds } from "../utils/time";
2
2
  import { ARBITRUM, ARBITRUM_SEPOLIA, AVALANCHE, AVALANCHE_FUJI, MEGAETH } from "./chains";
3
3
  import { getTokenBySymbol, getWrappedToken } from "./tokens";
4
- export const SUBACCOUNT_MESSAGE = "Generate a GMX 1CT (One-Click Trading) session. Only sign this message on a trusted website.";
5
4
  export const SUBACCOUNT_DOCS_URL = "https://docs.gmx.io/docs/trading/overview/#express-trading-and-one-click-trading";
6
5
  export const DEFAULT_SUBACCOUNT_EXPIRY_DURATION = periodToSeconds(7, "1d"); // 1 week
7
6
  export const DEFAULT_SUBACCOUNT_MAX_ALLOWED_COUNT = 90;
@@ -918,6 +918,48 @@ export const MARKETS = {
918
918
  longTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
919
919
  shortTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
920
920
  },
921
+ // LINK/USD [USDG-USDG]
922
+ "0x71Dd0524F8e0aab8FE59E6B3888145A8ac117Aa3": {
923
+ marketTokenAddress: "0x71Dd0524F8e0aab8FE59E6B3888145A8ac117Aa3",
924
+ indexTokenAddress: "0xf97f4df75117a78c1A5a0DBb814Af92458539FB4",
925
+ longTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
926
+ shortTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
927
+ },
928
+ // PEPE/USD [USDG-USDG]
929
+ "0x1e5bf5390a75a6ab085bd4e7E63A00074C43B858": {
930
+ marketTokenAddress: "0x1e5bf5390a75a6ab085bd4e7E63A00074C43B858",
931
+ indexTokenAddress: "0x25d887Ce7a35172C62FeBFD67a1856F20FaEbB00",
932
+ longTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
933
+ shortTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
934
+ },
935
+ // DOGE/USD [USDG-USDG]
936
+ "0x2bDCC67868abA103406dCeb4F853B6BF2227AB14": {
937
+ marketTokenAddress: "0x2bDCC67868abA103406dCeb4F853B6BF2227AB14",
938
+ indexTokenAddress: "0xC4da4c24fd591125c3F47b340b6f4f76111883d8",
939
+ longTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
940
+ shortTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
941
+ },
942
+ // BNB/USD [USDG-USDG]
943
+ "0x55556D4734DFA693F554985818dBcA07a0225986": {
944
+ marketTokenAddress: "0x55556D4734DFA693F554985818dBcA07a0225986",
945
+ indexTokenAddress: "0xa9004A5421372E1D83fB1f85b0fc986c912f91f3",
946
+ longTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
947
+ shortTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
948
+ },
949
+ // SUI/USD [USDG-USDG]
950
+ "0x460E90d682920E0Fe500aa180a45326d2089f18f": {
951
+ marketTokenAddress: "0x460E90d682920E0Fe500aa180a45326d2089f18f",
952
+ indexTokenAddress: "0x197aa2DE1313c7AD50184234490E12409B2a1f95",
953
+ longTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
954
+ shortTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
955
+ },
956
+ // XMR/USD [USDG-USDG]
957
+ "0x51C4C72263D705Ab2F78531Bd3c8a43D362CCB53": {
958
+ marketTokenAddress: "0x51C4C72263D705Ab2F78531Bd3c8a43D362CCB53",
959
+ indexTokenAddress: "0x13674172E6E44D31d4bE489d5184f3457c40153A",
960
+ longTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
961
+ shortTokenAddress: "0x004B506865409877C9fA29bfb1ebA929984B9bbC",
962
+ },
921
963
  },
922
964
  [AVALANCHE]: {
923
965
  // BTC/USD [BTC-USDC]
@@ -353,6 +353,7 @@ export const TOKENS = {
353
353
  symbol: "WSTETH",
354
354
  address: "0x5979D7b546E38E414F7E9822514be443A4800529",
355
355
  decimals: 18,
356
+ tracksPriceOf: "ETH",
356
357
  imageUrl: "https://assets.coingecko.com/coins/images/18834/standard/wstETH.png?1696518295",
357
358
  },
358
359
  {
@@ -372,6 +373,7 @@ export const TOKENS = {
372
373
  symbol: "TBTC",
373
374
  address: "0x6c84a8f1c29108F47a79964b5Fe888D4f4D0dE40",
374
375
  decimals: 18,
376
+ tracksPriceOf: "BTC",
375
377
  imageUrl: "https://assets.coingecko.com/coins/images/11224/standard/0x18084fba666a33d37592fa2633fd49a74dd93a88.png?1696511155",
376
378
  isPermitSupported: false,
377
379
  contractVersion: "1",
@@ -3,6 +3,7 @@ import arbitrumTickers from "./fixtures/arbitrumTickers.json";
3
3
  import arbitrumTokens from "./fixtures/arbitrumTokens.json";
4
4
  const keeperResponsesByPathname = {
5
5
  "/markets": arbitrumMarkets,
6
+ "/markets/state": { markets: [] },
6
7
  "/tokens": arbitrumTokens,
7
8
  "/prices/tickers": arbitrumTickers,
8
9
  };
@@ -2,7 +2,7 @@ import { zeroHash } from "viem";
2
2
  import { describe, expect, it } from "vitest";
3
3
  import { USD_DECIMALS } from "../../configs/factors";
4
4
  import { mockMarketsInfoData, mockTokensData, usdToToken } from "../../test/mock";
5
- import { getFundingFactorPerPeriod, getPositionFee, getPriceImpactForPosition, getPriceImpactForSwap, getPriceImpactUsd, } from "../fees";
5
+ import { getFundingFactorPerPeriod, getPositionFee, getPriceImpactForDeposit, getPriceImpactForPosition, getPriceImpactForSwap, getPriceImpactUsd, } from "../fees";
6
6
  import { numberToBigint } from "../numbers";
7
7
  import { convertToTokenAmountForIncrease } from "../tokens";
8
8
  import { getDecreasePositionSizeDeltaInTokens } from "../trade/decrease";
@@ -360,3 +360,55 @@ describe("getPositionFee", () => {
360
360
  expect(proOnly.positionFeeUsd).toBe(90n * dollar);
361
361
  });
362
362
  });
363
+ describe("getPriceImpactForDeposit", () => {
364
+ // ETH/USD [WETH-USDC] on Arbitrum at block 526837562, expected values from the deployed
365
+ // SwapPricingUtils.getPriceImpactUsdForDeposit (973adf75) called with the same pool state and prices
366
+ const ethPrice = 1777957764443186000000000000000000n;
367
+ const usdcPrice = 999881577197723200000000000000n;
368
+ const marketInfo = mockMarketsInfoData(mockTokensData({
369
+ ETH: { prices: { minPrice: ethPrice, maxPrice: ethPrice } },
370
+ USDC: { prices: { minPrice: usdcPrice, maxPrice: usdcPrice } },
371
+ }), ["ETH-ETH-USDC"], {
372
+ "ETH-ETH-USDC": {
373
+ longPoolAmount: 11200442852540875237781n,
374
+ shortPoolAmount: 19023130046947n,
375
+ swapImpactFactorPositive: 200000000000000000000n,
376
+ swapImpactFactorNegative: 300000000000000000000n,
377
+ swapImpactExponentFactor: 2000000000000000000000000000000n,
378
+ virtualPoolAmountForLongToken: 12100173541245288232032n,
379
+ virtualPoolAmountForShortToken: 20730405002499n,
380
+ },
381
+ })["ETH-ETH-USDC"];
382
+ it.each([
383
+ {
384
+ name: "re-prices a positive impact with the rebate",
385
+ longUsd: 0n,
386
+ shortUsd: 89303706029n * 10n ** 24n,
387
+ expected: 30295834035264610872343888839400n,
388
+ balanceWasImproved: true,
389
+ },
390
+ {
391
+ name: "re-prices a positive impact that flips the pool to the other side",
392
+ longUsd: 0n,
393
+ shortUsd: 1339555590447n * 10n ** 24n,
394
+ expected: 99716103800429893412909032511600n,
395
+ balanceWasImproved: true,
396
+ },
397
+ {
398
+ name: "keeps a negative impact",
399
+ longUsd: 893037060298n * 10n ** 24n,
400
+ shortUsd: 0n,
401
+ expected: -717763671959376778427532591564600n,
402
+ balanceWasImproved: false,
403
+ },
404
+ {
405
+ name: "takes the fee tier from the virtual pool when it sets the impact",
406
+ longUsd: 0n,
407
+ shortUsd: 1699798681236129440n * 10n ** 18n,
408
+ expected: -127253359863946285154770016211800n,
409
+ balanceWasImproved: false,
410
+ },
411
+ ])("$name like the contract TRD-21", ({ longUsd, shortUsd, expected, balanceWasImproved }) => {
412
+ expect(getPriceImpactForDeposit(marketInfo, marketInfo.longToken, marketInfo.shortToken, longUsd, shortUsd)).toEqual({ priceImpactDeltaUsd: expected, balanceWasImproved });
413
+ });
414
+ });
@@ -0,0 +1,99 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { APP_TO_GMSOL_USD_DIVISOR, appContractPriceToGmsolUnitPrice, appTokenPriceToGmsolUnitPrice, appUsdToGmsolUsd, gmsolUnitPriceToAppTokenPrice, gmsolUsdToAppUsd, toGmsolTokenAmount, U128_MAX, U64_MAX, } from "../gmsol";
3
+ import { convertToContractPrice } from "../tokens";
4
+ const D = APP_TO_GMSOL_USD_DIVISOR; // 10^10
5
+ describe("gmsol units", () => {
6
+ // Whole-token price (USD × 10^30) / 10^(decimals + 10) = unit price (USD × 10^20 per smallest unit).
7
+ it.each([
8
+ // USDC $1.0001: 10001·10^26 / 10^16 = 10001·10^10
9
+ { decimals: 6, price: 10001n * 10n ** 26n, unitPrice: 100010000000000n },
10
+ // BTC $97,123.45: 9712345·10^28 / 10^18 = 9712345·10^10
11
+ { decimals: 8, price: 9712345n * 10n ** 28n, unitPrice: 97123450000000000n },
12
+ // SOL $150.25: 15025·10^28 / 10^19 = 15025·10^9
13
+ { decimals: 9, price: 15025n * 10n ** 28n, unitPrice: 15025000000000n },
14
+ // ETH $3,456.78: 345678·10^28 / 10^28 = 345678
15
+ { decimals: 18, price: 345678n * 10n ** 28n, unitPrice: 345678n },
16
+ // $10^9 at 24 decimals: 10^39 / 10^34 = 10^5
17
+ { decimals: 24, price: 10n ** 39n, unitPrice: 100000n },
18
+ { decimals: 9, price: 0n, unitPrice: 0n },
19
+ ])("converts a whole-token price at $decimals decimals", ({ decimals, price, unitPrice }) => {
20
+ expect(appTokenPriceToGmsolUnitPrice({ price, decimals, rounding: "floor" })).toBe(unitPrice);
21
+ expect(appTokenPriceToGmsolUnitPrice({ price, decimals, rounding: "ceil" })).toBe(unitPrice);
22
+ expect(gmsolUnitPriceToAppTokenPrice({ unitPrice, decimals })).toBe(price);
23
+ // Same result through the app contract price (USD × 10^30 per smallest unit).
24
+ expect(appContractPriceToGmsolUnitPrice(convertToContractPrice(price, decimals), "floor")).toBe(unitPrice);
25
+ });
26
+ // Around a multiple k·divisor: floor gives k-1, k, k; ceil gives k, k, k+1.
27
+ it.each([
28
+ { name: "usd", divisor: D, convert: (v, r) => appUsdToGmsolUsd(v, r) },
29
+ {
30
+ name: "contract price",
31
+ divisor: D,
32
+ convert: (v, r) => appContractPriceToGmsolUnitPrice(v, r),
33
+ },
34
+ ...[6, 8, 9, 18, 24].map((decimals) => ({
35
+ name: `token price, ${decimals} decimals`,
36
+ divisor: 10n ** BigInt(decimals) * D,
37
+ convert: (v, r) => appTokenPriceToGmsolUnitPrice({ price: v, decimals, rounding: r }),
38
+ })),
39
+ ])("rounds at the boundary: $name", ({ divisor, convert }) => {
40
+ const k = 7n;
41
+ expect(convert(k * divisor - 1n, "floor")).toBe(k - 1n);
42
+ expect(convert(k * divisor - 1n, "ceil")).toBe(k);
43
+ expect(convert(k * divisor, "floor")).toBe(k);
44
+ expect(convert(k * divisor, "ceil")).toBe(k);
45
+ expect(convert(k * divisor + 1n, "floor")).toBe(k);
46
+ expect(convert(k * divisor + 1n, "ceil")).toBe(k + 1n);
47
+ // Smallest representable input.
48
+ expect(convert(divisor, "floor")).toBe(1n);
49
+ });
50
+ it("rounds a ceil price once, unlike the flooring contract price", () => {
51
+ // 7 unit prices plus 1 at 9 decimals: the contract price floors away the extra 1 before the ceil.
52
+ const decimals = 9;
53
+ const price = 7n * 10n ** BigInt(decimals) * D + 1n;
54
+ expect(appTokenPriceToGmsolUnitPrice({ price, decimals, rounding: "ceil" })).toBe(8n);
55
+ expect(appContractPriceToGmsolUnitPrice(convertToContractPrice(price, decimals), "ceil")).toBe(7n);
56
+ });
57
+ it("converts USD values exactly and at the u128 limit", () => {
58
+ // $1,000,000 size: 10^36 / 10^10 = 10^26
59
+ expect(appUsdToGmsolUsd(10n ** 36n, "floor")).toBe(10n ** 26n);
60
+ expect(gmsolUsdToAppUsd(10n ** 26n)).toBe(10n ** 36n);
61
+ expect(appUsdToGmsolUsd(0n, "ceil")).toBe(0n);
62
+ expect(appUsdToGmsolUsd(U128_MAX * D, "floor")).toBe(U128_MAX);
63
+ expect(appUsdToGmsolUsd(U128_MAX * D + D - 1n, "floor")).toBe(U128_MAX);
64
+ expect(() => appUsdToGmsolUsd(U128_MAX * D + 1n, "ceil")).toThrow(RangeError);
65
+ expect(() => appUsdToGmsolUsd((U128_MAX + 1n) * D, "floor")).toThrow(RangeError);
66
+ expect(gmsolUsdToAppUsd(U128_MAX)).toBe(U128_MAX * D);
67
+ expect(() => gmsolUsdToAppUsd(U128_MAX + 1n)).toThrow(RangeError);
68
+ expect(() => gmsolUnitPriceToAppTokenPrice({ unitPrice: U128_MAX + 1n, decimals: 9 })).toThrow(RangeError);
69
+ });
70
+ it("checks token amounts against u64", () => {
71
+ expect(toGmsolTokenAmount(0n)).toBe(0n);
72
+ expect(toGmsolTokenAmount(1500000000n)).toBe(1500000000n); // 1.5 SOL in lamports
73
+ expect(toGmsolTokenAmount(U64_MAX)).toBe(U64_MAX);
74
+ expect(() => toGmsolTokenAmount(U64_MAX + 1n)).toThrow(RangeError);
75
+ });
76
+ it.each([
77
+ ["negative usd", () => appUsdToGmsolUsd(-1n, "floor")],
78
+ ["negative gmsol usd", () => gmsolUsdToAppUsd(-1n)],
79
+ ["negative amount", () => toGmsolTokenAmount(-1n)],
80
+ ["negative price", () => appTokenPriceToGmsolUnitPrice({ price: -1n, decimals: 9, rounding: "floor" })],
81
+ ["number input", () => appUsdToGmsolUsd(1e30, "floor")],
82
+ ["number amount", () => toGmsolTokenAmount(1)],
83
+ ["unknown rounding", () => appUsdToGmsolUsd(D, "round")],
84
+ [
85
+ "fractional decimals",
86
+ () => appTokenPriceToGmsolUnitPrice({ price: 10n ** 30n, decimals: 8.5, rounding: "floor" }),
87
+ ],
88
+ ["negative decimals", () => gmsolUnitPriceToAppTokenPrice({ unitPrice: 1n, decimals: -1 })],
89
+ ["decimals above 30", () => appTokenPriceToGmsolUnitPrice({ price: 10n ** 50n, decimals: 31, rounding: "floor" })],
90
+ // $0.0001 at 24 decimals: 10^26 / 10^34 floors to 0, so no gmsol unit price exists.
91
+ [
92
+ "price below one unit price",
93
+ () => appTokenPriceToGmsolUnitPrice({ price: 10n ** 26n, decimals: 24, rounding: "ceil" }),
94
+ ],
95
+ ["usd below one gmsol unit", () => appUsdToGmsolUsd(D - 1n, "ceil")],
96
+ ])("rejects %s", (_, convert) => {
97
+ expect(convert).toThrow(RangeError);
98
+ });
99
+ });
@@ -4,8 +4,8 @@ import { ARBITRUM } from "../../../configs/chains";
4
4
  import { getTokenBySymbol, getWrappedToken, NATIVE_TOKEN_ADDRESS } from "../../../configs/tokens";
5
5
  import { mockExternalSwap } from "../../../test/mock";
6
6
  import { expandDecimals, parseValue, USD_DECIMALS } from "../../numbers";
7
- import { OrderType } from "../../orders/types";
8
- import { buildIncreaseOrderPayload, getBatchExternalCalls, getBatchExternalSwapGasLimit, getBatchIsNativePayment, getBatchTotalExecutionFee, getBatchTotalPayCollateralAmount, getIsEmptyBatch, getIsInvalidBatchReceiver, } from "../../orderTransactions";
7
+ import { DecreasePositionSwapType, OrderType } from "../../orders/types";
8
+ import { buildIncreaseOrderPayload, buildUpdateOrderPayload, getBatchExternalCalls, getBatchExternalSwapGasLimit, getBatchIsNativePayment, getBatchTotalExecutionFee, getBatchTotalPayCollateralAmount, getIsBatchPayoutOnlyToAccount, getIsEmptyBatch, getIsInvalidBatchReceiver, } from "../../orderTransactions";
9
9
  // Common tokens and addresses
10
10
  const CHAIN_ID = ARBITRUM;
11
11
  const ACCOUNT = "0x1234567890123456789012345678901234567890";
@@ -101,6 +101,72 @@ describe("Batch Utils", () => {
101
101
  expect(getIsInvalidBatchReceiver(batch, ACCOUNT)).toBe(false);
102
102
  });
103
103
  });
104
+ describe("getIsBatchPayoutOnlyToAccount", () => {
105
+ const OTHER = "0x9999999999999999999999999999999999999999";
106
+ const ORDER_KEY = `0x${"ab".repeat(32)}`;
107
+ const OWN_ORDER = { receiver: ACCOUNT, cancellationReceiver: zeroAddress, callbackContract: zeroAddress };
108
+ const update = (payout) => buildUpdateOrderPayload({
109
+ chainId: CHAIN_ID,
110
+ indexTokenAddress: WETH.address,
111
+ orderKey: ORDER_KEY,
112
+ orderType: OrderType.LimitDecrease,
113
+ sizeDeltaUsd: parseValue("2000", USD_DECIMALS),
114
+ triggerPrice: parseValue("3000", USD_DECIMALS),
115
+ acceptablePrice: parseValue("2990", USD_DECIMALS),
116
+ minOutputAmount: 0n,
117
+ autoCancel: false,
118
+ validFromTime: 0n,
119
+ decreasePositionSwapType: DecreasePositionSwapType.NoSwap,
120
+ executionFeeTopUp: 0n,
121
+ ...payout,
122
+ });
123
+ const cancel = (payout) => ({ orderKey: ORDER_KEY, ...payout });
124
+ it.each([
125
+ { case: "own create", creates: [ACCOUNT], expected: true },
126
+ { case: "create for another receiver", creates: [OTHER], expected: false },
127
+ { case: "edit of an own order", updates: [OWN_ORDER], expected: true },
128
+ { case: "cancel of an own order", cancels: [OWN_ORDER], expected: true },
129
+ {
130
+ case: "cancel refunding the account",
131
+ cancels: [{ ...OWN_ORDER, cancellationReceiver: ACCOUNT }],
132
+ expected: true,
133
+ },
134
+ {
135
+ case: "edit of an order paying another receiver",
136
+ updates: [{ ...OWN_ORDER, receiver: OTHER }],
137
+ expected: false,
138
+ },
139
+ {
140
+ case: "cancel refunding another account",
141
+ cancels: [{ ...OWN_ORDER, cancellationReceiver: OTHER }],
142
+ expected: false,
143
+ },
144
+ {
145
+ case: "cancel refunding a callback contract",
146
+ cancels: [{ ...OWN_ORDER, callbackContract: OTHER }],
147
+ expected: false,
148
+ },
149
+ { case: "cancel of an order with unknown payout", cancels: [{}], expected: false },
150
+ {
151
+ case: "edit with an unknown cancellation receiver",
152
+ updates: [{ ...OWN_ORDER, cancellationReceiver: undefined }],
153
+ expected: false,
154
+ },
155
+ {
156
+ case: "own create with a foreign cancel",
157
+ creates: [ACCOUNT],
158
+ cancels: [{ ...OWN_ORDER, receiver: OTHER }],
159
+ expected: false,
160
+ },
161
+ ])("$case -> $expected", ({ creates = [], updates = [], cancels = [], expected }) => {
162
+ const batch = {
163
+ createOrderParams: creates.map((receiver) => buildIncreaseOrderPayload({ ...commonMarketIncreaseParams, receiver })),
164
+ updateOrderParams: updates.map(update),
165
+ cancelOrderParams: cancels.map(cancel),
166
+ };
167
+ expect(getIsBatchPayoutOnlyToAccount(batch, ACCOUNT)).toBe(expected);
168
+ });
169
+ });
104
170
  describe("getBatchExternalCalls", () => {
105
171
  it("combines external calls from multiple orders", () => {
106
172
  const params1 = {
@@ -380,7 +380,7 @@ describe("Decrease Order Payloads", () => {
380
380
  },
381
381
  numbers: {
382
382
  sizeDeltaUsd: parseValue("250", USD_DECIMALS), // 1000/4
383
- initialCollateralDeltaAmount: params.collateralDeltaAmount / 4n, // 1/4
383
+ initialCollateralDeltaAmount: 0n,
384
384
  triggerPrice: 0n,
385
385
  acceptablePrice: 0n,
386
386
  executionFee: EXECUTION_FEE_AMOUNT / 4n,
@@ -400,7 +400,7 @@ describe("Decrease Order Payloads", () => {
400
400
  ...params,
401
401
  acceptablePrice: 0n,
402
402
  triggerPrice: 0n,
403
- collateralDeltaAmount: params.collateralDeltaAmount / 4n,
403
+ collateralDeltaAmount: 0n,
404
404
  executionFeeAmount: params.executionFeeAmount / 4n,
405
405
  sizeDeltaUsd: params.sizeDeltaUsd / 4n,
406
406
  sizeDeltaInTokens: params.sizeDeltaInTokens / 4n,
@@ -414,7 +414,7 @@ describe("Decrease Order Payloads", () => {
414
414
  isNativePayment: false,
415
415
  isNativeReceive: true,
416
416
  initialCollateralTokenAddress: WETH.address,
417
- initialCollateralDeltaAmount: params.collateralDeltaAmount / 4n,
417
+ initialCollateralDeltaAmount: 0n,
418
418
  tokenTransfers: [
419
419
  {
420
420
  amount: EXECUTION_FEE_AMOUNT / 4n,
@@ -469,7 +469,7 @@ describe("Decrease Order Payloads", () => {
469
469
  },
470
470
  numbers: {
471
471
  sizeDeltaUsd: parseValue("250", USD_DECIMALS), // 1000/4
472
- initialCollateralDeltaAmount: params.collateralDeltaAmount / 4n, // 1/4
472
+ initialCollateralDeltaAmount: 0n,
473
473
  triggerPrice: MaxUint256,
474
474
  acceptablePrice: MaxUint256,
475
475
  executionFee: EXECUTION_FEE_AMOUNT / 4n,
@@ -489,7 +489,7 @@ describe("Decrease Order Payloads", () => {
489
489
  ...params,
490
490
  acceptablePrice: MaxUint256,
491
491
  triggerPrice: MaxUint256,
492
- collateralDeltaAmount: params.collateralDeltaAmount / 4n,
492
+ collateralDeltaAmount: 0n,
493
493
  executionFeeAmount: params.executionFeeAmount / 4n,
494
494
  sizeDeltaUsd: params.sizeDeltaUsd / 4n,
495
495
  sizeDeltaInTokens: params.sizeDeltaInTokens / 4n,
@@ -502,7 +502,7 @@ describe("Decrease Order Payloads", () => {
502
502
  isNativePayment: false,
503
503
  isNativeReceive: true,
504
504
  initialCollateralTokenAddress: WETH.address,
505
- initialCollateralDeltaAmount: params.collateralDeltaAmount / 4n,
505
+ initialCollateralDeltaAmount: 0n,
506
506
  tokenTransfers: [
507
507
  {
508
508
  amount: EXECUTION_FEE_AMOUNT / 4n,
@@ -47,6 +47,46 @@ describe("Exchange Router Multicall", () => {
47
47
  externalSwapQuote: undefined,
48
48
  };
49
49
  describe("getBatchOrderMulticallPayload", () => {
50
+ it("starts with the token permits, before any order pulls the token", () => {
51
+ const payAmount = parseValue("1000", USDC.decimals);
52
+ const firstOrder = buildIncreaseOrderPayload({
53
+ ...commonParams,
54
+ payTokenAddress: USDC.address,
55
+ payTokenAmount: payAmount,
56
+ });
57
+ const secondOrder = buildIncreaseOrderPayload({
58
+ ...commonParams,
59
+ payTokenAddress: USDC.address,
60
+ payTokenAmount: payAmount,
61
+ });
62
+ const permit = {
63
+ token: USDC.address,
64
+ owner: RECEIVER,
65
+ spender: getContract(CHAIN_ID, "SyntheticsRouter"),
66
+ value: 2n * payAmount,
67
+ deadline: 2000000000n,
68
+ v: 27,
69
+ r: `0x${"11".repeat(32)}`,
70
+ s: `0x${"22".repeat(32)}`,
71
+ onchainParams: { name: "USD Coin", version: "2", nonce: 0n },
72
+ };
73
+ const result = getBatchOrderMulticallPayload({
74
+ chainId: CHAIN_ID,
75
+ params: { createOrderParams: [firstOrder, secondOrder], updateOrderParams: [], cancelOrderParams: [] },
76
+ tokenPermits: [permit],
77
+ });
78
+ expect(result.multicall.map((call) => call.method)).toEqual([
79
+ "makeExternalCalls",
80
+ "sendWnt",
81
+ "sendTokens",
82
+ "createOrder",
83
+ "sendWnt",
84
+ "sendTokens",
85
+ "createOrder",
86
+ ]);
87
+ expect(result.multicall[0].params[0]).toEqual([USDC.address]);
88
+ expect(result.multicall[0].params.slice(2)).toEqual([[], []]);
89
+ });
50
90
  it("Token transfers with native only payment", () => {
51
91
  const params = {
52
92
  ...commonParams,