Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v1' into draftERC5115
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTimepunk committed Jun 10, 2024
2 parents ade1fde + c6ce3fe commit 9cc2bf0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 123 deletions.
2 changes: 1 addition & 1 deletion lib/forge-std
2 changes: 1 addition & 1 deletion lib/pigeon
57 changes: 1 addition & 56 deletions src/forms/ERC5115Form.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ contract ERC5115Form is IERC5115Form, BaseForm, LiquidityHandler {
/// @dev opinionated function not part of the 5115 eip
error FUNCTION_NOT_IMPLEMENTED();

/// @dev Error emitted when the tokenIn is not valid
error INVALID_TOKEN_IN();

/// @dev Error emitted when the tokenOut is not valid
error INVALID_TOKEN_OUT();

/// @dev Error emitted when the tokenIn is not encoded in the extraFormData
error ERC5115FORM_TOKEN_IN_NOT_ENCODED();

Expand Down Expand Up @@ -606,8 +600,6 @@ contract ERC5115Form is IERC5115Form, BaseForm, LiquidityHandler {
internal
returns (uint256 shares)
{
_validateTokenIn(vaultTokenIn_);

IStandardizedYield v = IStandardizedYield(vault);

address sharesReceiver = singleVaultData_.retain4626 ? singleVaultData_.receiverAddress : address(this);
Expand Down Expand Up @@ -638,8 +630,6 @@ contract ERC5115Form is IERC5115Form, BaseForm, LiquidityHandler {
internal
returns (uint256 assets)
{
_validateTokenOut(vaultTokenOut_);

address assetsReceiver =
singleVaultData_.liqData.txData.length == 0 ? singleVaultData_.receiverAddress : address(this);

Expand Down Expand Up @@ -670,51 +660,6 @@ contract ERC5115Form is IERC5115Form, BaseForm, LiquidityHandler {
if (assets == 0) revert Error.WITHDRAW_ZERO_COLLATERAL();
}

function _validateTokenIn(address tokenIn_) internal view {
try this.isValidTokenIn(tokenIn_) returns (bool isValid) {
if (!isValid) {
revert INVALID_TOKEN_IN();
}
} catch {
address[] memory tokensIn = this.getTokensIn();
bool found;
for (uint256 i = 0; i < tokensIn.length; i++) {
if (tokensIn[i] == tokenIn_) {
found = true;
break;
}
}
if (!found) {
revert INVALID_TOKEN_IN();
}
}
/// @dev token in must also match the asset set in the wrapper
if (tokenIn_ != asset) revert INVALID_TOKEN_IN();
}

function _validateTokenOut(address tokenOut_) internal view {
try this.isValidTokenOut(tokenOut_) returns (bool isValid) {
if (!isValid) {
revert INVALID_TOKEN_OUT();
}
} catch {
address[] memory tokensOut = this.getTokensOut();
bool found;
for (uint256 i = 0; i < tokensOut.length; i++) {
if (tokensOut[i] == tokenOut_) {
found = true;
break;
}
}
if (!found) {
revert INVALID_TOKEN_OUT();
}
}

/// @dev token out must also match the asset set in the wrapper
if (tokenOut_ != IERC5115To4626Wrapper(vault).getMainTokenOut()) revert INVALID_TOKEN_OUT();
}

function _isWithdrawTxDataAmountInvalid(
uint256 bridgeDecodedAmount_,
uint256 redeemedAmount_,
Expand All @@ -731,7 +676,7 @@ contract ERC5115Form is IERC5115Form, BaseForm, LiquidityHandler {
}

function _processEmergencyWithdraw(address receiverAddress_, uint256 amount_) internal {
IStandardizedYield v = IStandardizedYield(vault);
IStandardizedYield v = IStandardizedYield(IERC5115To4626Wrapper(vault).getUnderlying5115Vault());
if (receiverAddress_ == address(0)) revert Error.ZERO_ADDRESS();

if (v.balanceOf(address(this)) < amount_) {
Expand Down
109 changes: 47 additions & 62 deletions src/forms/wrappers/ERC5115To4626Wrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ contract ERC5115To4626Wrapper is IERC5115To4626Wrapper {
/// @dev if receiver is this contract
error INVALID_RECEIVER();

/// @dev if asset is not found in the tokensIn
error MAIN_TOKEN_IN_NOT_FOUND();
/// @dev Error emitted when the tokenIn is not valid
error INVALID_TOKEN_IN();

/// @dev if asset is not found in the tokensOut
error MAIN_TOKEN_OUT_NOT_FOUND();
/// @dev Error emitted when the tokenOut is not valid
error INVALID_TOKEN_OUT();

//////////////////////////////////////////////////////////////
// STORAGE //
Expand All @@ -33,43 +33,15 @@ contract ERC5115To4626Wrapper is IERC5115To4626Wrapper {

address internal constant NATIVE = address(0);

//////////////////////////////////////////////////////////////
// MODIFIER //
//////////////////////////////////////////////////////////////

/// @dev these modifiers help protect against undesired changes of tokenIn or out
modifier validateTokenIn() {
bool valid;
try this.isValidTokenIn(asset) returns (bool res) {
valid = res;
} catch {
valid = _validateTokenIn(asset, vault);
}

if (!valid) revert MAIN_TOKEN_IN_NOT_FOUND();
_;
}

modifier validateTokenOut() {
bool valid;
try this.isValidTokenOut(asset) returns (bool res) {
valid = res;
} catch {
valid = _validateTokenOut(asset, vault);
}

if (!valid) revert MAIN_TOKEN_OUT_NOT_FOUND();
_;
}

//////////////////////////////////////////////////////////////
// CONSTRUCTOR //
//////////////////////////////////////////////////////////////

constructor(address vault_, address tokenIn_, address tokenOut_) {
/// @dev validate tokens in and out
if (!_validateTokenIn(tokenIn_, vault_)) revert MAIN_TOKEN_IN_NOT_FOUND();
if (!_validateTokenOut(tokenOut_, vault_)) revert MAIN_TOKEN_OUT_NOT_FOUND();
_validateTokenIn(tokenIn_, vault_);
_validateTokenOut(tokenOut_, vault_);

vault = vault_;
asset = tokenIn_;
mainTokenOut = tokenOut_;
Expand Down Expand Up @@ -107,9 +79,10 @@ contract ERC5115To4626Wrapper is IERC5115To4626Wrapper {
)
external
payable
validateTokenIn
returns (uint256 amountSharesOut)
{
_validateTokenIn(tokenIn, vault);

/// @dev receiver cannot be this wrapper contract
if (receiver == address(this)) revert INVALID_RECEIVER();

Expand Down Expand Up @@ -140,9 +113,10 @@ contract ERC5115To4626Wrapper is IERC5115To4626Wrapper {
bool burnFromInternalBalance
)
external
validateTokenOut
returns (uint256 amountTokenOut)
{
_validateTokenOut(tokenOut, vault);

/// @dev receiver cannot be this wrapper contract
if (receiver == address(this)) revert INVALID_RECEIVER();

Expand Down Expand Up @@ -284,37 +258,48 @@ contract ERC5115To4626Wrapper is IERC5115To4626Wrapper {
else if (amount != 0) IERC20(token).safeTransferFrom(from, address(this), amount);
}

function _validateTokenIn(address token_, address vault_) internal view returns (bool) {
bool valid;

address[] memory tokensIn = IStandardizedYield(vault_).getTokensIn();
uint256 len = tokensIn.length;
for (uint256 i = 0; i < len; ++i) {
if (tokensIn[i] == token_) {
valid = true;
break;
} else if (i == len - 1) {
valid = false;
function _validateTokenIn(address token_, address vault_) internal view {
try IStandardizedYield(vault_).isValidTokenIn(token_) returns (bool isValid) {
if (!isValid) {
revert INVALID_TOKEN_IN();
}
} catch {
address[] memory tokensIn = IStandardizedYield(vault_).getTokensIn();
bool found;
for (uint256 i = 0; i < tokensIn.length; i++) {
if (tokensIn[i] == token_) {
found = true;
break;
}
}
if (!found) {
revert INVALID_TOKEN_IN();
}
}

return valid;
/// @dev token in must also match the asset set in the wrapper
if (token_ != asset) revert INVALID_TOKEN_IN();
}

function _validateTokenOut(address token_, address vault_) internal view returns (bool) {
bool valid;

address[] memory tokensOut = IStandardizedYield(vault_).getTokensOut();
uint256 len = tokensOut.length;
for (uint256 i = 0; i < len; ++i) {
if (tokensOut[i] == token_) {
valid = true;
break;
} else if (i == len - 1) {
valid = false;
function _validateTokenOut(address token_, address vault_) internal view {
try IStandardizedYield(vault_).isValidTokenOut(token_) returns (bool isValid) {
if (!isValid) {
revert INVALID_TOKEN_OUT();
}
} catch {
address[] memory tokensOut = IStandardizedYield(vault_).getTokensOut();
bool found;
for (uint256 i = 0; i < tokensOut.length; i++) {
if (tokensOut[i] == token_) {
found = true;
break;
}
}
if (!found) {
revert INVALID_TOKEN_OUT();
}
}

return valid;
/// @dev token out must also match the asset set in the wrapper
if (token_ != IERC5115To4626Wrapper(vault).getMainTokenOut()) revert INVALID_TOKEN_OUT();
}
}
6 changes: 3 additions & 3 deletions test/unit/crosschain-data/extensions/CoreStateRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ contract CoreStateRegistryTest is ProtocolActions {
getContract(AVAX, "DAI"),
getContract(AVAX, "DstSwapper"),
AVAX,
419_972_359,
419_800_730,
0
);

Expand Down Expand Up @@ -1396,7 +1396,7 @@ contract CoreStateRegistryTest is ProtocolActions {
getContract(AVAX, "DAI"),
getContract(AVAX, "DstSwapper"),
AVAX,
234_296_506_866_750_873,
111_629_656_688_722_279,
0
);

Expand All @@ -1418,7 +1418,7 @@ contract CoreStateRegistryTest is ProtocolActions {
v.finalTokens[0] = getContract(AVAX, "DAI");
v.finalTokens[1] = getContract(AVAX, "DAI");

v.amounts[0] = 419_950_757_613_293_461_130;
v.amounts[0] = 419_941_560_086_336_667_640;
v.amounts[1] = 419_972_359;

vm.prank(deployer);
Expand Down

0 comments on commit 9cc2bf0

Please sign in to comment.