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

small refactor

This commit is contained in:
alexcos20 2020-09-30 02:59:15 -07:00
parent 54cb529ff3
commit 6f645d6e8f
2 changed files with 68 additions and 92 deletions

View File

@ -9,22 +9,20 @@ export interface PoolDetails {
tokens: string[] tokens: string[]
} }
export interface PoolAction { export interface PoolTransaction {
poolAddress: string poolAddress: string
dtAddress: string
caller: string caller: string
transactionHash: string transactionHash: string
blockNumber: number blockNumber: number
timestamp: number
tokenIn?: string tokenIn?: string
tokenOut?: string tokenOut?: string
tokenAmountIn?: string tokenAmountIn?: string
tokenAmountOut?: string tokenAmountOut?: string
type: 'join' | 'exit' | 'swap'
} }
export interface PoolLogs {
joins?: PoolAction[]
exits?: PoolAction[]
swaps?: PoolAction[]
}
/** /**
* Ocean Pools submodule exposed under ocean.pool * Ocean Pools submodule exposed under ocean.pool
*/ */
@ -104,7 +102,7 @@ export class OceanPool extends Pool {
* @param {String} poolAddress * @param {String} poolAddress
* @return {string} * @return {string}
*/ */
public async getDTAddress(account: string, poolAddress: string): Promise<string> { public async getDTAddress(poolAddress: string): Promise<string> {
this.dtAddress = null this.dtAddress = null
const tokens = await this.getCurrentTokens(poolAddress) const tokens = await this.getCurrentTokens(poolAddress)
let token: string let token: string
@ -137,7 +135,7 @@ export class OceanPool extends Pool {
* @return {String} * @return {String}
*/ */
public async getDTReserve(account: string, poolAddress: string): Promise<string> { public async getDTReserve(account: string, poolAddress: string): Promise<string> {
await this.getDTAddress(account, poolAddress) await this.getDTAddress(poolAddress)
return super.getReserve(account, poolAddress, this.dtAddress) return super.getReserve(account, poolAddress, this.dtAddress)
} }
@ -161,7 +159,7 @@ export class OceanPool extends Pool {
console.error('oceanAddress is not defined') console.error('oceanAddress is not defined')
return null return null
} }
await this.getDTAddress(account, poolAddress) await this.getDTAddress(poolAddress)
// TODO - check balances first // TODO - check balances first
await super.approve( await super.approve(
@ -202,7 +200,7 @@ export class OceanPool extends Pool {
console.error('oceanAddress is not defined') console.error('oceanAddress is not defined')
return null return null
} }
await this.getDTAddress(account, poolAddress) await this.getDTAddress(poolAddress)
return this.swapExactAmountOut( return this.swapExactAmountOut(
account, account,
poolAddress, poolAddress,
@ -226,7 +224,7 @@ export class OceanPool extends Pool {
poolAddress: string, poolAddress: string,
amount: string amount: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
await this.getDTAddress(account, poolAddress) await this.getDTAddress(poolAddress)
await super.approve( await super.approve(
account, account,
this.dtAddress, this.dtAddress,
@ -256,7 +254,7 @@ export class OceanPool extends Pool {
amount: string, amount: string,
maximumPoolShares: string maximumPoolShares: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
await this.getDTAddress(account, poolAddress) await this.getDTAddress(poolAddress)
// TODO Check balance of PoolShares before doing exit // TODO Check balance of PoolShares before doing exit
return this.exitswapExternAmountOut( return this.exitswapExternAmountOut(
account, account,
@ -368,7 +366,7 @@ export class OceanPool extends Pool {
poolAddress: string, poolAddress: string,
dtRequired: string dtRequired: string
): Promise<string> { ): Promise<string> {
await this.getDTAddress(account, poolAddress) await this.getDTAddress(poolAddress)
const tokenBalanceIn = await this.getReserve(account, poolAddress, this.oceanAddress) const tokenBalanceIn = await this.getReserve(account, poolAddress, this.oceanAddress)
const tokenWeightIn = await this.getDenormalizedWeight( const tokenWeightIn = await this.getDenormalizedWeight(
account, account,
@ -439,82 +437,69 @@ export class OceanPool extends Pool {
*/ */
public async getPoolLogs( public async getPoolLogs(
poolAddress: string, poolAddress: string,
account?: string, account?: string
swaps?: boolean, ): Promise<PoolTransaction[]> {
joins?: boolean, const results: PoolTransaction[] = []
exits?: boolean
): Promise<PoolLogs> {
const results: PoolLogs = { joins: [], exits: [], swaps: [] }
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account })
const dtAddress = await this.getDTAddress(poolAddress)
const myFilter: Filter = account ? { caller: account } : {} const myFilter: Filter = account ? { caller: account } : {}
let events: EventData[] let events: EventData[]
if (swaps) { events = await pool.getPastEvents('LOG_SWAP', {
events = await pool.getPastEvents('LOG_SWAP', { filter: myFilter,
filter: myFilter, fromBlock: 0,
fromBlock: 0, toBlock: 'latest'
toBlock: 'latest' })
}) for (let i = 0; i < events.length; i++) {
for (let i = 0; i < events.length; i++) { if (account) {
if (account) { if (events[i].returnValues[0] === account) {
if (events[i].returnValues[0] === account) { results.push(await this.getEventData('swap', poolAddress, dtAddress, events[i]))
results.swaps.push(this.getEventData('swap', poolAddress, events[i]))
}
} else {
results.swaps.push(this.getEventData('swap', poolAddress, events[i]))
} }
} else {
results.push(await this.getEventData('swap', poolAddress, dtAddress, events[i]))
} }
} }
if (joins) {
events = await pool.getPastEvents('LOG_JOIN', { events = await pool.getPastEvents('LOG_JOIN', {
filter: myFilter, filter: myFilter,
fromBlock: 0, fromBlock: 0,
toBlock: 'latest' toBlock: 'latest'
}) })
for (let i = 0; i < events.length; i++) { for (let i = 0; i < events.length; i++) {
if (account) { if (account) {
if (events[i].returnValues[0] === account) { if (events[i].returnValues[0] === account) {
results.joins.push(this.getEventData('join', poolAddress, events[i])) results.push(await this.getEventData('join', poolAddress, dtAddress, events[i]))
}
} else {
results.joins.push(this.getEventData('join', poolAddress, events[i]))
} }
} else {
results.push(await this.getEventData('join', poolAddress, dtAddress, events[i]))
} }
} }
if (exits) {
events = await pool.getPastEvents('LOG_EXIT', { events = await pool.getPastEvents('LOG_EXIT', {
filter: myFilter, filter: myFilter,
fromBlock: 0, fromBlock: 0,
toBlock: 'latest' toBlock: 'latest'
}) })
for (let i = 0; i < events.length; i++) { for (let i = 0; i < events.length; i++) {
if (account) { if (account) {
if (events[i].returnValues[0] === account) { if (events[i].returnValues[0] === account) {
results.exits.push(this.getEventData('exit', poolAddress, events[i])) results.push(await this.getEventData('exit', poolAddress, dtAddress, events[i]))
}
} else {
results.exits.push(this.getEventData('exit', poolAddress, events[i]))
} }
} else {
results.push(await this.getEventData('exit', poolAddress, dtAddress, events[i]))
} }
} }
return results return results
} }
/** /**
* Get all logs on all pools for a specific address * Get all logs on all pools for a specific address
* @param {String} account * @param {String} account
* @param {Boolean} swaps Include swaps
* @param {Boolean} joins Include joins
* @param {Boolean} exits Include exits
* @return {PoolLogs} * @return {PoolLogs}
*/ */
public async getAllPoolLogs( public async getAllPoolLogs(account: string): Promise<PoolTransaction[]> {
account: string, const results: PoolTransaction[] = []
swaps?: boolean,
joins?: boolean,
exits?: boolean
): Promise<PoolLogs> {
const results: PoolLogs = { joins: [], exits: [], swaps: [] }
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, {
from: account from: account
}) })
@ -524,36 +509,27 @@ export class OceanPool extends Pool {
toBlock: 'latest' toBlock: 'latest'
}) })
for (let i = 0; i < events.length; i++) { for (let i = 0; i < events.length; i++) {
const logs = await this.getPoolLogs( const logs = await this.getPoolLogs(events[i].returnValues[0], account)
events[i].returnValues[0], for (let j = 0; j < logs.length; j++) results.push(logs[j])
account,
swaps,
joins,
exits
)
logs.joins.forEach((log) => {
results.joins.push(log)
})
logs.exits.forEach((log) => {
results.exits.push(log)
})
logs.swaps.forEach((log) => {
results.swaps.push(log)
})
} }
return results return results
} }
private getEventData( private async getEventData(
action: 'swap' | 'join' | 'exit', action: 'swap' | 'join' | 'exit',
poolAddress: string, poolAddress: string,
dtAddress: string,
data: EventData data: EventData
): PoolAction { ): Promise<PoolTransaction> {
let result: PoolAction = { const blockDetails = await this.web3.eth.getBlock(data.blockNumber)
let result: PoolTransaction = {
poolAddress, poolAddress,
dtAddress,
caller: data.returnValues[0], caller: data.returnValues[0],
transactionHash: data.transactionHash, transactionHash: data.transactionHash,
blockNumber: data.blockNumber blockNumber: data.blockNumber,
timestamp: parseInt(String(blockDetails.timestamp)),
type: action
} }
switch (action) { switch (action) {

View File

@ -277,11 +277,11 @@ describe('Balancer flow', () => {
}) })
it('ALice should get the logs for her pool', async () => { it('ALice should get the logs for her pool', async () => {
const poolLogs = await Pool.getPoolLogs(greatPool, null, true, true, true) const poolLogs = await Pool.getPoolLogs(greatPool, null)
assert(poolLogs.joins.length > 0) assert(poolLogs.length > 0)
}) })
it('Bob should get the logs for all his activities', async () => { it('Bob should get the logs for all his activities', async () => {
const poolLogs = await Pool.getAllPoolLogs(bob, true, true, true) const poolLogs = await Pool.getAllPoolLogs(bob)
assert(poolLogs.swaps.length > 0) assert(poolLogs.length > 0)
}) })
}) })