-
Notifications
You must be signed in to change notification settings - Fork 3
Dev #75
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
base: stg
Are you sure you want to change the base?
Dev #75
Changes from 44 commits
911bf22
4af0597
bae2f51
877c41c
8131ca9
d42cd03
4b9e3fd
beea422
4d63f8a
b194ab1
3d681f1
23cc390
6d77bc7
16795ff
a00b29b
928b7e4
b945126
e40769b
cf02876
2607918
b59b6e8
f2b10f8
6265c85
0a3f046
8ba706c
32dd3a9
9010bc4
3027f84
20b933c
12e12cf
56d073d
fc2b427
21ff51e
025de27
4f2686c
bf38a1c
5d40dee
319b61a
f007876
b200545
418bfb6
ba596f8
00e96ce
7fccd3d
ecc41ba
795483c
0207e1c
4ee8460
ec72246
b2e85eb
ec92156
536a357
e9dc828
8b85e97
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 |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ async function _loadContractInstance( | |
| try { | ||
| const implementationContract = Implementation(web3, implementationAddress) | ||
| const contract = await implementationContract.methods | ||
| .getPublicVariables() | ||
| .getPublicVariablesV2() | ||
| .call() | ||
| const stats = await implementationContract.methods.getStats().call() | ||
|
|
||
|
|
@@ -39,10 +39,14 @@ async function _loadContractInstance( | |
|
|
||
| const { | ||
| _state: state, | ||
| _price: price, // cost to purchase the contract | ||
| _limit: limit, // max th provided | ||
| _speed: speed, // th/s of contract | ||
| _length: length, // duration of the contract in seconds | ||
| _terms: { | ||
| _price: price, // cost to purchase the contract | ||
| _limit: limit, // max th provided | ||
| _speed: speed, // th/s of contract | ||
| _length: length, // duration of the contract in seconds | ||
| _version: version, | ||
| _profitTarget: profitTarget | ||
| }, | ||
| _startingBlockTimestamp: timestamp, // timestamp of the block at moment of purchase | ||
| _buyer: buyer, // wallet address of the purchasing party | ||
| _seller: seller, // wallet address of the selling party | ||
|
Comment on lines
38
to
54
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 destructuring assignment from
Comment on lines
38
to
54
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 destructuring assignment from
Comment on lines
41
to
54
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 destructuring assignment from Recommended Change: const { _state: state, _terms: terms = {} } = contract;
const { _price: price, _limit: limit, _speed: speed, _length: length, _version: version, _profitTarget: profitTarget } = terms;This change ensures that if |
||
|
|
@@ -60,6 +64,8 @@ async function _loadContractInstance( | |
| speed: data._speed, | ||
| length: data._length, | ||
| limit: data._limit, | ||
| version: data._version, | ||
| profitTarget: data._profitTarget | ||
| } | ||
| } | ||
|
|
||
|
Comment on lines
66
to
73
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 conditional block from lines 59-69 checks if
Comment on lines
66
to
73
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 conditional block from lines 66-73 checks if Recommended Change: if (walletAddress && hasFutureTerms && seller === walletAddress) {
try {
const data = await implementationContract.methods.futureTerms().call();
futureTerms = {
price: data._price,
speed: data._speed,
length: data._length,
limit: data._limit,
version: data._version,
profitTarget: data._profitTarget
};
} catch (error) {
logger.error('Failed to fetch future terms:', error);
}
}This change ensures that errors during the fetching of future terms are caught and logged, preventing potential crashes and improving the reliability of the application. |
||
|
|
@@ -84,9 +90,12 @@ async function _loadContractInstance( | |
| hasFutureTerms, | ||
| futureTerms, | ||
| history: buyerHistory, | ||
| version, | ||
| profitTarget | ||
| }, | ||
| } | ||
| } catch (err) { | ||
| logger.error(err) | ||
| logger.error( | ||
| 'Error when trying to load Contracts by address in the Implementation contract: ', | ||
| err | ||
|
|
@@ -109,20 +118,32 @@ async function getContracts( | |
| lumerin, | ||
| cloneFactory, | ||
| addresses, | ||
| walletAddress | ||
| walletAddress, | ||
| eventBus | ||
| ) { | ||
| return Promise.all( | ||
| addresses.map((address) => | ||
| getContract( | ||
| web3, | ||
| web3Subscriptionable, | ||
| lumerin, | ||
| cloneFactory, | ||
| address, | ||
| walletAddress | ||
| ) | ||
| const chunkSize = 5 | ||
| const result = [] | ||
| for (let i = 0; i < addresses.length; i += chunkSize) { | ||
| const contracts = await Promise.all( | ||
| addresses | ||
| .slice(i, i + chunkSize) | ||
| .map((address) => | ||
| getContract( | ||
| web3, | ||
| web3Subscriptionable, | ||
| lumerin, | ||
| cloneFactory, | ||
| address, | ||
| walletAddress | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| eventBus.emit('contract-updated', { | ||
| actives: contracts, | ||
| }) | ||
| result.push(...contracts) | ||
| } | ||
| return result | ||
| } | ||
|
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 defined in these lines does not have any error handling mechanism. If any of the operations fail, the error will propagate up the call stack and may cause the application to crash. It's recommended to wrap the function body in a try-catch block and handle any potential errors appropriately. This could involve logging the error, returning a default value, or re-throwing the error after some cleanup. |
||
|
|
||
| /** | ||
|
|
@@ -155,24 +176,31 @@ async function getContract( | |
| return contractInfo.data | ||
| } | ||
|
|
||
| /** | ||
| * @param {import('contracts-js').CloneFactoryContext} cloneFactory | ||
| */ | ||
| const getMarketplaceFee = (cloneFactory) => async () => { | ||
| return await cloneFactory.methods.marketplaceFee().call() | ||
| } | ||
|
|
||
| /** | ||
| * @param {import('web3').default} web3 | ||
| * @param {import('contracts-js').CloneFactoryContext} cloneFactory | ||
| */ | ||
| function createContract(web3, cloneFactory, plugins) { | ||
| function createContract(web3, cloneFactory) { | ||
| if (!web3) { | ||
| logger.error('Not a valid Web3 instance') | ||
| return | ||
|
Comment on lines
+120
to
123
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
Comment on lines
+120
to
123
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 |
||
| } | ||
|
|
||
| return async function (params) { | ||
| // const { gasPrice } = await plugins.wallet.getGasPrice() | ||
| let { | ||
| price, | ||
| limit = 0, | ||
| speed, | ||
| duration, | ||
| sellerAddress, | ||
| profit = 0, | ||
| validatorAddress = '0x0000000000000000000000000000000000000000', | ||
| privateKey, | ||
| } = params | ||
|
|
@@ -191,37 +219,41 @@ function createContract(web3, cloneFactory, plugins) { | |
|
|
||
| const account = web3.eth.accounts.privateKeyToAccount(privateKey) | ||
| web3.eth.accounts.wallet.create(0).add(account) | ||
| const marketplaceFee = await cloneFactory.methods.marketplaceFee().call() | ||
|
|
||
| const gas = await cloneFactory.methods | ||
| .setCreateNewRentalContract( | ||
| .setCreateNewRentalContractV2( | ||
| price, | ||
| limit, | ||
| speed, | ||
| duration, | ||
| +profit, | ||
| validatorAddress, | ||
| pubKey.toString('hex') | ||
| ) | ||
| .estimateGas({ | ||
| from: sellerAddress, | ||
| value: marketplaceFee, | ||
| }) | ||
|
|
||
| return cloneFactory.methods | ||
| .setCreateNewRentalContract( | ||
| .setCreateNewRentalContractV2( | ||
| price, | ||
| limit, | ||
| speed, | ||
| duration, | ||
| +profit, | ||
| validatorAddress, | ||
| pubKey.toString('hex') | ||
| ) | ||
| .send({ from: sellerAddress, gas }) | ||
| .send({ from: sellerAddress, gas, value: marketplaceFee }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param {import('web3').default} web3 | ||
| */ | ||
| function cancelContract(web3) { | ||
| function cancelContract(web3, cloneFactory) { | ||
| if (!web3) { | ||
| logger.error('Not a valid Web3 instance') | ||
| return | ||
|
|
@@ -230,7 +262,6 @@ function cancelContract(web3) { | |
| return async function (params) { | ||
| const { | ||
| walletAddress, | ||
| gasLimit = 1000000, | ||
| contractId, | ||
| privateKey, | ||
| closeOutType, | ||
|
|
@@ -239,17 +270,21 @@ function cancelContract(web3) { | |
| const account = web3.eth.accounts.privateKeyToAccount(privateKey) | ||
| web3.eth.accounts.wallet.create(0).add(account) | ||
|
|
||
| const marketplaceFee = await cloneFactory.methods.marketplaceFee().call() | ||
|
|
||
| const gas = await Implementation(web3, contractId) | ||
| .methods.setContractCloseOut(closeOutType) | ||
| .estimateGas({ | ||
| from: walletAddress, | ||
| value: marketplaceFee, | ||
| }) | ||
|
|
||
| return await Implementation(web3, contractId) | ||
| .methods.setContractCloseOut(closeOutType) | ||
| .send({ | ||
| from: walletAddress, | ||
| gas, | ||
| value: marketplaceFee, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -267,7 +302,6 @@ function setContractDeleteStatus(web3, cloneFactory, onUpdate) { | |
| return async function (params) { | ||
| const { | ||
| walletAddress, | ||
| gasLimit = 3000000, | ||
| contractId, | ||
| privateKey, | ||
| deleteContract, | ||
|
|
@@ -311,8 +345,8 @@ function setContractDeleteStatus(web3, cloneFactory, onUpdate) { | |
| */ | ||
| function purchaseContract(web3, cloneFactory, lumerin) { | ||
| return async (params) => { | ||
| const { walletId, contractId, url, privateKey, price } = params | ||
| const sendOptions = { from: walletId, gas: 1_000_000 } | ||
| const { walletId, contractId, url, privateKey, price, version } = params | ||
| const sendOptions = { from: walletId } | ||
|
|
||
| //getting pubkey from contract to be purchased | ||
| const implementationContract = Implementation(web3, contractId) | ||
|
|
@@ -335,23 +369,42 @@ function purchaseContract(web3, cloneFactory, lumerin) { | |
| throw new Error('Contract is deleted already') | ||
| } | ||
|
|
||
| const totalPrice = (+price + price * 0.01).toString() | ||
| const increaseAllowanceEstimate = await lumerin.methods | ||
| .increaseAllowance(cloneFactory.options.address, price) | ||
| .estimateGas({ | ||
| from: walletId, | ||
| }) | ||
|
|
||
| await lumerin.methods | ||
| .increaseAllowance(cloneFactory.options.address, totalPrice) | ||
| .send(sendOptions) | ||
| .increaseAllowance(cloneFactory.options.address, price) | ||
| .send({ | ||
| from: walletId, | ||
| gas: increaseAllowanceEstimate, | ||
| }) | ||
|
|
||
| const marketplaceFee = await cloneFactory.methods.marketplaceFee().call() | ||
|
|
||
| const purchaseGas = await cloneFactory.methods | ||
| .setPurchaseRentalContract(contractId, ciphertext.toString('hex')) | ||
| .setPurchaseRentalContract( | ||
| contractId, | ||
| ciphertext.toString('hex'), | ||
| version | ||
| ) | ||
| .estimateGas({ | ||
| from: sendOptions.from, | ||
| value: marketplaceFee, | ||
| }) | ||
|
|
||
| const purchaseResult = await cloneFactory.methods | ||
| .setPurchaseRentalContract(contractId, ciphertext.toString('hex')) | ||
| .setPurchaseRentalContract( | ||
| contractId, | ||
| ciphertext.toString('hex'), | ||
| version | ||
| ) | ||
| .send({ | ||
| ...sendOptions, | ||
| gas: purchaseGas, | ||
| value: marketplaceFee, | ||
| }) | ||
|
|
||
| logger.debug('Finished puchase transaction', purchaseResult) | ||
|
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 |
||
|
|
@@ -375,20 +428,21 @@ function editContract(web3, cloneFactory, lumerin) { | |
| limit = 0, | ||
| speed, | ||
| duration, | ||
| profit = 0 | ||
| } = params | ||
| const sendOptions = { from: walletId, gas: 1_000_000 } | ||
| const sendOptions = { from: walletId } | ||
|
|
||
| const account = web3.eth.accounts.privateKeyToAccount(privateKey) | ||
| web3.eth.accounts.wallet.create(0).add(account) | ||
|
Comment on lines
421
to
359
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 defined in these lines is creating a new account and adding it to the wallet but it does not check if an account with the same private key already exists in the wallet. This could lead to duplicate accounts in the wallet. It's recommended to check if an account with the same private key already exists before adding it to the wallet. |
||
|
|
||
| const editGas = await cloneFactory.methods | ||
| .setUpdateContractInformation(contractId, price, limit, speed, duration) | ||
| .setUpdateContractInformationV2(contractId, price, limit, speed, duration, +profit) | ||
| .estimateGas({ | ||
| from: sendOptions.from, | ||
| }) | ||
|
|
||
| }); | ||
| const editResult = await cloneFactory.methods | ||
| .setUpdateContractInformation(contractId, price, limit, speed, duration) | ||
| .setUpdateContractInformationV2(contractId, price, limit, speed, duration, +profit) | ||
| .send({ | ||
| ...sendOptions, | ||
| gas: editGas, | ||
|
Comment on lines
345
to
371
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. In the function |
||
|
|
@@ -406,4 +460,5 @@ module.exports = { | |
| purchaseContract, | ||
| setContractDeleteStatus, | ||
| editContract, | ||
| getMarketplaceFee, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,9 +33,10 @@ class ContractEventsListener { | |
| .on('connected', () => { | ||
| logger.debug(`Start listen contract (${id}) events`) | ||
| }) | ||
| .on('data', () => { | ||
| .on('data', async () => { | ||
| logger.debug(`Contract (${id}) updated`) | ||
| if (this.onUpdate){ | ||
| await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
| this.onUpdate(id, this.walletAddress || walletAddress) | ||
| } | ||
|
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 use of an arbitrary delay with |
||
| }) | ||
|
|
@@ -49,9 +50,10 @@ class ContractEventsListener { | |
| .on('connected', () => { | ||
| logger.debug('Start listen clone factory events') | ||
| }) | ||
| .on('data', (event) => { | ||
| .on('data', async (event) => { | ||
| const contractId = event.returnValues._address | ||
| logger.debug('New contract created', contractId) | ||
| await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
| this.onUpdate(contractId, this.walletAddress) | ||
| }) | ||
|
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. Similar to the previous issue, there is an arbitrary delay introduced with |
||
| } | ||
|
|
||
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 code performs asynchronous operations (
implementationContract.methods.getPublicVariablesV2().call()andimplementationContract.methods.getStats().call()) without any error handling mechanism. If any of these operations fail, for example, due to a network issue or an invalid contract address, it will result in an unhandled promise rejection, potentially causing runtime issues. It's recommended to wrap these asynchronous operations in a try-catch block to handle any errors that may occur, ensuring the application can gracefully handle the failure scenario.