From 86ff645c88ca9773e4b57f2f09817ed3516bf2d1 Mon Sep 17 00:00:00 2001 From: codywall Date: Mon, 23 Oct 2023 17:14:31 -0700 Subject: [PATCH 1/2] Generate 500 dummy contracts --- src/store/selectors/contracts.js | 79 +++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/src/store/selectors/contracts.js b/src/store/selectors/contracts.js index bb85e687..2c6de995 100644 --- a/src/store/selectors/contracts.js +++ b/src/store/selectors/contracts.js @@ -1,9 +1,86 @@ import { createSelector } from 'reselect'; import sortBy from 'lodash/sortBy'; +function getRandomHex(size = 40) { + let result = []; + let characters = 'ABCDEF0123456789'; + let charactersLength = characters.length; + for (let i = 0; i < size; i++) { + result.push( + characters.charAt(Math.floor(Math.random() * charactersLength)) + ); + } + return '0x' + result.join(''); +} + +function createRandomContract() { + const contract = { + id: getRandomHex(), + price: Math.floor(Math.random() * 100000000).toString(), + speed: Math.floor( + Math.random() * (1500000000000000 - 50000000000000 + 1) + 50000000000000 + ), + length: Math.floor(Math.random() * (28800 - 3600 + 1) + 3600), // random rounded number between 1 and 8 hours + buyer: '0x0000000000000000000000000000000000000000', + seller: getRandomHex(), + timestamp: + Date.now() - + Math.floor(Math.random() * (3 * 7 * 24 * 60 * 60 * 1000)).toString(), // random timestamp within the last 3 weeks + state: '0', + encryptedPoolData: '', + limit: '0', + isDead: false, + balance: (Math.random() * 10000000).toString(), + stats: { + successCount: Math.floor(Math.random() * 10).toString(), + failCount: Math.floor(Math.random() * 10).toString() + }, + hasFutureTerms: false, + futureTerms: null, + history: [], + version: '0' + }; + + return contract; +} + +function generateDummyContracts() { + let newContracts = {}; + for (let i = 0; i < 500; i++) { + const newContract = createRandomContract(); + newContracts[newContract.id] = newContract; + } + return newContracts; +} + +// Cache the contracts to prevent regeneration on refresh +let cachedDummyContracts = null; + +function getDummyContracts() { + if (!cachedDummyContracts) { + // If contracts haven't been created yet, generate them + cachedDummyContracts = generateDummyContracts(); + } + return cachedDummyContracts; +} + // Returns the array of transactions of the current chain/wallet/address. // The items are mapped to contain properties useful for rendering. -export const getContracts = state => state.contracts; +export const getContracts = state => { + const dummyContracts = getDummyContracts(); + const realContracts = state.contracts; + + const combinedActiveContracts = { + ...realContracts.actives, + ...dummyContracts + }; + + const combinedContracts = { + ...realContracts, + actives: combinedActiveContracts + }; + return combinedContracts; +}; export const getActiveContracts = createSelector(getContracts, contractsData => sortBy(Object.values(contractsData.actives), 'timestamp') From d2b1db9417391827ef4290b1a302c65f4fa93bd6 Mon Sep 17 00:00:00 2001 From: codywall Date: Fri, 27 Oct 2023 15:52:58 -0700 Subject: [PATCH 2/2] Generate unique dummy address, validate dummy contracts --- src/store/selectors/contracts.js | 61 ++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/src/store/selectors/contracts.js b/src/store/selectors/contracts.js index 2c6de995..d278f827 100644 --- a/src/store/selectors/contracts.js +++ b/src/store/selectors/contracts.js @@ -1,28 +1,18 @@ import { createSelector } from 'reselect'; +import { v4 as uuidv4 } from 'uuid'; +import * as nodeCrypto from 'crypto'; import sortBy from 'lodash/sortBy'; -function getRandomHex(size = 40) { - let result = []; - let characters = 'ABCDEF0123456789'; - let charactersLength = characters.length; - for (let i = 0; i < size; i++) { - result.push( - characters.charAt(Math.floor(Math.random() * charactersLength)) - ); - } - return '0x' + result.join(''); -} - function createRandomContract() { const contract = { - id: getRandomHex(), + id: generateContractAddress(), price: Math.floor(Math.random() * 100000000).toString(), speed: Math.floor( Math.random() * (1500000000000000 - 50000000000000 + 1) + 50000000000000 ), length: Math.floor(Math.random() * (28800 - 3600 + 1) + 3600), // random rounded number between 1 and 8 hours buyer: '0x0000000000000000000000000000000000000000', - seller: getRandomHex(), + seller: generateContractAddress(), timestamp: Date.now() - Math.floor(Math.random() * (3 * 7 * 24 * 60 * 60 * 1000)).toString(), // random timestamp within the last 3 weeks @@ -40,19 +30,54 @@ function createRandomContract() { history: [], version: '0' }; + if (isValidDummyContract(contract)) { + return contract; + } else { + console.log('Invalid dummy contract generated'); + } +} + +function generateContractAddress() { + // Generate a UUID + const uniqueId = uuidv4(); - return contract; + // Hash the UUID using SHA-256 to get a predictable length + const hash = nodeCrypto.createHash('sha256'); + hash.update(uniqueId); + const hashedUniqueId = hash.digest('hex'); // This will create a 64 characters hexadecimal string + + // Get the first 40 characters to conform to the Ethereum address style + const address = '0x' + hashedUniqueId.substring(0, 40); + + return address; // this is synchronous and returns immediately } function generateDummyContracts() { let newContracts = {}; - for (let i = 0; i < 500; i++) { - const newContract = createRandomContract(); - newContracts[newContract.id] = newContract; + + for (let i = 0; i < 5; i++) { + const newContract = createRandomContract(); // synchronous call + if (newContract) { + newContracts[newContract.id] = newContract; // Check to prevent any issue if a contract was not created properly + } } return newContracts; } +function isValidDummyContract(contract) { + // Regular expression to validate if a string is a hexadecimal number + const hexRegExp = /^0x[a-fA-F0-9]{40}$/; + + return ( + hexRegExp.test(contract.id) && + contract.price > 0 && + contract.speed >= 50000000000000 && // strictly for dummy data + contract.speed <= 1500000000000000 && // strictly for dummy data + hexRegExp.test(contract.seller) && + hexRegExp.test(contract.buyer) + ); +} + // Cache the contracts to prevent regeneration on refresh let cachedDummyContracts = null;