Skip to content

Commit

Permalink
Merge branch 'main' into fix/cypress-test
Browse files Browse the repository at this point in the history
  • Loading branch information
saeta-eth authored Jul 23, 2024
2 parents 4d4d7f4 + 94145ca commit 1318b2f
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 72 deletions.
28 changes: 14 additions & 14 deletions packages/builder/src/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,67 +178,67 @@ describe('runtime.ts', () => {

describe('gas settings', () => {
it('sets the gas price', async () => {
const gasPrice = '123456';
const gasPrice = viem.parseGwei('123456');
const newRuntime = runtime.derive({
gasPrice,
});

expect(newRuntime.gasPrice).toBe(viem.parseGwei(gasPrice).toString());
expect(newRuntime.gasPrice).toBe(gasPrice);
expect(newRuntime.gasFee).toBeUndefined();
expect(newRuntime.priorityGasFee).toBeUndefined();
});
it('sets the gas price and derive again', async () => {
const gasPrice = '123456';
const gasPrice = viem.parseGwei('123456');
const newRuntime = runtime.derive({
gasPrice,
});

const newNewRuntime = newRuntime.derive({});

expect(newNewRuntime.gasPrice).toBe(viem.parseGwei(gasPrice).toString());
expect(newNewRuntime.gasPrice).toBe(gasPrice);
expect(newNewRuntime.gasFee).toBeUndefined();
expect(newNewRuntime.priorityGasFee).toBeUndefined();
});

it('sets gas fee price', async () => {
const gasFee = '123456';
const gasFee = viem.parseGwei('123456');
const newRuntime = runtime.derive({
gasFee,
});

expect(newRuntime.gasFee).toBe(viem.parseGwei(gasFee).toString());
expect(newRuntime.gasFee).toBe(gasFee);
expect(newRuntime.gasPrice).toBeUndefined();
expect(newRuntime.priorityGasFee).toBeUndefined();
});

it('sets priority gas fee price', async () => {
const gasFee = '123456';
const priorityGasFee = '012345';
const gasFee = viem.parseGwei('123456');
const priorityGasFee = viem.parseGwei('012345');
const newRuntime = runtime.derive({
gasFee,
priorityGasFee,
});

expect(newRuntime.gasFee).toBe(viem.parseGwei(gasFee).toString());
expect(newRuntime.priorityGasFee).toBe(viem.parseGwei(priorityGasFee).toString());
expect(newRuntime.gasFee).toBe(gasFee);
expect(newRuntime.priorityGasFee).toBe(priorityGasFee);
expect(newRuntime.gasPrice).toBeUndefined();
});

it('ignore gas price if gas fee is set', async () => {
const gasFee = '123456';
const gasPrice = '012345';
const gasFee = viem.parseGwei('123456');
const gasPrice = viem.parseGwei('012345');
const newRuntime = runtime.derive({
gasFee,
gasPrice,
});

expect(newRuntime.gasFee).toBe(viem.parseGwei(gasFee).toString());
expect(newRuntime.gasFee).toBe(gasFee);
expect(newRuntime.priorityGasFee).toBeUndefined();
expect(newRuntime.gasPrice).toBeUndefined();
});

it('throw if priority gas fee is set without gas fee', async () => {
const priorityGasFee = '012345';
const priorityGasFee = viem.parseGwei('012345');
expect(() =>
runtime.derive({
priorityGasFee,
Expand Down
24 changes: 9 additions & 15 deletions packages/builder/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@ export class CannonStorage extends EventEmitter {
}
}

const parseGasValue = (value: string | undefined) => {
if (!value) return undefined;

return viem.parseGwei(value).toString();
};

export class ChainBuilderRuntime extends CannonStorage implements ChainBuilderRuntimeInfo {
provider: viem.PublicClient;
readonly chainId: number;
Expand All @@ -121,9 +115,9 @@ export class ChainBuilderRuntime extends CannonStorage implements ChainBuilderRu
ctx: ChainBuilderContext | null;
private publicSourceCode: boolean | undefined;
private signals: { cancelled: boolean } = { cancelled: false };
private _gasPrice: string | undefined;
private _gasFee: string | undefined;
private _priorityGasFee: string | undefined;
private _gasPrice: bigint | undefined;
private _gasFee: bigint | undefined;
private _priorityGasFee: bigint | undefined;

private cleanSnapshot: any;

Expand Down Expand Up @@ -177,14 +171,14 @@ export class ChainBuilderRuntime extends CannonStorage implements ChainBuilderRu
}
}

this._gasFee = parseGasValue(info.gasFee);
this._priorityGasFee = parseGasValue(info.priorityGasFee);
this._gasFee = info.gasFee;
this._priorityGasFee = info.priorityGasFee;

if (info.gasPrice) {
if (info.gasFee) {
debug(yellow('WARNING: gasPrice is ignored when gasFee is set'));
} else {
this._gasPrice = parseGasValue(info.gasPrice);
this._gasPrice = info.gasPrice;
}
}
}
Expand All @@ -193,13 +187,13 @@ export class ChainBuilderRuntime extends CannonStorage implements ChainBuilderRu
this.signals.cancelled = true;
}

get gasPrice(): string | undefined {
get gasPrice(): bigint | undefined {
return this._gasPrice;
}
get gasFee(): string | undefined {
get gasFee(): bigint | undefined {
return this._gasFee;
}
get priorityGasFee(): string | undefined {
get priorityGasFee(): bigint | undefined {
return this._priorityGasFee;
}

Expand Down
9 changes: 9 additions & 0 deletions packages/builder/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,15 @@ export const routerSchema = z
* Used to force new copy of a contract (not actually used)
*/
salt: z.string().optional().describe('Used to force new copy of a contract (not actually used)'),
/**
* Override transaction settings
*/
overrides: z
.object({
gasLimit: z.string().optional(),
})
.optional()
.describe('Override transaction settings'),
/**
* List of operations that this operation depends on, which Cannon will execute first. If unspecified, Cannon automatically detects dependencies.
*/
Expand Down
18 changes: 18 additions & 0 deletions packages/builder/src/steps/clone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ describe('steps/clone.ts', () => {
});
});

describe('getInputs()', () => {
it('has inputs', async () => {
expect(
action.getInputs(
{
source: '<%= settings.a %>',
var: { woot: '<%= settings.b %>', wah: '<%= settings.c %>' },
options: { woot: '<%= settings.d %>', wah: '<%= settings.e %>', tags: '<%= settings.f %>' },
},
[]
)
).toEqual({
accesses: ['settings.a', 'settings.b', 'settings.c', 'settings.d', 'settings.e', 'settings.f'],
unableToCompute: false,
});
});
});

describe('configInject()', () => {
it('injects all fields', async () => {
const result = action.configInject(
Expand Down
4 changes: 4 additions & 0 deletions packages/builder/src/steps/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ const cloneSpec = {
accesses = mergeTemplateAccesses(accesses, computeTemplateAccesses(config.sourcePreset, possibleFields));
accesses = mergeTemplateAccesses(accesses, computeTemplateAccesses(config.targetPreset, possibleFields));

if (config.var) {
_.forEach(config.var, (a) => (accesses = mergeTemplateAccesses(accesses, computeTemplateAccesses(a, possibleFields))));
}

if (config.options) {
_.forEach(
config.options,
Expand Down
25 changes: 25 additions & 0 deletions packages/builder/src/steps/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ const routerStep = {
config.salt = template(config.salt)(ctx);
}

if (config?.overrides?.gasLimit) {
config.overrides.gasLimit = template(config.overrides.gasLimit)(ctx);
}

return config;
},

Expand All @@ -75,6 +79,10 @@ const routerStep = {
...config.contracts.map((c) => (c.includes('.') ? `imports.${c.split('.')[0]}` : `contracts.${c}`))
);

if (config?.overrides) {
accesses = mergeTemplateAccesses(accesses, computeTemplateAccesses(config.overrides.gasLimit, possibleFields));
}

return accesses;
},

Expand Down Expand Up @@ -157,6 +165,23 @@ const routerStep = {
}),
chain: undefined,
});

if (config.overrides?.gasLimit) {
preparedTxn.gas = BigInt(config.overrides.gasLimit);
}

if (runtime.gasPrice) {
preparedTxn.gasPrice = runtime.gasPrice;
}

if (runtime.gasFee) {
preparedTxn.maxFeePerGas = runtime.gasFee;
}

if (runtime.priorityGasFee) {
preparedTxn.maxPriorityFeePerGas = runtime.priorityGasFee;
}

const hash = await signer.wallet.sendTransaction(preparedTxn as any);

const receipt = await runtime.provider.waitForTransactionReceipt({ hash });
Expand Down
6 changes: 3 additions & 3 deletions packages/builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ export interface ChainBuilderRuntimeInfo {
allowPartialDeploy: boolean;

// Gas price to use for transactions
gasPrice?: string;
gasPrice?: bigint;

// Base and Priority gas fee to use for transactions - EIP1559
gasFee?: string;
priorityGasFee?: string;
gasFee?: bigint;
priorityGasFee?: bigint;
}

export interface PackageState {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ interface Params {
publicSourceCode?: boolean;
providerUrl?: string;
registryPriority?: 'local' | 'onchain' | 'offline';
gasPrice?: string;
gasFee?: string;
priorityGasFee?: string;
gasPrice?: bigint;
gasFee?: bigint;
priorityGasFee?: bigint;
writeScript?: string;
writeScriptFormat?: WriteScriptFormat;
}
Expand Down
22 changes: 14 additions & 8 deletions packages/cli/src/commands/interact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 +340,21 @@ async function execTxn({
args,
});

let txn: viem.TransactionRequest | null = null;
let txn: viem.TransactionRequest = {
to: contract.address,
data: callData,
value: value || 0,
} as any;

// estimate gas
try {
const chain = getChainById(await provider.getChainId());
txn = (await provider.prepareTransactionRequest({
account: signer.wallet.account || signer.address,
chain,
to: contract.address,
data: callData,
value: value || 0,
})) as any;
txn = (await provider.prepareTransactionRequest(
_.assign(txn, {
account: signer.wallet.account || signer.address,
chain,
})
)) as any;

console.log(gray(` > calldata: ${txn!.data}`));
console.log(gray(` > estimated gas required: ${txn!.gas}`));
Expand All @@ -365,6 +368,9 @@ async function execTxn({
console.log(green(bold(' ✅ txn will succeed')));
} catch (err) {
console.error(red(`❌ txn will most likely fail: ${(err as Error).toString()}`));
console.error('Txn gas limit has been set to 1,000,000 due to simulation failure');
console.error('txn data', txn);
txn.gas = BigInt(1000000);
}

if (signer != null) {
Expand Down
14 changes: 3 additions & 11 deletions packages/cli/src/commandsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ const anvilOptions = [
flags: '--timeout [number]',
description: 'Timeout in ms for requests sent to remote JSON-RPC server in forking mode.',
},
{
flags: '--block-base-fee-per-gas [number]',
description: 'The base fee in a block.',
},
{
flags: '--code-size-limit [number]',
description: 'EIP-170: Contract code size limit in bytes. Useful to increase this because of tests.',
Expand All @@ -82,10 +78,6 @@ const anvilOptions = [
flags: '--gas-limit [number]',
description: 'The block gas limit.',
},
{
flags: '--gas-price [number]',
description: 'The gas price.',
},
{
flags: '--accounts [number]',
description: 'Number of dev accounts to generate and configure.',
Expand Down Expand Up @@ -293,15 +285,15 @@ const commandsConfig = {
},
{
flags: '--gas-price <gasPrice>',
description: 'Specify a gas price to use for the deployment',
description: 'The gas price used by all transactions processed by this build. Expressed in GWEI.',
},
{
flags: '--max-gas-fee <maxGasFee>',
description: 'Specify max fee per gas (EIP-1559) for deployment',
description: 'Specify max fee per gas (EIP-1559) for all transactions processed by this build. Expressed in GWEI.',
},
{
flags: '--max-priority-gas-fee <maxpriorityGasFee>',
description: 'Specify max fee per gas (EIP-1559) for deployment',
description: 'Specify max fee per gas (EIP-1559) for all transactions processed by this build. Expressed in GWEI.',
},
{
flags: '--skip-compile',
Expand Down
15 changes: 0 additions & 15 deletions packages/cli/src/util/anvil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ export type AnvilOptions = {
* @defaultValue 45000
*/
timeout?: number | undefined;
/**
* The base fee in a block.
*/
blockBaseFeePerGas?: number | bigint | undefined;
/**
* The chain id.
*/
Expand All @@ -94,14 +90,6 @@ export type AnvilOptions = {
* Disable the `call.gas_limit <= block.gas_limit` constraint.
*/
disableBlockGasLimit?: boolean | undefined;
/**
* The block gas limit.
*/
gasLimit?: number | bigint | undefined;
/**
* The gas price.
*/
gasPrice?: number | bigint | undefined;
/**
* Number of dev accounts to generate and configure.
*
Expand Down Expand Up @@ -220,12 +208,9 @@ const anvilOptionKeys = [
'noStorageCaching',
'retries',
'timeout',
'blockBaseFeePerGas',
'chainId',
'codeSizeLimit',
'disableBlockGasLimit',
'gasLimit',
'gasPrice',
'accounts',
'balance',
'derivationPath',
Expand Down
Loading

0 comments on commit 1318b2f

Please sign in to comment.