1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00
This commit is contained in:
alexcos20 2020-07-17 02:51:04 -07:00
parent 5d055fb485
commit 7b8f08e3c1
4 changed files with 461 additions and 397 deletions

View File

@ -1,55 +1,34 @@
import { BalancerPool } from './balancerlib' import { Pool } from './balancerlib'
export class OceanPool extends BalancerPool { export class OceanPool extends Pool {
/** Ocean related functions */ /** Ocean related functions */
public oceanAddress: string = null public oceanAddress: string = null
public dtAddress: string = null public dtAddress: string = null
constructor( constructor(
web3: any, web3: any,
account: string,
FactoryABI: any = null, FactoryABI: any = null,
PoolABI: any = null, PoolABI: any = null,
factoryAddress: string = null, factoryAddress: string = null,
oceanAddress: string = null oceanAddress: string = null,
gaslimit?: number
) { ) {
super(web3, account, FactoryABI, PoolABI, factoryAddress) super(web3, FactoryABI, PoolABI, factoryAddress, gaslimit)
if (oceanAddress) { if (oceanAddress) {
this.oceanAddress = oceanAddress this.oceanAddress = oceanAddress
} }
} }
/* async getNetworkAddresses(): Promise<void> {
if (this.factoryAddress && this.oceanAddress) {
return
}
const netId = await this.web3.eth.net.getId()
switch (netId) {
case 1:
if (!this.factoryAddress)
this.factoryAddress = '0x9424B1412450D0f8Fc2255FAf6046b98213B76Bd'
if (!this.oceanAddress)
this.oceanAddress = '0x985dd3D42De1e256d09e1c10F112bCCB8015AD41'
break
case 42:
if (!this.factoryAddress)
this.factoryAddress = '0x8f7F78080219d4066A8036ccD30D588B416a40DB'
if (!this.oceanAddress) this.oceanAddress = null
break
default:
this.factoryAddress = null
this.oceanAddress = null
}
} */
/** /**
* create DataToken pool * create DataToken pool
@param {String} account
* @param {String} token Data Token Address * @param {String} token Data Token Address
* @param {String} amount Data Token Amount * @param {String} amount Data Token Amount
* @param {String} weight Data Token Weight * @param {String} weight Data Token Weight
* @return {any} * @return {any}
*/ */
public async createDTPool( public async createDTPool(
account: string,
token: string, token: string,
amount: string, amount: string,
weight: string, weight: string,
@ -64,7 +43,7 @@ export class OceanPool extends BalancerPool {
console.error('Weight out of bounds (min 1, max9)') console.error('Weight out of bounds (min 1, max9)')
return null return null
} }
const address = await super.newPool() 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)
const tokens = [ const tokens = [
@ -80,65 +59,64 @@ export class OceanPool extends BalancerPool {
} }
] ]
this.dtAddress = token this.dtAddress = token
await super.addToPool(tokens) await super.addToPool(account, address, tokens)
await super.setSwapFee(fee) await super.setSwapFee(account, address, fee)
if (finalize) await super.finalize() if (finalize) await super.finalize(account, address)
return address return address
} }
/** /* Get DataToken address of token in this pool
* Load a previous created DataToken pool * @param {String} account
* @param {String} address Data Token Address * @param {String} poolAddress
* @return {any} * @return {string}
*/ */
public async loadDTPool(address: string): Promise<any> { public async getDTAddress(account: string, poolAddress: string): Promise<string>{
if (this.oceanAddress == null) { this.dtAddress = null
console.error('oceanAddress is not defined') const tokens = await this.getCurrentTokens(account,poolAddress)
return null
}
await super.loadPool(address)
const tokens = await this.getCurrentTokens()
let token let token
for (token of tokens) { for (token of tokens) {
if (token !== this.oceanAddress) this.dtAddress = token if (token !== this.oceanAddress) this.dtAddress = token
} }
return this return this.dtAddress
} }
/** /**
* Get Ocean Token balance of a pool * Get Ocean Token balance of a pool
* @param {String} account
* @param {String} poolAddress
* @return {any} * @return {any}
*/ */
public async getOceanBalance(): Promise<any> { public async getOceanReserve(account: string, poolAddress: string): Promise<any> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
console.error('oceanAddress is not defined') console.error('oceanAddress is not defined')
return null return null
} }
return super.getBalance(this.oceanAddress) return super.getReserve(account, poolAddress, this.oceanAddress)
} }
/** /**
* Get Data Token balance of a pool * Get Data Token balance of a pool
* @param {String} account
* @param {String} poolAddress
* @return {any} * @return {any}
*/ */
public async getDTBalance(): Promise<any> { public async getDTReserve(account: string, poolAddress: string): Promise<any> {
if (this.dtAddress == null) { await this.getDTAddress(account, poolAddress)
console.error( return super.getReserve(account, poolAddress, this.dtAddress)
'dtAddress is not defined. Did you do loadDTPool or createDTPool ?'
)
return null
}
return super.getBalance(this.dtAddress)
} }
/** /**
* Buy Data Token from a pool * Buy Data Token from a pool
* @param {String} account
* @param {String} poolAddress
* @param {String} amount Data Token Amount * @param {String} amount Data Token Amount
* @param {String} oceanAmount Ocean Token Amount payed * @param {String} oceanAmount Ocean Token Amount payed
* @param {String} maxPrice Maximum Price to pay * @param {String} maxPrice Maximum Price to pay
* @return {any} * @return {any}
*/ */
public async buyDT( public async buyDT(
account: string,
poolAddress: string,
amount: string, amount: string,
oceanAmount: string, oceanAmount: string,
maxPrice: string maxPrice: string
@ -147,21 +125,19 @@ export class OceanPool extends BalancerPool {
console.error('oceanAddress is not defined') console.error('oceanAddress is not defined')
return null return null
} }
if (this.dtAddress == null) { await this.getDTAddress(account, poolAddress)
console.error(
'dtAddress is not defined. Did you do loadDTPool or createDTPool ?'
)
return null
}
// TODO - check balances first // TODO - check balances first
await super.approve( await super.approve(
account,
this.oceanAddress, this.oceanAddress,
this.poolAddress, poolAddress,
this.web3.utils.toWei(oceanAmount) this.web3.utils.toWei(oceanAmount)
) )
return this.swapExactAmountOut( return this.swapExactAmountOut(
account,
poolAddress,
this.oceanAddress, this.oceanAddress,
oceanAmount, oceanAmount,
this.dtAddress, this.dtAddress,
@ -172,12 +148,16 @@ export class OceanPool extends BalancerPool {
/** /**
* Sell Data Token * Sell Data Token
* @param {String} account
* @param {String} poolAddress
* @param {String} amount Data Token Amount * @param {String} amount Data Token Amount
* @param {String} oceanAmount Ocean Token Amount expected * @param {String} oceanAmount Ocean Token Amount expected
* @param {String} maxPrice Minimum Price to sell * @param {String} maxPrice Minimum Price to sell
* @return {any} * @return {any}
*/ */
public async sellDT( public async sellDT(
account: string,
poolAddress: string,
amount: string, amount: string,
oceanAmount: string, oceanAmount: string,
minPrice: string minPrice: string
@ -186,13 +166,10 @@ export class OceanPool extends BalancerPool {
console.error('oceanAddress is not defined') console.error('oceanAddress is not defined')
return null return null
} }
if (this.dtAddress == null) { await this.getDTAddress(account, poolAddress)
console.error(
'dtAddress is not defined. Did you do loadDTPool or createDTPool ?'
)
return null
}
return this.swapExactAmountOut( return this.swapExactAmountOut(
account,
poolAddress,
this.dtAddress, this.dtAddress,
amount, amount,
this.oceanAddress, this.oceanAddress,
@ -203,89 +180,128 @@ export class OceanPool extends BalancerPool {
/** /**
* Add Data Token amount to pool liquidity * Add Data Token amount to pool liquidity
* @param {String} account
* @param {String} poolAddress
* @param {String} amount Data Token Amount * @param {String} amount Data Token Amount
* @return {any} * @return {any}
*/ */
public async addDTLiquidity(amount: string): Promise<any> { public async addDTLiquidity(
if (this.dtAddress == null) { account: string,
console.error( poolAddress: string,
'dtAddress is not defined. Did you do loadDTPool or createDTPool ?' amount: string
) ): Promise<any> {
return null await this.getDTAddress(account, poolAddress)
} await super.approve(
await this.approve( account,
this.dtAddress, this.dtAddress,
this.poolAddress, poolAddress,
this.web3.utils.toWei(amount) this.web3.utils.toWei(amount)
) )
const result = await this.joinswapExternAmountIn(this.dtAddress, amount, '0') const result = await super.joinswapExternAmountIn(
account,
poolAddress,
this.dtAddress,
amount,
'0'
)
return result return result
} }
/** /**
* Remove Data Token amount from pool liquidity * Remove Data Token amount from pool liquidity
* @param {String} account
* @param {String} poolAddress
* @param {String} amount pool Liquidity Amount * @param {String} amount pool Liquidity Amount
* @return {any} * @return {any}
*/ */
public removeDTLiquidity(amount: string, maximumPoolShares: string): Promise<any> { public async removeDTLiquidity(
if (this.dtAddress == null) { account: string,
console.error( poolAddress: string,
'dtAddress is not defined. Did you do loadDTPool or createDTPool ?' amount: string,
) maximumPoolShares: string
return null ): Promise<any> {
} await this.getDTAddress(account, poolAddress)
// TODO Check balance of PoolShares before doing exit // TODO Check balance of PoolShares before doing exit
return this.exitswapExternAmountOut(this.dtAddress, amount, maximumPoolShares) return this.exitswapExternAmountOut(
account,
poolAddress,
this.dtAddress,
amount,
maximumPoolShares
)
} }
/** /**
* Add Ocean Token amount to pool liquidity * Add Ocean Token amount to pool liquidity
* @param {String} account
* @param {String} poolAddress
* @param {String} amount Data Token Amount * @param {String} amount Data Token Amount
* @return {any} * @return {any}
*/ */
public async addOceanLiquidity(amount: string): Promise<any> { public async addOceanLiquidity(
account: string,
poolAddress: string,
amount: string
): Promise<any> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
console.error('oceanAddress is not defined') console.error('oceanAddress is not defined')
return null return null
} }
await this.approve( await super.approve(
account,
this.oceanAddress, this.oceanAddress,
this.poolAddress, poolAddress,
this.web3.utils.toWei(amount) this.web3.utils.toWei(amount)
) )
const result = await this.joinswapExternAmountIn(this.oceanAddress, amount, '0') const result = await super.joinswapExternAmountIn(
account,
poolAddress,
this.oceanAddress,
amount,
'0'
)
return result return result
} }
/** /**
* Remove Ocean Token amount from pool liquidity * Remove Ocean Token amount from pool liquidity
* @param {String} account
* @param {String} poolAddress
* @param {String} amount pool Liquidity Amount * @param {String} amount pool Liquidity Amount
* @return {any} * @return {any}
*/ */
public removeOceanLiquidity(amount: string, maximumPoolShares: string): Promise<any> { public removeOceanLiquidity(
account: string,
poolAddress: string,
amount: string,
maximumPoolShares: string
): Promise<any> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
console.error('oceanAddress is not defined') console.error('oceanAddress is not defined')
return null return null
} }
// TODO Check balance of PoolShares before doing exit // TODO Check balance of PoolShares before doing exit
return this.exitswapExternAmountOut(this.oceanAddress, amount, maximumPoolShares) return super.exitswapExternAmountOut(
account,
poolAddress,
this.oceanAddress,
amount,
maximumPoolShares
)
} }
/** /**
* Get Data Token Price from pool * Get Data Token Price from pool
* @param {String} account
* @param {String} poolAddress
* @return {any} * @return {any}
*/ */
public async getDTPrice(): Promise<any> { public async getDTPrice(account: string, poolAddress: string): Promise<any> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
console.error('oceanAddress is not defined') console.error('oceanAddress is not defined')
return null return null
} }
if (this.dtAddress == null) { await this.getDTAddress(account, poolAddress)
console.error( return super.getSpotPrice(account, poolAddress, this.dtAddress, this.oceanAddress)
'dtAddress is not defined. Did you do loadDTPool or createDTPool ?'
)
return null
}
return this.getSpotPrice(this.dtAddress, this.oceanAddress)
} }
} }

