Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BridgerL2 Withdraw #336

Merged
merged 15 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
757f98b
Add withdraw functionality with error handling and events to BridgerL…
ylv-io Jan 9, 2025
897b9f1
Add KINTO_CHAINID constant, update BridgerL2.sol with new error handl…
ylv-io Jan 10, 2025
44e5632
Add KYC check to BridgerL2, update constructors and tests for kintoID…
ylv-io Jan 10, 2025
8210d1b
Add pre-hook call functions with allowlists and corresponding tests t…
ylv-io Jan 15, 2025
92245b4
Merge remote-tracking branch 'origin' into feat/l2-bridger
ylv-io Jan 15, 2025
b011cbf
Refactor BridgerL2 script and contract: update imports, enhance deplo…
ylv-io Jan 15, 2025
def3d41
Refactor BridgerL2 to use EnumerableSet for bridgeVaults, update setB…
ylv-io Jan 15, 2025
e81c8e1
Allow BridgerL2 to withdraw by modifying srcPreHookCall logic, skip t…
ylv-io Jan 15, 2025
99e52f4
Add tests for ERC20 withdrawal, allowlist management, and bridge vaul…
ylv-io Jan 15, 2025
eadb01c
Add BridgerL2V12 implementation and update migration script to save c…
ylv-io Jan 15, 2025
1769a85
Refactor BridgerL2.t.sol: adjust ERC20 withdrawal amount, remove skip…
ylv-io Jan 16, 2025
f0f6ebd
Remove unnecessary newline in UpgradeBridgerL2Script contract within …
ylv-io Jan 16, 2025
9497c13
Add view modifier to testDstPreHookCallCall__WhenSenderIsKintoWalletS…
ylv-io Jan 16, 2025
491ba11
Add new functions, events, and error handling to JSON artifacts and u…
ylv-io Jan 16, 2025
63c1c32
Upgrade BridgerL2 to version V13 with new implementation, update migr…
ylv-io Jan 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 73 additions & 6 deletions src/bridger/BridgerL2.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import {IBridger} from "@kinto-core/interfaces/bridger/IBridger.sol";

import "@kinto-core/interfaces/bridger/IBridgerL2.sol";
import "@kinto-core/interfaces/IKintoWalletFactory.sol";
import "@kinto-core/interfaces/IKintoWallet.sol";
import {IBridge} from "@kinto-core/interfaces/bridger/IBridge.sol";

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";
Expand All @@ -13,6 +16,7 @@
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {SignatureChecker} from "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol";

/**
Expand All @@ -25,27 +29,49 @@
contract BridgerL2 is Initializable, UUPSUpgradeable, OwnableUpgradeable, ReentrancyGuard, IBridgerL2 {
using SignatureChecker for address;
using ECDSA for bytes32;
using SafeERC20 for IERC20;

/* ============ Errors ============ */

/// @notice The amount is invalid.
/// @param amount The invalid amount.
error InvalidAmount(uint256 amount);

/// @notice The vault is not permitted.
error InvalidVault(address vault);

/// @notice Balance is too low for bridge operation.
/// @param amount The amount required.
error BalanceTooLow(uint256 amount, uint256 balance);

/* ============ Events ============ */

event Claim(address indexed wallet, address indexed asset, uint256 amount);

event Withdraw(address indexed user, address indexed l1Address, address indexed inputAsset, uint256 amount);

/* ============ Constants ============ */

/// @notice Treasure contract address.
address public constant TREASURY = 0x793500709506652Fcc61F0d2D0fDa605638D4293;

/* ============ State Variables ============ */
IKintoWalletFactory public immutable walletFactory;

/// @dev Mapping of all depositors by user address and asset address
/// @notice Mapping of all depositors by user address and asset address
mapping(address => mapping(address => uint256)) public override deposits;
/// @dev Deposit totals per asset
/// @notice Deposit totals per asset
mapping(address => uint256) public override depositTotals;
/// @dev Count of deposits
/// @notice Count of deposits
uint256 public override depositCount;
/// @dev Enable or disable the locks
/// @notice Enable or disable the locks
bool public override unlocked;
/// @dev Phase IV assets
/// @notice Phase IV assets
address[] public depositedAssets;
/// @dev admin wallet
/// @notice admin wallet
address public immutable adminWallet;
/// @notice List of allowed vaults for the Bridge.
mapping(address => bool) public bridgeVaults;

