Skip to content

Commit

Permalink
feat: add decimals scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlak committed Nov 28, 2023
1 parent ea8406b commit f223b8f
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions contracts/strategies/G3MStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import "./IG3MStrategy.sol";
import "./G3MStrategyLib.sol";
import "../interfaces/IPortfolio.sol";
import "../libraries/SwapLib.sol";
import "../libraries/AssemblyLib.sol";
import "../libraries/PoolLib.sol";

contract G3MStrategy is IG3MStrategy {
using AssemblyLib for *;

address public immutable portfolio;

mapping(uint64 => IG3MStrategy.Config) public configs;
Expand Down Expand Up @@ -92,15 +96,20 @@ contract G3MStrategy is IG3MStrategy {
bool sellAsset,
uint256 amountIn,
address swapper
) external view returns (uint256) {
) external view returns (uint256 output) {
PortfolioPool memory pool = IPortfolioStruct(portfolio).pools(poolId);

// uint256 protocolFee = IPortfolio(portfolio).protocolFee();
PortfolioPair memory pair =
IPortfolioStruct(portfolio).pairs(PoolId.wrap(poolId).pairId());

amountIn = amountIn.scaleToWad(
sellAsset ? pair.decimalsAsset : pair.decimalsQuote
);

uint256 fees = amountIn * pool.feeBasisPoints / BASIS_POINT_DIVISOR;
uint256 amountInMinusFees = amountIn - fees;

uint256 amountOut = G3MStrategyLib.computeAmountOutGivenAmountIn(
output = G3MStrategyLib.computeAmountOutGivenAmountIn(
amountInMinusFees,
sellAsset ? pool.virtualX : pool.virtualY,
sellAsset
Expand All @@ -112,7 +121,8 @@ contract G3MStrategy is IG3MStrategy {
: configs[poolId].weightX
);

return amountOut;
uint256 outputDec = sellAsset ? pair.decimalsQuote : pair.decimalsAsset;
output = output.scaleFromWadDown(outputDec);
}

/// @inheritdoc IPortfolioStrategy
Expand Down

0 comments on commit f223b8f

Please sign in to comment.