Skip to content

Commit

Permalink
fix gas cost calculation for optimism based L2s (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
beshup authored Dec 2, 2023
1 parent bd14436 commit 0a26f1e
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 21 deletions.
3 changes: 3 additions & 0 deletions file-system/abi/L1Block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"abi": [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"DEPOSITOR_ACCOUNT","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"basefee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"batcherHash","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"hash","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"l1FeeOverhead","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"l1FeeScalar","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"number","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"sequenceNumber","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint64","name":"_number","type":"uint64"},{"internalType":"uint64","name":"_timestamp","type":"uint64"},{"internalType":"uint256","name":"_basefee","type":"uint256"},{"internalType":"bytes32","name":"_hash","type":"bytes32"},{"internalType":"uint64","name":"_sequenceNumber","type":"uint64"},{"internalType":"bytes32","name":"_batcherHash","type":"bytes32"},{"internalType":"uint256","name":"_l1FeeOverhead","type":"uint256"},{"internalType":"uint256","name":"_l1FeeScalar","type":"uint256"}],"name":"setL1BlockValues","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"timestamp","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"version","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]
}
86 changes: 70 additions & 16 deletions file-system/config.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,75 @@
{
"addresses": {
"ethereum": "0xd687847559A3bEc088251f3cC33E7BAf31e4aB48",
"goerli": "0x29e3F4B932c1E0B989E3B6AbCf56Ae342c5AD65a",
"polygon": "0x117542b736cB5314a59453081b66208863CC1Acc",
"mumbai": "0x513bd3bc623c42a807fBD162a58682941A12935F",
"arbitrum": "0x4c3896dd0b55B3B62D560620B1D8bF99643fFCCE",
"arbitrum-goerli": "0x11Eb3C36fa9b2B0F4Ee67BCe1016960588A814ba",
"optimism": "0x21c3a69EaD9b81863B83757ff2645803fF7c7690",
"optimism-goerli": "0x20475183625aE0eD5Dcd2553a660B06FF52af8Bd",
"base": "0x799d1CC242637847756f0400d1F83FCF94Cb051e",
"base-goerli": "0xB644D70A52b4e555815EF3Ec76488dbdA9DF972D",
"zora": "0xB627f0469683f68aC78E1deD4eFA8545aa4c4DE3",
"zora-goerli": "0xAFfC7C9BfB48FFD2a580e1a0d36f8cc7D45Dcb58"
"ethereum": {
"networkType": "mainnet",
"isOPBasedL2": false,
"fileDeployer": "0xd687847559A3bEc088251f3cC33E7BAf31e4aB48"
},
"goerli": {
"networkType": "testnet",
"isOPBasedL2": false,
"fileDeployer": "0x29e3F4B932c1E0B989E3B6AbCf56Ae342c5AD65a"
},
"polygon": {
"networkType": "mainnet",
"isOPBasedL2": false,
"fileDeployer": "0x117542b736cB5314a59453081b66208863CC1Acc"
},
"mumbai": {
"networkType": "testnet",
"isOPBasedL2": false,
"fileDeployer": "0x513bd3bc623c42a807fBD162a58682941A12935F"
},
"arbitrum": {
"networkType": "mainnet",
"isOPBasedL2": false,
"fileDeployer": "0x4c3896dd0b55B3B62D560620B1D8bF99643fFCCE"
},
"arbitrum-goerli": {
"networkType": "testnet",
"isOPBasedL2": false,
"fileDeployer": "0x11Eb3C36fa9b2B0F4Ee67BCe1016960588A814ba"
},
"optimism": {
"networkType": "mainnet",
"isOPBasedL2": true,
"fileDeployer": "0x21c3a69EaD9b81863B83757ff2645803fF7c7690",
"opL1Block": "0x4200000000000000000000000000000000000015"
},
"optimism-goerli": {
"networkType": "testnet",
"isOPBasedL2": true,
"fileDeployer": "0x20475183625aE0eD5Dcd2553a660B06FF52af8Bd",
"opL1Block": "0x4200000000000000000000000000000000000015"
},
"base": {
"networkType": "mainnet",
"isOPBasedL2": true,
"fileDeployer": "0x799d1CC242637847756f0400d1F83FCF94Cb051e",
"opL1Block": "0x4200000000000000000000000000000000000015"
},
"base-goerli": {
"networkType": "testnet",
"isOPBasedL2": true,
"fileDeployer": "0xB644D70A52b4e555815EF3Ec76488dbdA9DF972D",
"opL1Block": "0x4200000000000000000000000000000000000015"
},
"zora": {
"networkType": "mainnet",
"isOPBasedL2": true,
"fileDeployer": "0xB627f0469683f68aC78E1deD4eFA8545aa4c4DE3",
"opL1Block": "0x4200000000000000000000000000000000000015"
},
"zora-goerli": {
"networkType": "testnet",
"isOPBasedL2": true,
"fileDeployer": "0xAFfC7C9BfB48FFD2a580e1a0d36f8cc7D45Dcb58",
"opL1Block": "0x4200000000000000000000000000000000000015"
}
},
"__accountsInstructions": [
"Add the private key to your env, named as `HL_FS_PK_<account name>`",
"Eg. HL_FS_PK_DEFAULT=<your private key>",
"Use fully capitalized account names as convention, the file system will auto capitalize your account name"
"Add the private key to your env, named as `HL_FS_PK_<account name>`",
"Eg. HL_FS_PK_DEFAULT=<your private key>",
"Use fully capitalized account names as convention, the file system will auto capitalize your account name"
]
}
}
2 changes: 1 addition & 1 deletion file-system/src/addFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @argument name File name in file-system/files
* @argument network Network name, as available in chains.json
* @argument contract Address of collection to add file to
* @argument account (optional) Account name if different from 'default', as available in file-system/config.json
* @argument account (optional) Account name if different from 'default', as available in env
*/

