Skip to content

Commit

Permalink
Merge pull request #6 from ValantisLabs/chore/verifier-module-context
Browse files Browse the repository at this point in the history
chore: add verifier module context on deposits and withdrawals
  • Loading branch information
0xparashar authored Sep 6, 2024
2 parents e12d5fa + d88d1fa commit a767baf
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 40 deletions.
31 changes: 21 additions & 10 deletions src/Validly.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,17 @@ contract Validly is IValidly, ERC20, ReentrancyGuard {
* @param _minShares Minimum amount of shares to mint.
* @param _deadline Block timestamp after which this call reverts.
* @param _recipient Address to mint LP tokens for.
* @param _verificationContext Bytes encoded payload, in case `pool` has a Verifier Module.
* @return shares Amount of shares minted.
*/
function deposit(uint256 _amount0, uint256 _amount1, uint256 _minShares, uint256 _deadline, address _recipient)
function deposit(
uint256 _amount0,
uint256 _amount1,
uint256 _minShares,
uint256 _deadline,
address _recipient,
bytes calldata _verificationContext
)
external
override
ensureDeadline(_deadline)
Expand Down Expand Up @@ -184,7 +192,8 @@ contract Validly is IValidly, ERC20, ReentrancyGuard {

_mint(_recipient, shares);

(amount0, amount1) = pool.depositLiquidity(amount0, amount1, msg.sender, "", abi.encode(msg.sender));
(amount0, amount1) =
pool.depositLiquidity(amount0, amount1, msg.sender, _verificationContext, abi.encode(msg.sender));
}

/**
Expand All @@ -194,16 +203,18 @@ contract Validly is IValidly, ERC20, ReentrancyGuard {
* @param _amount1Min Minimum amount of token1 required for `_recipient`.
* @param _deadline Block timestamp after which this call reverts.
* @param _recipient Address to receive token0 and token1 amounts.
* @param _verificationContext Bytes encoded payload, in case `pool` has a Verifier Module.
* @return amount0 Amount of token0 withdrawn. WARNING: Potentially innacurate in case token0 is rebase.
* @return amount1 Amount of token1 withdrawn. WARNING: Potentially innacurate in case token1 is rebase.
*/
function withdraw(uint256 _shares, uint256 _amount0Min, uint256 _amount1Min, uint256 _deadline, address _recipient)
external
override
ensureDeadline(_deadline)
nonReentrant
returns (uint256 amount0, uint256 amount1)
{
function withdraw(
uint256 _shares,
uint256 _amount0Min,
uint256 _amount1Min,
uint256 _deadline,
address _recipient,
bytes calldata _verificationContext
) external override ensureDeadline(_deadline) nonReentrant returns (uint256 amount0, uint256 amount1) {
if (_shares == 0) revert Validly__withdraw_zeroShares();

if (_recipient == address(0)) {
Expand All @@ -228,7 +239,7 @@ contract Validly is IValidly, ERC20, ReentrancyGuard {

_burn(msg.sender, _shares);

pool.withdrawLiquidity(amount0, amount1, msg.sender, _recipient, "");
pool.withdrawLiquidity(amount0, amount1, msg.sender, _recipient, _verificationContext);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/ValidlyFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ contract ValidlyFactory is IValidlyFactory {
*/
function createPool(SovereignPoolConstructorArgs memory _args, bool _isStable) external returns (address validly) {
_args.poolManager = address(this);
// This factory does not support Sovereign Pools with Verifier Modules
_args.verifierModule = address(0);

address pool = protocolFactory.deploySovereignPool(_args);

Expand Down
24 changes: 17 additions & 7 deletions src/interfaces/IValidly.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@ interface IValidly is ISovereignALM {

function decimals1() external view returns (uint256);

function deposit(uint256 _amount0, uint256 _amount1, uint256 _minShares, uint256 _deadline, address _recipient)
external
returns (uint256 shares, uint256 amount0, uint256 amount1);

function withdraw(uint256 _shares, uint256 _amount0Min, uint256 _amount1Min, uint256 _deadline, address _recipient)
external
returns (uint256 amount0, uint256 amount1);
function deposit(
uint256 _amount0,
uint256 _amount1,
uint256 _minShares,
uint256 _deadline,
address _recipient,
bytes calldata _verificationContext
) external returns (uint256 shares, uint256 amount0, uint256 amount1);

function withdraw(
uint256 _shares,
uint256 _amount0Min,
uint256 _amount1Min,
uint256 _deadline,
address _recipient,
bytes calldata _verificationContext
) external returns (uint256 amount0, uint256 amount1);
}
28 changes: 14 additions & 14 deletions test/Validly.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,26 @@ contract ValidlyTest is Test {
vm.warp(block.timestamp + 1);

vm.expectRevert(Validly.Validly__deadlineExpired.selector);
volatilePair.deposit(10 ether, 0, 0, block.timestamp - 1, address(0));
volatilePair.deposit(10 ether, 0, 0, block.timestamp - 1, address(0), "");

vm.expectRevert(Validly.Validly__deposit_invalidRecipient.selector);
volatilePair.deposit(10 ether, 0, 0, block.timestamp + 1, address(0));
volatilePair.deposit(10 ether, 0, 0, block.timestamp + 1, address(0), "");

vm.expectRevert(Validly.Validly__deposit_lessThanMinShares.selector);
volatilePair.deposit(10000, 10000, 10000, block.timestamp + 1, address(this));
volatilePair.deposit(10000, 10000, 10000, block.timestamp + 1, address(this), "");

token0.mint(address(this), 1000 ether);
token1.mint(address(this), 1000 ether);

token0.approve(address(volatilePair), 1000 ether);
token1.approve(address(volatilePair), 1000 ether);

volatilePair.deposit(1 ether, 10 ether, 0, block.timestamp + 1, address(this));
volatilePair.deposit(1 ether, 10 ether, 0, block.timestamp + 1, address(this), "");

volatilePair.deposit(1 ether, 20 ether, 0, block.timestamp + 1, address(this));
volatilePair.deposit(1 ether, 20 ether, 0, block.timestamp + 1, address(this), "");

vm.expectRevert(Validly.Validly__deposit_zeroShares.selector);
volatilePair.deposit(1 ether, 0, 0, block.timestamp + 1, address(this));
volatilePair.deposit(1 ether, 0, 0, block.timestamp + 1, address(this), "");
}

function test_withdraw() public {
Expand All @@ -96,13 +96,13 @@ contract ValidlyTest is Test {
uint256 shares = volatilePair.balanceOf(address(this));

vm.expectRevert(Validly.Validly__deadlineExpired.selector);
volatilePair.withdraw(shares, 0, 0, block.timestamp - 1, address(0));
volatilePair.withdraw(shares, 0, 0, block.timestamp - 1, address(0), "");

vm.expectRevert(Validly.Validly__withdraw_zeroShares.selector);
volatilePair.withdraw(0, 0, 0, block.timestamp + 1, address(this));
volatilePair.withdraw(0, 0, 0, block.timestamp + 1, address(this), "");

vm.expectRevert(Validly.Validly__withdraw_invalidRecipient.selector);
volatilePair.withdraw(shares, 0, 0, block.timestamp + 1, address(0));
volatilePair.withdraw(shares, 0, 0, block.timestamp + 1, address(0), "");

uint256 sharesToWithdraw = shares / 2;

Expand All @@ -111,16 +111,16 @@ contract ValidlyTest is Test {
uint256 expectedAmount1 = Math.mulDiv(reserve1, sharesToWithdraw, volatilePair.totalSupply());

vm.expectRevert(Validly.Validly__withdraw_AmountZero.selector);
volatilePair.withdraw(1, 0, 0, block.timestamp + 1, address(this));
volatilePair.withdraw(1, 0, 0, block.timestamp + 1, address(this), "");

vm.expectRevert(Validly.Validly__withdraw_insufficientToken0Withdrawn.selector);
volatilePair.withdraw(sharesToWithdraw, 100 ether, 0, block.timestamp + 1, address(this));
volatilePair.withdraw(sharesToWithdraw, 100 ether, 0, block.timestamp + 1, address(this), "");

vm.expectRevert(Validly.Validly__withdraw_insufficientToken1Withdrawn.selector);
volatilePair.withdraw(sharesToWithdraw, 0, 100 ether, block.timestamp + 1, address(this));
volatilePair.withdraw(sharesToWithdraw, 0, 100 ether, block.timestamp + 1, address(this), "");

(uint256 amount0, uint256 amount1) =
volatilePair.withdraw(sharesToWithdraw, 0, 0, block.timestamp + 1, address(this));
volatilePair.withdraw(sharesToWithdraw, 0, 0, block.timestamp + 1, address(this), "");

assertEq(amount0, expectedAmount0);
assertEq(amount1, expectedAmount1);
Expand All @@ -133,7 +133,7 @@ contract ValidlyTest is Test {
token0.approve(address(stablePair), 1000 ether);
token1.approve(address(stablePair), 1000 ether);

stablePair.deposit(100 ether, 100 ether, 0, block.timestamp + 1, address(this));
stablePair.deposit(100 ether, 100 ether, 0, block.timestamp + 1, address(this), "");

SovereignPoolSwapParams memory params;

Expand Down
18 changes: 9 additions & 9 deletions test/ValidlyFuzz.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ contract ValidlyFuzzTest is Test {

if (expectedShares < 1000) {
vm.expectRevert(stdError.arithmeticError);
volatilePair.deposit(amount0, amount1, 0, block.timestamp + 1, address(1));
volatilePair.deposit(amount0, amount1, 0, block.timestamp + 1, address(1), "");
return;
}

if (amount0 == 0 || amount1 == 0 || expectedShares == 1000) {
vm.expectRevert(Validly.Validly__deposit_zeroShares.selector);
volatilePair.deposit(amount0, amount1, 0, block.timestamp + 1, address(1));
volatilePair.deposit(amount0, amount1, 0, block.timestamp + 1, address(1), "");
return;
}

(uint256 shares, uint256 amount0Deposited, uint256 amount1Deposited) =
volatilePair.deposit(amount0, amount1, 0, block.timestamp + 1, address(1));
volatilePair.deposit(amount0, amount1, 0, block.timestamp + 1, address(1), "");

assertEq(amount0Deposited, amount0);
assertEq(amount1Deposited, amount1);
Expand All @@ -99,12 +99,12 @@ contract ValidlyFuzzTest is Test {
Math.min(Math.mulDiv(amount0, totalSupply, reserve0), Math.mulDiv(amount1, totalSupply, reserve1));
if (amount0 == 0 || amount1 == 0 || expectedShares == 0) {
vm.expectRevert(Validly.Validly__deposit_zeroShares.selector);
volatilePair.deposit(amount0, amount1, 0, block.timestamp + 1, address(1));
volatilePair.deposit(amount0, amount1, 0, block.timestamp + 1, address(1), "");
return;
}

(uint256 shares, uint256 amount0Deposited, uint256 amount1Deposited) =
volatilePair.deposit(amount0, amount1, 0, block.timestamp + 1, address(1));
volatilePair.deposit(amount0, amount1, 0, block.timestamp + 1, address(1), "");

assertLe(amount0Deposited, amount0);
assertLe(amount1Deposited, amount1);
Expand All @@ -130,13 +130,13 @@ contract ValidlyFuzzTest is Test {

if (shares == 0) {
vm.expectRevert(Validly.Validly__withdraw_zeroShares.selector);
volatilePair.withdraw(shares, 0, 0, block.timestamp + 1, address(this));
volatilePair.withdraw(shares, 0, 0, block.timestamp + 1, address(this), "");
return;
}

if (totalSupply == 0) {
vm.expectRevert(stdError.divisionError);
volatilePair.withdraw(shares, 0, 0, block.timestamp + 1, address(this));
volatilePair.withdraw(shares, 0, 0, block.timestamp + 1, address(this), "");
return;
}

Expand All @@ -145,11 +145,11 @@ contract ValidlyFuzzTest is Test {

if (expectedAmount0 == 0 || expectedAmount1 == 0) {
vm.expectRevert(Validly.Validly__withdraw_AmountZero.selector);
volatilePair.withdraw(shares, 0, 0, block.timestamp + 1, address(this));
volatilePair.withdraw(shares, 0, 0, block.timestamp + 1, address(this), "");
return;
}

(uint256 amount0, uint256 amount1) = volatilePair.withdraw(shares, 0, 0, block.timestamp + 1, address(this));
(uint256 amount0, uint256 amount1) = volatilePair.withdraw(shares, 0, 0, block.timestamp + 1, address(this), "");

assertEq(amount0, expectedAmount0);
assertEq(amount1, expectedAmount1);
Expand Down

0 comments on commit a767baf

Please sign in to comment.