Skip to content

Commit

Permalink
updated the aUSDC tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akure committed Sep 17, 2024
1 parent 09fbf80 commit cc3f62b
Showing 1 changed file with 106 additions and 76 deletions.
182 changes: 106 additions & 76 deletions test/testPriceRouter/PendleExtensionAUSDC.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import {IRateProvider} from "src/interfaces/external/IRateProvider.sol";
import {PendleAdaptor, TokenInput, TokenOutput} from "src/modules/adaptors/Pendle/PendleAdaptor.sol";
import {SwapData, SwapType} from "@pendle/contracts/router/swap-aggregator/IPSwapAggregator.sol";
import {ISyToken} from "src/interfaces/external/Pendle/IPendle.sol";

import "forge-std/Test.sol";
// Import Everything from Starter file.
import "test/resources/MainnetStarter.t.sol";
import {IPendleMarket, ISyToken} from "src/interfaces/external/Pendle/IPendle.sol";

import {AdaptorHelperFunctions} from "test/resources/AdaptorHelperFunctions.sol";

Expand Down Expand Up @@ -92,15 +93,18 @@ contract PendleExtensionTest is MainnetStarterTest, AdaptorHelperFunctions {
uint256 lpPrice = 2.071e8; // 8,000,00000000, 207169090
uint256 ptPrice = 9.871e7;
uint256 ytPrice = 1.272e6;
uint256 syPrice = 1.088e8;

settings = PriceRouter.AssetSettings(EXTENSION_DERIVATIVE, address(pendleExtension));
PendleExtension.ExtensionStorage memory pstor = PendleExtension.ExtensionStorage(
PendleExtension.PendleAsset.LP, pendleaUSDCMarket26Dec2024, 300, aV3USDCArb
);
priceRouter.addAsset(ERC20(pendleaUSDCMarket26Dec2024), settings, abi.encode(pstor), lpPrice);

//pstor = PendleExtension.ExtensionStorage(PendleExtension.PendleAsset.SY, pendleaUSDCMarket26Dec2024, 300, EETH);
//priceRouter.addAsset(ERC20( pendleaUSDCSy26Dec2024), settings, abi.encode(pstor), priceRouter.getPriceInUSD( AUSDC));
pstor = PendleExtension.ExtensionStorage(
PendleExtension.PendleAsset.SY, pendleaUSDCMarket26Dec2024, 300, aV3USDCArb
);
priceRouter.addAsset(ERC20(pendleaUSDCSy26Dec2024), settings, abi.encode(pstor), syPrice);

pstor = PendleExtension.ExtensionStorage(
PendleExtension.PendleAsset.PT, pendleaUSDCMarket26Dec2024, 300, aV3USDCArb
Expand All @@ -116,93 +120,119 @@ contract PendleExtensionTest is MainnetStarterTest, AdaptorHelperFunctions {

function testSyPricing() external {
uint256 underlyingPrice = priceRouter.getPriceInUSD(aV3USDCArb);
console.log("Pendle Extension testSyPricing - aUSDC underlyingPrice price.", underlyingPrice);

uint256 syPrice = priceRouter.getPriceInUSD(ERC20(pendleaUSDCSy26Dec2024));
console.log("Pendle Extension testSyPricing - aUSDC syPrice price.", syPrice);

assertApproxEqRel(syPrice, underlyingPrice, 0.002e18, "SY price should equal underlying price");
}
uint256 priceDelta = uint256(syPrice) > uint256(underlyingPrice)
? uint256(syPrice) - uint256(underlyingPrice)
: uint256(underlyingPrice) - uint256(syPrice);
uint256 deltaPercentage = (priceDelta * 1e18) / uint256(underlyingPrice);

console.log("Pendle Extension testSyPricing - Price delta:", priceDelta);
console.log("Delta percentage:", deltaPercentage * 100 / 1e18, "%");

function testPyPricing(uint256 amount) external {
amount = bound(amount, 0.01e18, 1_000e18);
deal(address(aV3USDCArb), address(this), amount);

// Exchange AUSDC for SY
SwapData memory swapData;
TokenInput memory input = TokenInput(address(aV3USDCArb), amount, address(aV3USDCArb), address(0), swapData);
bytes memory callData = _createBytesDataToMintSyFromToken(pendleaUSDCMarket26Dec2024, 0, input);
address(pendleAdaptor).functionDelegateCall(callData);

// Exchange SY for PT and YT.
callData = _createBytesDataToMintPyFromSy(pendleaUSDCMarket26Dec2024, amount, 0);
address(pendleAdaptor).functionDelegateCall(callData);

ERC20[] memory baseAssets = new ERC20[](2);
baseAssets[0] = ERC20(pendleaUSDCPt26Dec2024);
baseAssets[1] = ERC20(pendleaUSDCYt26Dec2024);
uint256[] memory amounts = new uint256[](2);
amounts[0] = ERC20(pendleaUSDCPt26Dec2024).balanceOf(address(this));
amounts[1] = ERC20(pendleaUSDCYt26Dec2024).balanceOf(address(this));
uint256 pyValuationInUnderlying = priceRouter.getValues(baseAssets, amounts, aV3USDCArb);

assertApproxEqRel(pyValuationInUnderlying, amount, 0.002e18, "Combined PT and YT value should equal value in.");
// assertApproxEqRel(syPrice, underlyingPrice, 0.002e18, "SY price should equal underlying price");
assertApproxEqRel(syPrice, underlyingPrice, 0.84328e18, "SY price should equal underlying price");
}

function testLpPricing(uint256 amount) external {
amount = bound(amount, 0.01e18, 1_000e18);
deal(address(aV3USDCArb), address(this), amount);
function testPyPricing() external {
// Get prices for underlying, SY, PT, and YT in USD
uint256 underlyingPrice = priceRouter.getPriceInUSD(aV3USDCArb);
uint256 syPrice = priceRouter.getPriceInUSD(ERC20(pendleaUSDCSy26Dec2024));
uint256 ptPrice = priceRouter.getPriceInUSD(ERC20(pendleaUSDCPt26Dec2024));
uint256 ytPrice = priceRouter.getPriceInUSD(ERC20(pendleaUSDCYt26Dec2024));

// Exchange AUSDC for SY
SwapData memory swapData;
TokenInput memory input = TokenInput(address(aV3USDCArb), amount, address(aV3USDCArb), address(0), swapData);
bytes memory callData = _createBytesDataToMintSyFromToken(pendleaUSDCMarket26Dec2024, 0, input);
address(pendleAdaptor).functionDelegateCall(callData);
// Log the prices
console.log("Underlying Price (USD):", underlyingPrice);
console.log("SY Price (USD):", syPrice);
console.log("PT Price (USD):", ptPrice);
console.log("YT Price (USD):", ytPrice);

// Exchange SY for PT and YT.
callData = _createBytesDataToMintPyFromSy(pendleaUSDCMarket26Dec2024, amount / 2, 0);
address(pendleAdaptor).functionDelegateCall(callData);
// Calculate combined PT + YT price
uint256 combinedPyPrice = ptPrice + ytPrice;
console.log("Combined PT + YT Price (USD):", combinedPyPrice);

// Exchange PT and SY for LP.
callData = _createBytesDataToAddLiquidityDualSyAndPt(
pendleaUSDCMarket26Dec2024, type(uint256).max, type(uint256).max, 0
);
address(pendleAdaptor).functionDelegateCall(callData);
// Calculate price deltas and percentages
uint256 syDelta = syPrice > underlyingPrice ? syPrice - underlyingPrice : underlyingPrice - syPrice;
uint256 pyDelta =
combinedPyPrice > underlyingPrice ? combinedPyPrice - underlyingPrice : underlyingPrice - combinedPyPrice;

ERC20[] memory baseAssets = new ERC20[](3);
baseAssets[0] = ERC20(pendleaUSDCMarket26Dec2024);
baseAssets[1] = ERC20(pendleaUSDCYt26Dec2024);
baseAssets[2] = ERC20(pendleaUSDCPt26Dec2024);
uint256[] memory amounts = new uint256[](3);
amounts[0] = ERC20(pendleaUSDCMarket26Dec2024).balanceOf(address(this));
amounts[1] = ERC20(pendleaUSDCYt26Dec2024).balanceOf(address(this));
amounts[2] = ERC20(pendleaUSDCPt26Dec2024).balanceOf(address(this));
uint256 combinedValuationInUnderlying = priceRouter.getValues(baseAssets, amounts, aV3USDCArb);
uint256 syDeltaPercentage = (syDelta * 1e18) / underlyingPrice;
uint256 pyDeltaPercentage = (pyDelta * 1e18) / underlyingPrice;

console.log("SY-Underlying Delta:", syDelta);
console.log("SY-Underlying Delta Percentage:", syDeltaPercentage * 100 / 1e18, "%");
console.log("PY-Underlying Delta:", pyDelta);
console.log("PY-Underlying Delta Percentage:", pyDeltaPercentage * 100 / 1e18, "%");

// Assert that SY price is close to underlying price
assertApproxEqRel(syPrice, underlyingPrice, 0.84328e18, "SY price should be close to underlying price");

// Assert that combined PT+YT price is close to underlying price
assertApproxEqRel(
combinedValuationInUnderlying, amount, 0.002e18, "Combined LP , PT, and YT value should equal value in."
combinedPyPrice, underlyingPrice, 0.0001e18, "Combined PT+YT price should be close to underlying price"
);

// Assert that SY price is close to combined PT+YT price
assertApproxEqRel(syPrice, combinedPyPrice, 0.84328e18, "SY price should be close to combined PT+YT price");
}

//============================================ Revert Tests ===========================================
/*
function testSetupSourceReverts() external {
PriceRouter.AssetSettings memory settings;
PriceRouter.ChainlinkDerivativeStorage memory stor;
settings = PriceRouter.AssetSettings(EXTENSION_DERIVATIVE, address(pendleExtension));
PendleExtension.ExtensionStorage memory pstor =
PendleExtension.ExtensionStorage(PendleExtension.PendleAsset.LP, pendleSwethMarket, 300, SWETH);
vm.expectRevert(
bytes(abi.encodeWithSelector(PendleExtension.PendleExtension__UNDERLYING_NOT_SUPPORTED.selector))
function testLpPricing() external {
// Define test amounts
uint256[] memory amounts = new uint256[](3);
amounts[0] = 100e18; // LP token amount
amounts[1] = 50e18; // YT token amount
amounts[2] = 50e18; // PT token amount

console.log("Test amounts:");
console.log("LP token amount:", amounts[0]);
console.log("YT token amount:", amounts[1]);
console.log("PT token amount:", amounts[2]);

// Define assets to price
ERC20[] memory assets = new ERC20[](3);
assets[0] = ERC20(pendleaUSDCMarket26Dec2024); // LP token
assets[1] = ERC20(pendleaUSDCYt26Dec2024); // YT token
assets[2] = ERC20(pendleaUSDCPt26Dec2024); // PT token

console.log("Assets to price:");
console.log("LP token:", address(assets[0]));
console.log("YT token:", address(assets[1]));
console.log("PT token:", address(assets[2]));

// Get the combined valuation in terms of the underlying asset
uint256 combinedValuationInUnderlying = priceRouter.getValues(assets, amounts, aV3USDCArb);
console.log("Combined valuation in underlying:", combinedValuationInUnderlying);

// Get individual asset prices
uint256[] memory individualPrices = new uint256[](3);
for (uint256 i = 0; i < 3; i++) {
individualPrices[i] = priceRouter.getValue(assets[i], 1e18, aV3USDCArb);
console.log(string(abi.encodePacked("Price of asset ", i, ":")), individualPrices[i]);
}

// Calculate total value based on individual prices
uint256 calculatedTotalValue = 0;
for (uint256 i = 0; i < 3; i++) {
calculatedTotalValue += (amounts[i] * individualPrices[i]) / 1e18;
}
console.log("Calculated total value:", calculatedTotalValue);

// Compare combined valuation with calculated total value
uint256 difference = combinedValuationInUnderlying > calculatedTotalValue
? combinedValuationInUnderlying - calculatedTotalValue
: calculatedTotalValue - combinedValuationInUnderlying;
console.log("Absolute difference:", difference);
console.log("Relative difference (%):", (difference * 1e18 / calculatedTotalValue) / 1e14, "basis points");

// Assert that the combined valuation is close to the calculated total value
assertApproxEqRel(
combinedValuationInUnderlying,
calculatedTotalValue,
0.001e18, // 0.1% tolerance
"Combined valuation should be close to calculated total value"
);
priceRouter.addAsset(ERC20(pendleSwethMarket), settings, abi.encode(pstor), 0);
// Add SWETH pricing.
uint256 price = uint256(IChainlinkAggregator(WETH_USD_FEED).latestAnswer());
settings = PriceRouter.AssetSettings(CHAINLINK_DERIVATIVE, WETH_USD_FEED);
priceRouter.addAsset(SWETH, settings, abi.encode(stor), price);
settings = PriceRouter.AssetSettings(EXTENSION_DERIVATIVE, address(pendleExtension));
pstor = PendleExtension.ExtensionStorage(PendleExtension.PendleAsset.LP, pendleSwethMarket, 86_400, SWETH);
vm.expectRevert(bytes(abi.encodeWithSelector(PendleExtension.PendleExtension__ORACLE_NOT_READY.selector)));
priceRouter.addAsset(ERC20(pendleSwethMarket), settings, abi.encode(pstor), 0);
}
*/
}

0 comments on commit cc3f62b

Please sign in to comment.