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

[WIP] Add Rebasable token to Optimism #58

Draft
wants to merge 65 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
3256d9f
create new contract that represents stETH on L2
kovalgek Oct 10, 2023
e4b2cbd
add wrap/unwrap functions
kovalgek Oct 11, 2023
82038ea
add shares to rebasable token
kovalgek Oct 13, 2023
380caaa
add unit tests
kovalgek Oct 17, 2023
eab8718
add deposit flow for new rebasable token
kovalgek Nov 4, 2023
c3bc0db
add withdraw flow for rebasable token on optimism
kovalgek Nov 5, 2023
eb98d0b
send rate in data and time during deposit flow
kovalgek Nov 9, 2023
800e33f
add possibility to depost 0 tokens
kovalgek Nov 9, 2023
d9ac556
init structs
kovalgek Nov 9, 2023
ddb9ca3
add comments
kovalgek Dec 11, 2023
a73bcf4
add gas test
kovalgek Dec 11, 2023
6d37edc
update gas test
kovalgek Dec 12, 2023
8098fe1
fix tests
kovalgek Dec 16, 2023
3335339
add token rate oracle
kovalgek Dec 20, 2023
86037dc
add tests for new token, renaming
kovalgek Dec 24, 2023
86788b2
simplify oracle, remove warnings
kovalgek Dec 27, 2023
ddffe1f
update unit tests for token rate oracle
kovalgek Dec 28, 2023
7c79d2d
fix integration tests for rebasable token
kovalgek Jan 9, 2024
6174943
Merge branch 'main' into feature/add_stETH_token_to_optimism
kovalgek Jan 9, 2024
f0b891a
add test for push token rate method
kovalgek Jan 9, 2024
e314bb1
unused return values fix
kovalgek Jan 9, 2024
4b56ccf
fix deployment scripts for new oracle and token
kovalgek Jan 9, 2024
3b82da7
add hearbeat to oracle
kovalgek Jan 10, 2024
171db5a
pr fixes from first rount review: apply unstructured storage in token…
kovalgek Jan 22, 2024
76b4ff3
PR fixes: fix comments, rename interface, abstract wst
kovalgek Jan 29, 2024
7abf60f
use real implemnations of non rebasable token and oracle contracts in…
kovalgek Feb 2, 2024
707da9b
update unit tests for bridges
kovalgek Feb 8, 2024
1cb344a
fix evetns + formating
kovalgek Feb 13, 2024
f9463f0
Merge branch 'main' into feature/add_stETH_token_to_optimism
kovalgek Feb 29, 2024
3fd1e94
move token rate to the base contract
kovalgek Mar 6, 2024
66b2501
use inheritance for token rate in l1 bridge, fix e2e tests
kovalgek Mar 6, 2024
7badd9e
add rebasable token e2e tests
kovalgek Mar 6, 2024
72bc8f3
add bridging-to e2e tests for rebasable token
kovalgek Mar 6, 2024
83e292b
add token rate observer
kovalgek Mar 7, 2024
dbf2f92
add observers array
kovalgek Mar 14, 2024
47c50c6
add factory and tests
kovalgek Mar 18, 2024
7e45410
add unit tests for notifier
kovalgek Mar 19, 2024
55111b8
add token rate pusher
kovalgek Mar 22, 2024
d99a52f
update unit tests for token rate oracle
kovalgek Mar 22, 2024
fb3eb40
remove observer array
kovalgek Mar 22, 2024
b167ebd
fix token rate publicher and notifier unit tests
kovalgek Mar 22, 2024
55d2e3f
fix small comments from PR review
kovalgek Mar 22, 2024
13712ca
feat(steth): intermediate work on adding ERC-2612/EIP-1271 permit
arwer13 Mar 27, 2024
6a46704
update optimism sdk version to fix integration tests
kovalgek Mar 29, 2024
b9d5e56
update addresses to run e2e tests
kovalgek Mar 29, 2024
a3f4544
fix integration test
kovalgek Mar 29, 2024
e2c29e0
deployment scripts
kovalgek Apr 1, 2024
d28d53d
Merge branch 'feature/add_stETH_token_to_optimism' into feat/steth-pe…
arwer13 Apr 1, 2024
0d2df1d
add token rate oracle integration tests
kovalgek Apr 1, 2024
8dc246e
fix happy path test for permit for rebasable
arwer13 Apr 2, 2024
ba0b41e
test(rebasable permit): move the rest tests from core stethpermit.tes…
arwer13 Apr 2, 2024
18a1dc5
add e2e tests for oracle
kovalgek Apr 2, 2024
624219c
Merge pull request #74 from lidofinance/feat/steth-permit
kovalgek Apr 2, 2024
5f86091
remove increaseAllowance and decreaseAllowance from tokens
kovalgek Apr 3, 2024
cc868ef
change function names
kovalgek Apr 3, 2024
06d00c7
rename stETH to rebasable token in env
kovalgek Apr 3, 2024
0b22b0f
deploy all script, refactor other deploy scripts
kovalgek Apr 8, 2024
f563fab
add permit to non-rebasable token
kovalgek Apr 9, 2024
3332e30
add unit tests for non-rebasable token
kovalgek Apr 10, 2024
0545e33
PR fixes: fix comments, readme, rename bridges, notifier refactoring
kovalgek Apr 10, 2024
a55b5f0
use mapping for tokens in bridges
kovalgek Apr 15, 2024
f54eecd
fix bridge unit and integration tests, remove mapping in storing toke…
kovalgek Apr 15, 2024
bc1c56f
fix scripts
kovalgek Apr 15, 2024
b62fbee
PR fixes: add comments, rename constracts, more interfaces to contrac…
kovalgek Apr 15, 2024
f387de6
PR fixes: rename tokens, add sanity check to oracle update, add unit …
kovalgek Apr 16, 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
14 changes: 11 additions & 3 deletions contracts/BridgeableTokens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,37 @@
/// @notice Contains the logic for validation of tokens used in the bridging process
contract BridgeableTokens {
/// @notice Address of the bridged token in the L1 chain
address public immutable l1Token;

Check warning on line 10 in contracts/BridgeableTokens.sol

View workflow job for this annotation

GitHub Actions / solhint

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @notice Address of the bridged rebasable token in the L1 chain
address public immutable l1TokenRebasable;

Check warning on line 13 in contracts/BridgeableTokens.sol

View workflow job for this annotation

GitHub Actions / solhint

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @notice Address of the token minted on the L2 chain when token bridged
address public immutable l2Token;

Check warning on line 16 in contracts/BridgeableTokens.sol

View workflow job for this annotation

GitHub Actions / solhint

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @notice Address of the rebasable token minted on the L2 chain when token bridged
address public immutable l2TokenRebasable;

Check warning on line 19 in contracts/BridgeableTokens.sol

View workflow job for this annotation

GitHub Actions / solhint

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @param l1Token_ Address of the bridged token in the L1 chain
/// @param l2Token_ Address of the token minted on the L2 chain when token bridged
constructor(address l1Token_, address l2Token_) {
constructor(address l1Token_, address l1TokenRebasable_, address l2Token_, address l2TokenRebasable_) {
l1Token = l1Token_;
l1TokenRebasable = l1TokenRebasable_;
l2Token = l2Token_;
l2TokenRebasable = l2TokenRebasable_;
}

/// @dev Validates that passed l1Token_ is supported by the bridge
modifier onlySupportedL1Token(address l1Token_) {
if (l1Token_ != l1Token) {
if (l1Token_ != l1Token && l1Token_ != l1TokenRebasable) {
revert ErrorUnsupportedL1Token();
}
_;
}

/// @dev Validates that passed l2Token_ is supported by the bridge
modifier onlySupportedL2Token(address l2Token_) {
if (l2Token_ != l2Token) {
if (l2Token_ != l2Token && l2Token_ != l2TokenRebasable) {
revert ErrorUnsupportedL2Token();
}
_;
Expand Down
8 changes: 6 additions & 2 deletions contracts/arbitrum/InterchainERC20TokenGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@
IInterchainTokenGateway
{
/// @notice Address of the router in the corresponding chain
address public immutable router;

Check warning on line 20 in contracts/arbitrum/InterchainERC20TokenGateway.sol

View workflow job for this annotation

GitHub Actions / solhint

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @inheritdoc IInterchainTokenGateway
address public immutable counterpartGateway;

Check warning on line 23 in contracts/arbitrum/InterchainERC20TokenGateway.sol

View workflow job for this annotation

GitHub Actions / solhint

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @param router_ Address of the router in the corresponding chain
/// @param counterpartGateway_ Address of the counterpart gateway used in the bridging process
/// @param l1Token_ Address of the bridged token in the Ethereum chain
/// @param l1TokenRebasable_ Address of the bridged token in the Ethereum chain
/// @param l2Token_ Address of the token minted on the Arbitrum chain when token bridged
/// @param l2TokenRebasable_ Address of the token minted on the Arbitrum chain when token bridged
constructor(
address router_,
address counterpartGateway_,
address l1Token_,
address l2Token_
) BridgeableTokens(l1Token_, l2Token_) {
address l1TokenRebasable_,
address l2Token_,
address l2TokenRebasable_
) BridgeableTokens(l1Token_, l1TokenRebasable_, l2Token_, l2TokenRebasable_) {
router = router_;
counterpartGateway = counterpartGateway_;
}
Expand Down
8 changes: 6 additions & 2 deletions contracts/arbitrum/L1ERC20TokenGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ contract L1ERC20TokenGateway is
address router_,
address counterpartGateway_,
address l1Token_,
address l2Token_
address l1TokenRebasable_,
address l2Token_,
address l2TokenRebasable_
)
InterchainERC20TokenGateway(
router_,
counterpartGateway_,
l1Token_,
l2Token_
l1TokenRebasable_,
l2Token_,
l2TokenRebasable_
)
L1CrossDomainEnabled(inbox_)
{}
Expand Down
9 changes: 7 additions & 2 deletions contracts/arbitrum/L2ERC20TokenGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@ contract L2ERC20TokenGateway is
/// @param router_ Address of the router in the L2 chain
/// @param counterpartGateway_ Address of the counterpart L1 gateway
/// @param l1Token_ Address of the bridged token in the L1 chain
/// @param l1TokenRebasable_ Address of the bridged token in the L1 chain
/// @param l2Token_ Address of the token minted on the Arbitrum chain when token bridged
constructor(
address arbSys_,
address router_,
address counterpartGateway_,
address l1Token_,
address l2Token_
address l1TokenRebasable_,
address l2Token_,
address l2TokenRebasable_
)
InterchainERC20TokenGateway(
router_,
counterpartGateway_,
l1Token_,
l2Token_
l1TokenRebasable_,
l2Token_,
l2TokenRebasable_
)
L2CrossDomainEnabled(arbSys_)
{}
Expand Down
37 changes: 29 additions & 8 deletions contracts/optimism/L1ERC20TokenBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import {BridgeableTokens} from "../BridgeableTokens.sol";
import {CrossDomainEnabled} from "./CrossDomainEnabled.sol";

import {IERC20Wrapable} from "../token/interfaces/IERC20Wrapable.sol";
kovalgek marked this conversation as resolved.
Show resolved Hide resolved
import "hardhat/console.sol";

Check failure on line 18 in contracts/optimism/L1ERC20TokenBridge.sol

View workflow job for this annotation

GitHub Actions / solhint

Unexpected import of console file

/// @author psirex
/// @notice The L1 ERC20 token bridge locks bridged tokens on the L1 side, sends deposit messages
/// on the L2 side, and finalizes token withdrawals from L2. Additionally, adds the methods for
Expand All @@ -32,19 +35,22 @@
/// @param messenger_ L1 messenger address being used for cross-chain communications
/// @param l2TokenBridge_ Address of the corresponding L2 bridge
/// @param l1Token_ Address of the bridged token in the L1 chain
/// @param l1TokenRebasable_ Address of the bridged token in the L1 chain
/// @param l2Token_ Address of the token minted on the L2 chain when token bridged
constructor(
address messenger_,
address l2TokenBridge_,
address l1Token_,
address l2Token_
) CrossDomainEnabled(messenger_) BridgeableTokens(l1Token_, l2Token_) {
address l1Token_, // wstETH
address l1TokenRebasable_, // stETH
address l2Token_,
address l2TokenRebasable_
) CrossDomainEnabled(messenger_) BridgeableTokens(l1Token_, l1TokenRebasable_, l2Token_, l2TokenRebasable_) {
l2TokenBridge = l2TokenBridge_;
}

/// @inheritdoc IL1ERC20Bridge
function depositERC20(
address l1Token_,
address l1Token_, // stETH or wstETH
address l2Token_,
uint256 amount_,
uint32 l2Gas_,
Expand All @@ -54,11 +60,26 @@
whenDepositsEnabled
onlySupportedL1Token(l1Token_)
onlySupportedL2Token(l2Token_)
{
{
if (Address.isContract(msg.sender)) {
revert ErrorSenderNotEOA();
}
_initiateERC20Deposit(msg.sender, msg.sender, amount_, l2Gas_, data_);

if(l1Token_ == l1TokenRebasable) {
bytes memory data = bytes.concat(hex'01', data_); // add rate
IERC20(l1TokenRebasable).safeTransferFrom(msg.sender, address(this), amount_);
IERC20(l1TokenRebasable).approve(l1Token, amount_);
uint256 wstETHAmount = IERC20Wrapable(l1Token).wrap(amount_);
console.log("wstETHAmount=",wstETHAmount);

Check failure on line 73 in contracts/optimism/L1ERC20TokenBridge.sol

View workflow job for this annotation

GitHub Actions / solhint

Unexpected console statement

uint256 balanceOfl1Token = IERC20(l1Token).balanceOf(address(this));
console.log("balanceOfl1Token=",balanceOfl1Token);

Check failure on line 76 in contracts/optimism/L1ERC20TokenBridge.sol

View workflow job for this annotation

GitHub Actions / solhint

Unexpected console statement

_initiateERC20Deposit(msg.sender, msg.sender, wstETHAmount, l2Gas_, data);
} else {
IERC20(l1Token).safeTransferFrom(msg.sender, address(this), amount_);
_initiateERC20Deposit(msg.sender, msg.sender, amount_, l2Gas_, data_);
}
}

/// @inheritdoc IL1ERC20Bridge
Expand Down Expand Up @@ -120,9 +141,8 @@
address to_,
uint256 amount_,
uint32 l2Gas_,
bytes calldata data_
bytes memory data_
) internal {
IERC20(l1Token).safeTransferFrom(from_, address(this), amount_);

bytes memory message = abi.encodeWithSelector(
IL2ERC20Bridge.finalizeDeposit.selector,
Expand All @@ -133,6 +153,7 @@
amount_,
data_
);
console.log("SEND l2TokenBridge=%s l1Token=%s l2Token=%s", l2TokenBridge, l1Token, l2Token);

Check failure on line 156 in contracts/optimism/L1ERC20TokenBridge.sol

View workflow job for this annotation

GitHub Actions / solhint

Unexpected console statement

sendCrossDomainMessage(l2TokenBridge, l2Gas_, message);

Expand Down
26 changes: 22 additions & 4 deletions contracts/optimism/L2ERC20TokenBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import {IL1ERC20Bridge} from "./interfaces/IL1ERC20Bridge.sol";
import {IL2ERC20Bridge} from "./interfaces/IL2ERC20Bridge.sol";
import {IERC20Bridged} from "../token/interfaces/IERC20Bridged.sol";
import {ERC20Rebasable} from "../token/ERC20Rebasable.sol";

import {BridgingManager} from "../BridgingManager.sol";
import {BridgeableTokens} from "../BridgeableTokens.sol";
import {CrossDomainEnabled} from "./CrossDomainEnabled.sol";

import { console } from "hardhat/console.sol";

Check failure on line 15 in contracts/optimism/L2ERC20TokenBridge.sol

View workflow job for this annotation

GitHub Actions / solhint

Unexpected import of console file

/// @author psirex
/// @notice The L2 token bridge works with the L1 token bridge to enable ERC20 token bridging
/// between L1 and L2. It acts as a minter for new tokens when it hears about
Expand All @@ -34,8 +37,10 @@
address messenger_,
address l1TokenBridge_,
address l1Token_,
address l2Token_
) CrossDomainEnabled(messenger_) BridgeableTokens(l1Token_, l2Token_) {
address l1TokenRebasable_,
address l2Token_,
address l2TokenRebasable_
) CrossDomainEnabled(messenger_) BridgeableTokens(l1Token_, l1TokenRebasable_, l2Token_, l2TokenRebasable_) {
l1TokenBridge = l1TokenBridge_;
}

Expand Down Expand Up @@ -75,8 +80,21 @@
onlySupportedL2Token(l2Token_)
onlyFromCrossDomainAccount(l1TokenBridge)
{
IERC20Bridged(l2Token_).bridgeMint(to_, amount_);
emit DepositFinalized(l1Token_, l2Token_, from_, to_, amount_, data_);
if (data_.length > 0 && data_[0] == hex'01') {
console.log("finalizeDeposit1 l2TokenRebasable balanceBefore=",ERC20Rebasable(l2TokenRebasable).balanceOf(to_));

Check failure on line 84 in contracts/optimism/L2ERC20TokenBridge.sol

View workflow job for this annotation

GitHub Actions / solhint

Unexpected console statement

ERC20Rebasable(l2TokenRebasable).mintShares(to_, amount_);

console.log("finalizeDeposit1 l2TokenRebasable balanceafter==",ERC20Rebasable(l2TokenRebasable).balanceOf(to_));

Check failure on line 88 in contracts/optimism/L2ERC20TokenBridge.sol

View workflow job for this annotation

GitHub Actions / solhint

Unexpected console statement

bytes memory data = data_[1:];
emit DepositFinalized(l1Token_, l2Token_, from_, to_, amount_, data);
} else {
console.log("finalizeDeposit2 data=", data_.length);

Check failure on line 93 in contracts/optimism/L2ERC20TokenBridge.sol

View workflow job for this annotation

GitHub Actions / solhint

Unexpected console statement

IERC20Bridged(l2Token_).bridgeMint(to_, amount_);
emit DepositFinalized(l1Token_, l2Token_, from_, to_, amount_, data_);
}
}

/// @notice Performs the logic for withdrawals by burning the token and informing
Expand Down
66 changes: 66 additions & 0 deletions contracts/stubs/ERC20Stub.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-FileCopyrightText: 2022 Lido <[email protected]>
// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.10;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { console } from "hardhat/console.sol";

contract ERC20Stub is IERC20 {

uint256 totalSupply_;
uint256 balanceOf_;
bool transfer_;
uint256 allowance_;
bool approve_;
bool transferFrom_;

constructor() {
totalSupply_ = 0;
balanceOf_ = 0;
transfer_ = true;
allowance_ = 0;
approve_ = true;
transferFrom_ = true;
}

function totalSupply() external view returns (uint256) {
return totalSupply_;
}

function balanceOf(address account) external view returns (uint256) {
return balanceOf_;
}

address public transferTo;
uint256 public transferAmount;

function transfer(address to, uint256 amount) external returns (bool) {
transferTo = to;
transferAmount = amount;
return true;
}

function allowance(address owner, address spender) external view returns (uint256) {
return 0;
}

function approve(address spender, uint256 amount) external returns (bool) {
return true;
}

address public transferFromAddress;
address public transferFromTo;
uint256 public transferFromAmount;

function transferFrom(
address from,
address to,
uint256 amount
) external returns (bool) {
transferFromAddress = from;
transferFromTo = to;
transferFromAmount = amount;
return true;
}
}
56 changes: 56 additions & 0 deletions contracts/stubs/ERC20WrapableStub.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-FileCopyrightText: 2022 Lido <[email protected]>
// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.10;

import {IERC20Bridged} from "../token/interfaces/IERC20Bridged.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC20Wrapable} from "../token/interfaces/IERC20Wrapable.sol";
// import {ERC20Core} from "../token/ERC20Core.sol";
import { console } from "hardhat/console.sol";

// represents wstETH on L1
contract ERC20WrapableStub is IERC20Wrapable, ERC20 {

IERC20 public stETH;
address public bridge;
uint256 public tokensRate; /// wst/st

constructor(IERC20 stETH_, string memory name_, string memory symbol_)
ERC20(name_, symbol_)
{
stETH = stETH_;
console.log("constructor wrap stETH=",address(stETH));

tokensRate = 2 * 10 **18;
_mint(msg.sender, 1000000 * 10**18);
}

function wrap(uint256 _stETHAmount) external returns (uint256) {
require(_stETHAmount > 0, "wstETH: can't wrap zero stETH");

uint256 wstETHAmount = (_stETHAmount * tokensRate) / (10 ** uint256(decimals()));


console.log("wrap msg.sender=",msg.sender);
console.log("wrap address(this)=",address(this));
console.log("wrap _stETHAmount=",_stETHAmount);
console.log("wrap wstETHAmount=",wstETHAmount);

_mint(msg.sender, wstETHAmount);
stETH.transferFrom(msg.sender, address(this), _stETHAmount);

return wstETHAmount;
}

function unwrap(uint256 _wstETHAmount) external returns (uint256) {
require(_wstETHAmount > 0, "wstETH: zero amount unwrap not allowed");

// uint256 stETHAmount = (_wstETHAmount * (10 ** uint256(decimals()))) / tokensRate;
// _burn(msg.sender, _wstETHAmount);
// stETH.transfer(msg.sender, stETHAmount);

return 0; //stETHAmount;
}
}
Loading
Loading