Skip to content

Commit

Permalink
handle swap kind EXACT_OUT for CPP
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Dec 22, 2024
1 parent 54505b5 commit 7cb2282
Show file tree
Hide file tree
Showing 5 changed files with 10,137 additions and 22 deletions.
19 changes: 14 additions & 5 deletions packages/foundry/contracts/pools/ConstantProductPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PoolSwapParams, Rounding } from "@balancer-labs/v3-interfaces/contracts
import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol";
import { FixedPoint } from "@balancer-labs/v3-solidity-utils/contracts/math/FixedPoint.sol";
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
import { SwapKind } from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol";

/**
* @title Constant Product Pool
Expand All @@ -25,15 +26,23 @@ contract ConstantProductPool is BalancerPoolToken, IBasePool {

/**
* @notice Execute a swap in the pool.
* @param params Swap parameters (see above for struct definition)
* @dev For AMM math walkthrough, see https://www.youtube.com/watch?v=QNPyFs8Wybk
* @param params Swap parameters
* @return amountCalculatedScaled18 Calculated amount for the swap
*/
function onSwap(PoolSwapParams calldata params) external pure returns (uint256 amountCalculatedScaled18) {
uint256 poolBalancetokenOut = params.balancesScaled18[params.indexOut];
uint256 poolBalancetokenIn = params.balancesScaled18[params.indexIn];
uint256 amountTokenIn = params.amountGivenScaled18;
uint256 poolBalancetokenIn = params.balancesScaled18[params.indexIn]; // X
uint256 poolBalancetokenOut = params.balancesScaled18[params.indexOut]; // Y

amountCalculatedScaled18 = (poolBalancetokenOut * amountTokenIn) / (poolBalancetokenIn + amountTokenIn);
if (params.kind == SwapKind.EXACT_IN) {
uint256 amountTokenIn = params.amountGivenScaled18; // dx
// dy = (Y * dx) / (X + dx)
amountCalculatedScaled18 = (poolBalancetokenOut * amountTokenIn) / (poolBalancetokenIn + amountTokenIn);
} else {
uint256 amountTokenOut = params.amountGivenScaled18; // dy
// dx = (X * dy) / (Y + dx)
amountCalculatedScaled18 = (poolBalancetokenIn * amountTokenOut) / (poolBalancetokenOut + amountTokenOut);
}
}

/**
Expand Down
18 changes: 12 additions & 6 deletions packages/nextjs/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,32 @@ export const Footer = () => {
href="https://github.com/balancer/scaffold-balancer-v3"
target="_blank"
rel="noreferrer"
className="link"
className="link no-underline hover:underline"
>
Github
</a>
</div>

<span>·</span>
<div className="text-center">
<a href="https://docs-v3.balancer.fi/" target="_blank" rel="noreferrer" className="link">
Docs
<a
href="https://github.com/balancer/balancer-v3-monorepo"
target="_blank"
rel="noreferrer"
className="link no-underline hover:underline"
>
Contracts
</a>
</div>
<span>·</span>
<div className="text-center">
<a
href="https://github.com/balancer/balancer-v3-monorepo"
href="https://docs-v3.balancer.fi/"
target="_blank"
rel="noreferrer"
className="link"
className="link no-underline hover:underline"
>
Contracts
Docs
</a>
</div>
</div>
Expand Down
Loading

0 comments on commit 7cb2282

Please sign in to comment.