Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added gasprice + gaslimit override params #117

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ task('setStages', 'Set stages for ERC721M')
.addParam('contract', 'contract address')
.addParam('stages', 'stages json file')
.addOptionalParam('gaspricegwei', 'Set gas price in Gwei')
.addOptionalParam('gaslimit', 'Set maximum gas units to spend on transaction', 500000, types.int)
.setAction(setStages);

task('setMintable', 'Set mintable state for ERC721M')
Expand Down Expand Up @@ -159,12 +160,15 @@ task('deploy', 'Deploy ERC721M')
.addOptionalParam<boolean>('useerc2198', 'whether or not to use ERC2198', true, types.boolean)
.addOptionalParam('erc2198royaltyreceiver', 'erc2198 royalty receiver address')
.addOptionalParam('erc2198royaltyfeenumerator', 'erc2198 royalty fee numerator')
.addOptionalParam('gaspricegwei', 'Set gas price in Gwei')
.addOptionalParam('gaslimit', 'Set maximum gas units to spend on transaction')
.setAction(deploy);

task('setBaseURI', 'Set the base uri')
.addParam('uri', 'uri')
.addParam('contract', 'contract address')
.addOptionalParam('gaspricegwei', 'Set gas price in Gwei')
.addOptionalParam('gaslimit', 'Set maximum gas units to spend on transaction', 500000, types.int)
.setAction(setBaseURI);

task('setCrossmintAddress', 'Set crossmint address')
Expand All @@ -182,6 +186,8 @@ task('ownerMint', 'Mint token(s) as owner')
.addParam('contract', 'contract address')
.addParam('qty', 'quantity to mint', '1')
.addOptionalParam('to', 'recipient address')
.addOptionalParam('gaspricegwei', 'Set gas price in Gwei')
.addOptionalParam('gaslimit', 'Set maximum gas units to spend on transaction')
.setAction(ownerMint);

task('setGlobalWalletLimit', 'Set the global wallet limit')
Expand Down
26 changes: 22 additions & 4 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { confirm } from '@inquirer/prompts';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { ContractDetails } from './common/constants';
import { checkCodeVersion, estimateGas } from './utils/helper';
import { Overrides } from 'ethers';

