mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
add stats
This commit is contained in:
parent
eaffea87d2
commit
c6a9f7d69b
@ -372,4 +372,167 @@ export class OceanPool extends Pool {
|
|||||||
swapFee
|
swapFee
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search all pools created by an address
|
||||||
|
* @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[] = []
|
||||||
|
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, {
|
||||||
|
from: account
|
||||||
|
})
|
||||||
|
let myFilter
|
||||||
|
if (account) {
|
||||||
|
myFilter = { registeredBy: account }
|
||||||
|
} else {
|
||||||
|
myFilter = {}
|
||||||
|
}
|
||||||
|
const events = await factory.getPastEvents('BPoolRegistered', {
|
||||||
|
filter: myFilter,
|
||||||
|
fromBlock: 0,
|
||||||
|
toBlock: 'latest'
|
||||||
|
})
|
||||||
|
for (let i = 0; i < events.length; i++) {
|
||||||
|
if (account) {
|
||||||
|
if (events[i].returnValues[1] === account) {
|
||||||
|
result.push(events[i].returnValues[0])
|
||||||
|
}
|
||||||
|
} else result.push(events[i].returnValues[0])
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all actions from a pool (join,exit,swap)
|
||||||
|
* @param {String} poolAddress Pool address
|
||||||
|
* @param {String} account
|
||||||
|
* @param {Boolean} swaps Include swaps
|
||||||
|
* @param {Boolean} joins Include joins
|
||||||
|
* @param {Boolean} exists Include exits
|
||||||
|
* @return {String[]}
|
||||||
|
*/
|
||||||
|
public async getPoolLogs(
|
||||||
|
poolAddress: string,
|
||||||
|
account?: string,
|
||||||
|
swaps?: boolean,
|
||||||
|
joins?: boolean,
|
||||||
|
exits?: boolean
|
||||||
|
): Promise<string[]> {
|
||||||
|
const results = []
|
||||||
|
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, {
|
||||||
|
from: account
|
||||||
|
})
|
||||||
|
let events
|
||||||
|
let myFilter
|
||||||
|
if (account) myFilter = { caller: account }
|
||||||
|
else myFilter = {}
|
||||||
|
if (swaps) {
|
||||||
|
events = await pool.getPastEvents('LOG_SWAP', {
|
||||||
|
filter: myFilter,
|
||||||
|
fromBlock: 0,
|
||||||
|
toBlock: 'latest'
|
||||||
|
})
|
||||||
|
for (let i = 0; i < events.length; i++) {
|
||||||
|
if (account) {
|
||||||
|
if (events[i].returnValues[0] === account) {
|
||||||
|
results.push(this.getEventData('swap', poolAddress, events[i]))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
results.push(this.getEventData('swap', poolAddress, events[i]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (joins) {
|
||||||
|
events = await pool.getPastEvents('LOG_JOIN', {
|
||||||
|
filter: myFilter,
|
||||||
|
fromBlock: 0,
|
||||||
|
toBlock: 'latest'
|
||||||
|
})
|
||||||
|
for (let i = 0; i < events.length; i++) {
|
||||||
|
if (account) {
|
||||||
|
if (events[i].returnValues[0] === account) {
|
||||||
|
results.push(this.getEventData('join', poolAddress, events[i]))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
results.push(this.getEventData('join', poolAddress, events[i]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (exits) {
|
||||||
|
events = await pool.getPastEvents('LOG_EXIT', {
|
||||||
|
filter: myFilter,
|
||||||
|
fromBlock: 0,
|
||||||
|
toBlock: 'latest'
|
||||||
|
})
|
||||||
|
for (let i = 0; i < events.length; i++) {
|
||||||
|
if (account) {
|
||||||
|
if (events[i].returnValues[0] === account) {
|
||||||
|
results.push(this.getEventData('exit', poolAddress, events[i]))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
results.push(this.getEventData('exit', poolAddress, events[i]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all logs on all pools for a specific address
|
||||||
|
* @param {String} account
|
||||||
|
* @param {Boolean} swaps Include swaps
|
||||||
|
* @param {Boolean} joins Include joins
|
||||||
|
* @param {Boolean} exists Include exits
|
||||||
|
* @return {String[]}
|
||||||
|
*/
|
||||||
|
public async getAllPoolLogs(
|
||||||
|
account: string,
|
||||||
|
swaps?: boolean,
|
||||||
|
joins?: boolean,
|
||||||
|
exits?: boolean
|
||||||
|
): Promise<any> {
|
||||||
|
const results = []
|
||||||
|
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, {
|
||||||
|
from: account
|
||||||
|
})
|
||||||
|
const events = await factory.getPastEvents('BPoolRegistered', {
|
||||||
|
filter: {},
|
||||||
|
fromBlock: 0,
|
||||||
|
toBlock: 'latest'
|
||||||
|
})
|
||||||
|
for (let i = 0; i < events.length; i++) {
|
||||||
|
results.push(
|
||||||
|
await this.getPoolLogs(events[i].returnValues[0], account, swaps, joins, exits)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
private getEventData(action: string, poolAddress: string, data: any) {
|
||||||
|
const result = Object()
|
||||||
|
result.action = action
|
||||||
|
result.poolAddress = poolAddress
|
||||||
|
result.caller = data.returnValues[0]
|
||||||
|
result.transactionHash = data.transactionHash
|
||||||
|
result.blockNumber = data.blockNumber
|
||||||
|
switch (action) {
|
||||||
|
case 'swap':
|
||||||
|
result.tokenIn = data.returnValues[1]
|
||||||
|
result.tokenOut = data.returnValues[2]
|
||||||
|
result.tokenAmountIn = data.returnValues[3]
|
||||||
|
result.tokenAmountOut = data.returnValues[4]
|
||||||
|
break
|
||||||
|
case 'join':
|
||||||
|
result.tokenIn = data.returnValues[1]
|
||||||
|
result.tokenAmountIn = data.returnValues[2]
|
||||||
|
break
|
||||||
|
case 'exit':
|
||||||
|
result.tokenOut = data.returnValues[1]
|
||||||
|
result.tokenAmountOut = data.returnValues[2]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ export interface TokensToAdd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Pool extends PoolFactory {
|
export class Pool extends PoolFactory {
|
||||||
private poolABI: AbiItem | AbiItem[]
|
public poolABI: AbiItem | AbiItem[]
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
web3: Web3,
|
web3: Web3,
|
||||||
|
@ -357,4 +357,76 @@ export class OceanFixedRateExchange {
|
|||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all exchanges, filtered by creator(if any)
|
||||||
|
* @param {String} account
|
||||||
|
* @return {Promise<FixedPricedExchange[]>}
|
||||||
|
*/
|
||||||
|
public async searchExchangesbyCreator(
|
||||||
|
account?: string
|
||||||
|
): Promise<FixedPricedExchange[]> {
|
||||||
|
const result: FixedPricedExchange[] = []
|
||||||
|
const events = await this.contract.getPastEvents('ExchangeCreated', {
|
||||||
|
filter: {},
|
||||||
|
fromBlock: 0,
|
||||||
|
toBlock: 'latest'
|
||||||
|
})
|
||||||
|
for (let i = 0; i < events.length; i++) {
|
||||||
|
if (account) {
|
||||||
|
if (events[i].returnValues[3] === account)
|
||||||
|
result.push(await this.getExchange(events[i].returnValues[0]))
|
||||||
|
} else result.push(await this.getExchange(events[i].returnValues[0]))
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all swaps for an exchange, filtered by account(if any)
|
||||||
|
* @param {String} exchangeId
|
||||||
|
* @param {String} account
|
||||||
|
* @return {Promise<any>}
|
||||||
|
*/
|
||||||
|
public async searchExchangesSwaps(exchangeId: string, account?: string): Promise<any> {
|
||||||
|
const result: FixedPricedExchange[] = []
|
||||||
|
const events = await this.contract.getPastEvents('Swapped', {
|
||||||
|
filter: { exchangeId: exchangeId },
|
||||||
|
fromBlock: 0,
|
||||||
|
toBlock: 'latest'
|
||||||
|
})
|
||||||
|
for (let i = 0; i < events.length; i++) {
|
||||||
|
if (account) {
|
||||||
|
if (events[i].returnValues[1] === account)
|
||||||
|
result.push(await this.getEventData(events[i]))
|
||||||
|
} else result.push(await this.getEventData(events[i]))
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all swaps for an account
|
||||||
|
* @param {String} account
|
||||||
|
* @return {Promise<any>}
|
||||||
|
*/
|
||||||
|
public async searchAllExchangesSwaps(account: string): Promise<any> {
|
||||||
|
const result = []
|
||||||
|
const events = await this.contract.getPastEvents('ExchangeCreated', {
|
||||||
|
filter: {},
|
||||||
|
fromBlock: 0,
|
||||||
|
toBlock: 'latest'
|
||||||
|
})
|
||||||
|
for (let i = 0; i < events.length; i++) {
|
||||||
|
result.push(await this.searchExchangesSwaps(events[i].returnValues[0], account))
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
private getEventData(data: any) {
|
||||||
|
const result = Object()
|
||||||
|
result.exchangeID = data.returnValues[0]
|
||||||
|
result.caller = data.returnValues[1]
|
||||||
|
result.baseTokenAmount = data.returnValues[2]
|
||||||
|
result.dataTokenAmount = data.returnValues[3]
|
||||||
|
return result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -270,4 +270,19 @@ describe('Balancer flow', () => {
|
|||||||
assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance))
|
assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance))
|
||||||
assert(parseFloat(poolShares) > parseFloat(newpoolShares))
|
assert(parseFloat(poolShares) > parseFloat(newpoolShares))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('ALice should get all the pools that she created', async () => {
|
||||||
|
const alicePools = await Pool.searchPoolsbyCreator(alice)
|
||||||
|
assert(alicePools.length > 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('ALice should get the logs for her pool', async () => {
|
||||||
|
const poolLogs = await Pool.getPoolLogs(greatPool, null, true, true, true)
|
||||||
|
assert(poolLogs.length > 0)
|
||||||
|
})
|
||||||
|
it('Bob should get the logs for all his activities', async () => {
|
||||||
|
const poolLogs = await Pool.getAllPoolLogs(bob, true, true, true)
|
||||||
|
assert(poolLogs.length > 0)
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
@ -226,4 +226,12 @@ describe('FixedRateExchange flow', () => {
|
|||||||
const exchangeDetails = await FixedRateClass.searchforDT(tokenAddress, tokenAmount)
|
const exchangeDetails = await FixedRateClass.searchforDT(tokenAddress, tokenAmount)
|
||||||
assert(exchangeDetails.length === 0)
|
assert(exchangeDetails.length === 0)
|
||||||
})
|
})
|
||||||
|
it('Bob should find all the exchanges created by Alice', async () => {
|
||||||
|
const exchangeDetails = await FixedRateClass.searchExchangesbyCreator(alice)
|
||||||
|
assert(exchangeDetails.length > 0)
|
||||||
|
})
|
||||||
|
it('Bob should find all his swaps', async () => {
|
||||||
|
const exchangeDetails = await FixedRateClass.searchAllExchangesSwaps(bob)
|
||||||
|
assert(exchangeDetails.length > 0)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user