Skip to content

Commit

Permalink
feat: eth_getTransactionCount can return 0x0 const if configured (#1389)
Browse files Browse the repository at this point in the history
* feat: add configuration on hardhat provider

* feat: add configuration on hardhat plugin with tests
  • Loading branch information
rodolfopietro97 authored Oct 6, 2024
1 parent f4ec03e commit 07f8f63
Show file tree
Hide file tree
Showing 10 changed files with 484 additions and 7 deletions.
13 changes: 12 additions & 1 deletion packages/hardhat-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ extendEnvironment((hre) => {
networkConfig.enableDelegation !== undefined &&
networkConfig.enableDelegation;

// 1.5 - Get the custom RPC Configuration
const rpcConfiguration = networkConfig.rpcConfiguration;
const ethGetTransactionCountMustReturn0 =
rpcConfiguration?.ethGetTransactionCountMustReturn0 !== undefined
? rpcConfiguration?.ethGetTransactionCountMustReturn0
: false;

// 2 - Check if network is vechain

if (!networkName.includes('vechain')) {
Expand All @@ -73,6 +80,7 @@ extendEnvironment((hre) => {

// 3 - Extend environment with the 'HardhatVeChainProvider'

console.log('networkConfig', networkConfig.rpcConfiguration);
// 3.1 - Create the provider
const hardhatVeChainProvider = new HardhatVeChainProvider(
createWalletFromHardhatNetworkConfig(networkConfig),
Expand All @@ -84,7 +92,10 @@ extendEnvironment((hre) => {
parent
),
debug,
enableDelegation
enableDelegation,
{
ethGetTransactionCountMustReturn0
}
);

// 3.2 - Extend environment
Expand Down
7 changes: 7 additions & 0 deletions packages/hardhat-plugin/src/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,12 @@ declare module 'hardhat/types/config' {
* Enable or not the fee's delegation of the transaction.
*/
enableDelegation?: boolean;

/**
* RPC Configuration
*/
rpcConfiguration?: {
ethGetTransactionCountMustReturn0: boolean;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { afterEach, beforeEach, describe, expect, test } from '@jest/globals';

import { resetHardhatContext } from 'hardhat/plugins-testing';
import {
type HardhatRuntimeEnvironment,
type HttpNetworkConfig
} from 'hardhat/types';
import { setHardhatContext } from './test-utils';

/**
* Simple hardhat project eth_getTransactionCount-default-value-project network configuration defined
*
* @group unit/eth_getTransactionCount-default-value-project
*/
describe('Using eth_getTransactionCount with default value as 0x0', () => {
/**
* Init hardhat runtime environment
*/
let hre: HardhatRuntimeEnvironment;

beforeEach(async function () {
// Set hardhat context
setHardhatContext('eth_getTransactionCount-default-value-project');

// Load hardhat environment
hre = await import('hardhat');
});

afterEach(function () {
resetHardhatContext();
});

/**
* Test suite for eth_getTransactionCount with default 0x0 value
*/
describe('Custom network configuration hardhat', () => {
/**
* Positive test cases for createWalletFromHardhatNetworkConfig function
*/
test('Should be able to get a default value of 0x0 from a project', async () => {
// Network configuration should be defined
expect(hre.config.networks.vechain_testnet).toBeDefined();

// Initialize network configuration AND check ethGetTransactionCountDefaultValue parameter
const networkConfig = hre.config.networks
.vechain_testnet as HttpNetworkConfig;
expect(
networkConfig.rpcConfiguration
?.ethGetTransactionCountMustReturn0
).toBe(true);

// Get the provider
expect(hre.VeChainProvider).toBeDefined();
const provider = hre.VeChainProvider;

// Expect 0 as the default value
const request = await provider?.request({
method: 'eth_getTransactionCount',
params: ['0x0b41c56e19c5151122568873a039fEa090937Fe2', 'latest']
});
expect(request).toBe('0x0');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { afterEach, beforeEach, describe, expect, test } from '@jest/globals';

import { resetHardhatContext } from 'hardhat/plugins-testing';
import {
type HardhatRuntimeEnvironment,
type HttpNetworkConfig
} from 'hardhat/types';
import { setHardhatContext } from './test-utils';

/**
* Simple hardhat project eth_getTransactionCount-no-value-project network configuration defined
*
* @group unit/eth_getTransactionCount-no-value-project
*/
describe('Using eth_getTransactionCount with no options specified. So it will return a random number', () => {
/**
* Init hardhat runtime environment
*/
let hre: HardhatRuntimeEnvironment;

beforeEach(async function () {
// Set hardhat context
setHardhatContext('eth_getTransactionCount-no-value-project');

// Load hardhat environment
hre = await import('hardhat');
});

afterEach(function () {
resetHardhatContext();
});

/**
* Test suite for eth_getTransactionCount with random value when no options are specified
*/
describe('Custom network configuration hardhat', () => {
/**
* Should be able to get a random value from a project when no options are specified
*/
test('Should be able to get a random value from a project when no options are specified', async () => {
// Network configuration should be defined
expect(hre.config.networks.vechain_testnet).toBeDefined();

// Initialize network configuration AND check ethGetTransactionCountDefaultValue parameter
const networkConfig = hre.config.networks
.vechain_testnet as HttpNetworkConfig;
expect(
networkConfig.rpcConfiguration
?.ethGetTransactionCountMustReturn0
).toBe(undefined);

// Get the provider
expect(hre.VeChainProvider).toBeDefined();
const provider = hre.VeChainProvider;

// Expect 0 as the default value
const request = await provider?.request({
method: 'eth_getTransactionCount',
params: ['0x0b41c56e19c5151122568873a039fEa090937Fe2', 'latest']
});
expect(request).not.toBe('0x0');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { afterEach, beforeEach, describe, expect, test } from '@jest/globals';

import { resetHardhatContext } from 'hardhat/plugins-testing';
import {
type HardhatRuntimeEnvironment,
type HttpNetworkConfig
} from 'hardhat/types';
import { setHardhatContext } from './test-utils';

/**
* Simple hardhat project eth_getTransactionCount-random-value-project network configuration defined
*
* @group unit/eth_getTransactionCount-random-value-project
*/
describe('Using eth_getTransactionCount with random value (not the default 0x0 value)', () => {
/**
* Init hardhat runtime environment
*/
let hre: HardhatRuntimeEnvironment;

beforeEach(async function () {
// Set hardhat context
setHardhatContext('eth_getTransactionCount-random-value-project');

// Load hardhat environment
hre = await import('hardhat');
});

afterEach(function () {
resetHardhatContext();
});

/**
* Test suite for eth_getTransactionCount with random value
*/
describe('Custom network configuration hardhat', () => {
/**
* Should be able to get random value from eth_getTransactionCount
*/
test('Should be able to get random value from eth_getTransactionCount', async () => {
// Network configuration should be defined
expect(hre.config.networks.vechain_testnet).toBeDefined();

// Initialize network configuration AND check ethGetTransactionCountDefaultValue parameter
const networkConfig = hre.config.networks
.vechain_testnet as HttpNetworkConfig;
expect(
networkConfig.rpcConfiguration
?.ethGetTransactionCountMustReturn0
).toBe(false);

// Get the provider
expect(hre.VeChainProvider).toBeDefined();
const provider = hre.VeChainProvider;

// Expect 0 as the default value
const request = await provider?.request({
method: 'eth_getTransactionCount',
params: ['0x0b41c56e19c5151122568873a039fEa090937Fe2', 'latest']
});
expect(request).not.toBe('0x0');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Simple hardhat configuration for testing with a VeChain network defined
*/

// We load the plugin here.
import { type HardhatUserConfig, type HttpNetworkConfig } from 'hardhat/types';

import '../../../src/index';
import { HDKey } from '@vechain/sdk-core';

/**
* Simple configuration for testing
*/
const vechainTestNetwork: HttpNetworkConfig = {
// Default network parameters
url: 'https://testnet.vechain.org',
timeout: 20000,
httpHeaders: {},
gas: 'auto',
gasPrice: 'auto',
gasMultiplier: 1,
accounts: {
mnemonic:
'vivid any call mammal mosquito budget midnight expose spirit approve reject system',
path: HDKey.VET_DERIVATION_PATH,
count: 3,
initialIndex: 0,
passphrase: 'VeChainThor'
},

// Custom parameters
delegator: {
delegatorPrivateKey:
'ea5383ac1f9e625220039a4afac6a7f868bf1ad4f48ce3a1dd78bd214ee4ace5'
},
debug: true,
enableDelegation: true,

// Custom RPC
rpcConfiguration: {
ethGetTransactionCountMustReturn0: true
}
};

/**
* Hardhat configuration
*/
const config: HardhatUserConfig = {
solidity: '0.8.17',
networks: {
vechain_testnet: vechainTestNetwork
},

/**
* @note: here we set vechain_testnet as the default network to simulate a command like this:
*
* ```sh
* npx hardhat --network vechain_testnet <command>
* ```
*/
defaultNetwork: 'vechain_testnet'
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Simple hardhat configuration for testing with a VeChain network defined
*/

// We load the plugin here.
import { type HardhatUserConfig, type HttpNetworkConfig } from 'hardhat/types';

import '../../../src/index';
import { HDKey } from '@vechain/sdk-core';

/**
* Simple configuration for testing
*/
const vechainTestNetwork: HttpNetworkConfig = {
// Default network parameters
url: 'https://testnet.vechain.org',
timeout: 20000,
httpHeaders: {},
gas: 'auto',
gasPrice: 'auto',
gasMultiplier: 1,
accounts: {
mnemonic:
'vivid any call mammal mosquito budget midnight expose spirit approve reject system',
path: HDKey.VET_DERIVATION_PATH,
count: 3,
initialIndex: 0,
passphrase: 'VeChainThor'
},

// Custom parameters
delegator: {
delegatorPrivateKey:
'ea5383ac1f9e625220039a4afac6a7f868bf1ad4f48ce3a1dd78bd214ee4ace5'
},
debug: true,
enableDelegation: true

// Custom RPC
// ... NOT GIVEN ...
};

/**
* Hardhat configuration
*/
const config: HardhatUserConfig = {
solidity: '0.8.17',
networks: {
vechain_testnet: vechainTestNetwork
},

/**
* @note: here we set vechain_testnet as the default network to simulate a command like this:
*
* ```sh
* npx hardhat --network vechain_testnet <command>
* ```
*/
defaultNetwork: 'vechain_testnet'
};

export default config;
Loading

1 comment on commit 07f8f63

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 98%
98.4% (4202/4270) 96.65% (1385/1433) 97.87% (877/896)
Title Tests Skipped Failures Errors Time
core 806 0 💤 0 ❌ 0 🔥 1m 49s ⏱️
network 733 0 💤 0 ❌ 0 🔥 4m 42s ⏱️
errors 42 0 💤 0 ❌ 0 🔥 17.397s ⏱️

Please sign in to comment.