export interface IDeployParams {
name: string;
Expand All @@ -25,8 +26,10 @@ export interface IDeployParams {
mintcurrency?: string;
useerc721c?: boolean;
useerc2198?: boolean;
erc2198royaltyreceiver?: string,
erc2198royaltyfeenumerator?: number,
erc2198royaltyreceiver?: string;
erc2198royaltyfeenumerator?: number;
gaspricegwei?: number;
gaslimit?: number;
}

export const deploy = async (
Expand Down Expand Up @@ -71,6 +74,14 @@ export const deploy = async (
maxsupply = hre.ethers.BigNumber.from('999999999');
}

const overrides: Overrides = {};
if (args.gaspricegwei) {
overrides.gasPrice = hre.ethers.BigNumber.from(args.gaspricegwei * 1e9);
}
if (args.gaslimit) {
overrides.gasLimit = hre.ethers.BigNumber.from(args.gaslimit);
}

const contractFactory = await hre.ethers.getContractFactory(contractName);

const params = [
Expand Down Expand Up @@ -100,11 +111,18 @@ export const deploy = async (
JSON.stringify(args, null, 2),
);

await estimateGas(hre, contractFactory.getDeployTransaction(...params));
if (
!(await estimateGas(
hre,
contractFactory.getDeployTransaction(...params),
overrides,
))
)
return;

if (!(await confirm({ message: 'Continue to deploy?' }))) return;

const contract = await contractFactory.deploy(...params);
const contract = await contractFactory.deploy(...params, overrides);
console.log('Deploying contract... ');
console.log('tx:', contract.deployTransaction.hash);

Expand Down
14 changes: 12 additions & 2 deletions scripts/ownerMint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { confirm } from '@inquirer/prompts';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { ContractDetails } from './common/constants';
import { estimateGas } from './utils/helper';
import { Overrides } from 'ethers';

export interface IOwnerMintParams {
contract: string;
to?: string;
qty?: string;
gaspricegwei?: number;
gaslimit?: number;
}

export const ownerMint = async (
Expand All @@ -19,13 +22,20 @@ export const ownerMint = async (
const contract = ERC721M.attach(args.contract);
const qty = ethers.BigNumber.from(args.qty ?? 1);
const to = args.to ?? (await contract.signer.getAddress());
const overrides: Overrides = {};
if (args.gaspricegwei) {
overrides.gasPrice = ethers.BigNumber.from(args.gaspricegwei * 1e9);
}
if (args.gaslimit) {
overrides.gasLimit = ethers.BigNumber.from(args.gaslimit);
}

const tx = await contract.populateTransaction.ownerMint(qty, to);
await estimateGas(hre, tx);
if (!(await estimateGas(hre, tx, overrides))) return;
console.log(`Going to mint ${qty.toNumber()} token(s) to ${to}`);
if (!await confirm({ message: 'Continue?' })) return;

const submittedTx = await contract.ownerMint(qty, to);
const submittedTx = await contract.ownerMint(qty, to, overrides);

console.log(`Submitted tx ${submittedTx.hash}`);
await submittedTx.wait();
Expand Down
21 changes: 14 additions & 7 deletions scripts/setBaseURI.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { ContractDetails } from './common/constants';
import { Overrides } from 'ethers';
import { estimateGas } from './utils/helper';

export interface ISetBaseURIParams {
uri: string;
contract: string;
gaspricegwei?: number;
gaslimit?: number;
}

export const setBaseURI = async (
args: ISetBaseURIParams,
hre: HardhatRuntimeEnvironment,
) => {
const { ethers } = hre;
const overrides: any = {gasLimit: 500_000};

const overrides: Overrides = {};
if (args.gaspricegwei) {
overrides.gasPrice = args.gaspricegwei * 1e9;
overrides.gasPrice = ethers.BigNumber.from(args.gaspricegwei * 1e9);
}
if (args.gaslimit) {
overrides.gasLimit = ethers.BigNumber.from(args.gaslimit);
}
const ERC721M = await ethers.getContractFactory(ContractDetails.ERC721M.name);
const contract = ERC721M.attach(args.contract);
const tx = await contract.setBaseURI(args.uri, overrides);
const tx = await contract.populateTransaction.setBaseURI(args.uri);
if (!(await estimateGas(hre, tx, overrides))) return;
const submittedTx = await contract.setBaseURI(args.uri, overrides);

console.log(`Submitted tx ${tx.hash}`);
console.log(`Submitted tx ${submittedTx.hash}`);

await tx.wait();
await submittedTx.wait();

console.log('Set baseURI:', tx.hash);
console.log('Set baseURI:', submittedTx.hash);
};
14 changes: 9 additions & 5 deletions scripts/setStages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { MerkleTree } from 'merkletreejs';
import fs from 'fs';
import { ContractDetails } from './common/constants';
import { estimateGas } from './utils/helper';
import { Overrides } from 'ethers';

export interface ISetStagesParams {
stages: string;
contract: string;
gaspricegwei?: number;
gaslimit?: number;
}

interface StageConfig {
Expand All @@ -29,13 +31,15 @@ export const setStages = async (
fs.readFileSync(args.stages, 'utf-8'),
) as StageConfig[];

const overrides: any = { gasLimit: 500_000 };

const ERC721M = await ethers.getContractFactory(ContractDetails.ERC721M.name);
const contract = ERC721M.attach(args.contract);

const overrides: Overrides = {};
if (args.gaspricegwei) {
overrides.gasPrice = args.gaspricegwei * 1e9;
overrides.gasPrice = ethers.BigNumber.from(args.gaspricegwei * 1e9);
}
if (args.gaslimit) {
overrides.gasLimit = ethers.BigNumber.from(args.gaslimit);
}
const merkleRoots = await Promise.all(
stagesConfig.map((stage) => {
Expand Down Expand Up @@ -85,8 +89,8 @@ export const setStages = async (
),
);

const tx = await contract.populateTransaction.setStages(stages, overrides);
estimateGas(hre, tx);
const tx = await contract.populateTransaction.setStages(stages);
if (!(await estimateGas(hre, tx, overrides))) return;

if (!await confirm({ message: 'Continue to set stages?' })) return;

Expand Down
81 changes: 74 additions & 7 deletions scripts/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { Deferrable } from 'ethers/lib/utils';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { TransactionRequest } from "@ethersproject/abstract-provider";
import * as child from 'child_process';
import { BigNumber, Overrides } from 'ethers';

const gasPricePctDiffAlert = 20; // Set threshold to alert when attempting to under/overpay against the current gas price median by X% (e.g. 20 = 20%)

export const checkCodeVersion = async () => {
const localLatestCommit = child.execSync('git rev-parse HEAD').toString().trim();
Expand All @@ -19,15 +22,79 @@ export const checkCodeVersion = async () => {
return true;
}

export const estimateGas = async (hre: HardhatRuntimeEnvironment, tx: Deferrable<TransactionRequest>) => {
export const estimateGas = async (
hre: HardhatRuntimeEnvironment,
tx: Deferrable<TransactionRequest>,
overrides?: Overrides,
) => {
const overrideGasLimit = overrides?.gasLimit as BigNumber;
const overrideGasPrice = overrides?.gasPrice as BigNumber;
const estimatedGasUnit = await hre.ethers.provider.estimateGas(tx);
const estimatedGasPrice = await hre.ethers.provider.getGasPrice();
const estimatedGas = estimatedGasUnit.mul(estimatedGasPrice);
console.log('Estimated gas unit: ', estimatedGasUnit.toString());
console.log('Estimated gas price (GWei): ', estimatedGasPrice.div(1000000000).toString());
console.log(`Estimated gas (${getTokenName(hre)}): `, hre.ethers.utils.formatEther(estimatedGas));
return estimatedGas;
}
const estimatedGasCost = estimatedGasUnit.mul(
overrideGasPrice ?? estimatedGasPrice,
);
if (overrideGasLimit && overrideGasLimit < estimatedGasUnit) {
const diffPct =
(estimatedGasUnit.toNumber() / overrideGasLimit.toNumber() - 1) * 100;
console.log(
'\x1b[31m[WARNING]\x1b[0m Estimated gas units required exceeds the limit set:',
`\x1b[33m${estimatedGasUnit.toNumber().toLocaleString()}\x1b[0m`,
`(${
diffPct > 0 ? '+' + diffPct.toFixed(2) : diffPct.toFixed(2)
}% to the --gaslimit \x1b[33m${overrideGasLimit
.toNumber()
.toLocaleString()}\x1b[0m)`,
);
if (
!(await confirm({
message: 'There is higher probability of failure. Continue?',
}))
)
return null;
} else
console.log(
'Estimated gas unit:',
`\x1b[33m${estimatedGasUnit.toNumber().toLocaleString()}\x1b[0m`,
);

const estimatedGasPriceFormat = estimatedGasPrice.div(1e9).toNumber();
const overrideGasPriceFormat = overrideGasPrice
? overrideGasPrice.div(1e9).toNumber()
: null;
if (
overrideGasPriceFormat &&
overrideGasPriceFormat !== estimatedGasPriceFormat
) {
const diffPct =
(overrideGasPriceFormat / estimatedGasPriceFormat - 1) * 100;
console.log(
'\x1b[31m[WARNING]\x1b[0m Override gas price set (GWEI):',
overrideGasPriceFormat,
`(${
diffPct > 0 ? '+' + diffPct.toFixed(2) : diffPct.toFixed(2)
}% to estimated gas price`,
estimatedGasPriceFormat,
`)`,
);
if (Math.abs(diffPct) > gasPricePctDiffAlert) {
const aboveOrBelow = diffPct > 0 ? 'ABOVE' : 'BELOW';
if (
!(await confirm({
message: `You are attempting to pay more than ${gasPricePctDiffAlert}% ${aboveOrBelow} estimated gas price. Continue?`,
}))
)
return null;
}
} else console.log('Estimated gas price (GWEI):', estimatedGasPriceFormat);

console.log(
`Estimated gas cost (${getTokenName(hre)}):`,
`\x1b[33m${hre.ethers.utils.formatEther(estimatedGasCost)}\x1b[0m`,
);

return estimatedGasCost;
};

const getTokenName = (hre: HardhatRuntimeEnvironment) => {
switch(hre.network.name) {
Expand Down
Loading