-
Notifications
You must be signed in to change notification settings - Fork 3
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
Generate 500 dummy contracts #304
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,111 @@ | ||
import { createSelector } from 'reselect'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import * as nodeCrypto from 'crypto'; | ||
import sortBy from 'lodash/sortBy'; | ||
|
||
function createRandomContract() { | ||
const contract = { | ||
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: generateContractAddress(), | ||
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' | ||
}; | ||
if (isValidDummyContract(contract)) { | ||
return contract; | ||
} else { | ||
console.log('Invalid dummy contract generated'); | ||
} | ||
} | ||
|
||
function generateContractAddress() { | ||
// Generate a UUID | ||
const uniqueId = uuidv4(); | ||
|
||
// 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 | ||
Comment on lines
+41
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
|
||
function generateDummyContracts() { | ||
let newContracts = {}; | ||
|
||
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; | ||
} | ||
Comment on lines
+55
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
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; | ||
|
||
function getDummyContracts() { | ||
if (!cachedDummyContracts) { | ||
// If contracts haven't been created yet, generate them | ||
cachedDummyContracts = generateDummyContracts(); | ||
} | ||
return cachedDummyContracts; | ||
} | ||
Comment on lines
+84
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
// 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') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
createRandomContract
function generates a contract with random values. However, it does not ensure that the generated contract is valid before returning it. If an invalid contract is generated, it will be logged to the console, but the function will still returnundefined
. This could lead to potential issues downstream where the returned value is used without checking forundefined
. To improve this, you should ensure that a valid contract is always returned. You could do this by generating a new contract in a loop until a valid one is created.