/* ============ Constructor & Upgrades ============ */
constructor(address _walletFactory) {
Expand Down Expand Up @@ -73,6 +99,47 @@
(newImplementation);
}

/* ============ Withdraw ============ */

/**
* @notice Internal function to handle withdrawals.
* @param user Address of the user.
* @param inputAsset Address of the input asset.
* @param amount Amount of the input asset.
* @param l1Address Kinto Wallet Address on L2 where tokens will be deposited.
* @param fee Amount paid as a fee.
* @param bridgeData Data required for the bridge.
*/
function withdrawERC20(

Check warning on line 113 in src/bridger/BridgerL2.sol

View check run for this annotation

Codecov / codecov/patch

src/bridger/BridgerL2.sol#L113

Added line #L113 was not covered by tests
address user,
address inputAsset,
uint256 amount,
address l1Address,
uint256 fee,
IBridger.BridgeData calldata bridgeData
) external nonReentrant {
if (bridgeData.gasFee > address(this).balance) revert BalanceTooLow(bridgeData.gasFee, address(this).balance);

Check warning on line 121 in src/bridger/BridgerL2.sol

View check run for this annotation

Codecov / codecov/patch

src/bridger/BridgerL2.sol#L121

Added line #L121 was not covered by tests
// slither-disable-next-line arbitrary-send-erc20
IERC20(inputAsset).safeTransferFrom(msg.sender, address(this), amount);

Check warning on line 123 in src/bridger/BridgerL2.sol

View check run for this annotation

Codecov / codecov/patch

src/bridger/BridgerL2.sol#L123

Added line #L123 was not covered by tests
// slither-disable-next-line arbitrary-send-erc20
IERC20(inputAsset).safeTransferFrom(msg.sender, TREASURY, fee);

Check warning on line 125 in src/bridger/BridgerL2.sol

View check run for this annotation

Codecov / codecov/patch

src/bridger/BridgerL2.sol#L125

Added line #L125 was not covered by tests

if (amount == 0) revert InvalidAmount(0);
if (bridgeVaults[bridgeData.vault] == false) revert InvalidVault(bridgeData.vault);

Check warning on line 128 in src/bridger/BridgerL2.sol

View check run for this annotation

Codecov / codecov/patch

src/bridger/BridgerL2.sol#L127-L128

Added lines #L127 - L128 were not covered by tests

// Approve max allowance to save on gas for future transfers
if (IERC20(inputAsset).allowance(address(this), bridgeData.vault) < amount) {
IERC20(inputAsset).safeApprove(bridgeData.vault, type(uint256).max);

Check warning on line 132 in src/bridger/BridgerL2.sol

View check run for this annotation

Codecov / codecov/patch

src/bridger/BridgerL2.sol#L131-L132

Added lines #L131 - L132 were not covered by tests
}

// slither-disable-next-line arbitrary-send-eth
IBridge(bridgeData.vault).bridge{value: bridgeData.gasFee}(

Check warning on line 136 in src/bridger/BridgerL2.sol

View check run for this annotation

Codecov / codecov/patch

src/bridger/BridgerL2.sol#L136

Added line #L136 was not covered by tests
l1Address, amount, bridgeData.msgGasLimit, bridgeData.connector, bridgeData.execPayload, bridgeData.options
);

emit Withdraw(user, l1Address, inputAsset, amount);

Check warning on line 140 in src/bridger/BridgerL2.sol

View check run for this annotation

Codecov / codecov/patch

src/bridger/BridgerL2.sol#L140

Added line #L140 was not covered by tests
}

/* ============ Privileged Functions ============ */

/**
Expand Down
2 changes: 2 additions & 0 deletions test/fork/bridger/BridgerL2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ contract BridgerL2Test is SignatureHelper, SharedSetup {
setUpKintoFork();
}

/* ============ Withdraw ============ */

/* ============ Claim Commitment (with real asset) ============ */

function testClaimCommitment_WhenRealAsset() public {
Expand Down
Loading