Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lumerin/wallet-core",
"version": "1.0.64",
"version": "1.0.65",
"author": {
"name": "Lumerin",
"email": "developer@lumerin.io",
Expand Down
25 changes: 19 additions & 6 deletions src/plugins/contracts/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,18 @@ async function getContract(
return contractInfo.data
}

/**
* @param {import('contracts-js').CloneFactoryContext} cloneFactory
*/
const getMarketplaceFee = (cloneFactory) => async () => {
return await cloneFactory.methods.marketplaceFee().call();
Comment on lines +161 to +162
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The function getMarketplaceFee is defined as a double arrow function, which is unnecessary and could lead to confusion. The outer function takes cloneFactory as a parameter but the inner function doesn't take any parameters. This could lead to potential issues if you expect to pass parameters to the inner function. I recommend simplifying this by removing the outer function and passing cloneFactory directly to the inner function.

}

/**
* @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
Expand Down Expand Up @@ -205,6 +212,8 @@ function createContract(web3, cloneFactory, plugins) {
from: sellerAddress,
})

const marketplaceFee = await cloneFactory.methods.marketplaceFee().call();

return cloneFactory.methods
.setCreateNewRentalContract(
price,
Expand All @@ -214,14 +223,14 @@ function createContract(web3, cloneFactory, plugins) {
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
Expand All @@ -245,11 +254,14 @@ function cancelContract(web3) {
from: walletAddress,
})

const marketplaceFee = await cloneFactory.methods.marketplaceFee().call();

return await Implementation(web3, contractId)
.methods.setContractCloseOut(closeOutType)
.send({
from: walletAddress,
gas,
value: marketplaceFee
})
}
}
Expand Down Expand Up @@ -335,23 +347,23 @@ function purchaseContract(web3, cloneFactory, lumerin) {
throw new Error('Contract is deleted already')
}

const totalPrice = (+price + price * 0.01).toString()

await lumerin.methods
.increaseAllowance(cloneFactory.options.address, totalPrice)
.increaseAllowance(cloneFactory.options.address, price)
.send(sendOptions)

const purchaseGas = await cloneFactory.methods
.setPurchaseRentalContract(contractId, ciphertext.toString('hex'))
.estimateGas({
from: sendOptions.from,
})
const marketplaceFee = await cloneFactory.methods.marketplaceFee().call();

const purchaseResult = await cloneFactory.methods
.setPurchaseRentalContract(contractId, ciphertext.toString('hex'))
.send({
...sendOptions,
gas: purchaseGas,
value: marketplaceFee
})

logger.debug('Finished puchase transaction', purchaseResult)
Expand Down Expand Up @@ -406,4 +418,5 @@ module.exports = {
purchaseContract,
setContractDeleteStatus,
editContract,
getMarketplaceFee
}
6 changes: 4 additions & 2 deletions src/plugins/contracts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
purchaseContract,
setContractDeleteStatus,
editContract,
getMarketplaceFee
} = require('./api')
const { ContractEventsListener } = require('./events-listener')

Expand Down Expand Up @@ -91,10 +92,11 @@ function createPlugin() {
return {
api: {
refreshContracts: refreshContracts(web3, lumerin, cloneFactory),
createContract: createContract(web3, cloneFactory, plugins),
cancelContract: cancelContract(web3),
createContract: createContract(web3, cloneFactory),
cancelContract: cancelContract(web3, cloneFactory),
purchaseContract: purchaseContract(web3, cloneFactory, lumerin),
editContract: editContract(web3, cloneFactory, lumerin),
getMarketplaceFee: getMarketplaceFee(cloneFactory),
setContractDeleteStatus: setContractDeleteStatus(
web3,
cloneFactory,
Comment on lines 92 to 102
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The code is creating new instances of the functions refreshContracts, createContract, cancelContract, purchaseContract, editContract, getMarketplaceFee, and setContractDeleteStatus every time this part of the code is executed. This could lead to performance issues if this code is called frequently, as each function creation in JavaScript is a relatively expensive operation.

To improve performance, consider refactoring the code to create these function instances only once and reuse them. This could be achieved by moving the function instantiation to a higher scope or using a design pattern that ensures only a single instance of each function is created, such as the Singleton or Factory patterns.

Expand Down