const fs = require("fs");
Expand Down
21 changes: 20 additions & 1 deletion file-system/src/checkCost.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ const ethers = require("ethers");

const config = require("../config.json");
const rpcConfig = require("../../chains.json");
const L1BlockABI = require("../abi/L1Block.json").abi;

const { FILE_NAME_FORMAT } = require("./constants.js");
const APPROX_DEPLOY_COMPUTE_PER_BYTE = BigInt(250);
const APPROX_DEPLOY_L1_COMPUTE_OP_BASED_PER_BYTE = BigInt(17);
const FEE_SCALAR_BASE = BigInt(1000000);

const checkCost = async (fileName, network, useLegacyGas) => {
if (network === "mainnet") network = "ethereum";
Expand All @@ -37,7 +40,23 @@ const checkCost = async (fileName, network, useLegacyGas) => {

const stats = await fs.promises.stat(filePath);
console.log(`File size: ${stats.size} bytes`);
const weiConsumed = gasPrice * APPROX_DEPLOY_COMPUTE_PER_BYTE * BigInt(stats.size);
let weiConsumed = gasPrice * APPROX_DEPLOY_COMPUTE_PER_BYTE * BigInt(stats.size);
if (config.addresses[network].isOPBasedL2) {
const l1Block = new ethers.Contract(config.addresses[network].opL1Block, L1BlockABI, provider);
const feeScalar = await l1Block.l1FeeScalar();

const l1Provider = ethers.getDefaultProvider(rpcConfig["ethereum"].rpc);
let l1GasPrice;
try {
const feeData = await l1Provider.getFeeData();
l1GasPrice = feeData.gasPrice;
} catch (error) {
throw new Error(`Failed to get gas fee data for ethereum (L1) using rpc ${rpcConfig["ethereum"].rpc}`);
}

const l1WeiConsumed = l1GasPrice * APPROX_DEPLOY_L1_COMPUTE_OP_BASED_PER_BYTE * BigInt(stats.size) * feeScalar / FEE_SCALAR_BASE;
weiConsumed = weiConsumed + l1WeiConsumed;
}
console.log(`At current gas fees, approximate cost to deploy on ${network} is ${ethers.formatEther(weiConsumed)} ETH`);
}

Expand Down
4 changes: 2 additions & 2 deletions file-system/src/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @description Deploys a file on-chain
* @argument file File name in file-system/files
* @argument network Network name, as available in chains.json
* @argument account (optional) Account name if different from 'default', as available in file-system/config.json
* @argument account (optional) Account name if different from 'default', as available in env
*/

const fs = require("fs");
Expand Down Expand Up @@ -86,7 +86,7 @@ const deploy = async (fileName, network, accountName) => {
return { names, fileContents, txIndex: index + 1, uploadChunkPaths }
}))

const fileDeployer = new ethers.Contract(config.addresses[network], FileDeployerABI, signer);
const fileDeployer = new ethers.Contract(config.addresses[network].fileDeployer, FileDeployerABI, signer);
const txHashes = [];
console.log("Submitting transactions...\n")
for (const uploadChunk of uploadChunks) {
Expand Down
2 changes: 1 addition & 1 deletion file-system/src/removeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @argument name File name in file-system/files
* @argument network Network name, as available in chains.json
* @argument contract Address of collection to add file to
* @argument account (optional) Account name if different from 'default', as available in file-system/config.json
* @argument account (optional) Account name if different from 'default', as available in env
*/

const commandLineArgs = require('command-line-args');
Expand Down

0 comments on commit 0a26f1e

Please sign in to comment.