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

Sepolia deposit adapter implementation #821

Merged
merged 41 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e21b86a
Sepolia deposit contract (interface for tests)
vp4242 Feb 7, 2024
876a3b0
Add basic test for Sepolia deposit
vp4242 Feb 7, 2024
7e1d8e2
Put simple deployment test
vp4242 Feb 7, 2024
bf3540a
Basic deposit adapter implementation
vp4242 Feb 7, 2024
3e89caf
Some experiments over adapter tests
vp4242 Feb 8, 2024
d5474b3
Simplify adapter
vp4242 Feb 8, 2024
6bb847f
Split tests, fix style
vp4242 Feb 8, 2024
3eeafd1
Improve contract readability
vp4242 Feb 8, 2024
52df1f9
Remove Sepolia deposit contract code, change all interaction to inter…
vp4242 Feb 12, 2024
287e868
Fix warnings
vp4242 Feb 12, 2024
04d6b7f
Add immutable
vp4242 Feb 13, 2024
cabc5cd
Add Ownable mix to Deposit
vp4242 Feb 13, 2024
2b905b2
Implement drain Bepolia logic and send back ether to contract owner
vp4242 Feb 13, 2024
53861a2
Remove general drain func as it now send eth to owner automatically
vp4242 Feb 13, 2024
da8ea29
Improve adapter logic
vp4242 Feb 14, 2024
c60e04a
Improve tests
vp4242 Feb 14, 2024
90f4b03
Add owner logging
vp4242 Feb 14, 2024
f13cb41
Add auto drain to deposit() and tests around
vp4242 Feb 14, 2024
3ff2df1
Fix contract warning
vp4242 Feb 14, 2024
f122422
Fix deployment test
vp4242 Feb 14, 2024
3180ee5
Remove cruft
vp4242 Feb 14, 2024
b5193c1
More readable name
vp4242 Feb 15, 2024
d14eb54
Fix naming
vp4242 Feb 15, 2024
97db353
Fix test
vp4242 Feb 15, 2024
b752379
Skip test for local node testing
vp4242 Feb 15, 2024
80d018d
Provide address for drain
vp4242 Feb 15, 2024
04d4267
Comment on how run Sepolia-specific tests
vp4242 Feb 15, 2024
fb73385
Add chain id
vp4242 Feb 15, 2024
a5793ec
Fix contract
vp4242 Feb 15, 2024
15320be
Combine tests and fix them after renamings
vp4242 Feb 15, 2024
b334ea4
Update contracts/0.8.9/SepoliaDepositAdapter.sol
vp4242 Feb 20, 2024
401b886
Update contracts/0.8.9/SepoliaDepositAdapter.sol
vp4242 Feb 20, 2024
312d708
Update test/0.8.9/sepolia-deposit-adapter.test.js
vp4242 Feb 20, 2024
e82e9b3
Extract interfaces and improve adapter contract
vp4242 Feb 20, 2024
c916979
Fix tests for contract changes
vp4242 Feb 20, 2024
e3a089e
Suppress warnings
vp4242 Feb 20, 2024
c9d0887
Details on while Adapter is required
vp4242 Feb 20, 2024
a3e2982
Add DepositEvent and checks for it
vp4242 Feb 20, 2024
a802e43
Fix comment
vp4242 Feb 20, 2024
7536ba8
fix: update storage layout action
TheDZhon Feb 20, 2024
2480651
Emit event on Bepolia recover
vp4242 Feb 21, 2024
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
88 changes: 81 additions & 7 deletions contracts/0.8.9/SepoliaDepositAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,98 @@
/* See contracts/COMPILERS.md */
pragma solidity 0.8.9;

import "@openzeppelin/contracts-v4.4/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts-v4.4/access/Ownable.sol";

