1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

parallelized get shares and tx

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
mihaisc 2020-11-10 16:49:59 +02:00
parent 1d717cb3b0
commit 33bcb47d45
No known key found for this signature in database
GPG Key ID: 4FB0C2329B4C6E29

View File

@ -171,10 +171,11 @@ export class OceanPool extends Pool {
const tokens = await this.getCurrentTokens(poolAddress) const tokens = await this.getCurrentTokens(poolAddress)
let token: string let token: string
for (token of tokens) { if (tokens != null)
// TODO: Potential timing attack, left side: true for (token of tokens) {
if (token !== this.oceanAddress) this.dtAddress = token // TODO: Potential timing attack, left side: true
} if (token !== this.oceanAddress) this.dtAddress = token
}
return this.dtAddress return this.dtAddress
} }
@ -886,7 +887,9 @@ export class OceanPool extends Pool {
* @return {String[]} - amount of ocean tokens received * @return {String[]} - amount of ocean tokens received
*/ */
public async getOceanReceived(poolAddress: string, dtAmount: string): Promise<string> { public async getOceanReceived(poolAddress: string, dtAmount: string): Promise<string> {
console.log('poolAddress', poolAddress)
const dtAddress = await this.getDTAddress(poolAddress) const dtAddress = await this.getDTAddress(poolAddress)
console.log('dtAddress', dtAddress)
return this.calcOutGivenIn(poolAddress, dtAddress, this.oceanAddress, dtAmount) return this.calcOutGivenIn(poolAddress, dtAddress, this.oceanAddress, dtAmount)
} }
@ -933,6 +936,20 @@ export class OceanPool extends Pool {
return result return result
} }
private async getResult(account: string, event: EventData): Promise<PoolShare> {
const shares = await super.sharesBalance(account, event.returnValues[0])
if (parseFloat(shares) > 0) {
const dtAddress = await this.getDTAddress(event.returnValues[0])
if (dtAddress) {
const onePool: PoolShare = {
shares,
poolAddress: event.returnValues[0],
did: didPrefixed(didNoZeroX(dtAddress))
}
return onePool
}
}
}
/** /**
* Search all pools in which a user has shares * Search all pools in which a user has shares
* @param {String} account * @param {String} account
@ -947,20 +964,17 @@ export class OceanPool extends Pool {
fromBlock: this.startBlock, fromBlock: this.startBlock,
toBlock: 'latest' toBlock: 'latest'
}) })
for (let i = 0; i < events.length; i++) { let results = await Promise.all(
const shares = await super.sharesBalance(account, events[i].returnValues[0]) events.map((event) => {
if (parseFloat(shares) > 0) { return this.getResult(account, event)
const dtAddress = await this.getDTAddress(events[i].returnValues[0]) })
if (dtAddress) { )
const onePool: PoolShare = { results = results.filter((share) => {
shares, return share !== undefined
poolAddress: events[i].returnValues[0], })
did: didPrefixed(didNoZeroX(dtAddress))
} result.push(...results)
result.push(onePool)
}
}
}
return result return result
} }
@ -1002,19 +1016,40 @@ export class OceanPool extends Pool {
fromBlock: startBlock, fromBlock: startBlock,
toBlock: 'latest' toBlock: 'latest'
}) })
for (let i = 0; i < events.length; i++) {
switch (events[i].topics[0]) { let eventResults = await Promise.all(
case swapTopic: events.map((event) => {
results.push(await this.getEventData('swap', poolAddress, dtAddress, events[i])) switch (event.topics[0]) {
break case swapTopic:
case joinTopic: return this.getEventData('swap', poolAddress, dtAddress, event)
results.push(await this.getEventData('join', poolAddress, dtAddress, events[i])) break
break case joinTopic:
case exitTopic: return this.getEventData('join', poolAddress, dtAddress, event)
results.push(await this.getEventData('exit', poolAddress, dtAddress, events[i])) break
break case exitTopic:
} return this.getEventData('exit', poolAddress, dtAddress, event)
} break
}
})
)
eventResults = eventResults.filter((share) => {
return share !== undefined
})
results.push(...eventResults)
// for (let i = 0; i < events.length; i++) {
// switch (events[i].topics[0]) {
// case swapTopic:
// results.push(await this.getEventData('swap', poolAddress, dtAddress, events[i]))
// break
// case joinTopic:
// results.push(await this.getEventData('join', poolAddress, dtAddress, events[i]))
// break
// case exitTopic:
// results.push(await this.getEventData('exit', poolAddress, dtAddress, events[i]))
// break
// }
// }
return results return results
} }
@ -1031,14 +1066,17 @@ export class OceanPool extends Pool {
fromBlock: this.startBlock, fromBlock: this.startBlock,
toBlock: 'latest' toBlock: 'latest'
}) })
for (let i = 0; i < events.length; i++) {
const logs = await this.getPoolLogs( let eventsResults = await Promise.all(
events[i].returnValues[0], events.map((event) => {
events[i].blockNumber, return this.getPoolLogs(event.returnValues[0], event.blockNumber, account)
account })
) )
for (let j = 0; j < logs.length; j++) results.push(logs[j])
} const eventResults = eventsResults.reduce((elem1, elem2) => elem1.concat(elem2))
console.log(eventResults)
results.push(...eventResults)
return results return results
} }