-
-
Save 10xhash/8e24d0765ee98def8c6409c71a7d2b17 to your computer and use it in GitHub Desktop.
2023-11-olympus-amplification parameter update
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setup an eth forkable rpc | |
put this file in test/ | |
run : forge test --mt testHash_IncorrectCalculationIfAmplificationParameterUpdate -vv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.8.15; | |
import "forge-std/Test.sol"; | |
import {IStablePool} from "src/libraries/Balancer/interfaces/IStablePool.sol"; | |
import {IVault} from "src/libraries/Balancer/interfaces/IVault.sol"; | |
import {FullMath} from "src/libraries/FullMath.sol"; | |
import {StableMath} from "src/libraries/Balancer/math/StableMath.sol"; | |
import {IVault} from "src/libraries/Balancer/interfaces/IVault.sol"; | |
import {IBasePool} from "src/libraries/Balancer/interfaces/IBasePool.sol"; | |
import {IWeightedPool} from "src/libraries/Balancer/interfaces/IWeightedPool.sol"; | |
import {IStablePool} from "src/libraries/Balancer/interfaces/IStablePool.sol"; | |
import {VaultReentrancyLib} from "src/libraries/Balancer/contracts/VaultReentrancyLib.sol"; | |
import {LogExpMath} from "src/libraries/Balancer/math/LogExpMath.sol"; | |
import {FixedPoint} from "src/libraries/Balancer/math/FixedPoint.sol"; | |
interface IStablePoolWithAmp is IStablePool { | |
function getAmplificationParameter() | |
external | |
view | |
returns (uint amp, bool isUpdating, uint precision); | |
} | |
contract PriceTest is Test { | |
using FullMath for uint256; | |
function testHash_IncorrectCalculationIfAmplificationParameterUpdate() public { | |
IVault balVault = IVault(0xBA12222222228d8Ba445958a75a0704d566BF2C8); | |
IStablePoolWithAmp pool = IStablePoolWithAmp(0x3dd0843A028C86e0b760b1A76929d1C5Ef93a2dd); | |
// auraBal pool | |
// fork block 15719717. amp update is started here | |
vm.rollFork(15719717); | |
(, uint cachedAmpFactor) = pool.getLastInvariant(); | |
{ | |
(, bool isUpdating, ) = pool.getAmplificationParameter(); | |
assert(isUpdating == true); | |
} | |
// if no join actions are performed, the cachedAmpFactor will not be updated | |
vm.rollFork(15769772); | |
assert(block.number == 15769772); | |
bytes32 poolId = pool.getPoolId(); | |
{ | |
(address[] memory tokens_, , ) = balVault.getPoolTokens(poolId); | |
// token0 is 80bal20eth lp token | |
assert(tokens_[0] == 0x5c6Ee304399DBdB9C8Ef030aB642B10820DB8F56); | |
} | |
uint actualAmpFactor; | |
(actualAmpFactor, , ) = pool.getAmplificationParameter(); | |
(, uint[] memory balances_, ) = balVault.getPoolTokens(poolId); | |
uint256[] memory scalingFactors = pool.getScalingFactors(); | |
{ | |
uint256 len = scalingFactors.length; | |
for (uint256 i; i < len; ++i) { | |
balances_[i] = FixedPoint.mulDown(balances_[i], scalingFactors[i]); | |
} | |
} | |
// lookup token auraBal and destination token lp token | |
uint destinationTokenIndex = 0; | |
uint lookupTokenIndex = 1; | |
{ | |
console.log("calculation with previous amp factor"); | |
uint lookupTokensPerDestinationToken; | |
lookupTokensPerDestinationToken = StableMath._calcOutGivenIn( | |
cachedAmpFactor, | |
balances_, | |
destinationTokenIndex, | |
lookupTokenIndex, | |
1e18, | |
StableMath._calculateInvariant(cachedAmpFactor, balances_) | |
); | |
// Downscale the amount to token decimals | |
lookupTokensPerDestinationToken = FixedPoint.divDown( | |
lookupTokensPerDestinationToken, | |
scalingFactors[lookupTokenIndex] | |
); | |
uint outputDecimals = 8; | |
lookupTokensPerDestinationToken = | |
(lookupTokensPerDestinationToken * 10 ** outputDecimals) / | |
1e18; | |
// bal lp token price https://thegraph.com/hosted-service/subgraph/balancer-labs/balancer-v2 | |
uint destinationTokenPrice = 1352719207; | |
console.log("bal lp price", destinationTokenPrice); | |
uint lookupTokenPrice; | |
lookupTokenPrice = destinationTokenPrice.mulDiv( | |
10 ** outputDecimals, | |
lookupTokensPerDestinationToken | |
); | |
console.log("aurabal price", lookupTokenPrice); | |
} | |
{ | |
console.log("calculation with updated amp factor"); | |
uint lookupTokensPerDestinationToken; | |
lookupTokensPerDestinationToken = StableMath._calcOutGivenIn( | |
actualAmpFactor, | |
balances_, | |
destinationTokenIndex, | |
lookupTokenIndex, | |
1e18, | |
StableMath._calculateInvariant(actualAmpFactor, balances_) | |
); | |
// Downscale the amount to token decimals | |
lookupTokensPerDestinationToken = FixedPoint.divDown( | |
lookupTokensPerDestinationToken, | |
scalingFactors[lookupTokenIndex] | |
); | |
uint outputDecimals = 8; | |
lookupTokensPerDestinationToken = | |
(lookupTokensPerDestinationToken * 10 ** outputDecimals) / | |
1e18; | |
// bal lp token price https://thegraph.com/hosted-service/subgraph/balancer-labs/balancer-v2 | |
uint destinationTokenPrice = 1352719207; | |
console.log("bal lp price", destinationTokenPrice); | |
uint lookupTokenPrice; | |
lookupTokenPrice = destinationTokenPrice.mulDiv( | |
10 ** outputDecimals, | |
lookupTokensPerDestinationToken | |
); | |
console.log("aurabal price", lookupTokenPrice); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment