-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: add deposit helper contract (#3)
* build: deposit helper contract * chore: cleanup * chore: add docs on all deposit helpers * chore: fmt * chore: fmt * chore: add withdraw helper contract * docs: format * feat: deploy mainnet
- Loading branch information
1 parent
842b12d
commit 94bd746
Showing
10 changed files
with
219 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters