Skip to content

Commit

Permalink
fix(cli): add support for legacy transactions in deploy script (#1178)
Browse files Browse the repository at this point in the history
Co-authored-by: alvarius <alvarius@lattice.xyz>
  • Loading branch information
TheGreatAxios and alvrs authored Jul 28, 2023
1 parent c963b46 commit 168a4cb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-hotels-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/cli": patch
---

Add support for legacy transactions in deploy script by falling back to `gasPrice` if `lastBaseFeePerGas` is not available
38 changes: 27 additions & 11 deletions packages/cli/src/utils/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ export async function deploy(
console.log("Initial nonce", nonce);

// Compute maxFeePerGas and maxPriorityFeePerGas like ethers, but allow for a multiplier to allow replacing pending transactions
let maxPriorityFeePerGas: number;
let maxFeePerGas: BigNumber;
let maxPriorityFeePerGas: number | undefined;
let maxFeePerGas: BigNumber | undefined;
let gasPrice: BigNumber | undefined;

await setInternalFeePerGas(priorityFeeMultiplier);

// Catch all to await any promises before exiting the script
Expand Down Expand Up @@ -402,6 +404,7 @@ export async function deploy(
nonce: nonce++,
maxPriorityFeePerGas,
maxFeePerGas,
gasPrice,
})
.then((c) => (disableTxWait ? c : c.deployed()));

Expand Down Expand Up @@ -497,7 +500,7 @@ export async function deploy(
const gasLimit = await contract.estimateGas[func].apply(null, args);
console.log(chalk.gray(`executing transaction: ${functionName} with nonce ${nonce}`));
const txPromise = contract[func]
.apply(null, [...args, { gasLimit, nonce: nonce++, maxPriorityFeePerGas, maxFeePerGas }])
.apply(null, [...args, { gasLimit, nonce: nonce++, maxPriorityFeePerGas, maxFeePerGas, gasPrice }])
.then((tx: any) => (confirmations === 0 ? tx : tx.wait(confirmations)));
promises.push(txPromise);
return txPromise;
Expand Down Expand Up @@ -542,15 +545,28 @@ export async function deploy(
async function setInternalFeePerGas(multiplier: number) {
// Compute maxFeePerGas and maxPriorityFeePerGas like ethers, but allow for a multiplier to allow replacing pending transactions
const feeData = await provider.getFeeData();
if (!feeData.lastBaseFeePerGas) throw new MUDError("Can not fetch lastBaseFeePerGas from RPC");
if (!feeData.lastBaseFeePerGas.eq(0) && (await signer.getBalance()).eq(0)) {
throw new MUDError(`Attempting to deploy to a chain with non-zero base fee with an account that has no balance.
If you're deploying to the Lattice testnet, you can fund your account by running 'pnpm mud faucet --address ${await signer.getAddress()}'`);
}

// Set the priority fee to 0 for development chains with no base fee, to allow transactions from unfunded wallets
maxPriorityFeePerGas = feeData.lastBaseFeePerGas.eq(0) ? 0 : Math.floor(1_500_000_000 * multiplier);
maxFeePerGas = feeData.lastBaseFeePerGas.mul(2).add(maxPriorityFeePerGas);
if (feeData.lastBaseFeePerGas) {
if (!feeData.lastBaseFeePerGas.eq(0) && (await signer.getBalance()).eq(0)) {
throw new MUDError(`Attempting to deploy to a chain with non-zero base fee with an account that has no balance.
If you're deploying to the Lattice testnet, you can fund your account by running 'pnpm mud faucet --address ${await signer.getAddress()}'`);
}

// Set the priority fee to 0 for development chains with no base fee, to allow transactions from unfunded wallets
maxPriorityFeePerGas = feeData.lastBaseFeePerGas.eq(0) ? 0 : Math.floor(1_500_000_000 * multiplier);
maxFeePerGas = feeData.lastBaseFeePerGas.mul(2).add(maxPriorityFeePerGas);
} else if (feeData.gasPrice) {
// Legacy chains with gasPrice instead of maxFeePerGas
if (!feeData.gasPrice.eq(0) && (await signer.getBalance()).eq(0)) {
throw new MUDError(
`Attempting to deploy to a chain with non-zero gas price with an account that has no balance.`
);
}

gasPrice = feeData.gasPrice;
} else {
throw new MUDError("Can not fetch fee data from RPC");
}
}
}

Expand Down

0 comments on commit 168a4cb

Please sign in to comment.