Skip to content

Instantly share code, notes, and snippets.

@vlzhr
Created October 6, 2021 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlzhr/3c70bb34d61b7635c60add3aa469e421 to your computer and use it in GitHub Desktop.
Save vlzhr/3c70bb34d61b7635c60add3aa469e421 to your computer and use it in GitHub Desktop.
// calculate amountOut for puzzle swap
// coef = 0.98 (excludes fee)
calculateAmountOut(coef: number) {
const tokenOut = this.state.tokenOut
const tokenIn = this.getTokenIn();
const BalanceIn = this.state.data.get("global_"+[this.poolData.tokenIds[tokenIn]]+"_balance")
const BalanceOut = this.state.data.get("global_"+[this.poolData.tokenIds[tokenOut]]+"_balance")
const amountOut = BalanceOut / this.poolData.tokenDecimals[tokenOut] *
(1 - (BalanceIn / (BalanceIn + this.poolData.tokenDecimals[tokenIn] * this.state.amountIn))
** (this.poolData.tokenShares[tokenIn] / this.poolData.tokenShares[tokenOut]))
return Math.floor(amountOut * this.poolData.tokenDecimals[tokenOut] * coef) / this.poolData.tokenDecimals[tokenOut]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment