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 12 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
40 changes: 29 additions & 11 deletions contracts/BridgeableTokens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,40 @@
/// @author psirex
/// @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;
/// @notice Address of the bridged non rebasable token in the L1 chain
address public immutable l1TokenNonRebasable;

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 token minted on the L2 chain when token bridged
address public immutable l2Token;
/// @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

/// @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_) {
l1Token = l1Token_;
l2Token = l2Token_;
/// @notice Address of the non rebasable token minted on the L2 chain when token bridged
address public immutable l2TokenNonRebasable;

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 l1TokenNonRebasable_ Address of the bridged non rebasable token in the L1 chain
/// @param l1TokenRebasable_ Address of the bridged rebasable token in the L1 chain
/// @param l2TokenNonRebasable_ Address of the non rebasable token minted on the L2 chain when token bridged
/// @param l2TokenRebasable_ Address of the rebasable token minted on the L2 chain when token bridged
constructor(address l1TokenNonRebasable_, address l1TokenRebasable_, address l2TokenNonRebasable_, address l2TokenRebasable_) {
l1TokenNonRebasable = l1TokenNonRebasable_;
l1TokenRebasable = l1TokenRebasable_;
l2TokenNonRebasable = l2TokenNonRebasable_;
l2TokenRebasable = l2TokenRebasable_;
}

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

/// @dev Validates that passed l2Token_ is supported by the bridge
modifier onlySupportedL2Token(address l2Token_) {
if (l2Token_ != l2Token) {
if (l2Token_ != l2TokenNonRebasable && l2Token_ != l2TokenRebasable) {
revert ErrorUnsupportedL2Token();
}
_;
Expand All @@ -43,6 +53,14 @@
_;
}

function isRebasableTokenFlow(address l1Token_, address l2Token_) internal view returns (bool) {
return l1Token_ == l1TokenRebasable && l2Token_ == l2TokenRebasable;
}

function isNonRebasableTokenFlow(address l1Token_, address l2Token_) internal view returns (bool) {
return l1Token_ == l1TokenNonRebasable && l2Token_ == l2TokenNonRebasable;
}

error ErrorUnsupportedL1Token();
error ErrorUnsupportedL2Token();
error ErrorAccountIsZeroAddress();
Expand Down
18 changes: 11 additions & 7 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 l2Token_ Address of the token minted on the Arbitrum chain when token bridged
/// @param l1TokenNonRebasable Address of the bridged token in the Ethereum chain
/// @param l1TokenRebasable_ Address of the bridged token in the Ethereum chain
/// @param l2TokenNonRebasable_ 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 l1TokenNonRebasable,
address l1TokenRebasable_,
address l2TokenNonRebasable_,
address l2TokenRebasable_
) BridgeableTokens(l1TokenNonRebasable, l1TokenRebasable_, l2TokenNonRebasable_, l2TokenRebasable_) {
router = router_;
counterpartGateway = counterpartGateway_;
}
Expand All @@ -44,8 +48,8 @@
view
returns (address)
{
if (l1Token_ == l1Token) {
return l2Token;
if (l1Token_ == l1TokenRebasable) {
return l2TokenNonRebasable;
}
return address(0);
}
Expand Down
12 changes: 8 additions & 4 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 Expand Up @@ -78,7 +82,7 @@ contract L1ERC20TokenGateway is
})
);

emit DepositInitiated(l1Token, from, to_, retryableTicketId, amount_);
emit DepositInitiated(l1TokenNonRebasable, from, to_, retryableTicketId, amount_);

return abi.encode(retryableTicketId);
}
Expand Down Expand Up @@ -113,7 +117,7 @@ contract L1ERC20TokenGateway is
sendCrossDomainMessage(
from_,
counterpartGateway,
getOutboundCalldata(l1Token, from_, to_, amount_, ""),
getOutboundCalldata(l1TokenNonRebasable, from_, to_, amount_, ""),
messageOptions
);
}
Expand Down
22 changes: 14 additions & 8 deletions contracts/arbitrum/L2ERC20TokenGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,26 @@ contract L2ERC20TokenGateway is
/// @param arbSys_ Address of the Arbitrum’s ArbSys contract in the L2 chain
/// @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 l2Token_ Address of the token minted on the Arbitrum chain when token bridged
/// @param l1TokenNonRebasable_ Address of the bridged token in the L1 chain
/// @param l1TokenRebasable_ Address of the bridged token in the L1 chain
/// @param l2TokenNonRebasable_ 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 arbSys_,
address router_,
address counterpartGateway_,
address l1Token_,
address l2Token_
address l1TokenNonRebasable_,
address l1TokenRebasable_,
address l2TokenNonRebasable_,
address l2TokenRebasable_
)
InterchainERC20TokenGateway(
router_,
counterpartGateway_,
l1Token_,
l2Token_
l1TokenNonRebasable_,
l1TokenRebasable_,
l2TokenNonRebasable_,
l2TokenRebasable_
)
L2CrossDomainEnabled(arbSys_)
{}
Expand All @@ -55,7 +61,7 @@ contract L2ERC20TokenGateway is
{
address from = L2OutboundDataParser.decode(router, data_);

IERC20Bridged(l2Token).bridgeBurn(from, amount_);
IERC20Bridged(l2TokenNonRebasable).bridgeBurn(from, amount_);

uint256 id = sendCrossDomainMessage(
from,
Expand Down Expand Up @@ -83,7 +89,7 @@ contract L2ERC20TokenGateway is
onlySupportedL1Token(l1Token_)
onlyFromCrossDomainAccount(counterpartGateway)
{
IERC20Bridged(l2Token).bridgeMint(to_, amount_);
IERC20Bridged(l2TokenNonRebasable).bridgeMint(to_, amount_);

emit DepositFinalized(l1Token_, from_, to_, amount_);
}
Expand Down
39 changes: 39 additions & 0 deletions contracts/optimism/DepositDataCodec.sol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an attack vector of asynchronous bridge upgrades

E.g., suppose L2 has been updated while L1 hasn't been yet
This case anyone would be able to pass arbitrary tokenRate and timestamp disrupting operations and breaking the protocol's invariants on L2.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vise versa, if L1 upgraded and L2 is not, then stETH deposits would lead to funds being stuck on the L1 bridge

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: 2023 Lido <[email protected]>
kovalgek marked this conversation as resolved.
Show resolved Hide resolved
// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.10;

contract DepositDataCodec {
kovalgek marked this conversation as resolved.
Show resolved Hide resolved
folkyatina marked this conversation as resolved.
Show resolved Hide resolved
kovalgek marked this conversation as resolved.
Show resolved Hide resolved

struct DepositData {
uint256 rate;
uint256 time;
bytes data;
}

function encodeDepositData(DepositData memory depositData) internal pure returns (bytes memory) {
bytes memory data = bytes.concat(
abi.encodePacked(depositData.rate),
abi.encodePacked(depositData.time),
abi.encodePacked(depositData.data)
);
return data;
}

function decodeDepositData(bytes calldata buffer) internal pure returns (DepositData memory) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

later on would be great to fuzz these methods


if (buffer.length < 32 * 2) {
revert ErrorDepositDataLength();
}

DepositData memory depositData = DepositData({
rate: uint256(bytes32(buffer[0:32])),
time: uint256(bytes32(buffer[32:64])),
data: buffer[64:]
});

return depositData;
}

error ErrorDepositDataLength();
}
kovalgek marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading