mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
add getPoolsSharesbyAddress
This commit is contained in:
parent
d1620d784f
commit
087b021c17
@ -10,12 +10,17 @@ 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_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 POOL_MAX_AMOUNT_OUT_LIMIT = 0.25 // maximum 1/4 of the pool reserve
|
||||||
|
const BPFACTORY_DEPLOY_BLOCK = 0
|
||||||
export interface PoolDetails {
|
export interface PoolDetails {
|
||||||
poolAddress: string
|
poolAddress: string
|
||||||
tokens: string[]
|
tokens: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AllPoolsShares {
|
||||||
|
poolAddress: string
|
||||||
|
shares: string
|
||||||
|
}
|
||||||
|
|
||||||
export interface TokensReceived {
|
export interface TokensReceived {
|
||||||
dtAmount: string
|
dtAmount: string
|
||||||
oceanAmount: string
|
oceanAmount: string
|
||||||
@ -849,7 +854,7 @@ export class OceanPool extends Pool {
|
|||||||
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress)
|
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress)
|
||||||
const events = await factory.getPastEvents('BPoolRegistered', {
|
const events = await factory.getPastEvents('BPoolRegistered', {
|
||||||
filter: {},
|
filter: {},
|
||||||
fromBlock: 0,
|
fromBlock: BPFACTORY_DEPLOY_BLOCK,
|
||||||
toBlock: 'latest'
|
toBlock: 'latest'
|
||||||
})
|
})
|
||||||
events.sort((a, b) => (a.blockNumber > b.blockNumber ? 1 : -1))
|
events.sort((a, b) => (a.blockNumber > b.blockNumber ? 1 : -1))
|
||||||
@ -886,7 +891,7 @@ export class OceanPool extends Pool {
|
|||||||
|
|
||||||
const events = await factory.getPastEvents('BPoolRegistered', {
|
const events = await factory.getPastEvents('BPoolRegistered', {
|
||||||
filter: account ? { registeredBy: account } : {},
|
filter: account ? { registeredBy: account } : {},
|
||||||
fromBlock: 0,
|
fromBlock: BPFACTORY_DEPLOY_BLOCK,
|
||||||
toBlock: 'latest'
|
toBlock: 'latest'
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -897,6 +902,32 @@ export class OceanPool extends Pool {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search all pools in which a user has shares
|
||||||
|
* @param {String} account
|
||||||
|
* @return {AllPoolsShares[]}
|
||||||
|
*/
|
||||||
|
public async getPoolsSharesbyAddress(account: string): Promise<AllPoolsShares[]> {
|
||||||
|
const result: AllPoolsShares[] = []
|
||||||
|
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: AllPoolsShares = { shares, poolAddress: events[i].returnValues[0] }
|
||||||
|
result.push(onePool)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log(result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get pool details
|
* Get pool details
|
||||||
* @param {String} poolAddress Pool address
|
* @param {String} poolAddress Pool address
|
||||||
@ -926,7 +957,7 @@ export class OceanPool extends Pool {
|
|||||||
|
|
||||||
events = await pool.getPastEvents('LOG_SWAP', {
|
events = await pool.getPastEvents('LOG_SWAP', {
|
||||||
filter,
|
filter,
|
||||||
fromBlock: 0,
|
fromBlock: BPFACTORY_DEPLOY_BLOCK,
|
||||||
toBlock: 'latest'
|
toBlock: 'latest'
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -937,7 +968,7 @@ export class OceanPool extends Pool {
|
|||||||
|
|
||||||
events = await pool.getPastEvents('LOG_JOIN', {
|
events = await pool.getPastEvents('LOG_JOIN', {
|
||||||
filter,
|
filter,
|
||||||
fromBlock: 0,
|
fromBlock: BPFACTORY_DEPLOY_BLOCK,
|
||||||
toBlock: 'latest'
|
toBlock: 'latest'
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -948,7 +979,7 @@ export class OceanPool extends Pool {
|
|||||||
|
|
||||||
events = await pool.getPastEvents('LOG_EXIT', {
|
events = await pool.getPastEvents('LOG_EXIT', {
|
||||||
filter,
|
filter,
|
||||||
fromBlock: 0,
|
fromBlock: BPFACTORY_DEPLOY_BLOCK,
|
||||||
toBlock: 'latest'
|
toBlock: 'latest'
|
||||||
})
|
})
|
||||||
for (let i = 0; i < events.length; i++) {
|
for (let i = 0; i < events.length; i++) {
|
||||||
@ -969,7 +1000,7 @@ export class OceanPool extends Pool {
|
|||||||
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress)
|
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress)
|
||||||
const events = await factory.getPastEvents('BPoolRegistered', {
|
const events = await factory.getPastEvents('BPoolRegistered', {
|
||||||
filter: {},
|
filter: {},
|
||||||
fromBlock: 0,
|
fromBlock: BPFACTORY_DEPLOY_BLOCK,
|
||||||
toBlock: 'latest'
|
toBlock: 'latest'
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -389,7 +389,6 @@ describe('Balancer flow', () => {
|
|||||||
if (consoleDebug) console.log('poolShares:' + poolShares)
|
if (consoleDebug) console.log('poolShares:' + poolShares)
|
||||||
assert(parseFloat(poolShares) > 0)
|
assert(parseFloat(poolShares) > 0)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Bob should remove Ocean liquidity from pool ', async () => {
|
it('Bob should remove Ocean liquidity from pool ', async () => {
|
||||||
const currentDtReserve = await Pool.getOceanReserve(greatPool)
|
const currentDtReserve = await Pool.getOceanReserve(greatPool)
|
||||||
const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob)
|
const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob)
|
||||||
@ -418,6 +417,11 @@ describe('Balancer flow', () => {
|
|||||||
assert(parseFloat(amounts.dtAmount) > 0)
|
assert(parseFloat(amounts.dtAmount) > 0)
|
||||||
assert(parseFloat(amounts.oceanAmount) > 0)
|
assert(parseFloat(amounts.oceanAmount) > 0)
|
||||||
})
|
})
|
||||||
|
it('ALice should get all her shares for all the pools', async () => {
|
||||||
|
const aliceShares = await Pool.getPoolsSharesbyAddress(alice)
|
||||||
|
assert(aliceShares.length > 0)
|
||||||
|
})
|
||||||
|
|
||||||
it('ALice should remove all liquidity', async () => {
|
it('ALice should remove all liquidity', async () => {
|
||||||
const aliceShares = await Pool.sharesBalance(alice, greatPool)
|
const aliceShares = await Pool.sharesBalance(alice, greatPool)
|
||||||
const aliceDtBalance = await datatoken.balance(tokenAddress, alice)
|
const aliceDtBalance = await datatoken.balance(tokenAddress, alice)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user