aftermath-ts-sdk 1.3.28 → 2.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 (410) hide show
  1. package/dist/index.d.ts +16828 -5
  2. package/dist/index.js +22695 -19
  3. package/dist/index.js.map +1 -0
  4. package/package.json +69 -60
  5. package/dist/general/apiHelpers/dynamicFieldsApiHelpers.d.ts +0 -41
  6. package/dist/general/apiHelpers/dynamicFieldsApiHelpers.d.ts.map +0 -1
  7. package/dist/general/apiHelpers/dynamicFieldsApiHelpers.js +0 -110
  8. package/dist/general/apiHelpers/eventsApiHelpers.d.ts +0 -43
  9. package/dist/general/apiHelpers/eventsApiHelpers.d.ts.map +0 -1
  10. package/dist/general/apiHelpers/eventsApiHelpers.js +0 -170
  11. package/dist/general/apiHelpers/inspectionsApiHelpers.d.ts +0 -28
  12. package/dist/general/apiHelpers/inspectionsApiHelpers.d.ts.map +0 -1
  13. package/dist/general/apiHelpers/inspectionsApiHelpers.js +0 -58
  14. package/dist/general/apiHelpers/objectsApiHelpers.d.ts +0 -77
  15. package/dist/general/apiHelpers/objectsApiHelpers.d.ts.map +0 -1
  16. package/dist/general/apiHelpers/objectsApiHelpers.js +0 -197
  17. package/dist/general/apiHelpers/transactionsApiHelpers.d.ts +0 -50
  18. package/dist/general/apiHelpers/transactionsApiHelpers.d.ts.map +0 -1
  19. package/dist/general/apiHelpers/transactionsApiHelpers.js +0 -256
  20. package/dist/general/dynamicGas/dynamicGas.d.ts +0 -48
  21. package/dist/general/dynamicGas/dynamicGas.d.ts.map +0 -1
  22. package/dist/general/dynamicGas/dynamicGas.js +0 -69
  23. package/dist/general/dynamicGas/dynamicGasTypes.d.ts +0 -37
  24. package/dist/general/dynamicGas/dynamicGasTypes.d.ts.map +0 -1
  25. package/dist/general/dynamicGas/dynamicGasTypes.js +0 -2
  26. package/dist/general/nfts/nftsApi.d.ts +0 -35
  27. package/dist/general/nfts/nftsApi.d.ts.map +0 -1
  28. package/dist/general/nfts/nftsApi.js +0 -124
  29. package/dist/general/nfts/nftsApiCasting.d.ts +0 -11
  30. package/dist/general/nfts/nftsApiCasting.d.ts.map +0 -1
  31. package/dist/general/nfts/nftsApiCasting.js +0 -115
  32. package/dist/general/nfts/nftsTypes.d.ts +0 -31
  33. package/dist/general/nfts/nftsTypes.d.ts.map +0 -1
  34. package/dist/general/nfts/nftsTypes.js +0 -2
  35. package/dist/general/priceFeeds/priceFeeds.d.ts +0 -3
  36. package/dist/general/priceFeeds/priceFeeds.d.ts.map +0 -1
  37. package/dist/general/priceFeeds/priceFeeds.js +0 -26
  38. package/dist/general/priceFeeds/priceFeedsApi.d.ts +0 -3
  39. package/dist/general/priceFeeds/priceFeedsApi.d.ts.map +0 -1
  40. package/dist/general/priceFeeds/priceFeedsApi.js +0 -33
  41. package/dist/general/priceFeeds/priceFeedsTypes.d.ts +0 -3
  42. package/dist/general/priceFeeds/priceFeedsTypes.d.ts.map +0 -1
  43. package/dist/general/priceFeeds/priceFeedsTypes.js +0 -7
  44. package/dist/general/prices/coinGeckoTypes.d.ts +0 -16
  45. package/dist/general/prices/coinGeckoTypes.d.ts.map +0 -1
  46. package/dist/general/prices/coinGeckoTypes.js +0 -2
  47. package/dist/general/prices/prices.d.ts +0 -92
  48. package/dist/general/prices/prices.d.ts.map +0 -1
  49. package/dist/general/prices/prices.js +0 -124
  50. package/dist/general/providers/aftermath.d.ts +0 -200
  51. package/dist/general/providers/aftermath.d.ts.map +0 -1
  52. package/dist/general/providers/aftermath.js +0 -258
  53. package/dist/general/providers/aftermathApi.d.ts +0 -197
  54. package/dist/general/providers/aftermathApi.d.ts.map +0 -1
  55. package/dist/general/providers/aftermathApi.js +0 -255
  56. package/dist/general/providers/index.d.ts +0 -3
  57. package/dist/general/providers/index.d.ts.map +0 -1
  58. package/dist/general/providers/index.js +0 -18
  59. package/dist/general/types/castingTypes.d.ts +0 -51
  60. package/dist/general/types/castingTypes.d.ts.map +0 -1
  61. package/dist/general/types/castingTypes.js +0 -8
  62. package/dist/general/types/configTypes.d.ts +0 -187
  63. package/dist/general/types/configTypes.d.ts.map +0 -1
  64. package/dist/general/types/configTypes.js +0 -2
  65. package/dist/general/types/generalTypes.d.ts +0 -349
  66. package/dist/general/types/generalTypes.d.ts.map +0 -1
  67. package/dist/general/types/generalTypes.js +0 -2
  68. package/dist/general/types/index.d.ts +0 -7
  69. package/dist/general/types/index.d.ts.map +0 -1
  70. package/dist/general/types/index.js +0 -22
  71. package/dist/general/types/moveErrorsInterface.d.ts +0 -6
  72. package/dist/general/types/moveErrorsInterface.d.ts.map +0 -1
  73. package/dist/general/types/moveErrorsInterface.js +0 -2
  74. package/dist/general/types/suiTypes.d.ts +0 -2
  75. package/dist/general/types/suiTypes.d.ts.map +0 -1
  76. package/dist/general/types/suiTypes.js +0 -5
  77. package/dist/general/utils/caller.d.ts +0 -58
  78. package/dist/general/utils/caller.d.ts.map +0 -1
  79. package/dist/general/utils/caller.js +0 -231
  80. package/dist/general/utils/casting.d.ts +0 -212
  81. package/dist/general/utils/casting.d.ts.map +0 -1
  82. package/dist/general/utils/casting.js +0 -273
  83. package/dist/general/utils/fixedUtils.d.ts +0 -109
  84. package/dist/general/utils/fixedUtils.d.ts.map +0 -1
  85. package/dist/general/utils/fixedUtils.js +0 -100
  86. package/dist/general/utils/helpers.d.ts +0 -408
  87. package/dist/general/utils/helpers.d.ts.map +0 -1
  88. package/dist/general/utils/helpers.js +0 -725
  89. package/dist/general/utils/iFixedUtils.d.ts +0 -78
  90. package/dist/general/utils/iFixedUtils.d.ts.map +0 -1
  91. package/dist/general/utils/iFixedUtils.js +0 -109
  92. package/dist/general/utils/index.d.ts +0 -3
  93. package/dist/general/utils/index.d.ts.map +0 -1
  94. package/dist/general/utils/index.js +0 -18
  95. package/dist/general/wallet/wallet.d.ts +0 -89
  96. package/dist/general/wallet/wallet.d.ts.map +0 -1
  97. package/dist/general/wallet/wallet.js +0 -120
  98. package/dist/general/wallet/walletApi.d.ts +0 -20
  99. package/dist/general/wallet/walletApi.d.ts.map +0 -1
  100. package/dist/general/wallet/walletApi.js +0 -66
  101. package/dist/index.d.ts.map +0 -1
  102. package/dist/packages/auth/auth.d.ts +0 -135
  103. package/dist/packages/auth/auth.d.ts.map +0 -1
  104. package/dist/packages/auth/auth.js +0 -289
  105. package/dist/packages/auth/authTypes.d.ts +0 -102
  106. package/dist/packages/auth/authTypes.d.ts.map +0 -1
  107. package/dist/packages/auth/authTypes.js +0 -2
  108. package/dist/packages/auth/index.d.ts +0 -2
  109. package/dist/packages/auth/index.d.ts.map +0 -1
  110. package/dist/packages/auth/index.js +0 -17
  111. package/dist/packages/coin/api/coinApi.d.ts +0 -33
  112. package/dist/packages/coin/api/coinApi.d.ts.map +0 -1
  113. package/dist/packages/coin/api/coinApi.js +0 -167
  114. package/dist/packages/coin/coin.d.ts +0 -320
  115. package/dist/packages/coin/coin.d.ts.map +0 -1
  116. package/dist/packages/coin/coin.js +0 -467
  117. package/dist/packages/coin/coinTypes.d.ts +0 -139
  118. package/dist/packages/coin/coinTypes.d.ts.map +0 -1
  119. package/dist/packages/coin/coinTypes.js +0 -2
  120. package/dist/packages/coin/index.d.ts +0 -2
  121. package/dist/packages/coin/index.d.ts.map +0 -1
  122. package/dist/packages/coin/index.js +0 -17
  123. package/dist/packages/dca/api/dcaApi.d.ts +0 -26
  124. package/dist/packages/dca/api/dcaApi.d.ts.map +0 -1
  125. package/dist/packages/dca/api/dcaApi.js +0 -60
  126. package/dist/packages/dca/dca.d.ts +0 -174
  127. package/dist/packages/dca/dca.d.ts.map +0 -1
  128. package/dist/packages/dca/dca.js +0 -230
  129. package/dist/packages/dca/dcaTypes.d.ts +0 -319
  130. package/dist/packages/dca/dcaTypes.d.ts.map +0 -1
  131. package/dist/packages/dca/dcaTypes.js +0 -2
  132. package/dist/packages/dca/index.d.ts +0 -2
  133. package/dist/packages/dca/index.d.ts.map +0 -1
  134. package/dist/packages/dca/index.js +0 -17
  135. package/dist/packages/farms/api/farmsApi.d.ts +0 -996
  136. package/dist/packages/farms/api/farmsApi.d.ts.map +0 -1
  137. package/dist/packages/farms/api/farmsApi.js +0 -1826
  138. package/dist/packages/farms/api/farmsApiCasting.d.ts +0 -39
  139. package/dist/packages/farms/api/farmsApiCasting.d.ts.map +0 -1
  140. package/dist/packages/farms/api/farmsApiCasting.js +0 -427
  141. package/dist/packages/farms/api/farmsApiCastingTypes.d.ts +0 -235
  142. package/dist/packages/farms/api/farmsApiCastingTypes.d.ts.map +0 -1
  143. package/dist/packages/farms/api/farmsApiCastingTypes.js +0 -2
  144. package/dist/packages/farms/farms.d.ts +0 -221
  145. package/dist/packages/farms/farms.d.ts.map +0 -1
  146. package/dist/packages/farms/farms.js +0 -302
  147. package/dist/packages/farms/farmsStakedPosition.d.ts +0 -229
  148. package/dist/packages/farms/farmsStakedPosition.d.ts.map +0 -1
  149. package/dist/packages/farms/farmsStakedPosition.js +0 -473
  150. package/dist/packages/farms/farmsStakingPool.d.ts +0 -285
  151. package/dist/packages/farms/farmsStakingPool.d.ts.map +0 -1
  152. package/dist/packages/farms/farmsStakingPool.js +0 -488
  153. package/dist/packages/farms/farmsTypes.d.ts +0 -571
  154. package/dist/packages/farms/farmsTypes.d.ts.map +0 -1
  155. package/dist/packages/farms/farmsTypes.js +0 -49
  156. package/dist/packages/farms/index.d.ts +0 -4
  157. package/dist/packages/farms/index.d.ts.map +0 -1
  158. package/dist/packages/farms/index.js +0 -19
  159. package/dist/packages/faucet/api/faucetApi.d.ts +0 -37
  160. package/dist/packages/faucet/api/faucetApi.d.ts.map +0 -1
  161. package/dist/packages/faucet/api/faucetApi.js +0 -145
  162. package/dist/packages/faucet/api/faucetApiCasting.d.ts +0 -7
  163. package/dist/packages/faucet/api/faucetApiCasting.d.ts.map +0 -1
  164. package/dist/packages/faucet/api/faucetApiCasting.js +0 -33
  165. package/dist/packages/faucet/api/faucetApiCastingTypes.d.ts +0 -10
  166. package/dist/packages/faucet/api/faucetApiCastingTypes.d.ts.map +0 -1
  167. package/dist/packages/faucet/api/faucetApiCastingTypes.js +0 -2
  168. package/dist/packages/faucet/faucet.d.ts +0 -16
  169. package/dist/packages/faucet/faucet.d.ts.map +0 -1
  170. package/dist/packages/faucet/faucet.js +0 -64
  171. package/dist/packages/faucet/faucetTypes.d.ts +0 -20
  172. package/dist/packages/faucet/faucetTypes.d.ts.map +0 -1
  173. package/dist/packages/faucet/faucetTypes.js +0 -2
  174. package/dist/packages/faucet/index.d.ts +0 -2
  175. package/dist/packages/faucet/index.d.ts.map +0 -1
  176. package/dist/packages/faucet/index.js +0 -17
  177. package/dist/packages/index.d.ts +0 -14
  178. package/dist/packages/index.d.ts.map +0 -1
  179. package/dist/packages/index.js +0 -29
  180. package/dist/packages/leveragedStaking/api/leveragedStakingApi.d.ts +0 -202
  181. package/dist/packages/leveragedStaking/api/leveragedStakingApi.d.ts.map +0 -1
  182. package/dist/packages/leveragedStaking/api/leveragedStakingApi.js +0 -1104
  183. package/dist/packages/leveragedStaking/api/leveragedStakingApiCasting.d.ts +0 -12
  184. package/dist/packages/leveragedStaking/api/leveragedStakingApiCasting.d.ts.map +0 -1
  185. package/dist/packages/leveragedStaking/api/leveragedStakingApiCasting.js +0 -69
  186. package/dist/packages/leveragedStaking/api/leveragedStakingApiCastingTypes.d.ts +0 -39
  187. package/dist/packages/leveragedStaking/api/leveragedStakingApiCastingTypes.d.ts.map +0 -1
  188. package/dist/packages/leveragedStaking/api/leveragedStakingApiCastingTypes.js +0 -2
  189. package/dist/packages/leveragedStaking/index.d.ts +0 -2
  190. package/dist/packages/leveragedStaking/index.d.ts.map +0 -1
  191. package/dist/packages/leveragedStaking/index.js +0 -17
  192. package/dist/packages/leveragedStaking/leveragedStaking.d.ts +0 -68
  193. package/dist/packages/leveragedStaking/leveragedStaking.d.ts.map +0 -1
  194. package/dist/packages/leveragedStaking/leveragedStaking.js +0 -154
  195. package/dist/packages/leveragedStaking/leveragedStakingTypes.d.ts +0 -48
  196. package/dist/packages/leveragedStaking/leveragedStakingTypes.d.ts.map +0 -1
  197. package/dist/packages/leveragedStaking/leveragedStakingTypes.js +0 -16
  198. package/dist/packages/limitOrders/api/limitOrdersApi.d.ts +0 -12
  199. package/dist/packages/limitOrders/api/limitOrdersApi.d.ts.map +0 -1
  200. package/dist/packages/limitOrders/api/limitOrdersApi.js +0 -25
  201. package/dist/packages/limitOrders/index.d.ts +0 -2
  202. package/dist/packages/limitOrders/index.d.ts.map +0 -1
  203. package/dist/packages/limitOrders/index.js +0 -17
  204. package/dist/packages/limitOrders/limitOrders.d.ts +0 -143
  205. package/dist/packages/limitOrders/limitOrders.d.ts.map +0 -1
  206. package/dist/packages/limitOrders/limitOrders.js +0 -190
  207. package/dist/packages/limitOrders/limitOrdersTypes.d.ts +0 -211
  208. package/dist/packages/limitOrders/limitOrdersTypes.d.ts.map +0 -1
  209. package/dist/packages/limitOrders/limitOrdersTypes.js +0 -2
  210. package/dist/packages/multisig/api/multisigApi.d.ts +0 -10
  211. package/dist/packages/multisig/api/multisigApi.d.ts.map +0 -1
  212. package/dist/packages/multisig/api/multisigApi.js +0 -39
  213. package/dist/packages/multisig/index.d.ts +0 -2
  214. package/dist/packages/multisig/index.d.ts.map +0 -1
  215. package/dist/packages/multisig/index.js +0 -17
  216. package/dist/packages/multisig/multisig.d.ts +0 -46
  217. package/dist/packages/multisig/multisig.d.ts.map +0 -1
  218. package/dist/packages/multisig/multisig.js +0 -65
  219. package/dist/packages/multisig/multisigTypes.d.ts +0 -26
  220. package/dist/packages/multisig/multisigTypes.d.ts.map +0 -1
  221. package/dist/packages/multisig/multisigTypes.js +0 -2
  222. package/dist/packages/nftAmm/api/nftAmmApi.d.ts +0 -93
  223. package/dist/packages/nftAmm/api/nftAmmApi.d.ts.map +0 -1
  224. package/dist/packages/nftAmm/api/nftAmmApi.js +0 -250
  225. package/dist/packages/nftAmm/api/nftAmmApiCasting.d.ts +0 -6
  226. package/dist/packages/nftAmm/api/nftAmmApiCasting.d.ts.map +0 -1
  227. package/dist/packages/nftAmm/api/nftAmmApiCasting.js +0 -43
  228. package/dist/packages/nftAmm/api/nftAmmApiCastingTypes.d.ts +0 -10
  229. package/dist/packages/nftAmm/api/nftAmmApiCastingTypes.d.ts.map +0 -1
  230. package/dist/packages/nftAmm/api/nftAmmApiCastingTypes.js +0 -2
  231. package/dist/packages/nftAmm/index.d.ts +0 -2
  232. package/dist/packages/nftAmm/index.d.ts.map +0 -1
  233. package/dist/packages/nftAmm/index.js +0 -17
  234. package/dist/packages/nftAmm/nftAmm.d.ts +0 -18
  235. package/dist/packages/nftAmm/nftAmm.d.ts.map +0 -1
  236. package/dist/packages/nftAmm/nftAmm.js +0 -73
  237. package/dist/packages/nftAmm/nftAmmMarket.d.ts +0 -54
  238. package/dist/packages/nftAmm/nftAmmMarket.d.ts.map +0 -1
  239. package/dist/packages/nftAmm/nftAmmMarket.js +0 -154
  240. package/dist/packages/nftAmm/nftAmmTypes.d.ts +0 -51
  241. package/dist/packages/nftAmm/nftAmmTypes.d.ts.map +0 -1
  242. package/dist/packages/nftAmm/nftAmmTypes.js +0 -2
  243. package/dist/packages/perpetuals/api/perpetualsApi.d.ts +0 -44
  244. package/dist/packages/perpetuals/api/perpetualsApi.d.ts.map +0 -1
  245. package/dist/packages/perpetuals/api/perpetualsApi.js +0 -1420
  246. package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts +0 -27
  247. package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts.map +0 -1
  248. package/dist/packages/perpetuals/api/perpetualsApiCasting.js +0 -463
  249. package/dist/packages/perpetuals/index.d.ts +0 -5
  250. package/dist/packages/perpetuals/index.d.ts.map +0 -1
  251. package/dist/packages/perpetuals/index.js +0 -20
  252. package/dist/packages/perpetuals/perpetuals.d.ts +0 -860
  253. package/dist/packages/perpetuals/perpetuals.d.ts.map +0 -1
  254. package/dist/packages/perpetuals/perpetuals.js +0 -1102
  255. package/dist/packages/perpetuals/perpetualsAccount.d.ts +0 -780
  256. package/dist/packages/perpetuals/perpetualsAccount.d.ts.map +0 -1
  257. package/dist/packages/perpetuals/perpetualsAccount.js +0 -1406
  258. package/dist/packages/perpetuals/perpetualsCastingTypes.d.ts +0 -170
  259. package/dist/packages/perpetuals/perpetualsCastingTypes.d.ts.map +0 -1
  260. package/dist/packages/perpetuals/perpetualsCastingTypes.js +0 -2
  261. package/dist/packages/perpetuals/perpetualsMarket.d.ts +0 -360
  262. package/dist/packages/perpetuals/perpetualsMarket.d.ts.map +0 -1
  263. package/dist/packages/perpetuals/perpetualsMarket.js +0 -475
  264. package/dist/packages/perpetuals/perpetualsTypes.d.ts +0 -2968
  265. package/dist/packages/perpetuals/perpetualsTypes.d.ts.map +0 -1
  266. package/dist/packages/perpetuals/perpetualsTypes.js +0 -165
  267. package/dist/packages/perpetuals/perpetualsVault.d.ts +0 -438
  268. package/dist/packages/perpetuals/perpetualsVault.d.ts.map +0 -1
  269. package/dist/packages/perpetuals/perpetualsVault.js +0 -601
  270. package/dist/packages/perpetuals/utils/index.d.ts +0 -2
  271. package/dist/packages/perpetuals/utils/index.d.ts.map +0 -1
  272. package/dist/packages/perpetuals/utils/index.js +0 -17
  273. package/dist/packages/perpetuals/utils/perpetualsOrderUtils.d.ts +0 -12
  274. package/dist/packages/perpetuals/utils/perpetualsOrderUtils.d.ts.map +0 -1
  275. package/dist/packages/perpetuals/utils/perpetualsOrderUtils.js +0 -60
  276. package/dist/packages/pools/api/poolsApi.d.ts +0 -342
  277. package/dist/packages/pools/api/poolsApi.d.ts.map +0 -1
  278. package/dist/packages/pools/api/poolsApi.js +0 -857
  279. package/dist/packages/pools/api/poolsApiCasting.d.ts +0 -13
  280. package/dist/packages/pools/api/poolsApiCasting.d.ts.map +0 -1
  281. package/dist/packages/pools/api/poolsApiCasting.js +0 -91
  282. package/dist/packages/pools/api/poolsApiCastingTypes.d.ts +0 -64
  283. package/dist/packages/pools/api/poolsApiCastingTypes.d.ts.map +0 -1
  284. package/dist/packages/pools/api/poolsApiCastingTypes.js +0 -2
  285. package/dist/packages/pools/index.d.ts +0 -3
  286. package/dist/packages/pools/index.d.ts.map +0 -1
  287. package/dist/packages/pools/index.js +0 -18
  288. package/dist/packages/pools/pool.d.ts +0 -459
  289. package/dist/packages/pools/pool.d.ts.map +0 -1
  290. package/dist/packages/pools/pool.js +0 -647
  291. package/dist/packages/pools/pools.d.ts +0 -405
  292. package/dist/packages/pools/pools.d.ts.map +0 -1
  293. package/dist/packages/pools/pools.js +0 -533
  294. package/dist/packages/pools/poolsTypes.d.ts +0 -469
  295. package/dist/packages/pools/poolsTypes.d.ts.map +0 -1
  296. package/dist/packages/pools/poolsTypes.js +0 -2
  297. package/dist/packages/pools/utils/cmmmCalculations.d.ts +0 -42
  298. package/dist/packages/pools/utils/cmmmCalculations.d.ts.map +0 -1
  299. package/dist/packages/pools/utils/cmmmCalculations.js +0 -1380
  300. package/dist/packages/referralVault/api/referralVaultApi.d.ts +0 -39
  301. package/dist/packages/referralVault/api/referralVaultApi.d.ts.map +0 -1
  302. package/dist/packages/referralVault/api/referralVaultApi.js +0 -125
  303. package/dist/packages/referralVault/index.d.ts +0 -2
  304. package/dist/packages/referralVault/index.d.ts.map +0 -1
  305. package/dist/packages/referralVault/index.js +0 -17
  306. package/dist/packages/referralVault/referralVault.d.ts +0 -47
  307. package/dist/packages/referralVault/referralVault.d.ts.map +0 -1
  308. package/dist/packages/referralVault/referralVault.js +0 -71
  309. package/dist/packages/referralVault/referralVaultTypes.d.ts +0 -2
  310. package/dist/packages/referralVault/referralVaultTypes.d.ts.map +0 -1
  311. package/dist/packages/referralVault/referralVaultTypes.js +0 -2
  312. package/dist/packages/referrals/index.d.ts +0 -2
  313. package/dist/packages/referrals/index.d.ts.map +0 -1
  314. package/dist/packages/referrals/index.js +0 -17
  315. package/dist/packages/referrals/referrals.d.ts +0 -27
  316. package/dist/packages/referrals/referrals.d.ts.map +0 -1
  317. package/dist/packages/referrals/referrals.js +0 -95
  318. package/dist/packages/referrals/referralsTypes.d.ts +0 -166
  319. package/dist/packages/referrals/referralsTypes.d.ts.map +0 -1
  320. package/dist/packages/referrals/referralsTypes.js +0 -2
  321. package/dist/packages/rewards/index.d.ts +0 -2
  322. package/dist/packages/rewards/index.d.ts.map +0 -1
  323. package/dist/packages/rewards/index.js +0 -17
  324. package/dist/packages/rewards/rewards.d.ts +0 -21
  325. package/dist/packages/rewards/rewards.d.ts.map +0 -1
  326. package/dist/packages/rewards/rewards.js +0 -65
  327. package/dist/packages/rewards/rewardsTypes.d.ts +0 -169
  328. package/dist/packages/rewards/rewardsTypes.d.ts.map +0 -1
  329. package/dist/packages/rewards/rewardsTypes.js +0 -2
  330. package/dist/packages/router/api/routerApi.d.ts +0 -35
  331. package/dist/packages/router/api/routerApi.d.ts.map +0 -1
  332. package/dist/packages/router/api/routerApi.js +0 -88
  333. package/dist/packages/router/api/routerApiCasting.d.ts +0 -6
  334. package/dist/packages/router/api/routerApiCasting.d.ts.map +0 -1
  335. package/dist/packages/router/api/routerApiCasting.js +0 -22
  336. package/dist/packages/router/api/routerApiCastingTypes.d.ts +0 -12
  337. package/dist/packages/router/api/routerApiCastingTypes.d.ts.map +0 -1
  338. package/dist/packages/router/api/routerApiCastingTypes.js +0 -2
  339. package/dist/packages/router/index.d.ts +0 -2
  340. package/dist/packages/router/index.d.ts.map +0 -1
  341. package/dist/packages/router/index.js +0 -17
  342. package/dist/packages/router/router.d.ts +0 -224
  343. package/dist/packages/router/router.d.ts.map +0 -1
  344. package/dist/packages/router/router.js +0 -283
  345. package/dist/packages/router/routerTypes.d.ts +0 -300
  346. package/dist/packages/router/routerTypes.d.ts.map +0 -1
  347. package/dist/packages/router/routerTypes.js +0 -2
  348. package/dist/packages/staking/api/stakingApi.d.ts +0 -129
  349. package/dist/packages/staking/api/stakingApi.d.ts.map +0 -1
  350. package/dist/packages/staking/api/stakingApi.js +0 -487
  351. package/dist/packages/staking/api/stakingApiCasting.d.ts +0 -11
  352. package/dist/packages/staking/api/stakingApiCasting.d.ts.map +0 -1
  353. package/dist/packages/staking/api/stakingApiCasting.js +0 -87
  354. package/dist/packages/staking/api/stakingApiCastingTypes.d.ts +0 -97
  355. package/dist/packages/staking/api/stakingApiCastingTypes.d.ts.map +0 -1
  356. package/dist/packages/staking/api/stakingApiCastingTypes.js +0 -2
  357. package/dist/packages/staking/index.d.ts +0 -2
  358. package/dist/packages/staking/index.d.ts.map +0 -1
  359. package/dist/packages/staking/index.js +0 -17
  360. package/dist/packages/staking/staking.d.ts +0 -385
  361. package/dist/packages/staking/staking.d.ts.map +0 -1
  362. package/dist/packages/staking/staking.js +0 -501
  363. package/dist/packages/staking/stakingTypes.d.ts +0 -575
  364. package/dist/packages/staking/stakingTypes.d.ts.map +0 -1
  365. package/dist/packages/staking/stakingTypes.js +0 -45
  366. package/dist/packages/sui/api/suiApi.d.ts +0 -21
  367. package/dist/packages/sui/api/suiApi.d.ts.map +0 -1
  368. package/dist/packages/sui/api/suiApi.js +0 -46
  369. package/dist/packages/sui/index.d.ts +0 -2
  370. package/dist/packages/sui/index.d.ts.map +0 -1
  371. package/dist/packages/sui/index.js +0 -17
  372. package/dist/packages/sui/sui.d.ts +0 -53
  373. package/dist/packages/sui/sui.d.ts.map +0 -1
  374. package/dist/packages/sui/sui.js +0 -77
  375. package/dist/packages/suiFrens/api/suiFrensApi.d.ts +0 -204
  376. package/dist/packages/suiFrens/api/suiFrensApi.d.ts.map +0 -1
  377. package/dist/packages/suiFrens/api/suiFrensApi.js +0 -756
  378. package/dist/packages/suiFrens/api/suiFrensApiCasting.d.ts +0 -21
  379. package/dist/packages/suiFrens/api/suiFrensApiCasting.d.ts.map +0 -1
  380. package/dist/packages/suiFrens/api/suiFrensApiCasting.js +0 -181
  381. package/dist/packages/suiFrens/api/suiFrensApiCastingTypes.d.ts +0 -77
  382. package/dist/packages/suiFrens/api/suiFrensApiCastingTypes.d.ts.map +0 -1
  383. package/dist/packages/suiFrens/api/suiFrensApiCastingTypes.js +0 -2
  384. package/dist/packages/suiFrens/index.d.ts +0 -4
  385. package/dist/packages/suiFrens/index.d.ts.map +0 -1
  386. package/dist/packages/suiFrens/index.js +0 -19
  387. package/dist/packages/suiFrens/stakedSuiFren.d.ts +0 -31
  388. package/dist/packages/suiFrens/stakedSuiFren.d.ts.map +0 -1
  389. package/dist/packages/suiFrens/stakedSuiFren.js +0 -90
  390. package/dist/packages/suiFrens/suiFren.d.ts +0 -32
  391. package/dist/packages/suiFrens/suiFren.d.ts.map +0 -1
  392. package/dist/packages/suiFrens/suiFren.js +0 -150
  393. package/dist/packages/suiFrens/suiFrens.d.ts +0 -56
  394. package/dist/packages/suiFrens/suiFrens.d.ts.map +0 -1
  395. package/dist/packages/suiFrens/suiFrens.js +0 -218
  396. package/dist/packages/suiFrens/suiFrensTypes.d.ts +0 -148
  397. package/dist/packages/suiFrens/suiFrensTypes.d.ts.map +0 -1
  398. package/dist/packages/suiFrens/suiFrensTypes.js +0 -8
  399. package/dist/packages/userData/index.d.ts +0 -2
  400. package/dist/packages/userData/index.d.ts.map +0 -1
  401. package/dist/packages/userData/index.js +0 -17
  402. package/dist/packages/userData/userData.d.ts +0 -89
  403. package/dist/packages/userData/userData.d.ts.map +0 -1
  404. package/dist/packages/userData/userData.js +0 -119
  405. package/dist/packages/userData/userDataTypes.d.ts +0 -29
  406. package/dist/packages/userData/userDataTypes.d.ts.map +0 -1
  407. package/dist/packages/userData/userDataTypes.js +0 -2
  408. package/dist/types.d.ts +0 -15
  409. package/dist/types.d.ts.map +0 -1
  410. package/dist/types.js +0 -31
