Skip to content

Commit

Permalink
fix evetns + formating
Browse files Browse the repository at this point in the history
  • Loading branch information
kovalgek committed Feb 13, 2024
1 parent 707da9b commit 1cb344a
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 42 deletions.
102 changes: 79 additions & 23 deletions contracts/optimism/L1ERC20TokenBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,28 @@ contract L1ERC20TokenBridge is
onlyFromCrossDomainAccount(L2_TOKEN_BRIDGE)
{
if (isRebasableTokenFlow(l1Token_, l2Token_)) {
uint256 stETHAmount = IERC20Wrapper(L1_TOKEN_NON_REBASABLE).unwrap(amount_);
IERC20(L1_TOKEN_REBASABLE).safeTransfer(to_, stETHAmount);
emit ERC20WithdrawalFinalized(l1Token_, l2Token_, from_, to_, amount_, data_);
uint256 rebasableTokenAmount = IERC20Wrapper(L1_TOKEN_NON_REBASABLE).unwrap(amount_);
IERC20(L1_TOKEN_REBASABLE).safeTransfer(to_, rebasableTokenAmount);

emit ERC20WithdrawalFinalized(
L1_TOKEN_REBASABLE,
L2_TOKEN_REBASABLE,
from_,
to_,
rebasableTokenAmount,
data_
);
} else if (isNonRebasableTokenFlow(l1Token_, l2Token_)) {
IERC20(L1_TOKEN_NON_REBASABLE).safeTransfer(to_, amount_);
emit ERC20WithdrawalFinalized(l1Token_, l2Token_, from_, to_, amount_, data_);

emit ERC20WithdrawalFinalized(
L1_TOKEN_NON_REBASABLE,
L2_TOKEN_NON_REBASABLE,
from_,
to_,
amount_,
data_
);
}
}

Expand All @@ -134,31 +150,81 @@ contract L1ERC20TokenBridge is
bytes memory data_
) internal {
if (isRebasableTokenFlow(l1Token_, l2Token_)) {

DepositData memory depositData = DepositData({
rate: uint96(L1_TOKEN_NON_REBASABLE_ADAPTER.tokenRate()),
timestamp: uint40(block.timestamp),
data: data_
});

bytes memory encodedDepositData = encodeDepositData(depositData);

if (amount_ == 0) {
_initiateERC20Deposit(l1Token_, l2Token_, msg.sender, to_, amount_, l2Gas_, encodedDepositData);
_initiateERC20Deposit(
L1_TOKEN_REBASABLE,
L2_TOKEN_REBASABLE,
msg.sender,
to_,
0,
l2Gas_,
encodedDepositData
);

emit ERC20DepositInitiated(
L1_TOKEN_REBASABLE,
L2_TOKEN_REBASABLE,
msg.sender,
to_,
0,
encodedDepositData
);

return;
}

// maybe loosing 1 wei for stETH. Check another method
IERC20(L1_TOKEN_REBASABLE).safeTransferFrom(msg.sender, address(this), amount_);
if(!IERC20(L1_TOKEN_REBASABLE).approve(L1_TOKEN_NON_REBASABLE, amount_)) revert ErrorRebasableTokenApprove();
if(!IERC20(L1_TOKEN_REBASABLE).approve(L1_TOKEN_NON_REBASABLE, amount_)) {
revert ErrorRebasableTokenApprove();
}
uint256 nonRebasableTokenAmount = IERC20Wrapper(L1_TOKEN_NON_REBASABLE).wrap(amount_);

// when 1 wei wasnt't transfer, can this wrap be failed?
uint256 wstETHAmount = IERC20Wrapper(L1_TOKEN_NON_REBASABLE).wrap(amount_);
_initiateERC20Deposit(L1_TOKEN_REBASABLE, L2_TOKEN_REBASABLE, msg.sender, to_, wstETHAmount, l2Gas_, encodedDepositData);
_initiateERC20Deposit(
L1_TOKEN_REBASABLE,
L2_TOKEN_REBASABLE,
msg.sender,
to_,
nonRebasableTokenAmount,
l2Gas_,
encodedDepositData
);

emit ERC20DepositInitiated(
L1_TOKEN_REBASABLE,
L2_TOKEN_REBASABLE,
msg.sender,
to_,
amount_,
encodedDepositData
);
} else if (isNonRebasableTokenFlow(l1Token_, l2Token_)) {
IERC20(L1_TOKEN_NON_REBASABLE).safeTransferFrom(msg.sender, address(this), amount_);
_initiateERC20Deposit(L1_TOKEN_NON_REBASABLE, L2_TOKEN_NON_REBASABLE, msg.sender, to_, amount_, l2Gas_, data_);

_initiateERC20Deposit(
L1_TOKEN_NON_REBASABLE,
L2_TOKEN_NON_REBASABLE,
msg.sender,
to_,
amount_,
l2Gas_,
data_
);

emit ERC20DepositInitiated(
L1_TOKEN_NON_REBASABLE,
L2_TOKEN_NON_REBASABLE,
msg.sender,
to_,
amount_,
data_
);
}
}

Expand All @@ -180,7 +246,6 @@ contract L1ERC20TokenBridge is
uint32 l2Gas_,
bytes memory data_
) internal {

bytes memory message = abi.encodeWithSelector(
IL2ERC20Bridge.finalizeDeposit.selector,
l1Token_,
Expand All @@ -192,15 +257,6 @@ contract L1ERC20TokenBridge is
);

sendCrossDomainMessage(L2_TOKEN_BRIDGE, l2Gas_, message);

emit ERC20DepositInitiated(
l1Token_,
l2Token_,
from_,
to_,
amount_,
data_
);
}

error ErrorSenderNotEOA();
Expand Down
62 changes: 52 additions & 10 deletions contracts/optimism/L2ERC20TokenBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,28 @@ contract L2ERC20TokenBridge is
DepositData memory depositData = decodeDepositData(data_);
ITokenRateOracle tokenRateOracle = ERC20Rebasable(L2_TOKEN_REBASABLE).TOKEN_RATE_ORACLE();
tokenRateOracle.updateRate(depositData.rate, depositData.timestamp);

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

uint256 rebasableTokenAmount = ERC20Rebasable(L2_TOKEN_REBASABLE).getTokensByShares(amount_);
emit DepositFinalized(l1Token_, l2Token_, from_, to_, rebasableTokenAmount, depositData.data);
emit DepositFinalized(
L1_TOKEN_REBASABLE,
L2_TOKEN_REBASABLE,
from_,
to_,
rebasableTokenAmount,
depositData.data
);
} else if (isNonRebasableTokenFlow(l1Token_, l2Token_)) {
IERC20Bridged(L2_TOKEN_NON_REBASABLE).bridgeMint(to_, amount_);
emit DepositFinalized(l1Token_, l2Token_, from_, to_, amount_, data_);
emit DepositFinalized(
L1_TOKEN_NON_REBASABLE,
L2_TOKEN_NON_REBASABLE,
from_,
to_,
amount_,
data_
);
}
}