View File

@ -15,49 +15,45 @@ export interface TokensToAdd {
weight: string weight: string
} }
export class BalancerFactory { export class PoolFactory {
public GASLIMIT_DEFAULT: number = 5000000 public GASLIMIT_DEFAULT: number = 5000000
public web3: any = null public web3: any = null
public account: string = null
private FactoryABI: any private FactoryABI: any
public factoryAddress: any public factoryAddress: any
constructor( constructor(
web3: any, web3: any,
account: string,
FactoryABI: any = null, FactoryABI: any = null,
factoryAddress: string = null factoryAddress: string = null,
gaslimit?: number
) { ) {
this.web3 = web3 this.web3 = web3
this.account = account
if (FactoryABI) this.FactoryABI = FactoryABI if (FactoryABI) this.FactoryABI = FactoryABI
else this.FactoryABI = jsonFactoryABI.abi else this.FactoryABI = jsonFactoryABI.abi
if (factoryAddress) { if (factoryAddress) {
this.factoryAddress = factoryAddress this.factoryAddress = factoryAddress
} }
if (gaslimit) this.GASLIMIT_DEFAULT = gaslimit
} }
/** /**
* Creates a new pool * Creates a new pool
*/ */
async newPool(): Promise<string> { async createPool(account: string): Promise<string> {
if (this.web3 == null) { if (this.web3 == null) {
console.error('Web3 object is null') console.error('Web3 object is null')
return null return null
} }
if (this.account == null) {
console.error('Account is null')
return null
}
if (this.factoryAddress == null) { if (this.factoryAddress == null) {
console.error('bfactoryAddress is null') console.error('bfactoryAddress is null')
return null return null
} }
const factory = new this.web3.eth.Contract(this.FactoryABI, this.factoryAddress, { const factory = new this.web3.eth.Contract(this.FactoryABI, this.factoryAddress, {
from: this.account from: account
}) })
const transactiondata = await factory.methods const transactiondata = await factory.methods
.newBPool() .newBPool()
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
let pooladdress = null let pooladdress = null
try { try {
pooladdress = transactiondata.events.LOG_NEW_POOL.returnValues[1] pooladdress = transactiondata.events.LOG_NEW_POOL.returnValues[1]
@ -68,19 +64,17 @@ export class BalancerFactory {
} }
} }
export class BalancerPool extends BalancerFactory { export class Pool extends PoolFactory {
private PoolABI: any private PoolABI: any
private pool: any = null
public poolAddress: string = null
constructor( constructor(
web3: any, web3: any,
account: string,
FactoryABI: any = null, FactoryABI: any = null,
PoolABI: any = null, PoolABI: any = null,
factoryAddress: string = null factoryAddress: string = null,
gaslimit?: number
) { ) {
super(web3, account, FactoryABI, factoryAddress) super(web3, FactoryABI, factoryAddress, gaslimit)
if (PoolABI) this.PoolABI = PoolABI if (PoolABI) this.PoolABI = PoolABI
else this.PoolABI = jsonPoolABI.abi else this.PoolABI = jsonPoolABI.abi
} }
@ -88,50 +82,24 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Creates a new pool * Creates a new pool
*/ */
async newPool(): Promise<string> { async createPool(account: string): Promise<string> {
const pooladdress = await super.newPool() const pooladdress = await super.createPool(account)
this.poolAddress = pooladdress
this.pool = new this.web3.eth.Contract(this.PoolABI, pooladdress, {
from: this.account
})
return pooladdress return pooladdress
} }
/**
* Loads a new pool
*/
async loadPool(address: string): Promise<any> {
if (this.web3 == null) {
console.error('Web3 object is null')
return null
}
if (this.account == null) {
console.error('Account is null')
return null
}
if (this.factoryAddress == null) {
console.error('Cannot init. Maybe wrong network?')
return null
}
try {
this.pool = new this.web3.eth.Contract(this.PoolABI, address, {
from: this.account
})
this.poolAddress = address
return address
} catch (e) {
console.error(e)
return null
}
}
/** /**
* Approve spender to spent amount tokens * Approve spender to spent amount tokens
* @param {String} account
* @param {String} tokenAddress * @param {String} tokenAddress
* @param {String} spender * @param {String} spender
* @param {String} amount (always expressed as wei) * @param {String} amount (always expressed as wei)
*/ */
async approve(tokenAddress: string, spender: string, amount: string): Promise<any> { async approve(
account: string,
tokenAddress: string,
spender: string,
amount: string
): Promise<any> {
const minABI = [ const minABI = [
{ {
constant: false, constant: false,
@ -158,25 +126,26 @@ export class BalancerPool extends BalancerFactory {
} }
] ]
const token = new this.web3.eth.Contract(minABI, tokenAddress, { const token = new this.web3.eth.Contract(minABI, tokenAddress, {
from: this.account from: account
}) })
let result = null let result = null
try { try {
result = await token.methods result = await token.methods
.approve(spender, amount) .approve(spender, amount)
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
return result return result
} }
async sharesBalance(address: string): Promise<any> { /**
if (this.pool == null) { * Get Pool shares
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') * @param {String} account
return null * @param {String} poolAddress
}
const tokenAddress = this.poolAddress */
async sharesBalance(account: string, poolAddress: string): Promise<any> {
const minABI = [ const minABI = [
{ {
constant: true, constant: true,
@ -198,15 +167,15 @@ export class BalancerPool extends BalancerFactory {
type: 'function' type: 'function'
} }
] ]
const token = new this.web3.eth.Contract(minABI, tokenAddress, { const token = new this.web3.eth.Contract(minABI, poolAddress, {
from: this.account from: account
}) })
let result = null let result = null
try { try {
result = this.web3.utils.fromWei( result = this.web3.utils.fromWei(
await token.methods await token.methods
.balanceOf(address) .balanceOf(account)
.call({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .call({ from: account, gas: this.GASLIMIT_DEFAULT })
) )
} catch (e) { } catch (e) {
console.error(e) console.error(e)
@ -216,29 +185,36 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Adds tokens to pool * Adds tokens to pool
* @param {String} account
* @param {String} poolAddress
* @param {Array} tokens Array of token object { address,amount,weight} * @param {Array} tokens Array of token object { address,amount,weight}
*/ */
async addToPool(tokens: TokensToAdd[]): Promise<void> { async addToPool(
if (this.pool == null) { account: string,
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') poolAddress: string,
return null tokens: TokensToAdd[]
} ): Promise<void> {
const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
from: account
})
let token let token
for (token of tokens) { for (token of tokens) {
try { try {
// approve spending first // approve spending first
await this.approve( await this.approve(
account,
token.address, token.address,
this.poolAddress, poolAddress,
this.web3.utils.toWei(token.amount) this.web3.utils.toWei(token.amount)
) )
await this.pool.methods await pool.methods
.bind( .bind(
token.address, token.address,
this.web3.utils.toWei(token.amount), this.web3.utils.toWei(token.amount),
this.web3.utils.toWei(token.weight) this.web3.utils.toWei(token.weight)
) )
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -247,18 +223,19 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Set pool fee * Set pool fee
* @param {String} account
* @param {String} poolAddress
* @param {String} fee (will be converted to wei) * @param {String} fee (will be converted to wei)
*/ */
async setSwapFee(fee: string): Promise<any> { async setSwapFee(account: string, poolAddress: string, fee: string): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods result = await pool.methods
.setSwapFee(this.web3.utils.toWei(fee)) .setSwapFee(this.web3.utils.toWei(fee))
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -267,17 +244,18 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Finalize a pool * Finalize a pool
* @param {String} account
* @param {String} poolAddress
*/ */
async finalize(): Promise<any> { async finalize(account: string, poolAddress: string): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods result = await pool.methods
.finalize() .finalize()
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -286,15 +264,16 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Get number of tokens composing this pool * Get number of tokens composing this pool
* @param {String} account
* @param {String} poolAddress
*/ */
async getNumTokens(): Promise<any> { async getNumTokens(account: string, poolAddress: string): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods.getNumTokens().call() result = await pool.methods.getNumTokens().call()
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -303,16 +282,17 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Get tokens composing this pool * Get tokens composing this pool
* @param {String} account
* @param {String} poolAddress
* @return {Array} * @return {Array}
*/ */
async getCurrentTokens(): Promise<any> { async getCurrentTokens(account: string, poolAddress: string): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods.getCurrentTokens().call() result = await pool.methods.getCurrentTokens().call()
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -321,16 +301,17 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Get the final tokens composing this pool * Get the final tokens composing this pool
* @param {String} account
* @param {String} poolAddress
* @return {Array} * @return {Array}
*/ */
async getFinalTokens(): Promise<any> { async getFinalTokens(account: string, poolAddress: string): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods.getFinalTokens().call() result = await pool.methods.getFinalTokens().call()
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -339,16 +320,17 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Get controller address of this pool * Get controller address of this pool
* @param {String} account
* @param {String} poolAddress
* @return {String} * @return {String}
*/ */
async getController(): Promise<any> { async getController(account: string, poolAddress: string): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods.getController().call() result = await pool.methods.getController().call()
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -357,19 +339,24 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Set controller address of this pool * Set controller address of this pool
* @param {String} address * @param {String} account
* @param {String} poolAddress
* @param {String} controllerAddress
* @return {String} * @return {String}
*/ */
async setController(address: string): Promise<any> { async setController(
if (this.pool == null) { account: string,
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') poolAddress: string,
return null controllerAddress: string
} ): Promise<any> {
const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
from: account
})
let result = null let result = null
try { try {
result = await this.pool.methods result = await pool.methods
.setController(address) .setController(controllerAddress)
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -378,17 +365,18 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Get if a token is bounded to a pool * Get if a token is bounded to a pool
* @param {String} account
* @param {String} poolAddress
* @param {String} token Address of the token * @param {String} token Address of the token
* @return {Boolean} * @return {Boolean}
*/ */
async isBound(token: string): Promise<any> { async isBound(account: string, poolAddress: string, token: string): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods.isBound(token).call() result = await pool.methods.isBound(token).call()
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -397,17 +385,23 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Get how many tokens are in the pool * Get how many tokens are in the pool
* @param {String} account
* @param {String} poolAddress
* @param {String} token Address of the token * @param {String} token Address of the token
* @return {Boolean} * @return {Boolean}
*/ */
async getBalance(token: string): Promise<any> { async getReserve(
if (this.pool == null) { account: string,
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') poolAddress: string,
return null token: string
} ): Promise<string> {
console.log('getReserve for token:' + token)
const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
from: account
})
let amount = null let amount = null
try { try {
const result = await this.pool.methods.getBalance(token).call() const result = await pool.methods.getBalance(token).call()
amount = this.web3.utils.fromWei(result) amount = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error(e) console.error(e)
@ -417,16 +411,17 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Get if a pool is finalized * Get if a pool is finalized
* @param {String} account
* @param {String} poolAddress
* @return {Boolean} * @return {Boolean}
*/ */
async isFinalized(): Promise<any> { async isFinalized(account: string, poolAddress: string): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods.isFinalized().call() result = await pool.methods.isFinalized().call()
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -435,16 +430,17 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Get pool fee * Get pool fee
* @return {String} * @param {String} account
* @param {String} poolAddress
* @return {String} Swap fee in wei
*/ */
async getSwapFee(): Promise<any> { async getSwapFee(account: string, poolAddress: string): Promise<string> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let fee = null let fee = null
try { try {
const result = await this.pool.methods.getSwapFee().call() const result = await pool.methods.getSwapFee().call()
fee = this.web3.utils.fromWei(result) fee = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error(e) console.error(e)
@ -454,17 +450,22 @@ export class BalancerPool extends BalancerFactory {
/** /**
* The normalized weight of a token. The combined normalized weights of all tokens will sum up to 1. (Note: the actual sum may be 1 plus or minus a few wei due to division precision loss) * The normalized weight of a token. The combined normalized weights of all tokens will sum up to 1. (Note: the actual sum may be 1 plus or minus a few wei due to division precision loss)
* @param {String} account
* @param {String} poolAddress
* @param {String} token * @param {String} token
* @return {Number} * @return {Number}
*/ */
async getNormalizedWeight(token: string): Promise<any> { async getNormalizedWeight(
if (this.pool == null) { account: string,
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') poolAddress: string,
return null token: string
} ): Promise<string> {
const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
from: account
})
let weight = null let weight = null
try { try {
const result = await this.pool.methods.getNormalizedWeight(token).call() const result = await pool.methods.getNormalizedWeight(token).call()
weight = this.web3.utils.fromWei(result) weight = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error(e) console.error(e)
@ -474,17 +475,22 @@ export class BalancerPool extends BalancerFactory {
/** /**
* getDenormalizedWeight of a token in pool * getDenormalizedWeight of a token in pool
* @param {String} account
* @param {String} poolAddress
* @param {String} token * @param {String} token
* @return {Number} * @return {Number}
*/ */
async getDenormalizedWeight(token: string): Promise<any> { async getDenormalizedWeight(
if (this.pool == null) { account: string,
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') poolAddress: string,
return null token: string
} ): Promise<string> {
const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
from: account
})
let weight = null let weight = null
try { try {
const result = await this.pool.methods.getDenormalizedWeight(token).call() const result = await pool.methods.getDenormalizedWeight(token).call()
weight = this.web3.utils.fromWei(result) weight = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error(e) console.error(e)
@ -494,16 +500,20 @@ export class BalancerPool extends BalancerFactory {
/** /**
* getTotalDenormalizedWeight in pool * getTotalDenormalizedWeight in pool
* @return {Number} * @param {String} account
* @param {String} poolAddress
* @return {String}
*/ */
async getTotalDenormalizedWeight(): Promise<any> { async getTotalDenormalizedWeight(
if (this.pool == null) { account: string,
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') poolAddress: string
return null ): Promise<string> {
} const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
from: account
})
let weight = null let weight = null
try { try {
const result = await this.pool.methods.getTotalDenormalizedWeight().call() const result = await pool.methods.getTotalDenormalizedWeight().call()
weight = this.web3.utils.fromWei(result) weight = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error(e) console.error(e)
@ -513,6 +523,8 @@ export class BalancerPool extends BalancerFactory {
/** /**
* swapExactAmountIn - Trades an exact tokenAmountIn of tokenIn taken from the caller by the pool, in exchange for at least minAmountOut of tokenOut given to the caller from the pool, with a maximum marginal price of maxPrice. Returns (tokenAmountOut, spotPriceAfter), where tokenAmountOut is the amount of token that came out of the pool, and spotPriceAfter is the new marginal spot price, ie, the result of getSpotPrice after the call. (These values are what are limited by the arguments; you are guaranteed tokenAmountOut >= minAmountOut and spotPriceAfter <= maxPrice). * swapExactAmountIn - Trades an exact tokenAmountIn of tokenIn taken from the caller by the pool, in exchange for at least minAmountOut of tokenOut given to the caller from the pool, with a maximum marginal price of maxPrice. Returns (tokenAmountOut, spotPriceAfter), where tokenAmountOut is the amount of token that came out of the pool, and spotPriceAfter is the new marginal spot price, ie, the result of getSpotPrice after the call. (These values are what are limited by the arguments; you are guaranteed tokenAmountOut >= minAmountOut and spotPriceAfter <= maxPrice).
* @param {String} account
* @param {String} poolAddress
* @param {String} tokenIn * @param {String} tokenIn
* @param {String} tokenAmountIn will be converted to wei * @param {String} tokenAmountIn will be converted to wei
* @param {String} tokenOut * @param {String} tokenOut
@ -521,19 +533,20 @@ export class BalancerPool extends BalancerFactory {
* @return {any} * @return {any}
*/ */
async swapExactAmountIn( async swapExactAmountIn(
account: string,
poolAddress: string,
tokenIn: string, tokenIn: string,
tokenAmountIn: string, tokenAmountIn: string,
tokenOut: string, tokenOut: string,
minAmountOut: string, minAmountOut: string,
maxPrice: string maxPrice: string
): Promise<any> { ): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods result = await pool.methods
.swapExactAmountIn( .swapExactAmountIn(
tokenIn, tokenIn,
this.web3.utils.toWei(tokenAmountIn), this.web3.utils.toWei(tokenAmountIn),
@ -541,7 +554,7 @@ export class BalancerPool extends BalancerFactory {
this.web3.utils.toWei(minAmountOut), this.web3.utils.toWei(minAmountOut),
this.web3.utils.toWei(maxPrice) this.web3.utils.toWei(maxPrice)
) )
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -550,6 +563,8 @@ export class BalancerPool extends BalancerFactory {
/** /**
* swapExactAmountOut * swapExactAmountOut
* @param {String} account
* @param {String} poolAddress
* @param {String} tokenIn * @param {String} tokenIn
* @param {String} maxAmountIn will be converted to wei * @param {String} maxAmountIn will be converted to wei
* @param {String} tokenOut * @param {String} tokenOut
@ -558,19 +573,20 @@ export class BalancerPool extends BalancerFactory {
* @return {any} * @return {any}
*/ */
async swapExactAmountOut( async swapExactAmountOut(
account: string,
poolAddress: string,
tokenIn: string, tokenIn: string,
maxAmountIn: string, maxAmountIn: string,
tokenOut: string, tokenOut: string,
minAmountOut: string, minAmountOut: string,
maxPrice: string maxPrice: string
): Promise<any> { ): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods result = await pool.methods
.swapExactAmountOut( .swapExactAmountOut(
tokenIn, tokenIn,
this.web3.utils.toWei(maxAmountIn), this.web3.utils.toWei(maxAmountIn),
@ -578,7 +594,7 @@ export class BalancerPool extends BalancerFactory {
this.web3.utils.toWei(minAmountOut), this.web3.utils.toWei(minAmountOut),
this.web3.utils.toWei(maxPrice) this.web3.utils.toWei(maxPrice)
) )
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -587,15 +603,21 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Join the pool, getting poolAmountOut pool tokens. This will pull some of each of the currently trading tokens in the pool, meaning you must have called approve for each token for this pool. These values are limited by the array of maxAmountsIn in the order of the pool tokens. * Join the pool, getting poolAmountOut pool tokens. This will pull some of each of the currently trading tokens in the pool, meaning you must have called approve for each token for this pool. These values are limited by the array of maxAmountsIn in the order of the pool tokens.
* @param {String} account
* @param {String} poolAddress
* @param {String} poolAmountOut will be converted to wei * @param {String} poolAmountOut will be converted to wei
* @param {String} maxAmountsIn array holding maxAmount per each token, will be converted to wei * @param {String} maxAmountsIn array holding maxAmount per each token, will be converted to wei
* @return {any} * @return {any}
*/ */
async joinPool(poolAmountOut: string, maxAmountsIn: any): Promise<any> { async joinPool(
if (this.pool == null) { account: string,
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') poolAddress: string,
return null poolAmountOut: string,
} maxAmountsIn: any
): Promise<any> {
const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
from: account
})
const weiMaxAmountsIn = [] const weiMaxAmountsIn = []
let amount let amount
for (amount of maxAmountsIn) { for (amount of maxAmountsIn) {
@ -603,9 +625,9 @@ export class BalancerPool extends BalancerFactory {
} }
let result = null let result = null
try { try {
result = await this.pool.methods result = await pool.methods
.joinPool(this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn) .joinPool(this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn)
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -614,15 +636,21 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Exit the pool, paying poolAmountIn pool tokens and getting some of each of the currently trading tokens in return. These values are limited by the array of minAmountsOut in the order of the pool tokens. * Exit the pool, paying poolAmountIn pool tokens and getting some of each of the currently trading tokens in return. These values are limited by the array of minAmountsOut in the order of the pool tokens.
* @param {String} account
* @param {String} poolAddress
* @param {String} poolAmountIn will be converted to wei * @param {String} poolAmountIn will be converted to wei
* @param {String} maxAmountsIn array holding maxAmount per each token, will be converted to wei * @param {String} maxAmountsIn array holding maxAmount per each token, will be converted to wei
* @return {any} * @return {any}
*/ */
async exitPool(poolAmountIn: string, minAmountsOut: string): Promise<any> { async exitPool(
if (this.pool == null) { account: string,
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') poolAddress: string,
return null poolAmountIn: string,
} minAmountsOut: string
): Promise<any> {
const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
from: account
})
const weiMinAmountsOut = [] const weiMinAmountsOut = []
let amount let amount
for (amount of minAmountsOut) { for (amount of minAmountsOut) {
@ -630,9 +658,9 @@ export class BalancerPool extends BalancerFactory {
} }
let result = null let result = null
try { try {
result = await this.pool.methods result = await pool.methods
.exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut) .exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut)
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -641,29 +669,32 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Pay tokenAmountIn of token tokenIn to join the pool, getting poolAmountOut of the pool shares. * Pay tokenAmountIn of token tokenIn to join the pool, getting poolAmountOut of the pool shares.
* @param {String} account
* @param {String} poolAddress
* @param {String} tokenIn * @param {String} tokenIn
* @param {String} tokenAmountIn will be converted to wei * @param {String} tokenAmountIn will be converted to wei
* @param {String} minPoolAmountOut will be converted to wei * @param {String} minPoolAmountOut will be converted to wei
* @return {any} * @return {any}
*/ */
async joinswapExternAmountIn( async joinswapExternAmountIn(
account: string,
poolAddress: string,
tokenIn: string, tokenIn: string,
tokenAmountIn: string, tokenAmountIn: string,
minPoolAmountOut: string minPoolAmountOut: string
): Promise<any> { ): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods result = await pool.methods
.joinswapExternAmountIn( .joinswapExternAmountIn(
tokenIn, tokenIn,
this.web3.utils.toWei(tokenAmountIn), this.web3.utils.toWei(tokenAmountIn),
this.web3.utils.toWei(minPoolAmountOut) this.web3.utils.toWei(minPoolAmountOut)
) )
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -672,29 +703,32 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Specify poolAmountOut pool shares that you want to get, and a token tokenIn to pay with. This costs tokenAmountIn tokens (these went into the pool). * Specify poolAmountOut pool shares that you want to get, and a token tokenIn to pay with. This costs tokenAmountIn tokens (these went into the pool).
* @param {String} account
* @param {String} poolAddress
* @param {String} tokenIn * @param {String} tokenIn
* @param {String} poolAmountOut will be converted to wei * @param {String} poolAmountOut will be converted to wei
* @param {String} maxAmountIn will be converted to wei * @param {String} maxAmountIn will be converted to wei
* @return {any} * @return {any}
*/ */
async joinswapPoolAmountOut( async joinswapPoolAmountOut(
account: string,
poolAddress: string,
tokenIn: string, tokenIn: string,
poolAmountOut: string, poolAmountOut: string,
maxAmountIn: string maxAmountIn: string
): Promise<any> { ): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods result = await pool.methods
.joinswapPoolAmountOut( .joinswapPoolAmountOut(
tokenIn, tokenIn,
this.web3.utils.toWei(poolAmountOut), this.web3.utils.toWei(poolAmountOut),
this.web3.utils.toWei(maxAmountIn) this.web3.utils.toWei(maxAmountIn)
) )
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -703,29 +737,32 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Pay poolAmountIn pool shares into the pool, getting minTokenAmountOut of the given token tokenOut out of the pool. * Pay poolAmountIn pool shares into the pool, getting minTokenAmountOut of the given token tokenOut out of the pool.
* @param {String} account
* @param {String} poolAddress
* @param {String} tokenOut * @param {String} tokenOut
* @param {String} poolAmountIn will be converted to wei * @param {String} poolAmountIn will be converted to wei
* @param {String} minTokenAmountOut will be converted to wei * @param {String} minTokenAmountOut will be converted to wei
* @return {any} * @return {any}
*/ */
async exitswapPoolAmountIn( async exitswapPoolAmountIn(
account: string,
poolAddress: string,
tokenOut: string, tokenOut: string,
poolAmountIn: string, poolAmountIn: string,
minTokenAmountOut: string minTokenAmountOut: string
): Promise<any> { ): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods result = await pool.methods
.exitswapPoolAmountIn( .exitswapPoolAmountIn(
tokenOut, tokenOut,
this.web3.utils.toWei(poolAmountIn), this.web3.utils.toWei(poolAmountIn),
this.web3.utils.toWei(minTokenAmountOut) this.web3.utils.toWei(minTokenAmountOut)
) )
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -734,29 +771,32 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Specify tokenAmountOut of token tokenOut that you want to get out of the pool. This costs poolAmountIn pool shares (these went into the pool). * Specify tokenAmountOut of token tokenOut that you want to get out of the pool. This costs poolAmountIn pool shares (these went into the pool).
* @param {String} account
* @param {String} poolAddress
* @param {String} tokenOut * @param {String} tokenOut
* @param {String} tokenAmountOut will be converted to wei * @param {String} tokenAmountOut will be converted to wei
* @param {String} maxPoolAmountIn will be converted to wei * @param {String} maxPoolAmountIn will be converted to wei
* @return {any} * @return {any}
*/ */
async exitswapExternAmountOut( async exitswapExternAmountOut(
account: string,
poolAddress: string,
tokenOut: string, tokenOut: string,
tokenAmountOut: string, tokenAmountOut: string,
maxPoolAmountIn: string maxPoolAmountIn: string
): Promise<any> { ): Promise<any> {
if (this.pool == null) { const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') from: account
return null })
}
let result = null let result = null
try { try {
result = await this.pool.methods result = await pool.methods
.exitswapExternAmountOut( .exitswapExternAmountOut(
tokenOut, tokenOut,
this.web3.utils.toWei(tokenAmountOut), this.web3.utils.toWei(tokenAmountOut),
this.web3.utils.toWei(maxPoolAmountIn) this.web3.utils.toWei(maxPoolAmountIn)
) )
.send({ from: this.account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@ -765,18 +805,24 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Get Spot Price of swaping tokenIn to tokenOut * Get Spot Price of swaping tokenIn to tokenOut
* @param {String} account
* @param {String} poolAddress
* @param {String} tokenIn * @param {String} tokenIn
* @param {String} tokenOut * @param {String} tokenOut
* @return {any} * @return {any}
*/ */
async getSpotPrice(tokenIn: string, tokenOut: string): Promise<any> { async getSpotPrice(
if (this.pool == null) { account: string,
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') poolAddress: string,
return null tokenIn: string,
} tokenOut: string
): Promise<any> {
const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
from: account
})
let price = null let price = null
try { try {
const result = await this.pool.methods.getSpotPrice(tokenIn, tokenOut).call() const result = await pool.methods.getSpotPrice(tokenIn, tokenOut).call()
price = this.web3.utils.fromWei(result) price = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error(e) console.error(e)
@ -786,18 +832,24 @@ export class BalancerPool extends BalancerFactory {
/** /**
* Get Spot Price of swaping tokenIn to tokenOut without fees * Get Spot Price of swaping tokenIn to tokenOut without fees
* @param {String} account
* @param {String} poolAddress
* @param {String} tokenIn * @param {String} tokenIn
* @param {String} tokenOut * @param {String} tokenOut
* @return {any} * @return {any}
*/ */
async getSpotPriceSansFee(tokenIn: string, tokenOut: string): Promise<any> { async getSpotPriceSansFee(
if (this.pool == null) { account: string,
console.error('BPool not initialiez. Maybe you missed newPool or loadPool ?') poolAddress: string,
return null tokenIn: string,
} tokenOut: string
): Promise<any> {
const pool = new this.web3.eth.Contract(this.PoolABI, poolAddress, {
from: account
})
let price = null let price = null
try { try {
const result = await this.pool.methods const result = await pool.methods
.getSpotPriceSansFee(tokenIn, tokenOut) .getSpotPriceSansFee(tokenIn, tokenOut)
.call() .call()
price = this.web3.utils.fromWei(result) price = this.web3.utils.fromWei(result)

View File

@ -18,6 +18,7 @@ import {
generateIntantiableConfigFromConfig generateIntantiableConfigFromConfig
} from '../Instantiable.abstract' } from '../Instantiable.abstract'
import { Compute } from './Compute' import { Compute } from './Compute'
import { OceanPool } from '../balancer/OceanPool'
/** /**
* Main interface for Ocean Protocol. * Main interface for Ocean Protocol.

View File

@ -25,20 +25,17 @@ describe('Balancer flow', () => {
let Pool let Pool
let OceanPoolContracts let OceanPoolContracts
let oceandatatoken let oceandatatoken
let alicePool
let alicePoolAddress let alicePoolAddress
let bobPool
let currentDtPrice let currentDtPrice
let bobPoolShares
let owner let owner
let bob let bob
let alice let alice
let contracts let contracts
let datatoken let datatoken
let tokenAddress let tokenAddress
let transactionId let consoleDebug: false
const tokenAmount = web3.utils.toWei('1000') const tokenAmount = '1000'
const transferAmount = web3.utils.toWei('200') const transferAmount = '200'
const blob = 'http://localhost:8030/api/v1/services/consume' const blob = 'http://localhost:8030/api/v1/services/consume'
describe('#test', () => { describe('#test', () => {
it('Initialize Ocean contracts v3', async () => { it('Initialize Ocean contracts v3', async () => {
@ -94,7 +91,7 @@ describe('Balancer flow', () => {
OceanPoolFactoryAddress = OceanPoolContracts.factoryAddress OceanPoolFactoryAddress = OceanPoolContracts.factoryAddress
assert(OceanPoolFactoryAddress !== null) assert(OceanPoolFactoryAddress !== null)
}) })
it('Deploy Spool/SFactory', async () => { /* it('Deploy Spool/SFactory', async () => {
const SContracts = new BalancerContractHandler( const SContracts = new BalancerContractHandler(
SFactory.abi, SFactory.abi,
SFactory.bytecode, SFactory.bytecode,
@ -109,11 +106,11 @@ describe('Balancer flow', () => {
const SFactoryAddress = SContracts.factoryAddress const SFactoryAddress = SContracts.factoryAddress
assert(SFactoryAddress !== null) assert(SFactoryAddress !== null)
}) })
*/
it('should initialize OceanPool class', async () => { it('should initialize OceanPool class', async () => {
Pool = new OceanPool( Pool = new OceanPool(
web3, web3,
alice,
OceanPoolFactory.abi, OceanPoolFactory.abi,
OceanPoolPool.abi, OceanPoolPool.abi,
OceanPoolFactoryAddress, OceanPoolFactoryAddress,
@ -135,126 +132,124 @@ describe('Balancer flow', () => {
transferAmount, transferAmount,
alice alice
) )
transactionId = ts.transactionHash
}) })
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(tokenAddress, 45, 9, '0.02') alicePoolAddress = await Pool.createDTPool(alice, tokenAddress, 45, 9, '0.02')
alicePool = await Pool.loadDTPool(alicePoolAddress)
assert(alicePool !== null)
}) })
it('Get pool information', async () => { it('Get pool information', async () => {
const currentTokens = await alicePool.getCurrentTokens() const currentTokens = await Pool.getCurrentTokens(alice, alicePoolAddress)
assert(currentTokens.length === 2) assert(currentTokens.length === 2)
assert(currentTokens.includes(tokenAddress)) assert(currentTokens.includes(tokenAddress))
assert(currentTokens.includes(oceanTokenAddress)) assert(currentTokens.includes(oceanTokenAddress))
}) })
it('Get pool swap fee', async () => { it('Get pool swap fee', async () => {
const currentSwapFee = await alicePool.getSwapFee() const currentSwapFee = await Pool.getSwapFee(alice, alicePoolAddress)
assert(currentSwapFee === '0.02') assert(currentSwapFee === '0.02')
}) })
it('Get dtPrice from the pool ', async () => { it('Get dtPrice from the pool ', async () => {
currentDtPrice = await alicePool.getDTPrice() currentDtPrice = await Pool.getDTPrice(alice, alicePoolAddress)
assert(currentDtPrice > 0) assert(currentDtPrice > 0)
}) })
it('Get dtToken pool reserve ', async () => { it('Get dtToken pool reserve ', async () => {
const currentDtReserve = await alicePool.getBalance(tokenAddress) const currentDtReserve = await Pool.getDTReserve(alice, alicePoolAddress)
assert(currentDtReserve > 0) assert(currentDtReserve > 0)
}) })
it('Get dtToken pool reserve ', async () => { it('Get dtToken pool reserve ', async () => {
const currentOceanReserve = await alicePool.getOceanBalance() const currentOceanReserve = await Pool.getOceanReserve(
alice,
alicePoolAddress
)
assert(currentOceanReserve > 0) assert(currentOceanReserve > 0)
}) })
it("Bob should load Alice's pool ", async () => {
bobPool = new OceanPool(
web3,
bob,
OceanPoolFactory.abi,
OceanPoolPool.abi,
OceanPoolFactoryAddress,
oceanTokenAddress
)
await bobPool.loadDTPool(alicePoolAddress)
})
it('Bob should buy a DT ', async () => { it('Bob should buy a DT ', async () => {
const maxPrice = parseFloat(currentDtPrice) * 2 const maxPrice = parseFloat(currentDtPrice) * 2
await bobPool.buyDT('1', '2', String(maxPrice)) await Pool.buyDT(bob, alicePoolAddress, '1', '2', String(maxPrice))
const bobDtBalance = await datatoken.balance(tokenAddress, bob) const bobDtBalance = await datatoken.balance(tokenAddress, bob)
const bobOceanBalance = await datatoken.balance(oceanTokenAddress, bob) const bobOceanBalance = await datatoken.balance(oceanTokenAddress, bob)
assert(bobDtBalance > 0) assert(bobDtBalance > 0)
assert(bobOceanBalance > 0) assert(bobOceanBalance > 0)
}) })
it('Bob should add DT liquidity to pool ', async () => { it('Bob should add DT liquidity to pool ', async () => {
const currentDtReserve = await alicePool.getBalance(tokenAddress) const currentDtReserve = await Pool.getDTReserve(bob, alicePoolAddress)
const bobDtBalance = web3.utils.fromWei( if (consoleDebug) console.log('currentDtReserve:' + currentDtReserve)
await datatoken.balance(tokenAddress, bob) const bobDtBalance = await datatoken.balance(tokenAddress, bob)
) if (consoleDebug) console.log('BOB DT Balance:' + bobDtBalance)
await Pool.addDTLiquidity(bob, alicePoolAddress, bobDtBalance)
await bobPool.addDTLiquidity(bobDtBalance) const newbobDtBalance = await datatoken.balance(tokenAddress, bob)
const newbobDtBalance = web3.utils.fromWei( const newDtReserve = await Pool.getDTReserve(bob, alicePoolAddress)
await datatoken.balance(tokenAddress, bob)
)
const newDtReserve = await alicePool.getBalance(tokenAddress) const sharesBalance = await Pool.sharesBalance(bob, alicePoolAddress)
if (consoleDebug) console.log('newDtReserve:' + newDtReserve)
const sharesBalance = await bobPool.sharesBalance(bob) if (consoleDebug) console.log('newbobDtBalance:' + newbobDtBalance)
if (consoleDebug) console.log('sharesBalance:' + sharesBalance)
assert(parseFloat(newbobDtBalance) < parseFloat(bobDtBalance)) assert(parseFloat(newbobDtBalance) < parseFloat(bobDtBalance))
assert(parseFloat(newDtReserve) > parseFloat(currentDtReserve)) assert(parseFloat(newDtReserve) > parseFloat(currentDtReserve))
assert(parseFloat(sharesBalance) > 0) assert(parseFloat(sharesBalance) > 0)
}) })
it('Bob should remove DT liquidity from pool ', async () => { it('Bob should remove DT liquidity from pool ', async () => {
const currentDtReserve = await alicePool.getBalance(tokenAddress) const currentDtReserve = await Pool.getDTReserve(bob, alicePoolAddress)
const bobDtBalance = web3.utils.fromWei( if (consoleDebug) console.log('currentDtReserve:' + currentDtReserve)
await datatoken.balance(tokenAddress, bob) const bobDtBalance = await datatoken.balance(tokenAddress, bob)
) if (consoleDebug) console.log('bobDtBalance:' + bobDtBalance)
const poolShares = await bobPool.sharesBalance(bob) const poolShares = await Pool.sharesBalance(bob, alicePoolAddress)
await bobPool.removeDTLiquidity('0.75', poolShares) if (consoleDebug) console.log('poolShares:' + poolShares)
await Pool.removeDTLiquidity(bob, alicePoolAddress, '0.75', poolShares)
const newDtReserve = await alicePool.getBalance(tokenAddress) const newDtReserve = await Pool.getDTReserve(bob, alicePoolAddress)
const newbobDtBalance = web3.utils.fromWei( if (consoleDebug) console.log('newDtReserve:' + newDtReserve)
await datatoken.balance(tokenAddress, bob) const newbobDtBalance = await datatoken.balance(tokenAddress, bob)
) if (consoleDebug) console.log('newbobDtBalance:' + newbobDtBalance)
const newpoolShares = await bobPool.sharesBalance(bob) const newpoolShares = await Pool.sharesBalance(bob, alicePoolAddress)
if (consoleDebug) console.log('newpoolShares:' + newpoolShares)
assert(parseFloat(newDtReserve) < parseFloat(currentDtReserve)) assert(parseFloat(newDtReserve) < parseFloat(currentDtReserve))
assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance)) assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance))
assert(parseFloat(poolShares) > parseFloat(newpoolShares)) assert(parseFloat(poolShares) > parseFloat(newpoolShares))
}) })
it('Bob should add Ocean liquidity to pool ', async () => { it('Bob should add Ocean liquidity to pool ', async () => {
const currentDtReserve = await alicePool.getBalance(oceanTokenAddress) const currentDtReserve = await Pool.getOceanReserve(bob, alicePoolAddress)
const bobDtBalance = web3.utils.fromWei( const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob)
await datatoken.balance(oceanTokenAddress, bob) if (consoleDebug) console.log('currentDtReserve:' + currentDtReserve)
) if (consoleDebug) console.log('bobDtBalance:' + bobDtBalance)
await bobPool.addOceanLiquidity('1') await Pool.addOceanLiquidity(bob, alicePoolAddress, '1')
const newbobDtBalance = web3.utils.fromWei( const newbobDtBalance = await datatoken.balance(oceanTokenAddress, bob)
await datatoken.balance(oceanTokenAddress, bob)
)
const newDtReserve = await alicePool.getBalance(oceanTokenAddress) const newDtReserve = await Pool.getOceanReserve(bob, alicePoolAddress)
const sharesBalance = await bobPool.sharesBalance(bob) const sharesBalance = await Pool.sharesBalance(bob, alicePoolAddress)
if (consoleDebug) console.log('newDtReserve:' + newDtReserve)
if (consoleDebug) console.log('newbobDtBalance:' + newbobDtBalance)
if (consoleDebug) console.log('sharesBalance:' + sharesBalance)
assert(parseFloat(newbobDtBalance) < parseFloat(bobDtBalance)) assert(parseFloat(newbobDtBalance) < parseFloat(bobDtBalance))
assert(parseFloat(newDtReserve) > parseFloat(currentDtReserve)) assert(parseFloat(newDtReserve) > parseFloat(currentDtReserve))
assert(parseFloat(sharesBalance) > 0) assert(parseFloat(sharesBalance) > 0)
}) })
it('Bob should remove Ocean liquidity from pool ', async () => { it('Bob should remove Ocean liquidity from pool ', async () => {
const currentDtReserve = await alicePool.getBalance(oceanTokenAddress) const currentDtReserve = await Pool.getOceanReserve(bob, alicePoolAddress)
const bobDtBalance = web3.utils.fromWei( const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob)
await datatoken.balance(oceanTokenAddress, bob)
)
const poolShares = await bobPool.sharesBalance(bob)
await bobPool.removeOceanLiquidity('0.75', poolShares)
const newDtReserve = await alicePool.getBalance(oceanTokenAddress) const poolShares = await Pool.sharesBalance(bob, alicePoolAddress)
const newbobDtBalance = web3.utils.fromWei( if (consoleDebug) console.log('currentDtReserve:' + currentDtReserve)
await datatoken.balance(oceanTokenAddress, bob) if (consoleDebug) console.log('bobDtBalance:' + bobDtBalance)
) if (consoleDebug) console.log('poolShares:' + poolShares)
const newpoolShares = await bobPool.sharesBalance(bob)
await Pool.removeOceanLiquidity(bob, alicePoolAddress, '0.75', poolShares)
const newDtReserve = await Pool.getOceanReserve(bob, alicePoolAddress)
const newbobDtBalance = await datatoken.balance(oceanTokenAddress, bob)
const newpoolShares = await Pool.sharesBalance(bob, alicePoolAddress)
if (consoleDebug) console.log('newDtReserve:' + newDtReserve)
if (consoleDebug) console.log('newbobDtBalance:' + newbobDtBalance)
if (consoleDebug) console.log('newpoolShares:' + newpoolShares)
assert(parseFloat(newDtReserve) < parseFloat(currentDtReserve)) assert(parseFloat(newDtReserve) < parseFloat(currentDtReserve))
assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance)) assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance))
assert(parseFloat(poolShares) > parseFloat(newpoolShares)) assert(parseFloat(poolShares) > parseFloat(newpoolShares))