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

add getPoolsbyCreator

This commit is contained in:
alexcos20 2020-09-24 22:15:33 -07:00
parent 34daf5cfe9
commit 1f15a67d64
3 changed files with 24 additions and 21 deletions

View File

@ -3,6 +3,11 @@ import { AbiItem } from 'web3-utils/types'
import { TransactionReceipt } from 'web3-core'
import { Pool } from './Pool'
export interface PoolDetails {
poolAddress: string
tokens: string[]
}
/**
* Ocean Pools submodule exposed under ocean.pool
*/
@ -84,7 +89,7 @@ export class OceanPool extends Pool {
*/
public async getDTAddress(account: string, poolAddress: string): Promise<string> {
this.dtAddress = null
const tokens = await this.getCurrentTokens(account, poolAddress)
const tokens = await this.getCurrentTokens(poolAddress)
let token: string
for (token of tokens) {
@ -335,10 +340,7 @@ export class OceanPool extends Pool {
toBlock: 'latest'
})
for (let i = 0; i < events.length; i++) {
const constituents = await super.getCurrentTokens(
account,
events[i].returnValues[0]
)
const constituents = await super.getCurrentTokens(events[i].returnValues[0])
if (constituents.includes(dtAddress)) result.push(events[i].returnValues[0])
}
return result
@ -378,8 +380,8 @@ export class OceanPool extends Pool {
* @param {String} account If empty, will return all pools ever created by anybody
* @return {String[]}
*/
public async searchPoolsbyCreator(account?: string): Promise<string[]> {
const result: string[] = []
public async getPoolsbyCreator(account?: string): Promise<PoolDetails[]> {
const result: PoolDetails[] = []
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, {
from: account
})
@ -397,13 +399,20 @@ export class OceanPool extends Pool {
for (let i = 0; i < events.length; i++) {
if (account) {
if (events[i].returnValues[1] === account) {
result.push(events[i].returnValues[0])
result.push(await this.getPoolDetails(events[i].returnValues[0]))
}
} else result.push(events[i].returnValues[0])
} else result.push(await this.getPoolDetails(events[i].returnValues[0]))
}
return result
}
public async getPoolDetails(poolAddress: string): Promise<PoolDetails> {
const details: PoolDetails = null
details.poolAddress = poolAddress
details.tokens = await super.getFinalTokens(poolAddress)
return details
}
/**
* Get all actions from a pool (join,exit,swap)
* @param {String} poolAddress Pool address

View File

@ -275,14 +275,11 @@ export class Pool extends PoolFactory {
/**
* Get tokens composing this pool
* @param {String} account
* @param {String} poolAddress
* @return {String[]}
*/
async getCurrentTokens(account: string, poolAddress: string): Promise<string[]> {
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, {
from: account
})
async getCurrentTokens(poolAddress: string): Promise<string[]> {
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
let result = null
try {
result = await pool.methods.getCurrentTokens().call()
@ -294,14 +291,11 @@ export class Pool extends PoolFactory {
/**
* Get the final tokens composing this pool
* @param {String} account
* @param {String} poolAddress
* @return {String[]}
*/
async getFinalTokens(account: string, poolAddress: string): Promise<string[]> {
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, {
from: account
})
async getFinalTokens(poolAddress: string): Promise<string[]> {
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
let result = null
try {
result = await pool.methods.getFinalTokens().call()

View File

@ -122,7 +122,7 @@ describe('Balancer flow', () => {
assert(String(n) === '2', 'unexpected num tokens: ' + n)
})
it('Get pool information', async () => {
const currentTokens = await Pool.getCurrentTokens(alice, alicePoolAddress)
const currentTokens = await Pool.getCurrentTokens(alicePoolAddress)
assert(currentTokens.length === 2)
assert(currentTokens.includes(tokenAddress))
assert(currentTokens.includes(oceanTokenAddress))
@ -272,7 +272,7 @@ describe('Balancer flow', () => {
})
it('ALice should get all the pools that she created', async () => {
const alicePools = await Pool.searchPoolsbyCreator(alice)
const alicePools = await Pool.getPoolsbyCreator(alice)
assert(alicePools.length > 0)
})