From de9956899a729452f249511f3202c96a651108c0 Mon Sep 17 00:00:00 2001 From: Samarendra Gouda Date: Mon, 15 Apr 2024 11:37:36 +0530 Subject: [PATCH 1/8] feat: added weeth contracts --- contracts/mainnet/connectors/weeth/events.sol | 7 +++ .../mainnet/connectors/weeth/helpers.sol | 10 +++ .../mainnet/connectors/weeth/interfaces.sol | 8 +++ contracts/mainnet/connectors/weeth/main.sol | 62 +++++++++++++++++++ test/mainnet/weeth/weeth.test.ts | 0 5 files changed, 87 insertions(+) create mode 100644 contracts/mainnet/connectors/weeth/events.sol create mode 100644 contracts/mainnet/connectors/weeth/helpers.sol create mode 100644 contracts/mainnet/connectors/weeth/interfaces.sol create mode 100644 contracts/mainnet/connectors/weeth/main.sol create mode 100644 test/mainnet/weeth/weeth.test.ts diff --git a/contracts/mainnet/connectors/weeth/events.sol b/contracts/mainnet/connectors/weeth/events.sol new file mode 100644 index 0000000..1b0b8c1 --- /dev/null +++ b/contracts/mainnet/connectors/weeth/events.sol @@ -0,0 +1,7 @@ +//SPDX-License-Identifier: MIT +pragma solidity 0.8.19; + +contract Events { + event LogDeposit(uint256 eETHAmount, uint256 weETHAmount, uint256 getId, uint256 setId); + event LogWithdraw(uint256 weETHAmount, uint256 eETHAmount, uint256 getId, uint256 setId); +} \ No newline at end of file diff --git a/contracts/mainnet/connectors/weeth/helpers.sol b/contracts/mainnet/connectors/weeth/helpers.sol new file mode 100644 index 0000000..3bc181e --- /dev/null +++ b/contracts/mainnet/connectors/weeth/helpers.sol @@ -0,0 +1,10 @@ +//SPDX-License-Identifier: MIT +pragma solidity 0.8.19; + +import "./interfaces.sol"; +import {TokenInterface} from "../../common/interfaces.sol"; + +contract Helpers{ + IWEETH constant weETHContract = IWEETH(0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee); + TokenInterface constant eETHContract = TokenInterface(0x35fA164735182de50811E8e2E824cFb9B6118ac2); +} diff --git a/contracts/mainnet/connectors/weeth/interfaces.sol b/contracts/mainnet/connectors/weeth/interfaces.sol new file mode 100644 index 0000000..896f333 --- /dev/null +++ b/contracts/mainnet/connectors/weeth/interfaces.sol @@ -0,0 +1,8 @@ +//SPDX-License-Identifier: MIT +pragma solidity 0.8.19; + +interface IWEETH{ + function balanceOf(address account) external view returns (uint256); + function wrap(uint256 _eETHAmount) external returns (uint256); + function unwrap(uint256 _weETHAmount) external returns (uint256); +} \ No newline at end of file diff --git a/contracts/mainnet/connectors/weeth/main.sol b/contracts/mainnet/connectors/weeth/main.sol new file mode 100644 index 0000000..c703d6e --- /dev/null +++ b/contracts/mainnet/connectors/weeth/main.sol @@ -0,0 +1,62 @@ +//SPDX-License-Identifier: MIT +pragma solidity 0.8.19; + +import "./helpers.sol"; +import {Basic} from "../../common/basic.sol"; + +contract WEETHContract is Helpers, Basic { + + /** + * @dev Deposit eETH into weETH. + * @notice Wrap eETH into weETH + * @param eETHAmount The amount of eETH to deposit. (For max: `uint256(-1)`) + * @param getId ID to retrieve eETH amount. + * @param setId ID stores the amount of weETH deposited. + */ + function deposit( + uint256 eETHAmount, + uint256 getId, + uint256 setId + ) external returns (string memory _eventName, bytes memory _eventParam) { + uint256 _eETHAmount = getUint(getId, eETHAmount); + _eETHAmount = _eETHAmount == type(uint256).max + ? eETHContract.balanceOf(address(this)) + : _eETHAmount; + + approve(eETHContract, address(weETHContract), _eETHAmount); + uint256 _weETHAmount = weETHContract.wrap(_eETHAmount); + + setUint(setId, _weETHAmount); + + _eventName = "LogDeposit(uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(_eETHAmount, _weETHAmount, getId, setId); + } + + /** + * @dev Withdraw eETH from weETH from Smart Account + * @notice Unwrap eETH from weETH + * @param weETHAmount The amount of weETH to withdraw. (For max: `uint256(-1)`) + * @param getId ID to retrieve weETH amount. + * @param setId ID stores the amount of eETH. + */ + function withdraw( + uint256 weETHAmount, + uint256 getId, + uint256 setId + ) external returns (string memory _eventName, bytes memory _eventParam) { + uint256 _weETHAmount = getUint(getId, weETHAmount); + _weETHAmount = _weETHAmount == type(uint256).max + ? weETHContract.balanceOf(address(this)) + : _weETHAmount; + + uint256 _eETHAmount = weETHContract.unwrap(_weETHAmount); + setUint(setId, _eETHAmount); + + _eventName = "LogWithdraw(uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(_weETHAmount, _eETHAmount, getId, setId); + } +} + +contract ConnectV2WEETH is WEETHContract { + string public constant name = "WEETH-v1.0"; +} diff --git a/test/mainnet/weeth/weeth.test.ts b/test/mainnet/weeth/weeth.test.ts new file mode 100644 index 0000000..e69de29 From f0a39003e5958e4a828d84ef51b1bf4c87a00051 Mon Sep 17 00:00:00 2001 From: Samarendra Gouda Date: Mon, 15 Apr 2024 14:11:37 +0530 Subject: [PATCH 2/8] fix: migrating old PR --- contracts/mainnet/connectors/eeth/events.sol | 6 + contracts/mainnet/connectors/eeth/helpers.sol | 8 + .../mainnet/connectors/eeth/interfaces.sol | 6 + contracts/mainnet/connectors/eeth/main.sol | 41 +++++ contracts/mainnet/connectors/weeth/events.sol | 2 +- .../mainnet/connectors/weeth/interfaces.sol | 2 +- contracts/mainnet/connectors/weeth/main.sol | 3 +- test/mainnet/eeth/eeth.test.ts | 98 ++++++++++ test/mainnet/weeth/weeth.test.ts | 167 ++++++++++++++++++ 9 files changed, 330 insertions(+), 3 deletions(-) create mode 100644 contracts/mainnet/connectors/eeth/events.sol create mode 100644 contracts/mainnet/connectors/eeth/helpers.sol create mode 100644 contracts/mainnet/connectors/eeth/interfaces.sol create mode 100644 contracts/mainnet/connectors/eeth/main.sol create mode 100644 test/mainnet/eeth/eeth.test.ts diff --git a/contracts/mainnet/connectors/eeth/events.sol b/contracts/mainnet/connectors/eeth/events.sol new file mode 100644 index 0000000..9843a4a --- /dev/null +++ b/contracts/mainnet/connectors/eeth/events.sol @@ -0,0 +1,6 @@ +//SPDX-License-Identifier: MIT +pragma solidity 0.8.19; + +contract Events { + event LogDeposit(uint256 amount, uint256 getId, uint256 setId); +} diff --git a/contracts/mainnet/connectors/eeth/helpers.sol b/contracts/mainnet/connectors/eeth/helpers.sol new file mode 100644 index 0000000..d020c84 --- /dev/null +++ b/contracts/mainnet/connectors/eeth/helpers.sol @@ -0,0 +1,8 @@ +//SPDX-License-Identifier: MIT +pragma solidity 0.8.19; + +import "./interfaces.sol"; + +contract Helpers{ + IEtherfiPool constant etherfiPool = IEtherfiPool(0x308861A430be4cce5502d0A12724771Fc6DaF216); +} diff --git a/contracts/mainnet/connectors/eeth/interfaces.sol b/contracts/mainnet/connectors/eeth/interfaces.sol new file mode 100644 index 0000000..2d8c78c --- /dev/null +++ b/contracts/mainnet/connectors/eeth/interfaces.sol @@ -0,0 +1,6 @@ +//SPDX-License-Identifier: MIT +pragma solidity 0.8.19; + +interface IEtherfiPool{ + function deposit() external payable returns (uint256); +} diff --git a/contracts/mainnet/connectors/eeth/main.sol b/contracts/mainnet/connectors/eeth/main.sol new file mode 100644 index 0000000..847355c --- /dev/null +++ b/contracts/mainnet/connectors/eeth/main.sol @@ -0,0 +1,41 @@ +//SPDX-License-Identifier: MIT +pragma solidity 0.8.19; + +import "./helpers.sol"; +import "./events.sol"; +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. + */ + function deposit( + uint256 amount, + uint256 getId, + uint256 setId + ) + public + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amount = getUint(getId, amount); + _amount = _amount == type(uint256).max + ? address(this).balance + : _amount; + + etherfiPool.deposit{value: _amount}(); + + setUint(setId, _amount); + + _eventName = "LogDeposit(uint256,uint256,uint256)"; + _eventParam = abi.encode(_amount, getId, setId); + } +} + +contract ConnectV2EETH is EETHContract { + string public name = "EETH-v1.0"; +} diff --git a/contracts/mainnet/connectors/weeth/events.sol b/contracts/mainnet/connectors/weeth/events.sol index 1b0b8c1..4e627a3 100644 --- a/contracts/mainnet/connectors/weeth/events.sol +++ b/contracts/mainnet/connectors/weeth/events.sol @@ -4,4 +4,4 @@ pragma solidity 0.8.19; contract Events { event LogDeposit(uint256 eETHAmount, uint256 weETHAmount, uint256 getId, uint256 setId); event LogWithdraw(uint256 weETHAmount, uint256 eETHAmount, uint256 getId, uint256 setId); -} \ No newline at end of file +} diff --git a/contracts/mainnet/connectors/weeth/interfaces.sol b/contracts/mainnet/connectors/weeth/interfaces.sol index 896f333..bb64e42 100644 --- a/contracts/mainnet/connectors/weeth/interfaces.sol +++ b/contracts/mainnet/connectors/weeth/interfaces.sol @@ -5,4 +5,4 @@ interface IWEETH{ function balanceOf(address account) external view returns (uint256); function wrap(uint256 _eETHAmount) external returns (uint256); function unwrap(uint256 _weETHAmount) external returns (uint256); -} \ No newline at end of file +} diff --git a/contracts/mainnet/connectors/weeth/main.sol b/contracts/mainnet/connectors/weeth/main.sol index c703d6e..a4074a0 100644 --- a/contracts/mainnet/connectors/weeth/main.sol +++ b/contracts/mainnet/connectors/weeth/main.sol @@ -2,9 +2,10 @@ pragma solidity 0.8.19; import "./helpers.sol"; +import "./events.sol"; import {Basic} from "../../common/basic.sol"; -contract WEETHContract is Helpers, Basic { +contract WEETHContract is Helpers, Basic, Events { /** * @dev Deposit eETH into weETH. diff --git a/test/mainnet/eeth/eeth.test.ts b/test/mainnet/eeth/eeth.test.ts new file mode 100644 index 0000000..d562e2a --- /dev/null +++ b/test/mainnet/eeth/eeth.test.ts @@ -0,0 +1,98 @@ +import hre from "hardhat"; +import { expect } from "chai"; +const { ethers } = hre; +import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector"; +import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"; +import { encodeSpells } from "../../../scripts/tests/encodeSpells"; +import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; +import { addresses } from "../../../scripts/tests/mainnet/addresses"; +import { abis } from "../../../scripts/constant/abis"; +import { ConnectV2EETH__factory } from "../../../typechain"; +import type { Signer, Contract } from "ethers"; + +describe("eETH Staking", function () { + const connectorName = "eETH-test"; + + let dsaWallet0: Contract; + let wallet0: Signer, wallet1: Signer; + let masterSigner: Signer; + let instaConnectorsV2: Contract; + let connector: Contract; + + before(async () => { + [wallet0, wallet1] = await ethers.getSigners(); + masterSigner = await getMasterSigner(); + instaConnectorsV2 = await ethers.getContractAt( + abis.core.connectorsV2, + addresses.core.connectorsV2 + ); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2EETH__factory, + signer: masterSigner, + connectors: instaConnectorsV2, + }); + console.log("Connector address", connector.address); + }); + + it("Should have contracts deployed.", async function () { + expect(!!instaConnectorsV2.address).to.be.true; + expect(!!connector.address).to.be.true; + expect(!!(await masterSigner.getAddress())).to.be.true; + }); + + describe("DSA wallet setup", function () { + it("Should build DSA v2", async function () { + dsaWallet0 = await buildDSAv2(await wallet0.getAddress()); + expect(!!dsaWallet0.address).to.be.true; + }); + + it("Deposit ETH into DSA wallet", async function () { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: ethers.utils.parseEther("10"), + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + ethers.utils.parseEther("10") + ); + }); + }); + + describe("Main", function () { + it("Should deposit ETH 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: "deposit", + 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) + ); + }); + }); +}); diff --git a/test/mainnet/weeth/weeth.test.ts b/test/mainnet/weeth/weeth.test.ts index e69de29..f3af88c 100644 --- a/test/mainnet/weeth/weeth.test.ts +++ b/test/mainnet/weeth/weeth.test.ts @@ -0,0 +1,167 @@ +import hre from "hardhat"; +import { expect } from "chai"; +const { ethers } = hre; +import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector"; +import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"; +import { encodeSpells } from "../../../scripts/tests/encodeSpells"; +import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; +import { addresses } from "../../../scripts/tests/mainnet/addresses"; +import { abis } from "../../../scripts/constant/abis"; +import { ConnectV2WEETH__factory } from "../../../typechain"; +import type { Signer, Contract } from "ethers"; + +describe("Wrapping / Unwrapping eETH", function () { + const connectorName = "weETH-test"; + + let dsaWallet0: Contract; + let wallet0: Signer, wallet1: Signer; + let masterSigner: Signer; + let instaConnectorsV2: Contract; + let connector: Contract; + + before(async () => { + [wallet0, wallet1] = await ethers.getSigners(); + masterSigner = await getMasterSigner(); + instaConnectorsV2 = await ethers.getContractAt( + abis.core.connectorsV2, + addresses.core.connectorsV2 + ); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2WEETH__factory, + signer: masterSigner, + connectors: instaConnectorsV2, + }); + console.log("Connector address", connector.address); + }); + + it("Should have contracts deployed.", async function () { + expect(!!instaConnectorsV2.address).to.be.true; + expect(!!connector.address).to.be.true; + expect(!!(await masterSigner.getAddress())).to.be.true; + }); + + describe("DSA wallet setup", function () { + it("Should build DSA v2", async function () { + dsaWallet0 = await buildDSAv2(await wallet0.getAddress()); + expect(!!dsaWallet0.address).to.be.true; + }); + + it("Deposit ETH into DSA wallet", async function () { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: ethers.utils.parseEther("10"), + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + ethers.utils.parseEther("10") + ); + }); + + it("Topup eETH into DSA wallet", async function () { + const eETHTAddress = "0x35fA164735182de50811E8e2E824cFb9B6118ac2"; + 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 eETHHolder = "0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee"; + const amount = ethers.utils.parseEther("10"); + + const eETHContract = await ethers.getContractAt(IERC20ABI, eETHTAddress); + await hre.network.provider.send("hardhat_setBalance", [ + eETHHolder, + "0x56BC75E2D63100000", + ]); + + console.log( + "Holder eETH Balance before topup:", + ethers.utils.formatEther(await eETHContract.balanceOf(eETHHolder)) + ); + console.log( + "holder ETH Balance before topup:", + ethers.utils.formatEther(await ethers.provider.getBalance(eETHHolder)) + ); + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [eETHHolder], + }); + const eETHHolderSigner = await ethers.getSigner(eETHHolder); + 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 eETH Balance after topup:", + ethers.utils.formatEther(balance) + ); + + await hre.network.provider.request({ + method: "hardhat_stopImpersonatingAccount", + params: [eETHHolder], + }); + }); + }); + describe("Main", function () { + it("Should wrap and unwrap eETH", async function () { + const eETHTAddress = "0x35fA164735182de50811E8e2E824cFb9B6118ac2"; + const weETHAddress = "0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee"; + const IERC20ABI = [ + "function balanceOf(address account) external view returns (uint256)", + ]; + const eETHContract = await ethers.getContractAt(IERC20ABI, eETHTAddress); + const weETHContract = await ethers.getContractAt(IERC20ABI, weETHAddress); + const amount = ethers.utils.parseEther("10"); + const initialBalance = await weETHContract.balanceOf(dsaWallet0.address); + console.log( + "weETH Balance before wrapping:", + ethers.utils.formatEther(initialBalance) + ); + console.log("Wrapping 10 eETH to weETH"); + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [amount, 0, 0], + }, + ]; + const spellsEncoded = encodeSpells(spells); + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...spellsEncoded, await wallet1.getAddress()); + const receipt = await tx.wait(); + const finalBalance = await weETHContract.balanceOf(dsaWallet0.address); + console.log( + "weETH Balance after wrapping:", + ethers.utils.formatEther(finalBalance) + ); + + const uint256Max = ethers.BigNumber.from(2).pow(256).sub(1); + const initialBalance2 = await eETHContract.balanceOf(dsaWallet0.address); + console.log( + "eETH Balance before unwrapping:", + ethers.utils.formatEther(initialBalance2) + ); + console.log("Unwrapping all eETH to ETH"); + const spells2 = [ + { + connector: connectorName, + method: "withdraw", + args: [uint256Max, 0, 0], + }, + ]; + const spellsEncoded2 = encodeSpells(spells2); + const tx2 = await dsaWallet0 + .connect(wallet0) + .cast(...spellsEncoded2, await wallet1.getAddress()); + const receipt2 = await tx2.wait(); + const finalBalance2 = await eETHContract.balanceOf(dsaWallet0.address); + console.log( + "eETH Balance after unwrapping:", + ethers.utils.formatEther(finalBalance2) + ); + }); + }); +}); From 6d4e7ce3676c62450bc93cbda6e293f5c333a66a Mon Sep 17 00:00:00 2001 From: Samarendra Gouda Date: Mon, 15 Apr 2024 14:30:09 +0530 Subject: [PATCH 3/8] fix: constant names --- contracts/mainnet/connectors/eeth/helpers.sol | 2 +- contracts/mainnet/connectors/eeth/main.sol | 2 +- contracts/mainnet/connectors/weeth/helpers.sol | 4 ++-- contracts/mainnet/connectors/weeth/main.sol | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/contracts/mainnet/connectors/eeth/helpers.sol b/contracts/mainnet/connectors/eeth/helpers.sol index d020c84..fc174ed 100644 --- a/contracts/mainnet/connectors/eeth/helpers.sol +++ b/contracts/mainnet/connectors/eeth/helpers.sol @@ -4,5 +4,5 @@ pragma solidity 0.8.19; import "./interfaces.sol"; contract Helpers{ - IEtherfiPool constant etherfiPool = IEtherfiPool(0x308861A430be4cce5502d0A12724771Fc6DaF216); + IEtherfiPool constant ETHERFI_POOL = IEtherfiPool(0x308861A430be4cce5502d0A12724771Fc6DaF216); } diff --git a/contracts/mainnet/connectors/eeth/main.sol b/contracts/mainnet/connectors/eeth/main.sol index 847355c..2d17e7a 100644 --- a/contracts/mainnet/connectors/eeth/main.sol +++ b/contracts/mainnet/connectors/eeth/main.sol @@ -27,7 +27,7 @@ contract EETHContract is Helpers, Basic, Events { ? address(this).balance : _amount; - etherfiPool.deposit{value: _amount}(); + ETHERFI_POOL.deposit{value: _amount}(); setUint(setId, _amount); diff --git a/contracts/mainnet/connectors/weeth/helpers.sol b/contracts/mainnet/connectors/weeth/helpers.sol index 3bc181e..57da4ba 100644 --- a/contracts/mainnet/connectors/weeth/helpers.sol +++ b/contracts/mainnet/connectors/weeth/helpers.sol @@ -5,6 +5,6 @@ import "./interfaces.sol"; import {TokenInterface} from "../../common/interfaces.sol"; contract Helpers{ - IWEETH constant weETHContract = IWEETH(0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee); - TokenInterface constant eETHContract = TokenInterface(0x35fA164735182de50811E8e2E824cFb9B6118ac2); + IWEETH constant WEETH_CONTRACT = IWEETH(0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee); + TokenInterface constant EETH_CONTRACT = TokenInterface(0x35fA164735182de50811E8e2E824cFb9B6118ac2); } diff --git a/contracts/mainnet/connectors/weeth/main.sol b/contracts/mainnet/connectors/weeth/main.sol index a4074a0..4686e12 100644 --- a/contracts/mainnet/connectors/weeth/main.sol +++ b/contracts/mainnet/connectors/weeth/main.sol @@ -21,11 +21,11 @@ contract WEETHContract is Helpers, Basic, Events { ) external returns (string memory _eventName, bytes memory _eventParam) { uint256 _eETHAmount = getUint(getId, eETHAmount); _eETHAmount = _eETHAmount == type(uint256).max - ? eETHContract.balanceOf(address(this)) + ? EETH_CONTRACT.balanceOf(address(this)) : _eETHAmount; - approve(eETHContract, address(weETHContract), _eETHAmount); - uint256 _weETHAmount = weETHContract.wrap(_eETHAmount); + approve(EETH_CONTRACT, address(WEETH_CONTRACT), _eETHAmount); + uint256 _weETHAmount = WEETH_CONTRACT.wrap(_eETHAmount); setUint(setId, _weETHAmount); @@ -47,10 +47,10 @@ contract WEETHContract is Helpers, Basic, Events { ) external returns (string memory _eventName, bytes memory _eventParam) { uint256 _weETHAmount = getUint(getId, weETHAmount); _weETHAmount = _weETHAmount == type(uint256).max - ? weETHContract.balanceOf(address(this)) + ? WEETH_CONTRACT.balanceOf(address(this)) : _weETHAmount; - uint256 _eETHAmount = weETHContract.unwrap(_weETHAmount); + uint256 _eETHAmount = WEETH_CONTRACT.unwrap(_weETHAmount); setUint(setId, _eETHAmount); _eventName = "LogWithdraw(uint256,uint256,uint256,uint256)"; From 29081d78848858f45a32044c049d8e414c10ca8d Mon Sep 17 00:00:00 2001 From: Samarendra Gouda Date: Mon, 15 Apr 2024 14:37:48 +0530 Subject: [PATCH 4/8] fix: visibility --- contracts/mainnet/connectors/eeth/helpers.sol | 2 +- contracts/mainnet/connectors/weeth/helpers.sol | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/contracts/mainnet/connectors/eeth/helpers.sol b/contracts/mainnet/connectors/eeth/helpers.sol index fc174ed..f770191 100644 --- a/contracts/mainnet/connectors/eeth/helpers.sol +++ b/contracts/mainnet/connectors/eeth/helpers.sol @@ -4,5 +4,5 @@ pragma solidity 0.8.19; import "./interfaces.sol"; contract Helpers{ - IEtherfiPool constant ETHERFI_POOL = IEtherfiPool(0x308861A430be4cce5502d0A12724771Fc6DaF216); + IEtherfiPool internal constant ETHERFI_POOL = IEtherfiPool(0x308861A430be4cce5502d0A12724771Fc6DaF216); } diff --git a/contracts/mainnet/connectors/weeth/helpers.sol b/contracts/mainnet/connectors/weeth/helpers.sol index 57da4ba..75a0818 100644 --- a/contracts/mainnet/connectors/weeth/helpers.sol +++ b/contracts/mainnet/connectors/weeth/helpers.sol @@ -5,6 +5,6 @@ import "./interfaces.sol"; import {TokenInterface} from "../../common/interfaces.sol"; contract Helpers{ - IWEETH constant WEETH_CONTRACT = IWEETH(0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee); - TokenInterface constant EETH_CONTRACT = TokenInterface(0x35fA164735182de50811E8e2E824cFb9B6118ac2); + IWEETH internal constant WEETH_CONTRACT = IWEETH(0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee); + TokenInterface internal constant EETH_CONTRACT = TokenInterface(0x35fA164735182de50811E8e2E824cFb9B6118ac2); } From 75568b48c2d699b5ee6f0992da851102302170ca Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Mon, 15 Apr 2024 13:34:15 +0400 Subject: [PATCH 5/8] feat: prettier --- contracts/mainnet/connectors/weeth/events.sol | 14 ++++++++++++-- contracts/mainnet/connectors/weeth/helpers.sol | 9 ++++++--- contracts/mainnet/connectors/weeth/interfaces.sol | 2 +- contracts/mainnet/connectors/weeth/main.sol | 1 - 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/contracts/mainnet/connectors/weeth/events.sol b/contracts/mainnet/connectors/weeth/events.sol index 4e627a3..10640c0 100644 --- a/contracts/mainnet/connectors/weeth/events.sol +++ b/contracts/mainnet/connectors/weeth/events.sol @@ -2,6 +2,16 @@ pragma solidity 0.8.19; contract Events { - event LogDeposit(uint256 eETHAmount, uint256 weETHAmount, uint256 getId, uint256 setId); - event LogWithdraw(uint256 weETHAmount, uint256 eETHAmount, uint256 getId, uint256 setId); + event LogDeposit( + uint256 eETHAmount, + uint256 weETHAmount, + uint256 getId, + uint256 setId + ); + event LogWithdraw( + uint256 weETHAmount, + uint256 eETHAmount, + uint256 getId, + uint256 setId + ); } diff --git a/contracts/mainnet/connectors/weeth/helpers.sol b/contracts/mainnet/connectors/weeth/helpers.sol index 75a0818..efb44b0 100644 --- a/contracts/mainnet/connectors/weeth/helpers.sol +++ b/contracts/mainnet/connectors/weeth/helpers.sol @@ -4,7 +4,10 @@ pragma solidity 0.8.19; import "./interfaces.sol"; import {TokenInterface} from "../../common/interfaces.sol"; -contract Helpers{ - IWEETH internal constant WEETH_CONTRACT = IWEETH(0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee); - TokenInterface internal constant EETH_CONTRACT = TokenInterface(0x35fA164735182de50811E8e2E824cFb9B6118ac2); +contract Helpers { + IWEETH internal constant WEETH_CONTRACT = + IWEETH(0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee); + + TokenInterface internal constant EETH_CONTRACT = + TokenInterface(0x35fA164735182de50811E8e2E824cFb9B6118ac2); } diff --git a/contracts/mainnet/connectors/weeth/interfaces.sol b/contracts/mainnet/connectors/weeth/interfaces.sol index bb64e42..52a3d7a 100644 --- a/contracts/mainnet/connectors/weeth/interfaces.sol +++ b/contracts/mainnet/connectors/weeth/interfaces.sol @@ -1,7 +1,7 @@ //SPDX-License-Identifier: MIT pragma solidity 0.8.19; -interface IWEETH{ +interface IWEETH { function balanceOf(address account) external view returns (uint256); function wrap(uint256 _eETHAmount) external returns (uint256); function unwrap(uint256 _weETHAmount) external returns (uint256); diff --git a/contracts/mainnet/connectors/weeth/main.sol b/contracts/mainnet/connectors/weeth/main.sol index 4686e12..6e35882 100644 --- a/contracts/mainnet/connectors/weeth/main.sol +++ b/contracts/mainnet/connectors/weeth/main.sol @@ -6,7 +6,6 @@ import "./events.sol"; import {Basic} from "../../common/basic.sol"; contract WEETHContract is Helpers, Basic, Events { - /** * @dev Deposit eETH into weETH. * @notice Wrap eETH into weETH From 5a152e1d4a0ba8847ac8d117d9bfa176e1bb7264 Mon Sep 17 00:00:00 2001 From: Samarendra Gouda Date: Mon, 15 Apr 2024 15:24:44 +0530 Subject: [PATCH 6/8] 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) + ); + }); }); }); From 4d5fdc23bf2e73346e75d9d8e90704063b564887 Mon Sep 17 00:00:00 2001 From: Samarendra Gouda Date: Mon, 15 Apr 2024 15:32:31 +0530 Subject: [PATCH 7/8] fix: remove-approval --- contracts/mainnet/connectors/eeth/main.sol | 3 --- test/mainnet/eeth/eeth.test.ts | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/contracts/mainnet/connectors/eeth/main.sol b/contracts/mainnet/connectors/eeth/main.sol index 5193281..3cec2fd 100644 --- a/contracts/mainnet/connectors/eeth/main.sol +++ b/contracts/mainnet/connectors/eeth/main.sol @@ -24,10 +24,7 @@ contract EETHContract is Helpers, Basic, Events { : _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); diff --git a/test/mainnet/eeth/eeth.test.ts b/test/mainnet/eeth/eeth.test.ts index 30f4bb2..8380c76 100644 --- a/test/mainnet/eeth/eeth.test.ts +++ b/test/mainnet/eeth/eeth.test.ts @@ -142,7 +142,7 @@ describe("eETH Staking", function () { }); it("Should deposit wETH into eETH", async function () { - const amount = ethers.utils.parseEther("1"); + const amount = ethers.utils.parseEther("10"); const eETHTAddress = "0x35fA164735182de50811E8e2E824cFb9B6118ac2"; const IERC20ABI = [ "function approve(address spender, uint256 amount) external returns (bool)", From 88b2da2d9f03945b77d7b757a57b1451486570d1 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Mon, 15 Apr 2024 15:22:20 +0400 Subject: [PATCH 8/8] feat: prettier --- contracts/mainnet/connectors/eeth/helpers.sol | 8 +++++--- contracts/mainnet/connectors/eeth/interfaces.sol | 2 +- contracts/mainnet/connectors/eeth/main.sol | 8 ++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/contracts/mainnet/connectors/eeth/helpers.sol b/contracts/mainnet/connectors/eeth/helpers.sol index a643bb5..4dce4e7 100644 --- a/contracts/mainnet/connectors/eeth/helpers.sol +++ b/contracts/mainnet/connectors/eeth/helpers.sol @@ -4,7 +4,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); +contract Helpers { + IEtherfiPool internal constant ETHERFI_POOL = + IEtherfiPool(0x308861A430be4cce5502d0A12724771Fc6DaF216); + TokenInterface internal constant WETH_CONTRACT = + TokenInterface(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); } diff --git a/contracts/mainnet/connectors/eeth/interfaces.sol b/contracts/mainnet/connectors/eeth/interfaces.sol index 2d8c78c..ced7a29 100644 --- a/contracts/mainnet/connectors/eeth/interfaces.sol +++ b/contracts/mainnet/connectors/eeth/interfaces.sol @@ -1,6 +1,6 @@ //SPDX-License-Identifier: MIT pragma solidity 0.8.19; -interface IEtherfiPool{ +interface IEtherfiPool { function deposit() external payable returns (uint256); } diff --git a/contracts/mainnet/connectors/eeth/main.sol b/contracts/mainnet/connectors/eeth/main.sol index 3cec2fd..d07fe87 100644 --- a/contracts/mainnet/connectors/eeth/main.sol +++ b/contracts/mainnet/connectors/eeth/main.sol @@ -9,16 +9,16 @@ contract EETHContract is Helpers, Basic, Events { /** * @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 wethAmount The amount of wETH 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 wethAmount, uint256 getId, uint256 setId ) public returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amount = getUint(getId, amount); + uint256 _amount = getUint(getId, wethAmount); _amount = _amount == type(uint256).max ? WETH_CONTRACT.balanceOf(address(this)) : _amount; @@ -27,7 +27,7 @@ contract EETHContract is Helpers, Basic, Events { WETH_CONTRACT.withdraw(_amount); uint256 _ethAfterBalance = address(this).balance; - uint256 _ethAmount = sub(_ethAfterBalance, _ethBeforeBalance); + uint256 _ethAmount = _ethAfterBalance - _ethBeforeBalance; ETHERFI_POOL.deposit{value: _ethAmount}(); setUint(setId, _ethAmount);