Skip to content

Commit

Permalink
build: deposit helper contract
Browse files Browse the repository at this point in the history
  • Loading branch information
antoncoding committed Jan 16, 2024
1 parent 842b12d commit 8d61619
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 5 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ jobs:
with:
version: nightly

- name: Run tests
run: forge test -vvv --fork-url https://rpc.lyra.finance
- name: Run lyra fork tests
run: forge test --match-contract FORK_LYRA_ --fork-url https://rpc.lyra.finance

- name: Run Mainnet fork tests
run: forge test --match-contract FORK_MAINNET_ --fork-url https://mainnet.infura.io/v3/743507feddbd4a8088614092511076bc -vvv
72 changes: 72 additions & 0 deletions src/helpers/LyraDepositWrapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import {IERC20} from "../../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {IWETH} from "../interfaces/IWETH.sol";
import {ISocketVault} from "../interfaces/ISocketVault.sol";
import {ILightAccountFactory} from "../interfaces/ILightAccountFactory.sol";

/**
* @title LyraDepositWrapper
* @dev Helper contract to wrap ETH into L2 WETH, or deposit any token to L2 smart contract wallet address
*/
contract LyraDepositWrapper {
///@dev L2 USDC address.
address public immutable weth;

///@dev Light Account factory address.
address public constant lightAccountFactory = 0x000000893A26168158fbeaDD9335Be5bC96592E2;

constructor(address _weth) {
weth = _weth;
}

/**
* @notice Wrap ETH into WETH and deposit to Lyra Chain via socket vault
*/
function depositETHToLyra(address socketVault, bool isSCW, uint256 gasLimit, address connector) external payable {
uint256 socketFee = ISocketVault(socketVault).getMinFees(connector, gasLimit);

uint256 depositAmount = msg.value - socketFee;

IWETH(weth).deposit{value: depositAmount}();
IERC20(weth).approve(socketVault, type(uint256).max);

address recipient = _getL2Receiver(isSCW);

ISocketVault(socketVault).depositToAppChain{value: socketFee}(recipient, depositAmount, gasLimit, connector);
}

/**
* @notice Deposit any token to Lyra Chain via socket vault.
* @dev This function help calculate L2 smart wallet addresses for users
*/
function depositToLyra(
address token,
address socketVault,
bool isSCW,
uint256 amount,
uint256 gasLimit,
address connector
) external payable {
IERC20(token).transferFrom(msg.sender, address(this), amount);
IERC20(token).approve(socketVault, type(uint256).max);

address recipient = _getL2Receiver(isSCW);

ISocketVault(socketVault).depositToAppChain{value: msg.value}(recipient, amount, gasLimit, connector);
}

/**
* @notice Return the receiver address on L2
*/
function _getL2Receiver(bool isScwWallet) internal view returns (address) {
if (isScwWallet) {
return ILightAccountFactory(lightAccountFactory).getAddress(msg.sender, 0);
} else {
return msg.sender;
}
}

receive() external payable {}
}
7 changes: 7 additions & 0 deletions src/interfaces/IWETH.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pragma solidity >=0.5.0;

interface IWETH {
function deposit() external payable;
function transfer(address to, uint256 value) external returns (bool);
function withdraw(uint256) external;
}
56 changes: 56 additions & 0 deletions test/fork-tests/deposit/DepositHelper.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;

import "lib/forge-std/src/Test.sol";

import {LyraDepositWrapper} from "src/helpers/LyraDepositWrapper.sol";
import {IERC20} from "../../../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {IWETH} from "src/interfaces/IWETH.sol";

/**
* forge test --fork-url https://mainnet.infura.io/v3/${INFURA_PROJECT_ID} -vvv
*/
contract FORK_MAINNET_LyraDepositWrapper is Test {
address public weth = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);

address public wethVault = address(0xD4efe33C66B8CdE33B8896a2126E41e5dB571b7e);
address public wethConnector = address(0xCf814e58f1649F94d37E51f730D6bF72409fA09c);

LyraDepositWrapper public wrapper;

uint256 public alicePk = 0xbabebabe;
address public alice = vm.addr(alicePk);

/**
* Only run the test when running with --fork flag, and connected to Lyra mainnet
*/
modifier onlyMainnet() {
if (block.chainid != 1) return;
_;
}

function setUp() public onlyMainnet {
wrapper = new LyraDepositWrapper(weth);

vm.deal(alice, 100 ether);
}

function test_deposit_ETH() public onlyMainnet {
vm.prank(alice);

wrapper.depositETHToLyra{value: 20 ether}(wethVault, true, 200_000, wethConnector);
}

function test_deposit_WETH() public onlyMainnet {
vm.startPrank(alice);
IWETH(weth).deposit{value: 20 ether}();
IERC20(weth).approve(address(wrapper), type(uint256).max);

uint256 socketFee = 0.03 ether;

wrapper.depositToLyra{value: socketFee}(weth, wethVault, true, 20 ether, 200_000, wethConnector);
vm.stopPrank();
}

receive() external payable {}
}
2 changes: 1 addition & 1 deletion test/fork-tests/withdraw/LyraWithdrawWrapper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {USDC} from "src/mocks/USDC.sol";
/**
* forge test --fork-url https://rpc.lyra.finance -vvv
*/
contract FORK_LyraWithdrawalTest is Test {
contract FORK_LYRA_LyraWithdrawalTest is Test {
address public immutable usdc = address(0x6879287835A86F50f784313dBEd5E5cCC5bb8481);

address public immutable controller = address(0x4C9faD010D8be90Aba505c85eacc483dFf9b8Fa9);
Expand Down
6 changes: 4 additions & 2 deletions test/fork-tests/withdraw/LyraWithdrawWrapperV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import {USDC} from "src/mocks/USDC.sol";
/**
* forge test --fork-url https://rpc.lyra.finance -vvv
*/
contract FORK_LyraWithdrawalV2Test is Test {
contract FORK_LYRA_LyraWithdrawalV2Test is Test {
address public usdc = address(0x6879287835A86F50f784313dBEd5E5cCC5bb8481);

// withdraw as official USDC
address public usdcController = address(0x4C9faD010D8be90Aba505c85eacc483dFf9b8Fa9);
address public usdc_Mainnet_Connector = address(0x1281C1464449DB73bdAa30928BCC63Dc25D8D187);
address public usdc_Arbi_Connector = address(0xBdE9e687F3A23Ebbc972c58D510dfc1f58Fb35EF); // as native USDC

// withdraw as USDC.e
address public usdc_Arbi_Connector = address(0xFc1e42b0C3Ff8d69FBe639a6a674fF5f0FcE778D);

// wbtc asset
address public wBTC = address(0x9b80ab732a6F1030326Af0014f106E12C4Db18EC);
Expand Down

0 comments on commit 8d61619

Please sign in to comment.