Skip to content

Commit

Permalink
Provide maxFeePerGas as param
Browse files Browse the repository at this point in the history
  • Loading branch information
gvladika committed Sep 11, 2023
1 parent 116d651 commit 03c98c0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 59 deletions.
19 changes: 14 additions & 5 deletions scripts/rollupCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { BigNumber } from 'ethers'
import { IERC20__factory } from '../build/types'
import { sleep } from './testSetup'

// 1 gwei
const MAX_FER_PER_GAS = BigNumber.from('1000000000')

interface RollupCreatedEvent {
event: string
address: string
Expand Down Expand Up @@ -75,11 +78,17 @@ export async function createRollup(feeToken?: string) {

// Call the createRollup function
console.log('Calling createRollup to generate a new rollup ...')
const createRollupTx = await rollupCreator[
'createRollup((uint64,uint64,address,uint256,bytes32,address,address,uint256,string,uint64,(uint256,uint256,uint256,uint256)),address,address[],address)'
](config.rollupConfig, config.batchPoster, config.validators, feeToken, {
value: feeCost,
})
const createRollupTx = await rollupCreator.createRollup(
config.rollupConfig,
config.batchPoster,
config.validators,
feeToken,
true,
MAX_FER_PER_GAS,
{
value: feeCost,
}
)
const createRollupReceipt = await createRollupTx.wait()

const rollupCreatedEvent = createRollupReceipt.events?.find(
Expand Down
48 changes: 36 additions & 12 deletions src/rollup/DeployHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ contract DeployHelper {
hex"04f90a388085174876e800830c35008080b909e5608060405234801561001057600080fd5b506109c5806100206000396000f3fe608060405234801561001057600080fd5b50600436106100a5576000357c010000000000000000000000000000000000000000000000000000000090048063a41e7d5111610078578063a41e7d51146101d4578063aabbb8ca1461020a578063b705676514610236578063f712f3e814610280576100a5565b806329965a1d146100aa5780633d584063146100e25780635df8122f1461012457806365ba36c114610152575b600080fd5b6100e0600480360360608110156100c057600080fd5b50600160a060020a038135811691602081013591604090910135166102b6565b005b610108600480360360208110156100f857600080fd5b5035600160a060020a0316610570565b60408051600160a060020a039092168252519081900360200190f35b6100e06004803603604081101561013a57600080fd5b50600160a060020a03813581169160200135166105bc565b6101c26004803603602081101561016857600080fd5b81019060208101813564010000000081111561018357600080fd5b82018360208201111561019557600080fd5b803590602001918460018302840111640100000000831117156101b757600080fd5b5090925090506106b3565b60408051918252519081900360200190f35b6100e0600480360360408110156101ea57600080fd5b508035600160a060020a03169060200135600160e060020a0319166106ee565b6101086004803603604081101561022057600080fd5b50600160a060020a038135169060200135610778565b61026c6004803603604081101561024c57600080fd5b508035600160a060020a03169060200135600160e060020a0319166107ef565b604080519115158252519081900360200190f35b61026c6004803603604081101561029657600080fd5b508035600160a060020a03169060200135600160e060020a0319166108aa565b6000600160a060020a038416156102cd57836102cf565b335b9050336102db82610570565b600160a060020a031614610339576040805160e560020a62461bcd02815260206004820152600f60248201527f4e6f7420746865206d616e616765720000000000000000000000000000000000604482015290519081900360640190fd5b6103428361092a565b15610397576040805160e560020a62461bcd02815260206004820152601a60248201527f4d757374206e6f7420626520616e204552433136352068617368000000000000604482015290519081900360640190fd5b600160a060020a038216158015906103b85750600160a060020a0382163314155b156104ff5760405160200180807f455243313832305f4143434550545f4d4147494300000000000000000000000081525060140190506040516020818303038152906040528051906020012082600160a060020a031663249cb3fa85846040518363ffffffff167c01000000000000000000000000000000000000000000000000000000000281526004018083815260200182600160a060020a0316600160a060020a031681526020019250505060206040518083038186803b15801561047e57600080fd5b505afa158015610492573d6000803e3d6000fd5b505050506040513d60208110156104a857600080fd5b5051146104ff576040805160e560020a62461bcd02815260206004820181905260248201527f446f6573206e6f7420696d706c656d656e742074686520696e74657266616365604482015290519081900360640190fd5b600160a060020a03818116600081815260208181526040808320888452909152808220805473ffffffffffffffffffffffffffffffffffffffff19169487169485179055518692917f93baa6efbd2244243bfee6ce4cfdd1d04fc4c0e9a786abd3a41313bd352db15391a450505050565b600160a060020a03818116600090815260016020526040812054909116151561059a5750806105b7565b50600160a060020a03808216600090815260016020526040902054165b919050565b336105c683610570565b600160a060020a031614610624576040805160e560020a62461bcd02815260206004820152600f60248201527f4e6f7420746865206d616e616765720000000000000000000000000000000000604482015290519081900360640190fd5b81600160a060020a031681600160a060020a0316146106435780610646565b60005b600160a060020a03838116600081815260016020526040808220805473ffffffffffffffffffffffffffffffffffffffff19169585169590951790945592519184169290917f605c2dbf762e5f7d60a546d42e7205dcb1b011ebc62a61736a57c9089d3a43509190a35050565b600082826040516020018083838082843780830192505050925050506040516020818303038152906040528051906020012090505b92915050565b6106f882826107ef565b610703576000610705565b815b600160a060020a03928316600081815260208181526040808320600160e060020a031996909616808452958252808320805473ffffffffffffffffffffffffffffffffffffffff19169590971694909417909555908152600284528181209281529190925220805460ff19166001179055565b600080600160a060020a038416156107905783610792565b335b905061079d8361092a565b156107c357826107ad82826108aa565b6107b85760006107ba565b815b925050506106e8565b600160a060020a0390811660009081526020818152604080832086845290915290205416905092915050565b6000808061081d857f01ffc9a70000000000000000000000000000000000000000000000000000000061094c565b909250905081158061082d575080155b1561083d576000925050506106e8565b61084f85600160e060020a031961094c565b909250905081158061086057508015155b15610870576000925050506106e8565b61087a858561094c565b909250905060018214801561088f5750806001145b1561089f576001925050506106e8565b506000949350505050565b600160a060020a0382166000908152600260209081526040808320600160e060020a03198516845290915281205460ff1615156108f2576108eb83836107ef565b90506106e8565b50600160a060020a03808316600081815260208181526040808320600160e060020a0319871684529091529020549091161492915050565b7bffffffffffffffffffffffffffffffffffffffffffffffffffffffff161590565b6040517f01ffc9a7000000000000000000000000000000000000000000000000000000008082526004820183905260009182919060208160248189617530fa90519096909550935050505056fea165627a7a72305820377f4a2d4301ede9949f163f319021a6e9c687c292a5e2b2c4734c126b524e6c00291ba01820182018201820182018201820182018201820182018201820182018201820a01820182018201820182018201820182018201820182018201820182018201820";

uint256 internal constant GASLIMIT = 100_000;
uint256 internal constant MAXFEEPERGAS = 1_000_000_000;

function _fundAndDeploy(
address inbox,
uint256 _value,
address _l2Address,
bytes memory payload,
bool _isUsingFeeToken
bool _isUsingFeeToken,
uint256 maxFeePerGas
) internal {
uint256 submissionCost = IInboxBase(inbox).calculateRetryableSubmissionFee(
0,
block.basefee
);
uint256 feeAmount = _value + submissionCost + GASLIMIT * MAXFEEPERGAS;
uint256 feeAmount = _value + submissionCost + GASLIMIT * maxFeePerGas;

// fund the target L2 address
if (_isUsingFeeToken) {
Expand All @@ -66,7 +66,7 @@ contract DeployHelper {
excessFeeRefundAddress: msg.sender,
callValueRefundAddress: msg.sender,
gasLimit: GASLIMIT,
maxFeePerGas: MAXFEEPERGAS,
maxFeePerGas: maxFeePerGas,
tokenTotalFeeAmount: feeAmount,
data: ""
});
Expand All @@ -78,48 +78,72 @@ contract DeployHelper {
excessFeeRefundAddress: msg.sender,
callValueRefundAddress: msg.sender,
gasLimit: GASLIMIT,
maxFeePerGas: MAXFEEPERGAS,
maxFeePerGas: maxFeePerGas,
data: ""
});
}
// send L2 msg to execute deployment transaction
IInboxBase(inbox).sendL2Message(payload);
}

function perform(address _inbox, address _nativeToken) external payable {
function perform(
address _inbox,
address _nativeToken,
uint256 _maxFeePerGas
) external payable {
bool isUsingFeeToken = _nativeToken != address(0);

_fundAndDeploy(
_inbox,
NICK_CREATE2_VALUE,
NICK_CREATE2_DEPLOYER,
NICK_CREATE2_PAYLOAD,
isUsingFeeToken
isUsingFeeToken,
_maxFeePerGas
);
_fundAndDeploy(
_inbox,
ERC2470_VALUE,
ERC2470_DEPLOYER,
ERC2470_PAYLOAD,
isUsingFeeToken,
_maxFeePerGas
);
_fundAndDeploy(_inbox, ERC2470_VALUE, ERC2470_DEPLOYER, ERC2470_PAYLOAD, isUsingFeeToken);
_fundAndDeploy(
_inbox,
ZOLTU_VALUE,
ZOLTU_CREATE2_DEPLOYER,
ZOLTU_CREATE2_PAYLOAD,
isUsingFeeToken
isUsingFeeToken,
_maxFeePerGas
);
_fundAndDeploy(
_inbox,
ERC1820_VALUE,
ERC1820_DEPLOYER,
ERC1820_PAYLOAD,
isUsingFeeToken,
_maxFeePerGas
);
_fundAndDeploy(_inbox, ERC1820_VALUE, ERC1820_DEPLOYER, ERC1820_PAYLOAD, isUsingFeeToken);

// if paying with ETH refund the caller
if (!isUsingFeeToken) {
payable(msg.sender).transfer(address(this).balance);
}
}

function getDeploymentTotalCost(IInboxBase inbox) public view returns (uint256) {
function getDeploymentTotalCost(IInboxBase inbox, uint256 maxFeePerGas)
public
view
returns (uint256)
{
uint256 submissionCost = inbox.calculateRetryableSubmissionFee(0, block.basefee);
return
NICK_CREATE2_VALUE +
ERC2470_VALUE +
ZOLTU_VALUE +
ERC1820_VALUE +
4 *
(submissionCost + GASLIMIT * MAXFEEPERGAS);
(submissionCost + GASLIMIT * maxFeePerGas);
}
}
54 changes: 20 additions & 34 deletions src/rollup/RollupCreator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,6 @@ contract RollupCreator is Ownable {
emit TemplatesUpdated();
}

/**
* @notice Create a new rollup and deploy L2 factories via retryable tickets
* @dev After this setup:
* @dev - Rollup should be the owner of bridge
* @dev - RollupOwner should be the owner of Rollup's ProxyAdmin
* @dev - RollupOwner should be the owner of Rollup
* @dev - Bridge should have a single inbox and outbox
* @dev - Validators and batch poster should be set if provided
* @param config The configuration for the rollup
* @param _batchPoster The address of the batch poster, not used when set to zero address
* @param _validators The list of validator addresses, not used when set to empty list
* @param _nativeToken Address of the custom fee token used by rollup. If rollup is ETH-based address(0) should be provided
* @return The address of the newly created rollup
*/
function createRollup(
Config memory config,
address _batchPoster,
address[] calldata _validators,
address _nativeToken
) external payable returns (address) {
return createRollup(config, _batchPoster, _validators, _nativeToken, true);
}

/**
* @notice Create a new rollup
* @dev After this setup:
Expand All @@ -121,20 +98,19 @@ contract RollupCreator is Ownable {
* doesn't require paying the L1 gas. If deployment is instead done directly via L2 TX, there is a risk of gas price
* spike which results in burned nonce 0. That would mean we permanently lost capability to deploy deterministic
* factory at expected canonical address.
* @param _maxFeePerGasForRetryables price bid for L2 execution.
* @return The address of the newly created rollup
*/
function createRollup(
Config memory config,
address _batchPoster,
address[] calldata _validators,
address _nativeToken,
bool _deployFactoriesToL2
bool _deployFactoriesToL2,
uint256 _maxFeePerGasForRetryables
) public payable returns (address) {
ProxyAdmin proxyAdmin = new ProxyAdmin();

// deploy and init upgrade executor
address upgradeExecutor = _deployUpgradeExecutor(config.owner, proxyAdmin);

// Create the rollup proxy to figure out the address and initialize it later
RollupProxy rollup = new RollupProxy{salt: keccak256(abi.encode(config))}();

Expand Down Expand Up @@ -168,6 +144,10 @@ contract RollupCreator is Ownable {
osp
);

// deploy and init upgrade executor
address upgradeExecutor = _deployUpgradeExecutor(config.owner, proxyAdmin);

// upgradeExecutor shall be proxyAdmin's owner
proxyAdmin.transferOwnership(address(upgradeExecutor));

// initialize the rollup with this contract as owner to set batch poster and validators
Expand Down Expand Up @@ -206,7 +186,9 @@ contract RollupCreator is Ownable {
IRollupAdmin(address(rollup)).setOwner(address(upgradeExecutor));

if (_deployFactoriesToL2) {
_deployFactories(address(bridgeContracts.inbox), _nativeToken);
_deployFactories(
address(bridgeContracts.inbox), _nativeToken, _maxFeePerGasForRetryables
);
}

emit RollupCreated(
Expand Down Expand Up @@ -246,24 +228,28 @@ contract RollupCreator is Ownable {
return address(upgradeExecutor);
}

function _deployFactories(address _inbox, address _nativeToken) internal {
function _deployFactories(address _inbox, address _nativeToken, uint256 _maxFeePerGas)
internal
{
if (_nativeToken == address(0)) {
// we need to fund 4 retryable tickets
uint256 cost = l2FactoriesDeployer.getDeploymentTotalCost(IInboxBase(_inbox));
uint256 cost =
l2FactoriesDeployer.getDeploymentTotalCost(IInboxBase(_inbox), _maxFeePerGas);

// do it
l2FactoriesDeployer.perform{value: cost}(_inbox, _nativeToken);
l2FactoriesDeployer.perform{value: cost}(_inbox, _nativeToken, _maxFeePerGas);

// refund the caller
(bool sent, ) = msg.sender.call{value: address(this).balance}("");
(bool sent,) = msg.sender.call{value: address(this).balance}("");
require(sent, "Refund failed");
} else {
// Transfer fee token amount needed to pay for retryable fees to the inbox.
uint256 totalFee = l2FactoriesDeployer.getDeploymentTotalCost(IInboxBase(_inbox));
uint256 totalFee =
l2FactoriesDeployer.getDeploymentTotalCost(IInboxBase(_inbox), _maxFeePerGas);
IERC20(_nativeToken).safeTransferFrom(msg.sender, _inbox, totalFee);

// do it
l2FactoriesDeployer.perform(_inbox, _nativeToken);
l2FactoriesDeployer.perform(_inbox, _nativeToken, _maxFeePerGas);
}
}
}
21 changes: 13 additions & 8 deletions test/foundry/RollupCreator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ contract RollupCreatorTest is Test {
IRollupUser public rollupUser;
DeployHelper public deployHelper;

// 1 gwei
uint256 MAX_FEE_PER_GAS = 1_000_000_000;

/* solhint-disable func-name-mixedcase */

function setUp() public {
Expand Down Expand Up @@ -99,7 +102,7 @@ contract RollupCreatorTest is Test {
validators[1] = makeAddr("validator2");

address rollupAddress = rollupCreator.createRollup{value: factoryDeploymentFunds}(
config, batchPoster, validators, address(0)
config, batchPoster, validators, address(0), true, MAX_FEE_PER_GAS
);

vm.stopPrank();
Expand Down Expand Up @@ -163,7 +166,7 @@ contract RollupCreatorTest is Test {
);

// check upgrade executor owns proxyAdmin
address upgradeExecutorExpectedAddress = computeCreateAddress(address(rollupCreator), 2);
address upgradeExecutorExpectedAddress = computeCreateAddress(address(rollupCreator), 4);
assertEq(
ProxyAdmin(_getProxyAdmin(address(rollup.sequencerInbox()))).owner(),
upgradeExecutorExpectedAddress,
Expand All @@ -188,7 +191,8 @@ contract RollupCreatorTest is Test {

// check funds are refunded
uint256 balanceAfter = deployer.balance;
uint256 factoryDeploymentCost = deployHelper.getDeploymentTotalCost(rollup.inbox());
uint256 factoryDeploymentCost =
deployHelper.getDeploymentTotalCost(rollup.inbox(), MAX_FEE_PER_GAS);
assertEq(balanceBefore - balanceAfter, factoryDeploymentCost, "Invalid balance");
}

Expand Down Expand Up @@ -223,8 +227,9 @@ contract RollupCreatorTest is Test {
address[] memory validators = new address[](2);
validators[0] = makeAddr("validator1");
validators[1] = makeAddr("validator2");
address rollupAddress =
rollupCreator.createRollup(config, batchPoster, validators, nativeToken);
address rollupAddress = rollupCreator.createRollup(
config, batchPoster, validators, nativeToken, true, MAX_FEE_PER_GAS
);

vm.stopPrank();

Expand Down Expand Up @@ -292,7 +297,7 @@ contract RollupCreatorTest is Test {
);

// check upgrade executor owns proxyAdmin
address upgradeExecutorExpectedAddress = computeCreateAddress(address(rollupCreator), 2);
address upgradeExecutorExpectedAddress = computeCreateAddress(address(rollupCreator), 4);
assertEq(
ProxyAdmin(_getProxyAdmin(address(rollup.sequencerInbox()))).owner(),
upgradeExecutorExpectedAddress,
Expand Down Expand Up @@ -346,7 +351,7 @@ contract RollupCreatorTest is Test {
validators[0] = makeAddr("validator1");
validators[1] = makeAddr("validator2");
address rollupAddress = rollupCreator.createRollup{value: factoryDeploymentFunds}(
config, batchPoster, validators, address(0)
config, batchPoster, validators, address(0), true, MAX_FEE_PER_GAS
);

vm.stopPrank();
Expand All @@ -356,7 +361,7 @@ contract RollupCreatorTest is Test {
address inbox = address(rollup.inbox());
address proxyAdmin = computeCreateAddress(address(rollupCreator), 1);
IUpgradeExecutor upgradeExecutor =
IUpgradeExecutor(computeCreateAddress(address(rollupCreator), 2));
IUpgradeExecutor(computeCreateAddress(address(rollupCreator), 4));

Dummy newLogicImpl = new Dummy();
bytes memory data = abi.encodeWithSelector(
Expand Down

0 comments on commit 03c98c0

Please sign in to comment.