-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from CrocSwap/scroll-l1gas
Add utils to estimate Scroll L1 gas
- Loading branch information
Showing
5 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@crocswap-libs/sdk", | ||
"version": "0.2.89", | ||
"version": "0.3.0", | ||
"description": "🛠🐊🛠 An SDK for building applications on top of CrocSwap", | ||
"author": "Ben Wolski <[email protected]>", | ||
"repository": "https://github.com/CrocSwap/sdk.git", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const L1_GAS_PRICE_ORACLE_ABI = [{"inputs":[{"internalType":"address","name":"_owner","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"l1BaseFee","type":"uint256"}],"name":"L1BaseFeeUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"overhead","type":"uint256"}],"name":"OverheadUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_oldOwner","type":"address"},{"indexed":true,"internalType":"address","name":"_newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"scalar","type":"uint256"}],"name":"ScalarUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_oldWhitelist","type":"address"},{"indexed":false,"internalType":"address","name":"_newWhitelist","type":"address"}],"name":"UpdateWhitelist","type":"event"},{"inputs":[{"internalType":"bytes","name":"_data","type":"bytes"}],"name":"getL1Fee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"_data","type":"bytes"}],"name":"getL1GasUsed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"l1BaseFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"overhead","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"scalar","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_l1BaseFee","type":"uint256"}],"name":"setL1BaseFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_overhead","type":"uint256"}],"name":"setOverhead","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_scalar","type":"uint256"}],"name":"setScalar","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newWhitelist","type":"address"}],"name":"updateWhitelist","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"whitelist","outputs":[{"internalType":"contract IWhitelist","name":"","type":"address"}],"stateMutability":"view","type":"function"}] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { BigNumber, Contract, PopulatedTransaction, Transaction, utils } from "ethers"; | ||
import { CrocEnv } from "../croc"; | ||
import { L1_GAS_PRICE_ORACLE_ABI } from "../abis/external/L1GasPriceOracle"; | ||
|
||
/** | ||
* Compute the raw transaction data for a given transaction. | ||
* | ||
* ref: https://docs.ethers.org/v5/cookbook/transactions/#cookbook--compute-raw-transaction | ||
*/ | ||
export function getRawTransaction(tx: Transaction) { | ||
function addKey(accum: any, key: keyof Transaction) { | ||
if (tx[key]) { accum[key] = tx[key]; } | ||
return accum; | ||
} | ||
|
||
// Extract the relevant parts of the transaction and signature | ||
const txFields = ["accessList","chainId","data","gasPrice","gasLimit","maxFeePerGas","maxPriorityFeePerGas","nonce","to","type","value"] as const; | ||
const sigFields = ["v","r","s"] as const; | ||
|
||
// Serialize the signed transaction | ||
const raw = utils.serializeTransaction(txFields.reduce(addKey, { }), sigFields.reduce(addKey, { })); | ||
|
||
// Double check things went well | ||
if (utils.keccak256(raw) !== tx.hash) { throw new Error("serializing failed!"); } | ||
|
||
return raw as `0x${string}`; | ||
} | ||
|
||
/** | ||
* Compute the raw transaction data for a given transaction without the signature. | ||
* | ||
* ref: https://docs.ethers.org/v5/cookbook/transactions/#cookbook--compute-raw-transaction | ||
*/ | ||
export function getUnsignedRawTransaction(tx: PopulatedTransaction) { | ||
function addKey(accum: any, key: keyof PopulatedTransaction) { | ||
if (tx[key]) { accum[key] = tx[key]; } | ||
return accum; | ||
} | ||
|
||
// Extract the relevant parts of the transaction and signature | ||
const txFields = ["accessList","chainId","data","gasPrice","gasLimit","maxFeePerGas","maxPriorityFeePerGas","nonce","to","type","value"] as const; | ||
|
||
// Serialize the signed transaction | ||
const raw = utils.serializeTransaction(txFields.reduce(addKey, { })); | ||
|
||
return raw as `0x${string}`; | ||
} | ||
|
||
/** | ||
* Estimates the additional L1 gas on Scroll for any data which is a RLP-encoded transaction with signature. | ||
*/ | ||
export async function estimateScrollL1Gas(crocEnv: CrocEnv, rawTransaction: `0x${string}`): Promise<BigNumber> { | ||
const crocContext = await crocEnv.context; | ||
const chainId = crocContext.chain.chainId; | ||
const isScroll = chainId === "0x82750" || chainId === "0x8274f"; | ||
if (!isScroll) { | ||
return BigNumber.from(0); | ||
} | ||
|
||
const L1_GAS_PRICE_ORACLE_ADDRESS = "0x5300000000000000000000000000000000000002"; | ||
const l1GasPriceOracle = new Contract(L1_GAS_PRICE_ORACLE_ADDRESS, L1_GAS_PRICE_ORACLE_ABI, crocContext.provider); | ||
|
||
// function getL1Fee(bytes memory _data) external view override returns (uint256); | ||
const l1Gas = await l1GasPriceOracle.getL1Fee(rawTransaction) as BigNumber; | ||
return l1Gas; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters