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

Merge pull request #428 from oceanprotocol/feature/getPoolsSharesbyAddress

add getPoolSharesByAddress
This commit is contained in:
Alex Coseru 2020-10-29 14:01:35 +02:00 committed by GitHub
commit aa2533e552
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 9 deletions

View File

@ -4,18 +4,24 @@ import { TransactionReceipt } from 'web3-core'
import { Pool } from './Pool'
import { EventData, Filter } from 'web3-eth-contract'
import BigNumber from 'bignumber.js'
import { SubscribablePromise, Logger } from '../utils'
import { SubscribablePromise, Logger, didNoZeroX, didPrefixed } from '../utils'
declare type PoolTransactionType = 'swap' | 'join' | 'exit'
const POOL_MAX_AMOUNT_IN_LIMIT = 0.25 // maximum 1/4 of the pool reserve
const POOL_MAX_AMOUNT_OUT_LIMIT = 0.25 // maximum 1/4 of the pool reserve
const BPFACTORY_DEPLOY_BLOCK = 0
export interface PoolDetails {
poolAddress: string
tokens: string[]
}
export interface PoolShare {
poolAddress: string
shares: string
did: string
}
export interface TokensReceived {
dtAmount: string
oceanAmount: string
@ -849,7 +855,7 @@ export class OceanPool extends Pool {
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress)
const events = await factory.getPastEvents('BPoolRegistered', {
filter: {},
fromBlock: 0,
fromBlock: BPFACTORY_DEPLOY_BLOCK,
toBlock: 'latest'
})
events.sort((a, b) => (a.blockNumber > b.blockNumber ? 1 : -1))
@ -886,7 +892,7 @@ export class OceanPool extends Pool {
const events = await factory.getPastEvents('BPoolRegistered', {
filter: account ? { registeredBy: account } : {},
fromBlock: 0,
fromBlock: BPFACTORY_DEPLOY_BLOCK,
toBlock: 'latest'
})
@ -897,6 +903,36 @@ export class OceanPool extends Pool {
return result
}
/**
* Search all pools in which a user has shares
* @param {String} account
* @return {AllPoolsShares[]}
*/
public async getPoolSharesByAddress(account: string): Promise<PoolShare[]> {
const result: PoolShare[] = []
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress)
const events = await factory.getPastEvents('BPoolRegistered', {
filter: account ? { registeredBy: account } : {},
fromBlock: BPFACTORY_DEPLOY_BLOCK,
toBlock: 'latest'
})
for (let i = 0; i < events.length; i++) {
const shares = await super.sharesBalance(account, events[i].returnValues[0])
if (shares) {
const onePool: PoolShare = {
shares,
poolAddress: events[i].returnValues[0],
did: didPrefixed(didNoZeroX(await this.getDTAddress(events[i].returnValues[0])))
}
result.push(onePool)
}
}
console.log(result)
return result
}
/**
* Get pool details
* @param {String} poolAddress Pool address
@ -926,7 +962,7 @@ export class OceanPool extends Pool {
events = await pool.getPastEvents('LOG_SWAP', {
filter,
fromBlock: 0,
fromBlock: BPFACTORY_DEPLOY_BLOCK,
toBlock: 'latest'
})
@ -937,7 +973,7 @@ export class OceanPool extends Pool {
events = await pool.getPastEvents('LOG_JOIN', {
filter,
fromBlock: 0,
fromBlock: BPFACTORY_DEPLOY_BLOCK,
toBlock: 'latest'
})
@ -948,7 +984,7 @@ export class OceanPool extends Pool {
events = await pool.getPastEvents('LOG_EXIT', {
filter,
fromBlock: 0,
fromBlock: BPFACTORY_DEPLOY_BLOCK,
toBlock: 'latest'
})
for (let i = 0; i < events.length; i++) {
@ -969,7 +1005,7 @@ export class OceanPool extends Pool {
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress)
const events = await factory.getPastEvents('BPoolRegistered', {
filter: {},
fromBlock: 0,
fromBlock: BPFACTORY_DEPLOY_BLOCK,
toBlock: 'latest'
})

View File

@ -389,7 +389,6 @@ describe('Balancer flow', () => {
if (consoleDebug) console.log('poolShares:' + poolShares)
assert(parseFloat(poolShares) > 0)
})
it('Bob should remove Ocean liquidity from pool ', async () => {
const currentDtReserve = await Pool.getOceanReserve(greatPool)
const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob)
@ -418,6 +417,11 @@ describe('Balancer flow', () => {
assert(parseFloat(amounts.dtAmount) > 0)
assert(parseFloat(amounts.oceanAmount) > 0)
})
it('ALice should get all her shares for all the pools', async () => {
const aliceShares = await Pool.getPoolSharesByAddress(alice)
assert(aliceShares.length > 0)
})
it('ALice should remove all liquidity', async () => {
const aliceShares = await Pool.sharesBalance(alice, greatPool)
const aliceDtBalance = await datatoken.balance(tokenAddress, alice)