From 5a152e1d4a0ba8847ac8d117d9bfa176e1bb7264 Mon Sep 17 00:00:00 2001 From: Samarendra Gouda Date: Mon, 15 Apr 2024 15:24:44 +0530 Subject: [PATCH] feat: wETH-eETH function --- contracts/mainnet/connectors/eeth/events.sol | 1 + contracts/mainnet/connectors/eeth/helpers.sol | 2 + contracts/mainnet/connectors/eeth/main.sol | 47 +++++++++-- test/mainnet/eeth/eeth.test.ts | 82 +++++++++++++++++++ 4 files changed, 125 insertions(+), 7 deletions(-) diff --git a/contracts/mainnet/connectors/eeth/events.sol b/contracts/mainnet/connectors/eeth/events.sol index 9843a4a..97ddcea 100644 --- a/contracts/mainnet/connectors/eeth/events.sol +++ b/contracts/mainnet/connectors/eeth/events.sol @@ -3,4 +3,5 @@ pragma solidity 0.8.19; contract Events { event LogDeposit(uint256 amount, uint256 getId, uint256 setId); + event LogDepositWeth(uint256 amount, uint256 getId, uint256 setId); } diff --git a/contracts/mainnet/connectors/eeth/helpers.sol b/contracts/mainnet/connectors/eeth/helpers.sol index f770191..a643bb5 100644 --- a/contracts/mainnet/connectors/eeth/helpers.sol +++ b/contracts/mainnet/connectors/eeth/helpers.sol @@ -2,7 +2,9 @@ pragma solidity 0.8.19; import "./interfaces.sol"; +import {TokenInterface} from "../../common/interfaces.sol"; contract Helpers{ IEtherfiPool internal constant ETHERFI_POOL = IEtherfiPool(0x308861A430be4cce5502d0A12724771Fc6DaF216); + TokenInterface internal constant WETH_CONTRACT = TokenInterface(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); } diff --git a/contracts/mainnet/connectors/eeth/main.sol b/contracts/mainnet/connectors/eeth/main.sol index 2d17e7a..5193281 100644 --- a/contracts/mainnet/connectors/eeth/main.sol +++ b/contracts/mainnet/connectors/eeth/main.sol @@ -7,12 +7,45 @@ import {Basic} from "../../common/basic.sol"; contract EETHContract is Helpers, Basic, Events { /** - * @dev deposit ETH into Etherfi. - * @notice stake Eth in Etherfi, users receive eETH tokens on a 1:1 basis representing their staked ETH. - * @param amount The amount of ETH to deposit. (For max: `uint256(-1)`) - * @param getId ID to retrieve amt. - * @param setId ID stores the amount of ETH deposited. - */ + * @dev deposit wETH into Etherfi. + * @notice unwrap wETH and stake ETH in Etherfi, users receive eETH tokens on a 1:1 basis representing their staked ETH. + * @param amount The amount of ETH to deposit. (For max: `uint256(-1)`) + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of ETH deposited. + */ + function depositWeth( + uint256 amount, + uint256 getId, + uint256 setId + ) public returns (string memory _eventName, bytes memory _eventParam) { + uint256 _amount = getUint(getId, amount); + _amount = _amount == type(uint256).max + ? WETH_CONTRACT.balanceOf(address(this)) + : _amount; + + uint256 _ethBeforeBalance = address(this).balance; + + WETH_CONTRACT.approve(address(WETH_CONTRACT), _amount); + WETH_CONTRACT.withdraw(_amount); + + uint256 _ethAfterBalance = address(this).balance; + + uint256 _ethAmount = sub(_ethAfterBalance, _ethBeforeBalance); + ETHERFI_POOL.deposit{value: _ethAmount}(); + + setUint(setId, _ethAmount); + + _eventName = "LogDepositWeth(uint256,uint256,uint256)"; + _eventParam = abi.encode(_amount, getId, setId); + } + + /** + * @dev deposit ETH into Etherfi. + * @notice stake ETH in Etherfi, users receive eETH tokens on a 1:1 basis representing their staked ETH. + * @param amount The amount of ETH to deposit. (For max: `uint256(-1)`) + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of ETH deposited. + */ function deposit( uint256 amount, uint256 getId, @@ -26,7 +59,7 @@ contract EETHContract is Helpers, Basic, Events { _amount = _amount == type(uint256).max ? address(this).balance : _amount; - + ETHERFI_POOL.deposit{value: _amount}(); setUint(setId, _amount); diff --git a/test/mainnet/eeth/eeth.test.ts b/test/mainnet/eeth/eeth.test.ts index d562e2a..30f4bb2 100644 --- a/test/mainnet/eeth/eeth.test.ts +++ b/test/mainnet/eeth/eeth.test.ts @@ -56,6 +56,52 @@ describe("eETH Staking", function () { ethers.utils.parseEther("10") ); }); + it("Topup wETH into DSA wallet", async function () { + const wETHAddress = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"; + const IERC20ABI = [ + "function approve(address spender, uint256 amount) external returns (bool)", + "function balanceOf(address account) external view returns (uint256)", + "function transfer(address recipient, uint256 amount) external returns (bool)", + ]; + const wETHHolder = "0xF04a5cC80B1E94C69B48f5ee68a08CD2F09A7c3E"; + const amount = ethers.utils.parseEther("10"); + + const eETHContract = await ethers.getContractAt(IERC20ABI, wETHAddress); + await hre.network.provider.send("hardhat_setBalance", [ + wETHHolder, + "0x56BC75E2D63100000", + ]); + + console.log( + "Holder wETH Balance before topup:", + ethers.utils.formatEther(await eETHContract.balanceOf(wETHHolder)) + ); + console.log( + "Holder ETH Balance before topup:", + ethers.utils.formatEther(await ethers.provider.getBalance(wETHHolder)) + ); + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [wETHHolder], + }); + const eETHHolderSigner = await ethers.getSigner(wETHHolder); + await eETHContract + .connect(eETHHolderSigner) + .approve(dsaWallet0.address, amount); + await eETHContract + .connect(eETHHolderSigner) + .transfer(dsaWallet0.address, amount); + const balance = await eETHContract.balanceOf(dsaWallet0.address); + console.log( + "DSA wETH Balance after topup:", + ethers.utils.formatEther(balance) + ); + + await hre.network.provider.request({ + method: "hardhat_stopImpersonatingAccount", + params: [wETHHolder], + }); + }); }); describe("Main", function () { @@ -94,5 +140,41 @@ describe("eETH Staking", function () { ethers.utils.formatEther(finalBalance) ); }); + + it("Should deposit wETH into eETH", async function () { + const amount = ethers.utils.parseEther("1"); + const eETHTAddress = "0x35fA164735182de50811E8e2E824cFb9B6118ac2"; + const IERC20ABI = [ + "function approve(address spender, uint256 amount) external returns (bool)", + "function balanceOf(address account) external view returns (uint256)", + ]; + const eETHContract = await ethers.getContractAt(IERC20ABI, eETHTAddress); + + const initialBalance = await eETHContract.balanceOf(dsaWallet0.address); + console.log( + "eETH Balance before:", + ethers.utils.formatEther(initialBalance) + ); + + const spells = [ + { + connector: connectorName, + method: "depositWeth", + args: [amount, 0, 0], + }, + ]; + + const spellsEncoded = encodeSpells(spells); + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), await wallet1.getAddress()); + const receipt = await tx.wait(); + + const finalBalance = await eETHContract.balanceOf(dsaWallet0.address); + console.log( + "eETH Balance after:", + ethers.utils.formatEther(finalBalance) + ); + }); }); });