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

make pool.create subscribable

This commit is contained in:
alexcos20 2020-10-21 02:27:58 -07:00
parent f5f9e3308d
commit dec9c57c94
2 changed files with 58 additions and 45 deletions

View File

@ -4,7 +4,7 @@ import { TransactionReceipt } from 'web3-core'
import { Pool } from './Pool' import { Pool } from './Pool'
import { EventData, Filter } from 'web3-eth-contract' import { EventData, Filter } from 'web3-eth-contract'
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
import { SubscribablePromise } from '../utils'
declare type PoolTransactionType = 'swap' | 'join' | 'exit' 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
@ -34,6 +34,13 @@ export interface PoolTransaction {
type: PoolTransactionType type: PoolTransactionType
} }
export enum PoolCreateProgressStep {
CreatingPool,
ApprovingDatatoken,
ApprovingOcean,
SetupPool
}
/** /**
* Ocean Pools submodule exposed under ocean.pool * Ocean Pools submodule exposed under ocean.pool
*/ */
@ -64,13 +71,13 @@ export class OceanPool extends Pool {
* @param {String} fee Swap fee. E.g. to get a 0.1% swapFee use `0.001`. The maximum allowed swapFee is `0.1` (10%). * @param {String} fee Swap fee. E.g. to get a 0.1% swapFee use `0.001`. The maximum allowed swapFee is `0.1` (10%).
* @return {String} * @return {String}
*/ */
public async createDTPool( public create(
account: string, account: string,
token: string, token: string,
amount: string, amount: string,
weight: string, weight: string,
fee: string fee: string
): Promise<string> { ): SubscribablePromise<PoolCreateProgressStep, string> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
console.error('ERROR: oceanAddress is not defined') console.error('ERROR: oceanAddress is not defined')
return null return null
@ -83,10 +90,13 @@ export class OceanPool extends Pool {
console.error('ERROR: Weight out of bounds (min 1, max9)') console.error('ERROR: Weight out of bounds (min 1, max9)')
return null return null
} }
return new SubscribablePromise(async (observer) => {
observer.next(PoolCreateProgressStep.CreatingPool)
const address = await super.createPool(account) const address = await super.createPool(account)
const oceanWeight = 10 - parseFloat(weight) const oceanWeight = 10 - parseFloat(weight)
const oceanAmount = (parseFloat(amount) * oceanWeight) / parseFloat(weight) const oceanAmount = (parseFloat(amount) * oceanWeight) / parseFloat(weight)
this.dtAddress = token this.dtAddress = token
observer.next(PoolCreateProgressStep.ApprovingDatatoken)
let txid let txid
txid = await this.approve( txid = await this.approve(
account, account,
@ -98,6 +108,7 @@ export class OceanPool extends Pool {
console.error('ERROR: Failed to call approve DT token') console.error('ERROR: Failed to call approve DT token')
return null return null
} }
observer.next(PoolCreateProgressStep.ApprovingOcean)
txid = await this.approve( txid = await this.approve(
account, account,
this.oceanAddress, this.oceanAddress,
@ -108,6 +119,7 @@ export class OceanPool extends Pool {
console.error('ERROR: Failed to call approve OCEAN token') console.error('ERROR: Failed to call approve OCEAN token')
return null return null
} }
observer.next(PoolCreateProgressStep.SetupPool)
txid = await super.setup( txid = await super.setup(
account, account,
address, address,
@ -124,6 +136,7 @@ export class OceanPool extends Pool {
return null return null
} }
return address return address
})
} }
/** /**

View File

@ -121,7 +121,7 @@ describe('Balancer flow', () => {
}) })
it('Alice creates a new OceanPool pool', async () => { it('Alice creates a new OceanPool pool', async () => {
/// new pool with total DT = 45 , dt weight=90% with swap fee 2% /// new pool with total DT = 45 , dt weight=90% with swap fee 2%
alicePoolAddress = await Pool.createDTPool(alice, tokenAddress, '45', '9', '0.02') alicePoolAddress = await Pool.create(alice, tokenAddress, '45', '9', '0.02')
const s = await Pool.getPoolSharesTotalSupply(alicePoolAddress) const s = await Pool.getPoolSharesTotalSupply(alicePoolAddress)
assert(String(s) === '100', 'totalSupply does not match: ' + s) assert(String(s) === '100', 'totalSupply does not match: ' + s)
const n = await Pool.getNumTokens(alicePoolAddress) const n = await Pool.getNumTokens(alicePoolAddress)