-
-
Save alephao/1a713154c7a4879ff0b8875a215bb3fd to your computer and use it in GitHub Desktop.
Deploy with low gas cost
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
WS_URL=wss://eth-mainnet.alchemyapi.io/v2/<key> |
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
import { execSync } from "child_process"; | |
import { BigNumber, ethers } from "ethers"; | |
import { formatUnits } from "ethers/lib/utils"; | |
import { exit } from "process"; | |
require("dotenv").config(); | |
// eth rpc web socket url | |
const WS_URL = process.env.WS_URL!; | |
const gwei = (val: string | number) => { | |
return ethers.utils.parseUnits(`${val}`, "gwei"); | |
}; | |
const deploy = () => { | |
// ... deployment code | |
exit(0); | |
} | |
const main = async () => { | |
execSync("forge build --force --optimize"); | |
const wsProvider = new ethers.providers.WebSocketProvider(WS_URL); | |
const target = gwei("28"); | |
let done = false; | |
// subscribe to every new head and check the baseFeePerGas | |
wsProvider._subscribe("block", ["newHeads"], (result: any) => { | |
if (done) { | |
return; | |
} | |
const baseFeePerGas = BigNumber.from(result.baseFeePerGas); | |
const humanReadableBaseFeePerGas = formatUnits( | |
baseFeePerGas.toString(), | |
"gwei" | |
); | |
if (baseFeePerGas.lte(target)) { | |
console.log(humanReadableBaseFeePerGas, "✅"); | |
done = true; | |
deploy(); | |
} else { | |
console.log(humanReadableBaseFeePerGas, "❌"); | |
} | |
}); | |
}; | |
main(); |
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
{ | |
"scripts": { | |
"deploy": "ts-node scripts/deploy.ts" | |
}, | |
"devDependencies": { | |
"@types/node": "^17.0.24", | |
"ts-node": "^10.7.0", | |
"typescript": "^4.6.3" | |
}, | |
"dependencies": { | |
"dotenv": "^16.0.0", | |
"ethers": "^5.6.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment