Skip to content

Commit 2727922

Browse files
bmzignicholaspai
andauthored
improve: query shared bridge address dynamically (#944)
* improve: query shared bridge address dynamically Signed-off-by: bennett <bennett@umaproject.org> * refactor --------- Signed-off-by: bennett <bennett@umaproject.org> Co-authored-by: nicholaspai <npai.nyc@gmail.com>
1 parent d68d1f7 commit 2727922

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

contracts/chain-adapters/ZkStack_Adapter.sol

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ contract ZkStack_Adapter is AdapterInterface {
4949
// Set l1Weth at construction time to make testing easier.
5050
WETH9Interface public immutable L1_WETH;
5151

52-
// SharedBridge address, which is read from the BridgeHub at construction.
53-
address public immutable SHARED_BRIDGE;
54-
5552
// The maximum gas price a transaction sent to this adapter may have. This is set to prevent a block producer from setting an artificially high priority fee
5653
// when calling a hub pool message relay, which would otherwise cause a large amount of ETH to be sent to L2.
5754
uint256 private immutable MAX_TX_GASPRICE;
@@ -87,7 +84,6 @@ contract ZkStack_Adapter is AdapterInterface {
8784
L2_GAS_LIMIT = _l2GasLimit;
8885
MAX_TX_GASPRICE = _maxTxGasprice;
8986
L1_GAS_TO_L2_GAS_PER_PUB_DATA_LIMIT = _l1GasToL2GasPerPubDataLimit;
90-
SHARED_BRIDGE = BRIDGE_HUB.sharedBridge();
9187
address gasToken = BRIDGE_HUB.baseToken(CHAIN_ID);
9288
if (gasToken != ETH_TOKEN_ADDRESS) {
9389
revert ETHGasTokenRequired();
@@ -160,7 +156,8 @@ contract ZkStack_Adapter is AdapterInterface {
160156
);
161157
} else {
162158
// An ERC20 that is not WETH.
163-
IERC20(l1Token).forceApprove(SHARED_BRIDGE, amount);
159+
address sharedBridge = BRIDGE_HUB.sharedBridge();
160+
IERC20(l1Token).forceApprove(sharedBridge, amount);
164161
txHash = BRIDGE_HUB.requestL2TransactionTwoBridges{ value: txBaseCost }(
165162
BridgeHubInterface.L2TransactionRequestTwoBridgesOuter({
166163
chainId: CHAIN_ID,
@@ -169,7 +166,7 @@ contract ZkStack_Adapter is AdapterInterface {
169166
l2GasLimit: L2_GAS_LIMIT,
170167
l2GasPerPubdataByteLimit: L1_GAS_TO_L2_GAS_PER_PUB_DATA_LIMIT,
171168
refundRecipient: L2_REFUND_ADDRESS,
172-
secondBridgeAddress: SHARED_BRIDGE,
169+
secondBridgeAddress: sharedBridge,
173170
secondBridgeValue: 0,
174171
secondBridgeCalldata: _secondBridgeCalldata(to, l1Token, amount)
175172
})

contracts/chain-adapters/ZkStack_CustomGasToken_Adapter.sol

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ contract ZkStack_CustomGasToken_Adapter is AdapterInterface {
6464
// Set l1Weth at construction time to make testing easier.
6565
WETH9Interface public immutable L1_WETH;
6666

67-
// SharedBridge address, which is read from the BridgeHub at construction.
68-
address public immutable SHARED_BRIDGE;
69-
7067
// Custom gas token address, which is read from the BridgeHub at construction.
7168
address public immutable CUSTOM_GAS_TOKEN;
7269

@@ -110,7 +107,6 @@ contract ZkStack_CustomGasToken_Adapter is AdapterInterface {
110107
L2_GAS_LIMIT = _l2GasLimit;
111108
MAX_TX_GASPRICE = _maxTxGasprice;
112109
L1_GAS_TO_L2_GAS_PER_PUB_DATA_LIMIT = _l1GasToL2GasPerPubDataLimit;
113-
SHARED_BRIDGE = BRIDGE_HUB.sharedBridge();
114110
CUSTOM_GAS_TOKEN = BRIDGE_HUB.baseToken(CHAIN_ID);
115111
if (CUSTOM_GAS_TOKEN == ETH_TOKEN_ADDRESS) {
116112
revert ETHGasTokenNotAllowed();
@@ -125,7 +121,7 @@ contract ZkStack_CustomGasToken_Adapter is AdapterInterface {
125121
*/
126122
function relayMessage(address target, bytes memory message) external payable override {
127123
uint256 txBaseCost = _pullCustomGas(L2_GAS_LIMIT);
128-
IERC20(CUSTOM_GAS_TOKEN).forceApprove(SHARED_BRIDGE, txBaseCost);
124+
IERC20(CUSTOM_GAS_TOKEN).forceApprove(BRIDGE_HUB.sharedBridge(), txBaseCost);
129125

130126
// Returns the hash of the requested L2 transaction. This hash can be used to follow the transaction status.
131127
bytes32 canonicalTxHash = BRIDGE_HUB.requestL2TransactionDirect(
@@ -163,13 +159,14 @@ contract ZkStack_CustomGasToken_Adapter is AdapterInterface {
163159
// A bypass proxy seems to no longer be needed to avoid deposit limits. The tracking of these limits seems to be deprecated.
164160
// See: https://github.com/matter-labs/era-contracts/blob/bce4b2d0f34bd87f1aaadd291772935afb1c3bd6/l1-contracts/contracts/bridge/L1ERC20Bridge.sol#L54-L55
165161
uint256 txBaseCost = _pullCustomGas(L2_GAS_LIMIT);
162+
address sharedBridge = BRIDGE_HUB.sharedBridge();
166163

167164
bytes32 txHash;
168165
if (l1Token == address(L1_WETH)) {
169166
// If the l1Token is WETH then unwrap it to ETH then send the ETH to the standard bridge along with the base
170167
// cost of custom gas tokens.
171168
L1_WETH.withdraw(amount);
172-
IERC20(CUSTOM_GAS_TOKEN).forceApprove(SHARED_BRIDGE, txBaseCost);
169+
IERC20(CUSTOM_GAS_TOKEN).forceApprove(sharedBridge, txBaseCost);
173170
// Note: When bridging ETH with `L2TransactionRequestTwoBridgesOuter`, the second bridge must be 0 for the shared bridge call to not revert.
174171
// https://github.com/matter-labs/era-contracts/blob/aafee035db892689df3f7afe4b89fd6467a39313/l1-contracts/contracts/bridge/L1SharedBridge.sol#L328
175172
txHash = BRIDGE_HUB.requestL2TransactionTwoBridges{ value: amount }(
@@ -180,14 +177,14 @@ contract ZkStack_CustomGasToken_Adapter is AdapterInterface {
180177
l2GasLimit: L2_GAS_LIMIT,
181178
l2GasPerPubdataByteLimit: L1_GAS_TO_L2_GAS_PER_PUB_DATA_LIMIT,
182179
refundRecipient: L2_REFUND_ADDRESS,
183-
secondBridgeAddress: SHARED_BRIDGE,
180+
secondBridgeAddress: sharedBridge,
184181
secondBridgeValue: amount,
185182
secondBridgeCalldata: _secondBridgeCalldata(to, ETH_TOKEN_ADDRESS, 0)
186183
})
187184
);
188185
} else if (l1Token == CUSTOM_GAS_TOKEN) {
189186
// The chain's custom gas token.
190-
IERC20(l1Token).forceApprove(SHARED_BRIDGE, txBaseCost + amount);
187+
IERC20(l1Token).forceApprove(sharedBridge, txBaseCost + amount);
191188
txHash = BRIDGE_HUB.requestL2TransactionDirect(
192189
BridgeHubInterface.L2TransactionRequestDirect({
193190
chainId: CHAIN_ID,
@@ -203,8 +200,8 @@ contract ZkStack_CustomGasToken_Adapter is AdapterInterface {
203200
);
204201
} else {
205202
// An ERC20 that is not WETH and not the custom gas token.
206-
IERC20(CUSTOM_GAS_TOKEN).forceApprove(SHARED_BRIDGE, txBaseCost);
207-
IERC20(l1Token).forceApprove(SHARED_BRIDGE, amount);
203+
IERC20(CUSTOM_GAS_TOKEN).forceApprove(sharedBridge, txBaseCost);
204+
IERC20(l1Token).forceApprove(sharedBridge, amount);
208205
txHash = BRIDGE_HUB.requestL2TransactionTwoBridges(
209206
BridgeHubInterface.L2TransactionRequestTwoBridgesOuter({
210207
chainId: CHAIN_ID,
@@ -213,7 +210,7 @@ contract ZkStack_CustomGasToken_Adapter is AdapterInterface {
213210
l2GasLimit: L2_GAS_LIMIT,
214211
l2GasPerPubdataByteLimit: L1_GAS_TO_L2_GAS_PER_PUB_DATA_LIMIT,
215212
refundRecipient: L2_REFUND_ADDRESS,
216-
secondBridgeAddress: SHARED_BRIDGE,
213+
secondBridgeAddress: sharedBridge,
217214
secondBridgeValue: 0,
218215
secondBridgeCalldata: _secondBridgeCalldata(to, l1Token, amount)
219216
})

0 commit comments

Comments
 (0)