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 { EventData, Filter } from 'web3-eth-contract'
import BigNumber from 'bignumber.js'
import { SubscribablePromise } from '../utils'
declare type PoolTransactionType = 'swap' | 'join' | 'exit'
const POOL_MAX_AMOUNT_IN_LIMIT = 0.25 // maximum 1/4 of the pool reserve
@ -34,6 +34,13 @@ export interface PoolTransaction {
type: PoolTransactionType
}
export enum PoolCreateProgressStep {
CreatingPool,
ApprovingDatatoken,
ApprovingOcean,
SetupPool
}
/**
* 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%).
* @return {String}
*/
public async createDTPool(
public create(
account: string,
token: string,
amount: string,
weight: string,
fee: string
): Promise<string> {
): SubscribablePromise<PoolCreateProgressStep, string> {
if (this.oceanAddress == null) {
console.error('ERROR: oceanAddress is not defined')
return null
@ -83,47 +90,53 @@ export class OceanPool extends Pool {
console.error('ERROR: Weight out of bounds (min 1, max9)')
return null
}
const address = await super.createPool(account)
const oceanWeight = 10 - parseFloat(weight)
const oceanAmount = (parseFloat(amount) * oceanWeight) / parseFloat(weight)
this.dtAddress = token
let txid
txid = await this.approve(
account,
token,
address,
this.web3.utils.toWei(String(amount))
)
if (!txid) {
console.error('ERROR: Failed to call approve DT token')
return null
}
txid = await this.approve(
account,
this.oceanAddress,
address,
this.web3.utils.toWei(String(oceanAmount))
)
if (!txid) {
console.error('ERROR: Failed to call approve OCEAN token')
return null
}
txid = await super.setup(
account,
address,
token,
this.web3.utils.toWei(String(amount)),
this.web3.utils.toWei(String(weight)),
this.oceanAddress,
this.web3.utils.toWei(String(oceanAmount)),
this.web3.utils.toWei(String(oceanWeight)),
this.web3.utils.toWei(fee)
)
if (!txid) {
console.error('ERROR: Failed to create a new pool')
return null
}
return address
return new SubscribablePromise(async (observer) => {
observer.next(PoolCreateProgressStep.CreatingPool)
const address = await super.createPool(account)
const oceanWeight = 10 - parseFloat(weight)
const oceanAmount = (parseFloat(amount) * oceanWeight) / parseFloat(weight)
this.dtAddress = token
observer.next(PoolCreateProgressStep.ApprovingDatatoken)
let txid
txid = await this.approve(
account,
token,
address,
this.web3.utils.toWei(String(amount))
)
if (!txid) {
console.error('ERROR: Failed to call approve DT token')
return null
}
observer.next(PoolCreateProgressStep.ApprovingOcean)
txid = await this.approve(
account,
this.oceanAddress,
address,
this.web3.utils.toWei(String(oceanAmount))
)
if (!txid) {
console.error('ERROR: Failed to call approve OCEAN token')
return null
}
observer.next(PoolCreateProgressStep.SetupPool)
txid = await super.setup(
account,
address,
token,
this.web3.utils.toWei(String(amount)),
this.web3.utils.toWei(String(weight)),
this.oceanAddress,
this.web3.utils.toWei(String(oceanAmount)),
this.web3.utils.toWei(String(oceanWeight)),
this.web3.utils.toWei(fee)
)
if (!txid) {
console.error('ERROR: Failed to create a new pool')
return null
}
return address
})
}
/**

View File

@ -121,7 +121,7 @@ describe('Balancer flow', () => {
})
it('Alice creates a new OceanPool pool', async () => {
/// 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)
assert(String(s) === '100', 'totalSupply does not match: ' + s)
const n = await Pool.getNumTokens(alicePoolAddress)