contract SepoliaDepositAdapter {

uint public constant TEST_VALUE = 16;
address public immutable depositContract;
interface IDepositContract {
event DepositEvent(
bytes pubkey,
bytes withdrawal_credentials,
bytes amount,
bytes signature,
bytes index
);

function deposit(
bytes calldata pubkey,
bytes calldata withdrawal_credentials,
bytes calldata signature,
bytes32 deposit_data_root
) external payable {
}
) external payable;

function get_deposit_root() external view returns (bytes32);

function get_deposit_count() external view returns (bytes memory);
}

// Sepolia deposit contract variant of the source code https://github.com/protolambda/testnet-dep-contract/blob/master/deposit_contract.sol
interface ISepoliaDepositContract is IDepositContract, IERC20 { }

// Sepolia testnet deposit contract have a bit different logic than the mainnet deposit contract.
// The differences are:
// 1. Sepolia contract require specific Bepolia token to be used for depositing. It burns this token after depositing.
// 2. It returns the ETH to the sender after depositing.
// This adapter is used to make the mainnet deposit contract compatible with the testnet deposit contract.
// For further information see Sepolia deposit contract variant source code link above.
contract SepoliaDepositAdapter is IDepositContract, Ownable {

event EthReceived(address sender, uint256 amount);

event EthRecovered(uint256 amount);

error EthRecoverFailed();

error BepoliaRecoverFailed();

error DepositFailed();

ISepoliaDepositContract public immutable originalContract;

constructor(address _deposit_contract) {
TheDZhon marked this conversation as resolved.
Show resolved Hide resolved
depositContract = _deposit_contract;
originalContract = ISepoliaDepositContract(_deposit_contract);
}

function get_deposit_root() override external view returns (bytes32) {
return originalContract.get_deposit_root();
}

function get_deposit_count() override external view returns (bytes memory) {
return originalContract.get_deposit_count();
}

receive() external payable {
emit EthReceived(msg.sender, msg.value);
}
TheDZhon marked this conversation as resolved.
Show resolved Hide resolved

function recoverEth() external onlyOwner {
TheDZhon marked this conversation as resolved.
Show resolved Hide resolved
uint256 balance = address(this).balance;
// solhint-disable-next-line avoid-low-level-calls
(bool success,) = owner().call{value: balance}("");
if (!success) {
revert EthRecoverFailed();
}
TheDZhon marked this conversation as resolved.
Show resolved Hide resolved
emit EthRecovered(balance);
}

function recoverBepolia() external onlyOwner {
uint256 bepoliaOwnTokens = originalContract.balanceOf(address(this));
bool success = originalContract.transfer(owner(), bepoliaOwnTokens);
TheDZhon marked this conversation as resolved.
Show resolved Hide resolved
if (!success) {
revert BepoliaRecoverFailed();
}
TheDZhon marked this conversation as resolved.
Show resolved Hide resolved
}

function deposit(
bytes calldata pubkey,
bytes calldata withdrawal_credentials,
bytes calldata signature,
bytes32 deposit_data_root
) override external payable {
originalContract.deposit{value: msg.value}(pubkey, withdrawal_credentials, signature, deposit_data_root);
// solhint-disable-next-line avoid-low-level-calls
(bool success,) = owner().call{value: msg.value}("");
if (!success) {
revert DepositFailed();
}
}
}
3 changes: 3 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ const getNetConfig = (networkName, ethAccountName) => {
if (networkName === 'hardhat' && process.env.HARDHAT_FORKING_URL) {
netConfig.forking = { url: process.env.HARDHAT_FORKING_URL }
}
if (networkName === 'hardhat' && process.env.HARDHAT_CHAIN_ID) {
netConfig.chainId = +process.env.HARDHAT_CHAIN_ID
}
return netConfig ? { [networkName]: netConfig } : {}
}

Expand Down
157 changes: 157 additions & 0 deletions test/0.8.9/sepolia-deposit-adapter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
const { contract, artifacts, ethers } = require('hardhat')
const { assert } = require('../helpers/assert')
const { ETH } = require('../helpers/utils')