Expand All @@ -114,19 +130,46 @@ contract L2ERC20TokenBridge is
bytes calldata data_
) internal {
if (l2Token_ == L2_TOKEN_REBASABLE) {

// TODO: maybe loosing 1 wei here as well
uint256 shares = ERC20Rebasable(L2_TOKEN_REBASABLE).getSharesByTokens(amount_);
ERC20Rebasable(L2_TOKEN_REBASABLE).burnShares(msg.sender, shares);

_initiateWithdrawal(L1_TOKEN_REBASABLE, L2_TOKEN_REBASABLE, msg.sender, to_, shares, l1Gas_, data_);
emit WithdrawalInitiated(L1_TOKEN_REBASABLE, L2_TOKEN_REBASABLE, msg.sender, to_, amount_, data_);
_initiateWithdrawal(
L1_TOKEN_REBASABLE,
L2_TOKEN_REBASABLE,
msg.sender,
to_,
shares,
l1Gas_,
data_
);
emit WithdrawalInitiated(
L1_TOKEN_REBASABLE,
L2_TOKEN_REBASABLE,
msg.sender,
to_,
amount_,
data_
);
} else if (l2Token_ == L2_TOKEN_NON_REBASABLE) {

IERC20Bridged(L2_TOKEN_NON_REBASABLE).bridgeBurn(msg.sender, amount_);

_initiateWithdrawal(L1_TOKEN_NON_REBASABLE, L2_TOKEN_NON_REBASABLE, msg.sender, to_, amount_, l1Gas_, data_);
emit WithdrawalInitiated(L1_TOKEN_NON_REBASABLE, L2_TOKEN_NON_REBASABLE, msg.sender, to_, amount_, data_);
_initiateWithdrawal(
L1_TOKEN_NON_REBASABLE,
L2_TOKEN_NON_REBASABLE,
msg.sender,
to_,
amount_,
l1Gas_,
data_
);
emit WithdrawalInitiated(
L1_TOKEN_NON_REBASABLE,
L2_TOKEN_NON_REBASABLE,
msg.sender,
to_,
amount_,
data_
);
}
}

Expand All @@ -148,7 +191,6 @@ contract L2ERC20TokenBridge is
uint32 l1Gas_,
bytes memory data_
) internal {

bytes memory message = abi.encodeWithSelector(
IL1ERC20Bridge.finalizeERC20Withdrawal.selector,
l1Token_,
Expand Down
10 changes: 5 additions & 5 deletions test/optimism/L1ERC20TokenBridge.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ unit("Optimism :: L1ERC20TokenBridge", ctxFactory)
l2TokenRebasable.address,
deployer.address,
deployer.address,
amountWrapped,
amount,
dataToReceive,
]);

Expand Down Expand Up @@ -474,8 +474,8 @@ unit("Optimism :: L1ERC20TokenBridge", ctxFactory)
const bridgeBalanceBefore = await l1TokenNonRebasable.balanceOf(l1TokenBridge.address);

const tx = await l1TokenBridge.depositERC20To(
l1TokenRebasable.address,
l2TokenRebasable.address,
l1TokenRebasable.address,
l2TokenRebasable.address,
recipient.address,
amount,
l2Gas,
Expand All @@ -490,7 +490,7 @@ unit("Optimism :: L1ERC20TokenBridge", ctxFactory)
l2TokenRebasable.address,
deployer.address,
recipient.address,
amountWrapped,
amount,
dataToReceive,
]);

Expand Down Expand Up @@ -790,7 +790,7 @@ unit("Optimism :: L1ERC20TokenBridge", ctxFactory)
l2TokenRebasable.address,
deployer.address,
recipient.address,
amount,
amountUnwrapped,
data,
]);

Expand Down
8 changes: 4 additions & 4 deletions test/optimism/bridging-rebase.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ scenario("Optimism :: Bridging integration test", ctxFactory)
l2TokenRebasable.address,
tokenHolderA.address,
tokenHolderA.address,
depositAmountNonRebasable,
depositAmountRebasable,
dataToSend,
]);

Expand Down Expand Up @@ -546,7 +546,7 @@ scenario("Optimism :: Bridging integration test", ctxFactory)
l2TokenRebasable.address,
tokenHolderA.address,
tokenHolderA.address,
withdrawalAmountNonRebasable,
withdrawalAmountRebasable,
"0x",
]);

Expand Down Expand Up @@ -609,7 +609,7 @@ scenario("Optimism :: Bridging integration test", ctxFactory)
l2TokenRebasable.address,
tokenHolderA.address,
tokenHolderB.address,
depositAmountNonRebasable,
depositAmountRebasable,
dataToSend,
]);

Expand Down Expand Up @@ -813,7 +813,7 @@ scenario("Optimism :: Bridging integration test", ctxFactory)
l2TokenRebasable.address,
tokenHolderB.address,
tokenHolderA.address,
withdrawalAmountNonRebasable,
withdrawalAmountRebasable,
"0x",
]);

Expand Down

0 comments on commit 1cb344a

Please sign in to comment.