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

pool docs updates, small refactor

This commit is contained in:
Matthias Kretschmann 2020-08-19 12:06:55 +02:00
parent 7f9bc8d58e
commit 94bc2295f2
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 100 additions and 96 deletions

View File

@ -1,8 +1,7 @@
import { Pool } from './balancerlib'
import Web3 from 'web3'
import { Pool } from './Pool'
export class OceanPool extends Pool {
/** Ocean related functions */
public oceanAddress: string = null
public dtAddress: string = null
@ -23,9 +22,9 @@ export class OceanPool extends Pool {
/**
* create DataToken pool
@param {String} account
* @param {String} token Data Token Address
* @param {String} amount Data Token Amount
* @param {String} weight Data Token Weight
* @param {String} token Data Token address
* @param {String} amount Data Token amount
* @param {String} weight Data Token weight
* @return {any}
*/
public async createDTPool(
@ -66,7 +65,8 @@ export class OceanPool extends Pool {
return address
}
/* Get DataToken address of token in this pool
/**
* Get DataToken address of token in this pool
* @param {String} account
* @param {String} poolAddress
* @return {string}
@ -110,9 +110,9 @@ export class OceanPool extends Pool {
* Buy Data Token from a pool
* @param {String} account
* @param {String} poolAddress
* @param {String} amount Data Token Amount
* @param {String} oceanAmount Ocean Token Amount payed
* @param {String} maxPrice Maximum Price to pay
* @param {String} amount Data Token amount
* @param {String} oceanAmount Ocean Token amount payed
* @param {String} maxPrice Maximum price to pay
* @return {any}
*/
public async buyDT(
@ -151,9 +151,9 @@ export class OceanPool extends Pool {
* Sell Data Token
* @param {String} account
* @param {String} poolAddress
* @param {String} amount Data Token Amount
* @param {String} oceanAmount Ocean Token Amount expected
* @param {String} maxPrice Minimum Price to sell
* @param {String} amount Data Token amount
* @param {String} oceanAmount Ocean Token amount expected
* @param {String} maxPrice Minimum price to sell
* @return {any}
*/
public async sellDT(
@ -183,7 +183,7 @@ export class OceanPool extends Pool {
* 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}
*/
public async addDTLiquidity(
@ -212,7 +212,7 @@ export class OceanPool extends Pool {
* Remove Data Token amount from pool liquidity
* @param {String} account
* @param {String} poolAddress
* @param {String} amount pool Liquidity Amount
* @param {String} amount Data Token amount
* @return {any}
*/
public async removeDTLiquidity(
@ -236,7 +236,7 @@ export class OceanPool extends Pool {
* Add Ocean Token amount to pool liquidity
* @param {String} account
* @param {String} poolAddress
* @param {String} amount Data Token Amount
* @param {String} amount Ocean Token amount in OCEAN
* @return {any}
*/
public async addOceanLiquidity(
@ -268,7 +268,7 @@ export class OceanPool extends Pool {
* Remove Ocean Token amount from pool liquidity
* @param {String} account
* @param {String} poolAddress
* @param {String} amount pool Liquidity Amount
* @param {String} amount Ocean Token amount in OCEAN
* @return {any}
*/
public removeOceanLiquidity(
@ -292,12 +292,12 @@ export class OceanPool extends Pool {
}
/**
* Get Data Token Price from pool
* Get Data Token price from pool
* @param {String} account
* @param {String} poolAddress
* @return {any}
* @return {String}
*/
public async getDTPrice(account: string, poolAddress: string): Promise<any> {
public async getDTPrice(account: string, poolAddress: string): Promise<string> {
if (this.oceanAddress == null) {
console.error('oceanAddress is not defined')
return null
@ -307,7 +307,7 @@ export class OceanPool extends Pool {
}
/**
* Search all pools that have DT in their composition
* Search all pools that have Data Token in their composition
* @param {String} account
* @param {String} dtAddress
* @return {String[]}

View File

@ -1,71 +1,17 @@
// import * as jsonFactoryABI from './artifacts/SFactory.json'
// import * as jsonPoolABI from './artifacts/SPool.json'
import * as jsonFactoryABI from '@oceanprotocol/contracts/artifacts/SFactory.json'
import * as jsonPoolABI from '@oceanprotocol/contracts/artifacts/SPool.json'
import Web3 from 'web3'
const Decimal = require('decimal.js')
import Decimal from 'decimal.js'
import * as jsonPoolABI from '@oceanprotocol/contracts/artifacts/SPool.json'
import { PoolFactory } from './PoolFactory'
/**
* Provides an interface to Balancer BPool & BFactory
*/
export interface TokensToAdd {
address: string
amount: string
weight: string
}
export class PoolFactory {
public GASLIMIT_DEFAULT: number = 5000000
public web3: any = null
public FactoryABI: any
public factoryAddress: any
constructor(
web3: Web3,
FactoryABI: any = null,
factoryAddress: string = null,
gaslimit?: number
) {
this.web3 = web3
if (FactoryABI) this.FactoryABI = FactoryABI
else this.FactoryABI = jsonFactoryABI.abi
if (factoryAddress) {
this.factoryAddress = factoryAddress
}
if (gaslimit) this.GASLIMIT_DEFAULT = gaslimit
}
/**
* Creates a new pool
*/
async createPool(account: string): Promise<string> {
if (this.web3 == null) {
console.error('Web3 object is null')
return null
}
if (this.factoryAddress == null) {
console.error('bfactoryAddress is null')
return null
}
const factory = new this.web3.eth.Contract(this.FactoryABI, this.factoryAddress, {
from: account
})
const transactiondata = await factory.methods
.newSPool()
.send({ from: account, gas: this.GASLIMIT_DEFAULT })
let pooladdress = null
try {
pooladdress = transactiondata.events.SPoolRegistered.returnValues[0]
} catch (e) {
console.error(e)
}
return pooladdress
}
}
export class Pool extends PoolFactory {
private PoolABI: any
@ -142,12 +88,12 @@ export class Pool extends PoolFactory {
}
/**
* Get Pool shares
* @param {String} account
* @param {String} poolAddress
*/
async sharesBalance(account: string, poolAddress: string): Promise<any> {
* Get Pool shares
* @param {String} account
* @param {String} poolAddress
* @return {String}
*/
async sharesBalance(account: string, poolAddress: string): Promise<string> {
const minABI = [
{
constant: true,
@ -859,20 +805,21 @@ export class Pool extends PoolFactory {
}
public async calcInGivenOut(
tokenBalanceIn,
tokenWeightIn,
tokenBalanceOut,
tokenWeightOut,
tokenAmountOut,
swapFee
tokenBalanceIn: string,
tokenWeightIn: string,
tokenBalanceOut: string,
tokenWeightOut: string,
tokenAmountOut: string,
swapFee: string
): Promise<string> {
const weightRatio = Decimal(tokenWeightOut).div(Decimal(tokenWeightIn))
const diff = Decimal(tokenBalanceOut).minus(tokenAmountOut)
const y = Decimal(tokenBalanceOut).div(diff)
const foo = y.pow(weightRatio).minus(Decimal(1))
const tokenAmountIn = Decimal(tokenBalanceIn)
const weightRatio = new Decimal(tokenWeightOut).div(new Decimal(tokenWeightIn))
const diff = new Decimal(tokenBalanceOut).minus(tokenAmountOut)
const y = new Decimal(tokenBalanceOut).div(diff)
const foo = y.pow(weightRatio).minus(new Decimal(1))
const tokenAmountIn = new Decimal(tokenBalanceIn)
.times(foo)
.div(Decimal(1).minus(Decimal(swapFee)))
return tokenAmountIn
.div(new Decimal(1).minus(new Decimal(swapFee)))
return tokenAmountIn.toString()
}
}

View File

@ -0,0 +1,57 @@
import Web3 from 'web3'
import * as jsonFactoryABI from '@oceanprotocol/contracts/artifacts/SFactory.json'
export class PoolFactory {
public GASLIMIT_DEFAULT: number = 5000000
public web3: any = null
public FactoryABI: any
public factoryAddress: any
constructor(
web3: Web3,
FactoryABI: any = null,
factoryAddress: string = null,
gaslimit?: number
) {
this.web3 = web3
if (FactoryABI) this.FactoryABI = FactoryABI
else this.FactoryABI = jsonFactoryABI.abi
if (factoryAddress) {
this.factoryAddress = factoryAddress
}
if (gaslimit) this.GASLIMIT_DEFAULT = gaslimit
}
/**
* Creates a new pool
*/
async createPool(account: string): Promise<string> {
if (this.web3 === null) {
console.error('Web3 object is null')
return null
}
if (this.factoryAddress === null) {
console.error('bfactoryAddress is null')
return null
}
const factory = new this.web3.eth.Contract(this.FactoryABI, this.factoryAddress, {
from: account
})
const transactiondata = await factory.methods
.newSPool()
.send({ from: account, gas: this.GASLIMIT_DEFAULT })
let pooladdress: string
try {
pooladdress = transactiondata.events.SPoolRegistered.returnValues[0]
} catch (e) {
console.error(e)
}
return pooladdress
}
}