const { EvmSnapshot } = require('../helpers/blockchain')

const SepoliaDepositAdapter = artifacts.require('SepoliaDepositAdapter')
const SepoliaDepositContract = artifacts.require('ISepoliaDepositContract')

// To run Sepolia Deposit Adapter tests:
// HARDHAT_FORKING_URL=<rpc url> HARDHAT_CHAIN_ID=11155111 npx hardhat test --grep "SepoliaDepositAdapter"
contract('SepoliaDepositAdapter', ([deployer]) => {
let depositAdapter
let snapshot
let bepoliaToken
const sepoliaDepositContractAddress = '0x7f02C3E3c98b133055B8B348B2Ac625669Ed295D'
const EOAddress = '0x6885E36BFcb68CB383DfE90023a462C03BCB2AE5'
const bepoliaTokenHolder = EOAddress
// const log = console.log
const log = () => {}

before('deploy lido with dao', async function () {
const { chainId } = await ethers.provider.getNetwork()
if (chainId !== 11155111) {
return this.skip()
}

depositAdapter = await SepoliaDepositAdapter.new(sepoliaDepositContractAddress)
log('depositAdapter address', depositAdapter.address)

bepoliaToken = await ethers.getContractAt('ISepoliaDepositContract', sepoliaDepositContractAddress)

const code = await ethers.provider.getCode(depositAdapter.address)
assert.notEqual(code, '0x')

snapshot = new EvmSnapshot(ethers.provider)
await snapshot.make()
})

afterEach(async () => {
await snapshot.rollback()
})

describe('SepoliaDepositAdapter Logic', () => {
TheDZhon marked this conversation as resolved.
Show resolved Hide resolved
TheDZhon marked this conversation as resolved.
Show resolved Hide resolved
it(`recover Bepolia tokens`, async () => {
const adapterAddr = depositAdapter.address
const BEPOLIA_TO_TRANSFER = 2
const bepoliaHolderInitialBalance = await bepoliaToken.balanceOf(bepoliaTokenHolder)
const impersonatedSigner = await ethers.getImpersonatedSigner(bepoliaTokenHolder)

log('bepoliaHolderInitialBalance', bepoliaHolderInitialBalance)
await bepoliaToken.connect(impersonatedSigner).transfer(adapterAddr, BEPOLIA_TO_TRANSFER)

assert.equals(await bepoliaToken.balanceOf(adapterAddr), BEPOLIA_TO_TRANSFER)

const bepoliaHolderEndBalance = await bepoliaToken.balanceOf(bepoliaTokenHolder)
assert.equals(bepoliaHolderEndBalance, bepoliaHolderInitialBalance - BEPOLIA_TO_TRANSFER)
log('bepoliaHolderEndBalance', bepoliaHolderEndBalance)

// Recover Bepolia tokens
await depositAdapter.recoverBepolia()

const bepoliaTokensOnAdapter = await bepoliaToken.balanceOf(adapterAddr)
assert.equals(bepoliaTokensOnAdapter, 0)

const [owner] = await ethers.getSigners()
const bepoliaTokenHolderEnd = await bepoliaToken.balanceOf(owner.address)
assert.equals(bepoliaTokenHolderEnd, BEPOLIA_TO_TRANSFER)
})

it(`call deposit on Adapter`, async () => {
const key = '0x90823dc2e5ab8a52a0b32883ea8451cbe4c921a42ce439f4fb306a90e9f267e463241da7274b6d44c2e4b95ddbcb0ad3'
const withdrawalCredentials = '0x005bfe00d82068a0c2a6687afaf969dad5a9c663cb492815a65d203885aaf993'
const sig =
'0x802899068eb4b37c95d46869947cac42b9c65b90fcb3fde3854c93ad5737800c01e9c82e174c8ed5cc18210bd60a94ea0082a850817b1dddd4096059b6846417b05094c59d3dd7f4028ed9dff395755f9905a88015b0ed200a7ec1ed60c24922'
const dataRoot = '0x8b09ed1d0fb3b8e3bb8398c6b77ee3d8e4f67c23cb70555167310ef02b06e5f5'

const adapterAddr = depositAdapter.address

const balance0ETH = await ethers.provider.getBalance(adapterAddr)
assert.equals(balance0ETH, 0)

const impersonatedSigner = await ethers.getImpersonatedSigner(bepoliaTokenHolder)
// Transfer 1 Bepolia token to depositCaller
await bepoliaToken.connect(impersonatedSigner).transfer(adapterAddr, 1)

const [owner] = await ethers.getSigners()
log('owner', owner.address)

const bepoliaTokenHolderBalance = await bepoliaToken.balanceOf(bepoliaTokenHolder)
const adapterBepoliaBalance = await bepoliaToken.balanceOf(adapterAddr)
log('bepoliaTokenHolder and adapter balances: ', bepoliaTokenHolderBalance, adapterBepoliaBalance)
// We need to have exactly 1 Bepolia token in the adapter
assert.equals(adapterBepoliaBalance, 1)

const depositRootBefore = await depositAdapter.get_deposit_root()
log('depositRoot', depositRootBefore)
const depositCountBefore = await depositAdapter.get_deposit_count()
log('depositCount', depositCountBefore)

const sepoliaDepositContract = await SepoliaDepositContract.at(sepoliaDepositContractAddress)

const receipt = await depositAdapter.deposit(key, withdrawalCredentials, sig, dataRoot, {
from: owner.address,
value: ETH(32),
})
assert.emits(receipt, 'EthReceived', { sender: sepoliaDepositContractAddress, amount: ETH(32) })
const depositEvents = await sepoliaDepositContract.getPastEvents('DepositEvent')
assert.equals(depositEvents.length, 1)
log('depositEvents', depositEvents, ETH(32))

assert.equals(depositEvents[0].args.pubkey, key)
assert.equals(depositEvents[0].args.withdrawal_credentials, withdrawalCredentials)
assert.equals(depositEvents[0].args.signature, sig)

const depositRootAfter = await depositAdapter.get_deposit_root()
log('depositRoot After', depositRootAfter)
const depositCountAfter = await depositAdapter.get_deposit_count()
log('depositCount After', depositCountAfter)
assert.notEqual(depositRootBefore, depositRootAfter)
assert.equals(BigInt(depositCountBefore) + BigInt('0x0100000000000000'), BigInt(depositCountAfter))

const ethAfterDeposit = await ethers.provider.getBalance(adapterAddr)
log('ethAfterDeposit', ethAfterDeposit.toString())
assert.equals(ethAfterDeposit, 0)

const adapterBepoliaBalanceAfter = await bepoliaToken.balanceOf(adapterAddr)
assert.equals(adapterBepoliaBalanceAfter, 0)
})

it(`recover ETH`, async () => {
const ETH_TO_TRANSFER = ETH(10)
const adapterAddr = depositAdapter.address

const balance0ETH = await ethers.provider.getBalance(adapterAddr)
assert.equals(balance0ETH, 0)

const [owner] = await ethers.getSigners()
log('owner', owner.address)
await owner.sendTransaction({
to: adapterAddr,
value: ETH_TO_TRANSFER,
})

const ethAfterDeposit = await ethers.provider.getBalance(adapterAddr)
log('ethAfterDeposit', ethAfterDeposit.toString())
assert.equals(ethAfterDeposit, ETH_TO_TRANSFER)

const receipt = await depositAdapter.recoverEth()
assert.emits(receipt, 'EthRecovered', { amount: ETH_TO_TRANSFER })

const balanceEthAfterRecover = await ethers.provider.getBalance(adapterAddr)
log('balanceEthAfterRecover', balanceEthAfterRecover.toString())
assert.equals(balanceEthAfterRecover, 0)
})
})
})
Loading