-
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 49 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 |
|---|---|---|
|
|
@@ -3,7 +3,6 @@ const logger = require('../../logger') | |
| const { encrypt } = require('ecies-geth') | ||
| const { Implementation } = require('contracts-js') | ||
| const { remove0xPrefix, add65BytesPrefix } = require('./helpers') | ||
| const { ContractEventsListener } = require('./events-listener') | ||
| const ethereumWallet = require('ethereumjs-wallet').default | ||
|
|
||
| /** | ||
|
|
@@ -19,7 +18,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() | ||
|
|
||
|
Comment on lines
21
to
27
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 code performs asynchronous operations ( |
||
|
|
@@ -39,10 +38,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 +63,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 +89,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 | ||
|
|
@@ -96,132 +104,76 @@ async function _loadContractInstance( | |
| } | ||
|
|
||
| /** | ||
| * @param {import('web3').default} web3 | ||
| * @param {import('web3').default} web3Subscriptionable | ||
| * @param {import('contracts-js').LumerinContext} lumerin | ||
| * @param {import('contracts-js').CloneFactoryContext} cloneFactory | ||
| * @param {string[]} addresses | ||
| * @param {string} walletAddress | ||
| */ | ||
| async function getContracts( | ||
| web3, | ||
| web3Subscriptionable, | ||
| lumerin, | ||
| cloneFactory, | ||
| addresses, | ||
| walletAddress | ||
| ) { | ||
| return Promise.all( | ||
| addresses.map((address) => | ||
| getContract( | ||
| web3, | ||
| web3Subscriptionable, | ||
| lumerin, | ||
| cloneFactory, | ||
| address, | ||
| walletAddress | ||
| ) | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * @param {import('web3').default} web3 | ||
| * @param {import('web3').default} web3Subscriptionable | ||
| * @param {import('contracts-js').LumerinContext} lumerin | ||
| * @param {string} contractId | ||
| * @param {string} walletAddress | ||
| */ | ||
| async function getContract( | ||
| web3, | ||
| web3Subscriptionable, | ||
| lumerin, | ||
| cloneFactory, | ||
| contractId, | ||
| walletAddress | ||
| ) { | ||
| const contractEventsListener = ContractEventsListener.getInstance() | ||
| const contractInfo = await _loadContractInstance( | ||
| web3, | ||
| contractId, | ||
| walletAddress | ||
| ) | ||
|
|
||
| contractEventsListener.addContract( | ||
| contractInfo.data.id, | ||
| Implementation(web3Subscriptionable, contractId), | ||
| walletAddress | ||
| ) | ||
| return contractInfo.data | ||
| 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 | ||
|
|
||
| const isWhitelisted = await cloneFactory.methods | ||
| .checkWhitelist(sellerAddress) | ||
| .call() | ||
| if (!isWhitelisted) { | ||
| throw new Error('seller is not whitelisted') | ||
| } | ||
|
|
||
| const tempWallet = ethereumWallet.fromPrivateKey( | ||
| Buffer.from(remove0xPrefix(privateKey), 'hex') | ||
| ) | ||
| const pubKey = tempWallet.getPublicKey() | ||
|
|
||
| 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 +182,6 @@ function cancelContract(web3) { | |
| return async function (params) { | ||
| const { | ||
| walletAddress, | ||
| gasLimit = 1000000, | ||
| contractId, | ||
| privateKey, | ||
| closeOutType, | ||
|
|
@@ -239,17 +190,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 +222,6 @@ function setContractDeleteStatus(web3, cloneFactory, onUpdate) { | |
| return async function (params) { | ||
| const { | ||
| walletAddress, | ||
| gasLimit = 3000000, | ||
| contractId, | ||
| privateKey, | ||
| deleteContract, | ||
|
|
@@ -295,9 +249,6 @@ function setContractDeleteStatus(web3, cloneFactory, onUpdate) { | |
| from: walletAddress, | ||
| gas, | ||
| }) | ||
| onUpdate(contractId, walletAddress).catch((err) => | ||
| logger.error(`Failed to refresh after setContractDeadStatus: ${err}`) | ||
| ) | ||
| return result | ||
| } | ||
| } | ||
|
|
@@ -311,8 +262,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 +286,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 +345,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 |
||
|
|
@@ -399,11 +370,10 @@ function editContract(web3, cloneFactory, lumerin) { | |
| } | ||
|
|
||
| module.exports = { | ||
| getContracts, | ||
| getContract, | ||
| createContract, | ||
| cancelContract, | ||
| purchaseContract, | ||
| setContractDeleteStatus, | ||
| editContract, | ||
| getMarketplaceFee, | ||
| } | ||
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 imports several modules and utilities at the top of the file. However, there is a potential security risk with the direct use of
ethereumjs-wallet. Using this package involves handling private keys directly, which can be risky if not managed securely. It's recommended to ensure that private keys are never exposed in logs, error messages, or through any form of insecure storage. Consider implementing additional security measures such as encryption-at-rest for stored keys and secure key management practices.