@@ -1,1380 +0,0 @@
1
- "use strict";
2
- var _a;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.CmmmCalculations = void 0;
5
- const utils_1 = require("../../../general/utils");
6
- const fixedUtils_1 = require("../../../general/utils/fixedUtils");
7
- // This file is the typescript version of on-chain calculations. See the .move file for license info.
8
- // These calculations are useful for estimating values on-chain but the JS number format is LESS PRECISE!
9
- // Do not expect these values to be identical to their on-chain counterparts.
10
- // The formula used here differs from that of Curve/Balancer. Our stables allow custom price pegs as opposed to
11
- // the constant 1:1 equal-weight peg. Also our pools do not have an upper coin type limit (practically).
12
- // Here is our construction:
13
- // Start with a pool with balances b1,...,bn > 0. Call the tuple B = (b1,...,bn).
14
- // Take weights w1,...,wn with 0 < wi < 1 and w1 + ... + wn = 1.
15
- // Let X stand for the tuple (x1,...,xn) in Rn.
16
- // For normalization we need the tuple T = (h,h,...,h) for some h > 0, solved for later.
17
- // The invariant is defined as the value of this h.
18
- // -- TODO: generalize this reference point T to lie on a chosen ray like (w1*h, w2*h, ..., wn*h).
19
- // -- This would allow setting the swap price to be centered at a chosen balance distribution instead
20
- // -- of the 1:1:...:1 diagonal balance distribution currently in use.
21
- // Define the sum function S: Rn -> R as S(X) = w1*x1 + ... + wn*xn.
22
- // Define the product function P: Rn -> R as P(X) = x1^w1 * ... * xn^wn.
23
- // Note P(T) = S(T) = h.
24
- // We want the sum to vanish on the coordinate hyperplanes too so instead use L where
25
- // L(X) = [2P(X) / (P(X) + P(T))] * S(X)
26
- // Then 0 <= L(X) < 2S(X) and L(T) = h.
27
- // The constant price surface is defined by the equation L(X) = L(B) and the product curve by
28
- // P(X) = P(B). Equivilantly by L(X) - L(B) = 0, P(X) - P(B) = 0.
29
- // Take a flatness parameter A, 0 <= A <= 1. Then (1-A) is the dual parameter:
30
- // 0 <= (1-A) <= 1 and A + (1-A) = 1. Take the linear combination of the defining functions
31
- // C(X) = A * L(X) + (1-A) * P(X). The stable curve is defined as the solution to C(X) = C(B).
32
- // Moreover we can solve for T from the equation C(T) = C(B), making all the following equal:
33
- // C(B) = L(T) = S(T) = P(T) = h.
34
- // The defining equation C(X) = C(B) can be rewritten in a computationally simpler form as
35
- // P(X) * (2A * S(X) + (1-A) * P(X)) = h * (A * P(X) + h).
36
- // To see these functions/equations in action check out https://www.desmos.com/calculator/eu5mfckuk9
37
- class CmmmCalculations {
38
- }
39
- exports.CmmmCalculations = CmmmCalculations;
40
- _a = CmmmCalculations;
41
- CmmmCalculations.minWeight = 0.01;
42
- // Having a minimum normalized weight imposes a limit on the maximum number of tokens;
43
- // i.e., the largest possible pool is one where all tokens have exactly the minimum weight.
44
- CmmmCalculations.maxWeightedTokens = 100;
45
- // Pool limits that arise from limitations in the fixed point power function (and the imposed 1:100 maximum weight
46
- // ratio).
47
- // Swap limits: amounts swapped may not be larger than this percentage of total balance.
48
- CmmmCalculations.maxInRatio = 0.3;
49
- CmmmCalculations.maxOutRatio = 0.3;
50
- // Invariant shrink limit: non-proportional exits cannot cause the invariant to decrease by less than this ratio.
51
- CmmmCalculations.minInvariantRatio = 0.7;
52
- // Invariant growth limit: non-proportional joins cannot cause the invariant to increase by more than this ratio.
53
- CmmmCalculations.maxInvariantRatio = 3;
54
- CmmmCalculations.maxNewtonAttempts = 255;
55
- CmmmCalculations.convergenceBound = 1e-9;
56
- CmmmCalculations.tolerance = 1e-13;
57
- CmmmCalculations.validityTolerance = 0.000001;
58
- // Invariant is used to govern pool behavior. Swaps are operations which change the pool balances without changing
59
- // the invariant (ignoring fees) and investments change the invariant without changing the distribution of balances.
60
- // Invariant and pool lp are almost in 1:1 correspondence -- e.g. burning lp in a withdraw proportionally lowers the pool invariant.
61
- // The difference is as swap fees are absorbed they increase the invariant without incrasing total lp, increasing lp worth.
62
- // Every pool operation either explicitly or implicity calls this function.
63
- CmmmCalculations.calcInvariant = (pool) => {
64
- let flatness = fixedUtils_1.FixedUtils.directCast(pool.flatness);
65
- // The value for h which we want is the one for which the balances vector B lies on the curve through T.
66
- // That is, C(T) = C(B). This turns out to be a quadratic equation which can be solved with
67
- // h = [sqrt[P(B) * (P(B) * (A*A + 4*(1-A)) + 8*A*S(B))] - A*P(B)] / 2.
68
- let sum = 0;
69
- let prod = 0;
70
- let balance;
71
- let weight;
72
- for (let coin of Object.values(pool.coins)) {
73
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
74
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
75
- sum += weight * balance;
76
- prod += weight * Math.log(balance);
77
- }
78
- prod = Math.exp(prod);
79
- return _a.calcInvariantQuadratic(prod, sum, flatness);
80
- };
81
- // The invariant for stables comes from a quadratic equation coming from the reference point T = (h,h,...,h).
82
- // h = [sqrt[p * (p * (A*A + 4*(1-A)) + 8*A*s)] - A*p] / 2.
83
- CmmmCalculations.calcInvariantQuadratic = (prod, sum, flatness) => (Math.sqrt(prod *
84
- (prod * (flatness * flatness + (1 - flatness) * 4) +
85
- flatness * sum * 8)) -
86
- flatness * prod) /
87
- 2;
88
- // This function is used for 1d optimization. It computes the full invariant components and their
89
- // portions which omit contribution from the balance in the `index` coordinate.
90
- // It returns (prod, sum, p0, s0, h) where:
91
- // prod = b1^w1 * ... * bn^wn
92
- // sum = w1*b1 + ... + wn*bn
93
- // p0 = b1^w1 * ... * [bi^w1] * ... * bn^wn (remove bi from prod)
94
- // s0 = w1*b1 + ... + [wi*bi] + ... + wn*bn (remove bi from sum)
95
- // h is the invariant
96
- CmmmCalculations.calcInvariantComponents = (pool, index) => {
97
- let flatness = fixedUtils_1.FixedUtils.directCast(pool.flatness);
98
- let prod = 0;
99
- let sum = 0;
100
- let p0 = 0;
101
- let s0 = 0;
102
- let balance;
103
- let weight;
104
- let p;
105
- let s;
106
- for (let [coinType, coin] of Object.entries(pool.coins)) {
107
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
108
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
109
- p = weight * Math.log(balance);
110
- s = weight * balance;
111
- prod = prod + p;
112
- sum = sum + s;
113
- if (coinType != index) {
114
- p0 = p0 + p;
115
- s0 = s0 + s;
116
- }
117
- }
118
- prod = Math.exp(prod);
119
- p0 = Math.exp(p0);
120
- return [
121
- prod,
122
- sum,
123
- p0,
124
- s0,
125
- _a.calcInvariantQuadratic(prod, sum, flatness),
126
- ];
127
- };
128
- // spot price is given in units of Bin / Bout
129
- CmmmCalculations.calcSpotPrice = (pool, coinTypeIn, coinTypeOut) => _a.calcSpotPriceWithFees(pool, coinTypeIn, coinTypeOut, true);
130
- // spot price is given in units of Bin / Bout
131
- CmmmCalculations.calcSpotPriceWithFees = (pool, coinTypeIn, coinTypeOut, ignoreFees) => {
132
- var _b, _c;
133
- let a = fixedUtils_1.FixedUtils.directCast(pool.flatness);
134
- let part1 = _a.calcSpotPriceBody(pool);
135
- let coinIn = pool.coins[coinTypeIn];
136
- let coinOut = pool.coins[coinTypeOut];
137
- let balanceIn = fixedUtils_1.FixedUtils.directCast(coinIn.normalizedBalance);
138
- let balanceOut = fixedUtils_1.FixedUtils.directCast(coinOut.normalizedBalance);
139
- let weightIn = fixedUtils_1.FixedUtils.directCast(coinIn.weight);
140
- let weightOut = fixedUtils_1.FixedUtils.directCast(coinOut.weight);
141
- let swapFeeIn = ignoreFees
142
- ? 0
143
- : fixedUtils_1.FixedUtils.directCast(coinIn.tradeFeeIn);
144
- let swapFeeOut = ignoreFees
145
- ? 0
146
- : fixedUtils_1.FixedUtils.directCast(coinIn.tradeFeeOut);
147
- let sbi = weightOut * balanceIn;
148
- // this is the only place where fee values are used
149
- let sbo = (1 -
150
- (ignoreFees
151
- ? 0
152
- : utils_1.Casting.bpsToPercentage((_c = (_b = pool.daoFeePoolObject) === null || _b === void 0 ? void 0 : _b.feeBps) !== null && _c !== void 0 ? _c : BigInt(0)))) *
153
- (1 - swapFeeIn) *
154
- (1 - swapFeeOut) *
155
- weightIn *
156
- balanceOut;
157
- return ((sbi * (part1 + 2 * a * balanceOut)) /
158
- (sbo * (part1 + 2 * a * balanceIn)));
159
- };
160
- // The spot price formula contains a factor of C0^2 / P(B0) + (1-A)P(B0), this returns that
161
- CmmmCalculations.calcSpotPriceBody = (pool) => {
162
- // The spot price formula comes from the partial derivatives of Cf, specifically -(dCf / dxOut) / (dCf / dxIn)
163
- let a = fixedUtils_1.FixedUtils.directCast(pool.flatness);
164
- let ac = 1 - a;
165
- let prod = 0;
166
- let sum = 0;
167
- let balance;
168
- let weight;
169
- // The spot price formula requires knowing the value of the invariant. We need the prod and sum parts
170
- // also later on so no need to compute them twice by calling calcInvariant, just evaluate here.
171
- for (let coin of Object.values(pool.coins)) {
172
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
173
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
174
- prod += weight * Math.log(balance);
175
- sum += weight * balance;
176
- }
177
- prod = Math.exp(prod);
178
- let invarnt = _a.calcInvariantQuadratic(prod, sum, a);
179
- return (invarnt * invarnt) / prod + ac * prod;
180
- };
181
- // 1d optimized swap function for finding out given in. Returns the amount out.
182
- CmmmCalculations.calcOutGivenIn = (pool, coinTypeIn, coinTypeOut, amountIn) => {
183
- if (coinTypeIn === coinTypeOut)
184
- throw Error("in and out must be different coins");
185
- let coinIn = pool.coins[coinTypeIn];
186
- let coinOut = pool.coins[coinTypeOut];
187
- let swapFeeIn = fixedUtils_1.FixedUtils.directCast(coinIn.tradeFeeIn);
188
- let swapFeeOut = fixedUtils_1.FixedUtils.directCast(coinOut.tradeFeeOut);
189
- if (swapFeeIn >= 1 || swapFeeOut >= 1) {
190
- // this swap is disabled
191
- return BigInt(0);
192
- }
193
- let flatness = fixedUtils_1.FixedUtils.directCast(pool.flatness);
194
- let oldIn = fixedUtils_1.FixedUtils.directCast(coinIn.normalizedBalance);
195
- let oldOut = fixedUtils_1.FixedUtils.directCast(coinOut.normalizedBalance);
196
- let wIn = fixedUtils_1.FixedUtils.directCast(coinIn.weight);
197
- let wOut = fixedUtils_1.FixedUtils.directCast(coinOut.weight);
198
- let [prod, , p0, s0, h] = _a.calcInvariantComponents(pool, coinTypeOut);
199
- let feedAmountIn = (1 - swapFeeIn) *
200
- fixedUtils_1.FixedUtils.castAndNormalize(coinIn.decimalsScalar, amountIn);
201
- let newIn = oldIn + feedAmountIn;
202
- let prodRatio = Math.pow(newIn / oldIn, wIn);
203
- let newP0 = p0 * prodRatio;
204
- // the initial estimate (xi) is from if there were only the product part of the curve
205
- let xi = Math.pow(prod / newP0, 1 / wOut);
206
- let newS0 = s0 + wIn * feedAmountIn;
207
- let tokenAmountOut = _a.getTokenBalanceGivenInvariantAndAllOtherBalances(flatness, wOut, h, xi, // initial estimate -- default can be (P(X) / p0)^n
208
- newP0, // P(B) / xi^(1/n) (everything but the missing part)
209
- newS0 // S(B) - xi / n (everything but the missing part)
210
- );
211
- let amountOut = fixedUtils_1.FixedUtils.uncastAndUnnormalize(coinOut.decimalsScalar, (oldOut - tokenAmountOut) * (1 - swapFeeOut));
212
- if (!_a.checkValid1dSwap(pool, coinTypeIn, coinTypeOut, amountIn, amountOut))
213
- throw Error("invalid 1d swap");
214
- return amountOut;
215
- };
216
- // 1d optimized swap function for finding in given out. Returns the amount in.
217
- CmmmCalculations.calcInGivenOut = (pool, coinTypeIn, coinTypeOut, amountOut) => {
218
- if (coinTypeIn === coinTypeOut)
219
- throw Error("in and out must be different coins");
220
- let coinIn = pool.coins[coinTypeIn];
221
- let coinOut = pool.coins[coinTypeOut];
222
- let swapFeeIn = fixedUtils_1.FixedUtils.directCast(coinIn.tradeFeeIn);
223
- let swapFeeOut = fixedUtils_1.FixedUtils.directCast(coinOut.tradeFeeOut);
224
- if (swapFeeIn >= 1 || swapFeeOut >= 1) {
225
- // this swap is disabled
226
- if (amountOut === BigInt(0))
227
- return BigInt(0);
228
- throw Error("this swap is disabled");
229
- }
230
- let flatness = fixedUtils_1.FixedUtils.directCast(pool.flatness);
231
- let oldIn = fixedUtils_1.FixedUtils.directCast(coinIn.normalizedBalance);
232
- let oldOut = fixedUtils_1.FixedUtils.directCast(coinOut.normalizedBalance);
233
- let wOut = fixedUtils_1.FixedUtils.directCast(coinOut.weight);
234
- let wIn = fixedUtils_1.FixedUtils.directCast(coinIn.weight);
235
- let [prod, , p0, s0, h] = _a.calcInvariantComponents(pool, coinTypeIn);
236
- let feedAmountOut = fixedUtils_1.FixedUtils.castAndNormalize(coinIn.decimalsScalar, amountOut) /
237
- (1 - swapFeeOut);
238
- let newOut = oldOut - feedAmountOut;
239
- let prodRatio = Math.pow(newOut / oldOut, wOut);
240
- let newP0 = p0 * prodRatio;
241
- // the initial estimate (xi) is from if there were only the product part of the curve
242
- let xi = Math.pow(prod / newP0, 1 / wIn);
243
- let newS0 = s0 - wOut * feedAmountOut;
244
- let tokenAmountIn = _a.getTokenBalanceGivenInvariantAndAllOtherBalances(flatness, wIn, h, xi, // initial estimate -- default can be (P(X) / p0)^n
245
- newP0, // P(B) / xi^(1/n) (everything but the missing part)
246
- newS0 // S(B) - xi / n (everything but the missing part)
247
- );
248
- let amountIn = fixedUtils_1.FixedUtils.uncastAndUnnormalize(coinOut.decimalsScalar, (tokenAmountIn - oldIn) / (1 - swapFeeIn));
249
- if (!_a.checkValid1dSwap(pool, coinTypeIn, coinTypeOut, amountIn, amountOut))
250
- throw Error("invalid 1d swap");
251
- return amountIn;
252
- };
253
- // For computing swap amounts. Given the current balances (and any other parameters) and an amounts in vector,
254
- // and a expected amounts out vector, determine the value of t > 0 such that t*expected_amounts_out
255
- // is a valid swap from balances corresponding to adding amounts_in to the pool. The correct value of t is the one for which
256
- // calc_swap_invariant(balances, ...parameters, amounts_in, t*expected_amounts_out) === calc_invariant_full(balances, ...parameters).
257
- CmmmCalculations.calcSwapFixedIn = (pool, amountsIn, amountsOutDirection) => {
258
- let coins = pool.coins;
259
- let invariant = _a.calcInvariant(pool);
260
- let a = fixedUtils_1.FixedUtils.directCast(pool.flatness);
261
- let ac = 1 - a;
262
- let t = 1; // assume that the expected amounts out are close to the true amounts out
263
- // this allows faster convergence if the caller chooses expected_amounts_out well
264
- let prevT = t;
265
- let balance;
266
- let weight;
267
- let amountIn;
268
- let amountOut;
269
- let feeIn;
270
- let feeOut;
271
- let prod;
272
- let prod1;
273
- let sum;
274
- let sum1;
275
- let part1;
276
- let part2;
277
- let part3;
278
- let part4;
279
- let skip;
280
- let drainT = Number.POSITIVE_INFINITY;
281
- let shifter = 1;
282
- // make sure no disabled coin type is expected
283
- for (let [coinType, coin] of Object.entries(coins)) {
284
- amountOut = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
285
- feeOut = fixedUtils_1.FixedUtils.complement(fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut));
286
- if (amountOut > 0) {
287
- if (feeOut === 0) {
288
- throw Error("this trade is disabled");
289
- }
290
- else {
291
- // pool is drained when b + Ain * (1 - Sin) - t * Aout / (1 - Sout) = 0, or t = (b + Ain * (1 - Sin)) * (1 - So) / Aout
292
- t =
293
- ((fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance) +
294
- fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0)) *
295
- fixedUtils_1.FixedUtils.complement(fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn))) /
296
- amountOut) *
297
- feeOut;
298
- drainT = Math.min(drainT, t);
299
- }
300
- }
301
- }
302
- // drain_t is the maximum t can possibly be. It will be 0 if expected amounts out is way too high.
303
- if (drainT === 0)
304
- return BigInt(0);
305
- while (shifter >= drainT)
306
- shifter /= 2;
307
- t = 1;
308
- for (let i = 0; i < _a.maxNewtonAttempts; ++i) {
309
- prod = 0;
310
- prod1 = 0;
311
- sum = 0;
312
- sum1 = 0;
313
- skip = false;
314
- for (let [coinType, coin] of Object.entries(coins)) {
315
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
316
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
317
- amountIn = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
318
- amountOut = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
319
- feeIn = fixedUtils_1.FixedUtils.complement(fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn));
320
- feeOut = fixedUtils_1.FixedUtils.complement(fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut));
321
- // pseudoin
322
- part1 = feeIn * amountIn;
323
- // pseudoout
324
- part2 = (t * amountOut) / feeOut;
325
- // pseudobalance
326
- if (part2 >= balance + part1 + 1) {
327
- skip = true;
328
- break;
329
- }
330
- part3 = balance + part1 - part2;
331
- // for derivatives: weight * expected_amounts_out / fee_out
332
- part4 = (weight * amountOut) / feeOut;
333
- prod += weight * Math.log(part3);
334
- prod1 += part4 / part3;
335
- sum += weight * part3;
336
- sum1 += part4;
337
- }
338
- prod = Math.exp(prod);
339
- part1 = a * sum;
340
- part2 = ac * prod;
341
- part3 = part1 + part2;
342
- part4 = a * invariant * prod1;
343
- t =
344
- (a * (sum + 2 * t * sum1) +
345
- part3 +
346
- 2 * prod1 * t * part3 -
347
- (t * part4 + invariant * (a + invariant / prod))) /
348
- (2 * (prod1 * part3 + a * sum1) - part4);
349
- if (utils_1.Helpers.closeEnough(t, prevT, _a.convergenceBound)) {
350
- if (!_a.checkValidSwap(pool, amountsIn, 1, amountsOutDirection, t))
351
- throw Error("invalid swap");
352
- return fixedUtils_1.FixedUtils.directUncast(t);
353
- }
354
- prevT = t;
355
- }
356
- throw Error("Newton diverged");
357
- };
358
- // Swaps but fixed amounts out. Given the pool's current state and a guaranteed out vector, and a expected in vector,
359
- // scale expected_amounts_in by t > 0 so that this swap is valid and return the correct value for t
360
- CmmmCalculations.calcSwapFixedOut = (pool, amountsInDirection, amountsOut) => {
361
- let coins = pool.coins;
362
- let invariant = _a.calcInvariant(pool);
363
- let a = fixedUtils_1.FixedUtils.directCast(pool.flatness);
364
- let ac = 1 - a;
365
- let t = 1; // assume that the expected amounts out are close to the true amounts out
366
- // this allows faster convergence if the caller chooses expected_amounts_out well
367
- let prevT = 0;
368
- let balance;
369
- let weight;
370
- let amountIn;
371
- let amountOut;
372
- let feeIn;
373
- let feeOut;
374
- let prod;
375
- let prod1;
376
- let sum;
377
- let sum1;
378
- let part1;
379
- let part2;
380
- let part3;
381
- let part4;
382
- // make sure no disabled coin type is expected
383
- for (let [coinType, coin] of Object.entries(coins)) {
384
- if (coin.tradeFeeOut >= fixedUtils_1.FixedUtils.fixedOneB &&
385
- (amountsOut[coinType] || BigInt(0)) > BigInt(0))
386
- throw Error("this trade is disabled");
387
- }
388
- for (let i = 0; i < _a.maxNewtonAttempts; ++i) {
389
- prod = 0;
390
- prod1 = 0;
391
- sum = 0;
392
- sum1 = 0;
393
- for (let [coinType, coin] of Object.entries(coins)) {
394
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
395
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
396
- amountIn = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsInDirection[coinType] || BigInt(0));
397
- amountOut = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOut[coinType] || BigInt(0));
398
- feeIn = 1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn);
399
- feeOut = 1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut);
400
- // pseudoin expected
401
- part1 = feeIn * amountIn;
402
- // pseudoout
403
- part2 = amountOut === 0 ? 0 : amountOut / feeOut;
404
- // pseudobalance
405
- part3 = balance + t * part1 - part2;
406
- // for derivatives: weight * fee_in * expected_amounts_in
407
- part4 = weight * part1;
408
- prod += weight * Math.log(part3);
409
- prod1 += part4 / part3;
410
- sum += weight * part3;
411
- sum1 += part4;
412
- }
413
- prod = Math.exp(prod);
414
- part1 = 2 * a * sum;
415
- part2 = ac * prod;
416
- part3 = part1 + part2;
417
- part4 =
418
- (part3 + part2) * prod1 + 2 * a * sum1 - a * invariant * prod1;
419
- t =
420
- (t * part4 + invariant * (a + invariant / prod) - part3) /
421
- part4;
422
- if (utils_1.Helpers.closeEnough(t, prevT, _a.convergenceBound)) {
423
- if (!_a.checkValidSwap(pool, amountsInDirection, 1, amountsOut, t))
424
- throw Error("invalid swap");
425
- return fixedUtils_1.FixedUtils.directUncast(t);
426
- }
427
- prevT = t;
428
- }
429
- throw Error("Newton diverged");
430
- };
431
- // Return the expected lp ratio for this deposit
432
- CmmmCalculations.calcDepositFixedAmounts = (pool, amountsIn) => {
433
- let invariant = _a.calcInvariant(pool);
434
- let coins = pool.coins;
435
- let a = fixedUtils_1.FixedUtils.directCast(pool.flatness);
436
- let ac = 1 - a;
437
- let balance;
438
- let weight;
439
- let amount;
440
- let prod = 0;
441
- let sum = 0;
442
- let r = _a.calcDepositFixedAmountsInitialEstimate(pool, amountsIn);
443
- let prevR = r;
444
- let fees = {};
445
- for (let [coinType, coin] of Object.entries(coins)) {
446
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
447
- amount = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
448
- fees[coinType] =
449
- r * (balance + amount) >= balance
450
- ? 1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn)
451
- : 1 / (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut));
452
- }
453
- let i = 0;
454
- let prod1;
455
- let sum1;
456
- let fee;
457
- let part1;
458
- let part2;
459
- let part3;
460
- let part4;
461
- while (i < _a.maxNewtonAttempts) {
462
- prod = 0;
463
- prod1 = 0;
464
- sum = 0;
465
- sum1 = 0;
466
- for (let [coinType, coin] of Object.entries(coins)) {
467
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
468
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
469
- amount = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
470
- fee = fees[coinType];
471
- part1 = balance + amount;
472
- part2 = fee * r * part1 + balance - fee * balance;
473
- part3 = weight * fee * part1;
474
- prod += weight * Math.log(part2);
475
- prod1 += part3 / part2;
476
- sum += weight * part2;
477
- sum1 += part3;
478
- }
479
- prod = Math.exp(prod);
480
- part3 = a * invariant * prod1;
481
- part4 = 2 * prod1 * (a * sum + ac * prod) + 2 * a * sum1;
482
- r =
483
- (r * part4 +
484
- invariant * (1 + invariant / prod) -
485
- (r * part3 + 2 * a * sum + ac * (prod + invariant))) /
486
- (part4 - part3);
487
- if (utils_1.Helpers.closeEnough(r, prevR, _a.convergenceBound)) {
488
- let scalar = fixedUtils_1.FixedUtils.directUncast(r);
489
- if (!_a.checkValidDeposit(pool, amountsIn, scalar))
490
- throw Error("invalid deposit");
491
- return scalar;
492
- }
493
- prevR = r;
494
- i += 1;
495
- }
496
- throw Error("Newton diverged");
497
- };
498
- CmmmCalculations.calcDepositFixedAmountsInitialEstimate = (pool, amountsIn) => {
499
- let invariant = _a.calcInvariant(pool);
500
- let coins = pool.coins;
501
- let a = fixedUtils_1.FixedUtils.directCast(pool.flatness);
502
- let ac = 1 - a;
503
- let balance;
504
- let amount;
505
- let weight;
506
- let r;
507
- let rMin = 0;
508
- let cfMin = 0;
509
- let prod = 0;
510
- let sum = 0;
511
- let part1;
512
- // start cf_max as corresponding to r = 1
513
- for (let [coinType, coin] of Object.entries(coins)) {
514
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
515
- amount = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
516
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
517
- // this is all in so use fees in
518
- part1 =
519
- balance + (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn)) * amount;
520
- prod += weight * Math.log(part1);
521
- sum += weight * part1;
522
- // r_min portion of the loop
523
- r =
524
- (fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut) * balance) /
525
- (balance + amount);
526
- rMin = Math.max(r, rMin);
527
- }
528
- prod = Math.exp(prod);
529
- let cfMax = (2 * a * prod * sum) / (prod + invariant) + ac * prod;
530
- let rMax = 1;
531
- let cf;
532
- for (let [coinType, coin] of Object.entries(coins)) {
533
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
534
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
535
- amount = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
536
- r = balance / (balance + amount);
537
- if (r <= rMin)
538
- continue;
539
- prod = 0;
540
- sum = 0;
541
- for (let [coinType2, coin2] of Object.entries(coins)) {
542
- balance = fixedUtils_1.FixedUtils.directCast(coin2.normalizedBalance);
543
- weight = fixedUtils_1.FixedUtils.directCast(coin2.weight);
544
- amount = fixedUtils_1.FixedUtils.castAndNormalize(coin2.decimalsScalar, amountsIn[coinType2] || BigInt(0));
545
- part1 = r * (balance + amount);
546
- if (part1 >= balance) {
547
- // r * (B0 + Din) >= B0 so use fees in
548
- part1 =
549
- balance +
550
- (1 - fixedUtils_1.FixedUtils.directCast(coin2.tradeFeeIn)) *
551
- (part1 - balance);
552
- }
553
- else {
554
- // r * (B0 + Din) < B0 so use fees out
555
- part1 =
556
- balance -
557
- (balance - part1) /
558
- fixedUtils_1.FixedUtils.complement(fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut));
559
- }
560
- prod += weight * Math.log(part1);
561
- sum += weight * part1;
562
- }
563
- prod = Math.exp(prod);
564
- cf = (2 * a * prod * sum) / (prod + invariant) + ac * prod;
565
- if (cf <= invariant) {
566
- // is a lower bound, check min
567
- if (cf >= cfMin) {
568
- rMin = r;
569
- cfMin = cf;
570
- }
571
- }
572
- if (cf >= invariant) {
573
- // is an upper bound, check max
574
- if (cf <= cfMax) {
575
- rMax = r;
576
- cfMax = cf;
577
- }
578
- }
579
- }
580
- r =
581
- cfMin === cfMax
582
- ? rMin
583
- : (rMin * cfMax + (rMax - rMin) * invariant - rMax * cfMin) /
584
- (cfMax - cfMin);
585
- return r;
586
- };
587
- // Return the expected amounts out for this withdrawal
588
- CmmmCalculations.calcWithdrawFlpAmountsOut = (pool, amountsOutDirection, lpRatio) => {
589
- let invariant = _a.calcInvariant(pool);
590
- let coins = pool.coins;
591
- let lpr = lpRatio;
592
- let lpc = 1 - lpr;
593
- let scaledInvariant = invariant * lpr;
594
- let a = fixedUtils_1.FixedUtils.directCast(pool.flatness);
595
- let ac = 1 - a;
596
- let i;
597
- let prevR = 0;
598
- let balance;
599
- let weight;
600
- let amountOut;
601
- let fee;
602
- let prod;
603
- let prod1;
604
- let sum;
605
- let sum1;
606
- let part1;
607
- let part2;
608
- let part3;
609
- let part4;
610
- let skip;
611
- let shrinker = 1;
612
- let [r, rDrain] = _a.calcWithdrawFlpAmountsOutInitialEstimate(pool, amountsOutDirection, lpRatio);
613
- while (shrinker >= rDrain)
614
- shrinker /= 2;
615
- let fees = {};
616
- for (let [coinType, coin] of Object.entries(coins)) {
617
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
618
- amountOut = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
619
- fees[coinType] =
620
- balance * lpc >= r * amountOut
621
- ? 1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn)
622
- : 1 / (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut));
623
- }
624
- i = 0;
625
- while (i < _a.maxNewtonAttempts) {
626
- prod = 0;
627
- prod1 = 0;
628
- sum = 0;
629
- sum1 = 0;
630
- skip = false;
631
- for (let [coinType, coin] of Object.entries(coins)) {
632
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
633
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
634
- amountOut = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
635
- fee = fees[coinType];
636
- part1 = balance * (lpr + lpc * fee);
637
- part2 = fee * r * amountOut;
638
- if (part2 + 1 >= part1) {
639
- // Overshot and drained pool. Set t to be closer to t_max and try again.
640
- skip = true;
641
- break;
642
- }
643
- else {
644
- part1 -= part2;
645
- }
646
- part2 = weight * fee * amountOut;
647
- prod += weight * Math.log(part1);
648
- prod1 += part2 / part1;
649
- sum += weight * part1;
650
- sum1 += part2;
651
- }
652
- if (skip) {
653
- r = rDrain - shrinker / Math.pow(2, i);
654
- i += 1;
655
- continue;
656
- }
657
- prod = Math.exp(prod);
658
- part1 = prod / scaledInvariant;
659
- part2 = 2 * a * sum;
660
- part3 = ac * (prod * part1 + 2 * prod + scaledInvariant) + part2;
661
- part4 = part3 * prod1 + 2 * a * (part1 + 1) * sum1;
662
- r =
663
- (r * part4 +
664
- part3 +
665
- part1 * part2 -
666
- prod -
667
- scaledInvariant * (2 + scaledInvariant / prod)) /
668
- part4;
669
- if (utils_1.Helpers.closeEnough(r, prevR, _a.convergenceBound)) {
670
- let returner = {};
671
- for (let coinType of Object.keys(coins)) {
672
- returner[coinType] = fixedUtils_1.FixedUtils.directUncast(r *
673
- fixedUtils_1.FixedUtils.directCast(amountsOutDirection[coinType] || BigInt(0)));
674
- }
675
- if (!_a.checkValidWithdraw(pool, returner, lpRatio))
676
- throw Error("invalid withdraw");
677
- return returner;
678
- }
679
- prevR = r;
680
- i += 1;
681
- }
682
- throw Error("Newton diverged");
683
- };
684
- CmmmCalculations.calcWithdrawFlpAmountsOutInitialEstimate = (pool, amountsOutDirection, lpRatio) => {
685
- let invariant = _a.calcInvariant(pool);
686
- let coins = pool.coins;
687
- let lpr = lpRatio;
688
- let lpc = 1 - lpr;
689
- let scaledInvariant = invariant * lpr;
690
- let a = fixedUtils_1.FixedUtils.directCast(pool.flatness);
691
- let ac = 1 - a;
692
- let keepT;
693
- let tDrain;
694
- let t;
695
- let cf;
696
- let tMin;
697
- let cfMin;
698
- let tMax;
699
- let cfMax;
700
- let balance;
701
- let weight;
702
- let amountOut;
703
- let fee;
704
- let prod;
705
- let sum;
706
- let part1;
707
- let part2;
708
- let part3;
709
- // the biggest cfMax can possibly be is f(0) which is this:
710
- tMax = 0;
711
- prod = 0;
712
- sum = 0;
713
- for (let coin of Object.values(coins)) {
714
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
715
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
716
- fee = fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn);
717
- part1 = balance * (1 + lpr * fee - fee);
718
- prod += weight * Math.log(part1);
719
- sum += weight * part1;
720
- }
721
- prod = Math.exp(prod);
722
- cfMax = (2 * a * prod * sum) / (prod + scaledInvariant) + ac * prod;
723
- // the smallest cfMin can be is 0 which occurs when the pool is drained
724
- cfMin = 0;
725
- tMin = Number.POSITIVE_INFINITY;
726
- for (let [coinType, coin] of Object.entries(coins)) {
727
- amountOut = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
728
- if (amountOut === 0)
729
- continue;
730
- t =
731
- (fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance) *
732
- fixedUtils_1.FixedUtils.complement(fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut) * lpRatio)) /
733
- amountOut;
734
- if (t < tMin)
735
- tMin = t;
736
- }
737
- tDrain = tMin;
738
- // remaining test points are the CF discontinuities: where B0 - t*D = R*B0
739
- for (let [coinTypeT, coinT] of Object.entries(coins)) {
740
- amountOut = fixedUtils_1.FixedUtils.castAndNormalize(coinT.decimalsScalar, amountsOutDirection[coinTypeT] || BigInt(0));
741
- if (amountOut === 0)
742
- continue;
743
- balance = fixedUtils_1.FixedUtils.directCast(coinT.normalizedBalance);
744
- t = (balance * lpc) / amountOut;
745
- prod = 0;
746
- sum = 0;
747
- keepT = true;
748
- for (let [coinType, coin] of Object.entries(coins)) {
749
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
750
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
751
- amountOut = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
752
- part1 = t * amountOut;
753
- if (part1 >= balance) {
754
- // this t is too large to be a bound because B0 - t*D overdraws the pool
755
- keepT = false;
756
- break;
757
- }
758
- part1 = balance - part1;
759
- part2 = lpr * balance;
760
- part3 =
761
- part1 >= part2
762
- ? part2 +
763
- fixedUtils_1.FixedUtils.complement(fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn)) *
764
- (part1 - part2)
765
- : part2 -
766
- (part2 - part1) /
767
- fixedUtils_1.FixedUtils.complement(fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut));
768
- prod += weight * Math.log(part3);
769
- sum += weight * part3;
770
- }
771
- if (keepT) {
772
- prod = Math.exp(prod);
773
- cf =
774
- (2 * a * prod * sum) / (prod + scaledInvariant) + ac * prod;
775
- if (cf >= scaledInvariant) {
776
- // upper bound, check against cfMax
777
- if (cf <= cfMax) {
778
- tMax = t;
779
- cfMax = cf;
780
- }
781
- }
782
- if (cf <= scaledInvariant) {
783
- // lower bound, check against cfMin
784
- if (cf >= cfMin) {
785
- tMin = t;
786
- cfMin = cf;
787
- }
788
- }
789
- }
790
- }
791
- // initial estimate is the linear interpolation between discontinuity bounds
792
- t =
793
- cfMax === cfMin
794
- ? tMin
795
- : (tMin * cfMax +
796
- tMax * scaledInvariant -
797
- tMax * cfMin -
798
- tMin * scaledInvariant) /
799
- (cfMax - cfMin);
800
- return [t, tDrain];
801
- };
802
- // Dusty direct all-coin deposit, returns the number s >= 0 so that amounts_in = s*B0 + dust.
803
- // When performing an all-coin deposit, call this function to get t then split amounts_in into s*B0 + dust.
804
- // At least one coordinate of dust will be 0. Send the s*B0 balances into the pool and mint s*total_lp.
805
- // The caller keeps the dust.
806
- CmmmCalculations.calcAllCoinDeposit = (pool, amountsIn) => {
807
- let coins = pool.coins;
808
- let balance;
809
- let amountIn;
810
- let s;
811
- let sMin = Number.POSITIVE_INFINITY;
812
- for (let [coinType, coin] of Object.entries(coins)) {
813
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
814
- amountIn = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
815
- s = amountIn / balance;
816
- if (s < sMin)
817
- sMin = s;
818
- }
819
- let returner = {};
820
- for (let coinType of Object.keys(coins))
821
- returner[coinType] = utils_1.Helpers.blendedOperations.mulNBB(sMin, amountsIn[coinType] || BigInt(0));
822
- return returner;
823
- };
824
- // Dusty direct all-coin withdraw, returns the number s >= 0 so that amounts_out + dust = s*B0.
825
- // The normal all-coin withdraw (take this exact amount of lp and give however much balances out)
826
- // should be done directly without this function -- just burn the lp and give the user
827
- // lp/total_lp * balance_i in each coordinate. This function is for finding how much lp it takes to
828
- // ensure that at least amounts_out comes out.
829
- CmmmCalculations.calcAllCoinWithdraw = (pool, amountsOut) => {
830
- let coins = pool.coins;
831
- let balance;
832
- let amountOut;
833
- let s;
834
- let sMax = 0;
835
- for (let [coinType, coin] of Object.entries(coins)) {
836
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
837
- amountOut = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOut[coinType] || BigInt(0));
838
- s = amountOut / balance;
839
- if (s > sMax)
840
- sMax = s;
841
- }
842
- let returner = {};
843
- for (let coinType of Object.keys(coins))
844
- returner[coinType] = utils_1.Helpers.blendedOperations.mulNBB(sMax, amountsOut[coinType] || BigInt(0));
845
- return returner;
846
- };
847
- // This function calculates the balance of a given token (index) given all the other balances (combined in p0, s0)
848
- // and the invariant along with an initial estimate. It is useful for 1d optimization.
849
- CmmmCalculations.getTokenBalanceGivenInvariantAndAllOtherBalances = (flatness, w, h, xi, // initial estimate -- default can be (P(X) / p0)^n
850
- p0, // P(B) / xi^(1/n) (everything but the missing part)
851
- s0 // S(B) - xi / n (everything but the missing part)
852
- ) => {
853
- if (isNaN(xi))
854
- throw new Error("initial estimate is not a number");
855
- // Standard Newton method used here
856
- // ---------------- setting constants ----------------
857
- // c1 = 2*A*w*w
858
- // c2 = 2*(1-A)*w*p0
859
- // c3 = A*(2*w*s0+t)
860
- // c4 = t*t/p0
861
- // c5 = (1-A)*p0
862
- // c6 = A*(2*s0+w*t)
863
- // c7 = 2*A*w*(w+1)
864
- // c8 = 2*(1-A)*p0
865
- // c9 = 2*A*w*s0
866
- // c10= A*w*t
867
- let ac = 1 - flatness;
868
- let aw = flatness * w;
869
- let acw = ac * w;
870
- let as0 = flatness * s0;
871
- let ah = flatness * h;
872
- let c1 = 2 * aw * w;
873
- let c2 = 2 * acw * p0;
874
- let c3 = 2 * w * as0 + ah;
875
- let c4 = (h * h) / p0;
876
- let c5 = ac * p0;
877
- let c6 = 2 * as0 + w * ah;
878
- let c7 = 2 * aw * (w + 1);
879
- let c8 = 2 * acw * p0;
880
- let c9 = 2 * aw * s0;
881
- let c10 = aw * h;
882
- // ---------------- iterating ----------------
883
- //x = (
884
- // x * (
885
- // (
886
- // x^w * (
887
- // c1 * x + c2 * x^w + c3
888
- // ) + c4
889
- // ) - x^w * (
890
- // c5 * x^w + c6
891
- // )
892
- // )
893
- //) / (
894
- // x^w * (
895
- // (
896
- // c7 * x + c8 * x^w + c9
897
- // ) - c10
898
- // )
899
- //)
900
- let x = xi;
901
- let xw; // x^w
902
- let topPos;
903
- let topNeg;
904
- let bottomPos;
905
- //let bottomNeg;
906
- let prevX = x;
907
- let i = 0;
908
- while (i < _a.maxNewtonAttempts) {
909
- xw = Math.pow(x, w);
910
- topPos = x * (xw * (c1 * x + c2 * xw + c3) + c4);
911
- topNeg = x * (xw * (c5 * xw + c6));
912
- bottomPos = c7 * x + c8 * xw + c9;
913
- //bottomNeg = c10;
914
- // If x jumps too much (bad initial estimate) then g(x) might overshoot into a negative number.
915
- // This only happens if x is supposed to be small. In this case, replace x with a small number and try again.
916
- // Once x is close enough to the true value g(x) won't overshoot anymore and this test will be skipped from then on.
917
- if (topPos < topNeg || bottomPos < c10) {
918
- x = 1 / Math.pow(2, i);
919
- i = i + 1;
920
- continue;
921
- }
922
- x = (topPos - topNeg) / (xw * (bottomPos - c10));
923
- // using relative error here (easier to pass) because js numbers are less precise
924
- if (utils_1.Helpers.closeEnough(x, prevX, _a.convergenceBound)) {
925
- return x;
926
- }
927
- prevX = x;
928
- i = i + 1;
929
- }
930
- throw Error("Newton diverged");
931
- };
932
- // Compute the invariant before swap and pseudoinvariant (invariant considering fees)
933
- // after the swap and see if they are the same up to a tolerance.
934
- // It also checks that this balance does not drain the pool i.e. the final balance is at least 1.
935
- // The scalars are here to avoid unnecessary vector creation. In most calls one scalar will be 10^18 (1).
936
- CmmmCalculations.checkValidSwap = (pool, amountsIn, amountsInScalar, amountsOut, amountsOutScalar) => {
937
- let coins = pool.coins;
938
- let flatness = fixedUtils_1.FixedUtils.directCast(pool.flatness);
939
- // balance = balances[i]
940
- let balance;
941
- // pseudobalance = balance + feedAmountIn - feedAmountOut
942
- let pseudobalance;
943
- // postbalance = balance + amountIn - amountOut
944
- let postbalance;
945
- let weight;
946
- let amountIn;
947
- let amountOut;
948
- let feedAmountIn;
949
- let feedAmountOut;
950
- let preprod = 0;
951
- let presum = 0;
952
- let pseudoprod = 0;
953
- let pseudosum = 0;
954
- let postprod = 0;
955
- let postsum = 0;
956
- for (let [coinType, coin] of Object.entries(coins)) {
957
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
958
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
959
- amountIn =
960
- fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0)) * amountsInScalar;
961
- amountOut =
962
- fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOut[coinType] || BigInt(0)) * amountsOutScalar;
963
- if (amountIn > 0 && amountOut > 0)
964
- return false;
965
- feedAmountIn =
966
- amountIn * (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn));
967
- feedAmountOut =
968
- amountOut === 0
969
- ? 0
970
- : amountOut / (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut));
971
- postbalance = balance + amountIn;
972
- if (amountOut > postbalance + 1)
973
- return false;
974
- postbalance -= -amountOut;
975
- pseudobalance = balance + feedAmountIn;
976
- if (feedAmountOut > pseudobalance + 1)
977
- return false;
978
- pseudobalance -= -feedAmountOut;
979
- preprod += weight * Math.log(balance);
980
- presum += weight * balance;
981
- postprod += weight * Math.log(postbalance);
982
- postsum += weight * postbalance;
983
- pseudoprod += weight * Math.log(pseudobalance);
984
- pseudosum += weight * pseudobalance;
985
- }
986
- preprod = Math.exp(preprod);
987
- postprod = Math.exp(postprod);
988
- pseudoprod = Math.exp(pseudoprod);
989
- let preinvariant = _a.calcInvariantQuadratic(preprod, presum, flatness);
990
- let postinvariant = _a.calcInvariantQuadratic(postprod, postsum, flatness);
991
- let pseudoinvariant = _a.calcInvariantQuadratic(pseudoprod, pseudosum, flatness);
992
- return (postinvariant * (1 + _a.tolerance) >= preinvariant &&
993
- (utils_1.Helpers.veryCloseInt(preinvariant, pseudoinvariant, fixedUtils_1.FixedUtils.fixedOneN) ||
994
- utils_1.Helpers.closeEnough(preinvariant, pseudoinvariant, _a.validityTolerance)));
995
- };
996
- // Compute the invariant before swap and pseudoinvariant (invariant considering fees)
997
- // after the swap and see if they are the same up to a tolerance.
998
- // It also checks that this balance does not drain the pool i.e. the final balance is at least 1.
999
- CmmmCalculations.checkValid1dSwap = (pool, coinTypeIn, coinTypeOut, amountInB, amountOutB) => {
1000
- if (coinTypeIn === coinTypeOut)
1001
- return false;
1002
- let coins = pool.coins;
1003
- let coinIn = coins[coinTypeIn];
1004
- let coinOut = coins[coinTypeOut];
1005
- let flatness = fixedUtils_1.FixedUtils.directCast(pool.flatness);
1006
- // balance = balances[i]
1007
- let balance;
1008
- // pseudobalance = balance + feed amount in - feed amount out
1009
- let pseudobalance;
1010
- // postbalance = balance + amount in - amount out
1011
- let postbalance;
1012
- let weight;
1013
- let amountIn = fixedUtils_1.FixedUtils.castAndNormalize(coinIn.decimalsScalar, amountInB);
1014
- let amountOut = fixedUtils_1.FixedUtils.castAndNormalize(coinOut.decimalsScalar, amountOutB);
1015
- let feedAmountIn = amountIn * (1 - fixedUtils_1.FixedUtils.directCast(coinIn.tradeFeeIn));
1016
- let feedAmountOut = amountOut === 0
1017
- ? 0
1018
- : amountOut / (1 - fixedUtils_1.FixedUtils.directCast(coinOut.tradeFeeOut));
1019
- let preprod = 0;
1020
- let presum = 0;
1021
- let pseudoprod = 0;
1022
- let pseudosum = 0;
1023
- let postprod = 0;
1024
- let postsum = 0;
1025
- let p;
1026
- let s;
1027
- for (let [coinType, coin] of Object.entries(coins)) {
1028
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
1029
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
1030
- p = weight * Math.log(balance);
1031
- s = weight * balance;
1032
- preprod += p;
1033
- presum += s;
1034
- if (coinType === coinTypeIn) {
1035
- pseudobalance = balance + feedAmountIn;
1036
- postbalance = balance + amountIn;
1037
- pseudoprod += weight * Math.log(pseudobalance);
1038
- pseudosum += weight * pseudobalance;
1039
- postprod += weight * Math.log(postbalance);
1040
- postsum += weight * postbalance;
1041
- }
1042
- else {
1043
- if (coinType === coinTypeOut) {
1044
- if (feedAmountOut > balance + 1 || amountOut > balance + 1)
1045
- return false;
1046
- pseudobalance = balance - feedAmountOut;
1047
- postbalance = balance - amountOut;
1048
- pseudoprod += weight * Math.log(pseudobalance);
1049
- pseudosum += weight * pseudobalance;
1050
- postprod += weight * Math.log(postbalance);
1051
- postsum += weight * postbalance;
1052
- }
1053
- else {
1054
- pseudoprod += p;
1055
- pseudosum += s;
1056
- postprod += p;
1057
- postsum += s;
1058
- }
1059
- }
1060
- }
1061
- preprod = Math.exp(preprod);
1062
- postprod = Math.exp(postprod);
1063
- pseudoprod = Math.exp(pseudoprod);
1064
- let preinvariant = _a.calcInvariantQuadratic(preprod, presum, flatness);
1065
- let postinvariant = _a.calcInvariantQuadratic(postprod, postsum, flatness);
1066
- let pseudoinvariant = _a.calcInvariantQuadratic(pseudoprod, pseudosum, flatness);
1067
- return (postinvariant * (1 + _a.tolerance) >= preinvariant &&
1068
- (utils_1.Helpers.veryCloseInt(preinvariant, pseudoinvariant, fixedUtils_1.FixedUtils.fixedOneN) ||
1069
- utils_1.Helpers.closeEnough(preinvariant, pseudoinvariant, _a.validityTolerance)));
1070
- };
1071
- // A fixed amount investment is a swap followed by an all coin investment. This function checks that the
1072
- // intermediate swap is allowed and corresponds to the claimed lp ratio.
1073
- CmmmCalculations.checkValidDeposit = (pool, amountsIn, lpRatioRaw) => {
1074
- // The supposed swap is from B0 to R*(B0 + Din)
1075
- // This test is check_valid_swap for those data
1076
- let coins = pool.coins;
1077
- let lpRatio = fixedUtils_1.FixedUtils.directCast(lpRatioRaw);
1078
- if (lpRatio > 1)
1079
- return false;
1080
- let flatness = fixedUtils_1.FixedUtils.directCast(pool.flatness);
1081
- // balance = balances[i]
1082
- let balance;
1083
- let weight;
1084
- // amount = amountsIn[i]
1085
- let amount;
1086
- // postbalance = lpRatio * (balance + amount)
1087
- let postbalance;
1088
- // pseudobalance = fee(postbalance - balance) + balance
1089
- let pseudobalance;
1090
- // diff = postbalance - balance
1091
- let diff;
1092
- // pseudodiff = fee(diff)
1093
- let pseudodiff;
1094
- let preprod = 0;
1095
- let presum = 0;
1096
- let pseudoprod = 0;
1097
- let pseudosum = 0;
1098
- let postprod = 0;
1099
- let postsum = 0;
1100
- for (let [coinType, coin] of Object.entries(coins)) {
1101
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
1102
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
1103
- amount = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
1104
- postbalance = lpRatio * (balance + amount);
1105
- if (postbalance >= balance) {
1106
- // use fee in
1107
- diff = postbalance - balance;
1108
- pseudodiff =
1109
- diff * (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn));
1110
- pseudobalance = balance + pseudodiff;
1111
- }
1112
- else {
1113
- // use fee out
1114
- diff = balance - postbalance;
1115
- pseudodiff =
1116
- diff === 0
1117
- ? 0
1118
- : diff / (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut));
1119
- if (pseudodiff >= balance + 1)
1120
- return false;
1121
- pseudobalance = balance - pseudodiff;
1122
- }
1123
- preprod += weight * Math.log(balance);
1124
- presum += weight * balance;
1125
- postprod += weight * Math.log(postbalance);
1126
- postsum += weight * postbalance;
1127
- pseudoprod += weight * Math.log(pseudobalance);
1128
- pseudosum += weight * pseudobalance;
1129
- }
1130
- preprod = Math.exp(preprod);
1131
- postprod = Math.exp(postprod);
1132
- pseudoprod = Math.exp(pseudoprod);
1133
- let preinvariant = _a.calcInvariantQuadratic(preprod, presum, flatness);
1134
- let postinvariant = _a.calcInvariantQuadratic(postprod, postsum, flatness);
1135
- let pseudoinvariant = _a.calcInvariantQuadratic(pseudoprod, pseudosum, flatness);
1136
- return (postinvariant * (1 + _a.tolerance) >= preinvariant &&
1137
- (utils_1.Helpers.veryCloseInt(preinvariant, pseudoinvariant, fixedUtils_1.FixedUtils.fixedOneN) ||
1138
- utils_1.Helpers.closeEnough(preinvariant, pseudoinvariant, _a.validityTolerance)));
1139
- };
1140
- // A fixed lp withdraw is an all coin withdraw followed by a swap.
1141
- // This function checks that the swap is valid.
1142
- CmmmCalculations.checkValidWithdraw = (pool, amountsOutSrc, lpRatio) => {
1143
- // Check that the swap from R*B0 to B0 - Dout is valid
1144
- let coins = pool.coins;
1145
- if (lpRatio > 1)
1146
- return false;
1147
- let flatness = fixedUtils_1.FixedUtils.directCast(pool.flatness);
1148
- // balance = balances[i]
1149
- let balance;
1150
- let weight;
1151
- // amount is scaled amounts out at i
1152
- let amount;
1153
- // scaledBalance = lpRatio * balance
1154
- let scaledBalance;
1155
- // postbalance = balance - amount
1156
- let postbalance;
1157
- // pseudobalance is postbalance but considering fees
1158
- let pseudobalance;
1159
- let diff;
1160
- let pseudodiff;
1161
- let preprod = 0;
1162
- let presum = 0;
1163
- let pseudoprod = 0;
1164
- let pseudosum = 0;
1165
- let postprod = 0;
1166
- let postsum = 0;
1167
- for (let [coinType, coin] of Object.entries(coins)) {
1168
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
1169
- scaledBalance = lpRatio * balance;
1170
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
1171
- amount = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOutSrc[coinType] || BigInt(0));
1172
- if (amount > scaledBalance + 1)
1173
- return false;
1174
- postbalance = balance - amount;
1175
- if (postbalance >= scaledBalance) {
1176
- // use fee in
1177
- diff = postbalance - scaledBalance;
1178
- pseudodiff =
1179
- diff *
1180
- fixedUtils_1.FixedUtils.complement(fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn));
1181
- pseudobalance = scaledBalance + pseudodiff;
1182
- }
1183
- else {
1184
- // use fee out
1185
- diff = scaledBalance - postbalance;
1186
- pseudodiff =
1187
- diff === 0
1188
- ? 0
1189
- : diff /
1190
- fixedUtils_1.FixedUtils.complement(fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut));
1191
- if (pseudodiff > scaledBalance + 1)
1192
- return false;
1193
- pseudobalance = scaledBalance - pseudodiff;
1194
- }
1195
- preprod += weight * Math.log(scaledBalance);
1196
- presum += weight * scaledBalance;
1197
- postprod += weight * Math.log(postbalance);
1198
- postsum += weight * postbalance;
1199
- pseudoprod += weight * Math.log(pseudobalance);
1200
- pseudosum += weight * pseudobalance;
1201
- }
1202
- preprod = Math.exp(preprod);
1203
- postprod = Math.exp(postprod);
1204
- pseudoprod = Math.exp(pseudoprod);
1205
- let preinvariant = _a.calcInvariantQuadratic(preprod, presum, flatness);
1206
- let postinvariant = _a.calcInvariantQuadratic(postprod, postsum, flatness);
1207
- let pseudoinvariant = _a.calcInvariantQuadratic(pseudoprod, pseudosum, flatness);
1208
- return (postinvariant * (1 + _a.tolerance) >= preinvariant &&
1209
- (utils_1.Helpers.veryCloseInt(preinvariant, pseudoinvariant, fixedUtils_1.FixedUtils.fixedOneN) ||
1210
- utils_1.Helpers.closeEnough(preinvariant, pseudoinvariant, _a.validityTolerance)));
1211
- };
1212
- // get an estimate for outGivenIn based on the spot price
1213
- CmmmCalculations.getEstimateOutGivenIn = (pool, coinTypeIn, coinTypeOut, amountIn) => utils_1.Helpers.blendedOperations.mulNBB(_a.calcSpotPriceWithFees(pool, coinTypeIn, coinTypeOut), amountIn);
1214
- // get an estimate for inGivenOut based on the spot price
1215
- CmmmCalculations.getEstimateInGivenOut = (pool, coinTypeIn, coinTypeOut, amountOut) => utils_1.Helpers.blendedOperations.mulNBB(1 /
1216
- _a.calcSpotPriceWithFees(pool, coinTypeIn, coinTypeOut), amountOut);
1217
- // get an estimate for swapFixedIn using the spot prices
1218
- // returns t > 0 such that t*amountsOutDirection agrees with amountsIn wrt spot prices
1219
- CmmmCalculations.getEstimateSwapFixedIn = (pool, amountsIn, amountsOutDirection) => {
1220
- // find t such that Ain + t*Aout lies in the tangent plane to the swap surface at balances in the given directions
1221
- // the gradient of the invariant function with fees is (with spot body E)
1222
- // Win * (1 - Sin) * (E + 2*A * Bin) / Bin or
1223
- // Wout * (E + 2*A * Bout) / (1-Sout) * Bout
1224
- // depending on whether the balance is coming in or going out
1225
- let coins = pool.coins;
1226
- let spotBody = _a.calcSpotPriceBody(pool);
1227
- let a = fixedUtils_1.FixedUtils.directCast(pool.flatness);
1228
- let balance;
1229
- let grad;
1230
- let amountIn;
1231
- let amountOut;
1232
- let inDotGrad = 0;
1233
- let outDotGrad = 0;
1234
- for (let [coinType, coin] of Object.entries(coins)) {
1235
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
1236
- amountIn = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
1237
- amountOut = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
1238
- grad =
1239
- amountIn === 0
1240
- ? (fixedUtils_1.FixedUtils.directCast(coin.weight) *
1241
- (spotBody + 2 * a * balance)) /
1242
- (balance * (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut)))
1243
- : (fixedUtils_1.FixedUtils.directCast(coin.weight) *
1244
- (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn)) *
1245
- (spotBody + 2 * a * balance)) /
1246
- balance;
1247
- inDotGrad += amountIn * grad;
1248
- outDotGrad += amountOut * grad;
1249
- }
1250
- return inDotGrad / outDotGrad;
1251
- };
1252
- // get an estimate for swapFixedOut using the spot prices
1253
- // returns t > 0 such that t*amountsInDirection agrees with amountsOut wrt spot prices
1254
- CmmmCalculations.getEstimateSwapFixedOut = (pool, amountsInDirection, amountsOut) => {
1255
- // find t such that Ain + t*Aout lies in the tangent plane to the swap surface at balances in the given directions
1256
- // the gradient of the invariant function with fees is (with spot body E)
1257
- // Win * (1 - Sin) * (E + 2*A * Bin) / Bin or
1258
- // Wout * (E + 2*A * Bout) / (1-Sout) * Bout
1259
- // depending on whether the balance is coming in or going out
1260
- let coins = pool.coins;
1261
- let spotBody = _a.calcSpotPriceBody(pool);
1262
- let a = fixedUtils_1.FixedUtils.directCast(pool.flatness);
1263
- let balance;
1264
- let grad;
1265
- let amountIn;
1266
- let amountOut;
1267
- let inDotGrad = 0;
1268
- let outDotGrad = 0;
1269
- for (let [coinType, coin] of Object.entries(coins)) {
1270
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
1271
- amountIn = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsInDirection[coinType] || BigInt(0));
1272
- amountOut = fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOut[coinType] || BigInt(0));
1273
- grad =
1274
- amountIn === 0
1275
- ? (fixedUtils_1.FixedUtils.directCast(coin.weight) *
1276
- (spotBody + 2 * a * balance)) /
1277
- (balance * (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut)))
1278
- : (fixedUtils_1.FixedUtils.directCast(coin.weight) *
1279
- (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn)) *
1280
- (spotBody + 2 * a * balance)) /
1281
- balance;
1282
- inDotGrad += amountIn * grad;
1283
- outDotGrad += amountOut * grad;
1284
- }
1285
- return outDotGrad / inDotGrad;
1286
- };
1287
- // Calculate an estimate for lpRatio using the spot price (linear estiamtion)
1288
- // This estimation will be very good for small values in amountsIn
1289
- CmmmCalculations.getEstimateDepositFixedAmounts = (pool, amountsIn) => {
1290
- // Initial estimate comes from testing the discontinuities and doing a linear
1291
- // approximation off the two closest test points. We use it to get the correct fees.
1292
- let r0 = _a.calcDepositFixedAmountsInitialEstimate(pool, amountsIn);
1293
- // Now r0 is on the correct side of B0 as the final t*(B0+Din). This tells us which fees apply.
1294
- // All we have to do is find the value of r for which r*(B0+Din) lies on the feed tangent plane at B0.
1295
- // the gradient of the invariant function with fees is (with spot body E)
1296
- // Win * (1 - Sin) * (E + 2*A * Bin) / Bin or
1297
- // Wout * (E + 2*A * Bout) / (1-Sout) * Bout
1298
- // depending on whether the balance is coming in or going out
1299
- let coins = pool.coins;
1300
- let spotBody = _a.calcSpotPriceBody(pool);
1301
- let a = fixedUtils_1.FixedUtils.directCast(pool.flatness);
1302
- // dot(B0, g)
1303
- let d1 = 0;
1304
- // dot(B0 + Din, g)
1305
- let d2 = 0;
1306
- let balance;
1307
- let weight;
1308
- let amount;
1309
- let grad;
1310
- let scaledAmount;
1311
- for (let [coinType, coin] of Object.entries(coins)) {
1312
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
1313
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
1314
- amount =
1315
- balance +
1316
- fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsIn[coinType] || BigInt(0));
1317
- scaledAmount = amount * r0;
1318
- grad =
1319
- scaledAmount < balance
1320
- ? // use amount out
1321
- (weight * (spotBody + 2 * a * balance)) /
1322
- (balance * (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut)))
1323
- : // use amount in
1324
- (weight *
1325
- (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn)) *
1326
- (spotBody + 2 * a * balance)) /
1327
- balance;
1328
- d1 += balance * grad;
1329
- d2 += amount * grad;
1330
- }
1331
- return d1 / d2;
1332
- };
1333
- // Calculate an estimate for amountsOut using the spot price (linear estiamtion)
1334
- // This estimation will be very good for lpRatios close to 1
1335
- // Since we still need the out vector for its direction we return t s.t. t*amountsOutDirection is the estimate.
1336
- CmmmCalculations.getEstimateWithdrawFlpAmountsOut = (pool, amountsOutDirection, lpRatio) => {
1337
- // Initial estimate comes from testing the discontinuities and doing a linear
1338
- // approximation off the two closest test points. We use it to get the correct fees.
1339
- let [r0, _rDrain] = _a.calcWithdrawFlpAmountsOutInitialEstimate(pool, amountsOutDirection, lpRatio);
1340
- // Now r0 is on the correct side of R*B0 as the final B0-t*Deout. This tells us which fees apply.
1341
- // All we have to do is find the value of t for which B0-t*Deout lies on the feed tangent plane at R*B0.
1342
- // the gradient of the invariant function with fees is (with spot body E)
1343
- // Win * (1 - Sin) * (E + 2*A * Bin) / Bin or
1344
- // Wout * (E + 2*A * Bout) / (1-Sout) * Bout
1345
- // depending on whether the balance is coming in or going out
1346
- let coins = pool.coins;
1347
- // Swap center is R*B0, not B0. Luckily the spot body formula is homogeneous.
1348
- let spotBody = _a.calcSpotPriceBody(pool) * lpRatio;
1349
- let a = fixedUtils_1.FixedUtils.directCast(pool.flatness);
1350
- // dot(B0, g)
1351
- let d1 = 0;
1352
- // dot(Deout, g)
1353
- let d2 = 0;
1354
- let balance;
1355
- let scaledAmount;
1356
- let weight;
1357
- let amount;
1358
- let grad;
1359
- for (let [coinType, coin] of Object.entries(coins)) {
1360
- balance = fixedUtils_1.FixedUtils.directCast(coin.normalizedBalance);
1361
- weight = fixedUtils_1.FixedUtils.directCast(coin.weight);
1362
- amount =
1363
- balance +
1364
- fixedUtils_1.FixedUtils.castAndNormalize(coin.decimalsScalar, amountsOutDirection[coinType] || BigInt(0));
1365
- scaledAmount = amount * r0;
1366
- grad =
1367
- scaledAmount < balance
1368
- ? // use amount out
1369
- (weight * (spotBody + 2 * a * balance)) /
1370
- (balance * (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeOut)))
1371
- : // use amount in
1372
- (weight *
1373
- (1 - fixedUtils_1.FixedUtils.directCast(coin.tradeFeeIn)) *
1374
- (spotBody + 2 * a * balance)) /
1375
- balance;
1376
- d1 += balance * grad;
1377
- d2 += amount * grad;
1378
- }
1379
- return ((1 - lpRatio) * d1) / d2;
1380
- };