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.90",
"version": "1.1.0",
"author": {
"name": "Lumerin",
"email": "developer@lumerin.io",
Expand Down
78 changes: 0 additions & 78 deletions src/plugins/contracts/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -104,78 +103,6 @@ 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,
eventBus
) {
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
}

/**
* @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
}

/**
* @param {import('contracts-js').CloneFactoryContext} cloneFactory
*/
Expand Down Expand Up @@ -329,9 +256,6 @@ function setContractDeleteStatus(web3, cloneFactory, onUpdate) {
from: walletAddress,
gas,
})
onUpdate(contractId, walletAddress).catch((err) =>
logger.error(`Failed to refresh after setContractDeadStatus: ${err}`)
)
return result
}
}
Expand Down Expand Up @@ -453,8 +377,6 @@ function editContract(web3, cloneFactory, lumerin) {
}

module.exports = {
getContracts,
getContract,
createContract,
cancelContract,
purchaseContract,
Expand Down
98 changes: 0 additions & 98 deletions src/plugins/contracts/events-listener.js

This file was deleted.

112 changes: 49 additions & 63 deletions src/plugins/contracts/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
//@ts-check
'use strict'

// const debug = require('debug')('lmr-wallet:core:contracts')
const logger = require('../../logger');
const logger = require('../../logger')
const { Lumerin, CloneFactory } = require('contracts-js')

/**
* @type {typeof import('web3').default}
*/
//@ts-ignore
const Web3 = require('web3')

const {
getContracts,
createContract,
cancelContract,
purchaseContract,
setContractDeleteStatus,
editContract,
getMarketplaceFee
getMarketplaceFee,
} = require('./api')
const { ContractEventsListener } = require('./events-listener')
const { Indexer } = require('./indexer')

/**
* Create a plugin instance.
Expand All @@ -35,76 +27,70 @@ function createPlugin() {
* @returns {{ api: {[key: string]:any}, events: string[], name: string }} The instance details.
*/
function start({ config, eventBus, plugins }) {
const { lmrTokenAddress, cloneFactoryAddress } = config
const {
lmrTokenAddress,
cloneFactoryAddress,
indexerUrl,
pollingInterval,
} = config
const { eth } = plugins

const web3 = eth.web3
const web3Subscriptionable = new Web3(plugins.eth.web3SubscriptionProvider)

const lumerin = Lumerin(web3, lmrTokenAddress)
const cloneFactory = CloneFactory(web3, cloneFactoryAddress)
const cloneFactorySubscriptionable = CloneFactory(
web3Subscriptionable,
cloneFactoryAddress
)

const refreshContracts =
(web3, lumerin, cloneFactory) => async (contractId, walletAddress) => {
eventBus.emit('contracts-scan-started', {})
ContractEventsListener.getInstance().walletAddress = walletAddress;
const addresses = contractId
? [contractId]
: await cloneFactory.methods
.getContractList()
.call()
.catch((error) => {
logger.error('cannot get list of contract addresses:', error)
throw error
})
const indexer = new Indexer(indexerUrl)

return getContracts(
web3,
web3Subscriptionable,
lumerin,
cloneFactory,
addresses,
walletAddress,
eventBus,
)
.then((contracts) => {
eventBus.emit('contracts-scan-finished', {
actives: contracts,
})
})
.catch(function (error) {
logger.error('Could not sync contracts/events', error)
throw error
const refreshContracts = async (contractId, walletAddress) => {
if (walletAddress) {
Indexer.walletAddr = walletAddress
Comment thread
alex-sandrk marked this conversation as resolved.
}
eventBus.emit('contracts-scan-started', {})

try {
const contracts = contractId
? await indexer.getContract(contractId)
: await indexer.getContracts()

eventBus.emit('contracts-scan-finished', {
actives: contracts,
})
} catch (error) {
logger.error(
`Could not sync contracts/events, params: ${contractId}, error:`,
error
)
throw error
}
}

const contractEventsListener = ContractEventsListener.create(
cloneFactorySubscriptionable,
config.debug
)
setInterval(() => {
refreshContracts()
}, pollingInterval)
Comment thread
alex-sandrk marked this conversation as resolved.

const onUpdate = refreshContracts(web3, lumerin, cloneFactory)
contractEventsListener.setOnUpdate(onUpdate)
const wrapAction = (fn) => async (params) => {
const contractId = params?.contractId
const result = await fn(params)
await new Promise((resolve) => setTimeout(resolve, 1000))
await refreshContracts(contractId).catch((error) => {
logger.error('Error refreshing contracts', error)
})
return result
}

const refreshContractsFn = refreshContracts(web3, lumerin, cloneFactory)
const purchaseContractFn = purchaseContract(web3, cloneFactory, lumerin)
const cancelContractFn = cancelContract(web3, cloneFactory)
return {
api: {
refreshContracts: refreshContractsFn,
createContract: createContract(web3, cloneFactory),
cancelContract: cancelContractFn,
purchaseContract: purchaseContractFn,
editContract: editContract(web3, cloneFactory, lumerin),
refreshContracts,
createContract: wrapAction(createContract(web3, cloneFactory)),
cancelContract: wrapAction(cancelContractFn),
purchaseContract: wrapAction(purchaseContractFn),
editContract: wrapAction(editContract(web3, cloneFactory, lumerin)),
getMarketplaceFee: getMarketplaceFee(cloneFactory),
setContractDeleteStatus: setContractDeleteStatus(
web3,
cloneFactory,
onUpdate,
setContractDeleteStatus: wrapAction(
setContractDeleteStatus(web3, cloneFactory)
),
},
events: [
Expand Down
Loading