mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
add getOceanNeeded
This commit is contained in:
parent
0436a1f6a0
commit
5a16ca9493
5
package-lock.json
generated
5
package-lock.json
generated
@ -2786,6 +2786,11 @@
|
||||
"integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=",
|
||||
"dev": true
|
||||
},
|
||||
"decimal.js": {
|
||||
"version": "10.2.0",
|
||||
"resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.2.0.tgz",
|
||||
"integrity": "sha512-vDPw+rDgn3bZe1+F/pyEwb1oMG2XTlRVgAa6B4KccTEpYgF8w6eQllVbQcfIJnZyvzFtFpxnpGtx8dd7DJp/Rw=="
|
||||
},
|
||||
"decode-uri-component": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/decode-uri-component/-/decode-uri-component-0.2.0.tgz",
|
||||
|
@ -40,6 +40,7 @@
|
||||
"@ethereum-navigator/navigator": "^0.5.0",
|
||||
"@oceanprotocol/contracts": "^0.3.1",
|
||||
"bignumber.js": "^9.0.0",
|
||||
"decimal.js": "^10.2.0",
|
||||
"fs": "0.0.1-security",
|
||||
"node-fetch": "^2.6.0",
|
||||
"save-file": "^2.3.1",
|
||||
|
@ -330,4 +330,41 @@ export class OceanPool extends Pool {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
public async getOceanNeeded(
|
||||
account: string,
|
||||
poolAddress: string,
|
||||
dtRequired: string
|
||||
): Promise<string> {
|
||||
await this.getDTAddress(account, poolAddress)
|
||||
const tokenBalanceIn = await this.getReserve(
|
||||
account,
|
||||
poolAddress,
|
||||
this.oceanAddress
|
||||
)
|
||||
const tokenWeightIn = await this.getDenormalizedWeight(
|
||||
account,
|
||||
poolAddress,
|
||||
this.oceanAddress
|
||||
)
|
||||
const tokenBalanceOut = await this.getReserve(
|
||||
account,
|
||||
poolAddress,
|
||||
this.dtAddress
|
||||
)
|
||||
const tokenWeightOut = await this.getDenormalizedWeight(
|
||||
account,
|
||||
poolAddress,
|
||||
this.dtAddress
|
||||
)
|
||||
const swapFee = await this.getSwapFee(account, poolAddress)
|
||||
return super.calcInGivenOut(
|
||||
tokenBalanceIn,
|
||||
tokenWeightIn,
|
||||
tokenBalanceOut,
|
||||
tokenWeightOut,
|
||||
dtRequired,
|
||||
swapFee
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
// import * as jsonFactoryABI from './artifacts/SFactory.json'
|
||||
// import * as jsonPoolABI from './artifacts/SPool.json'
|
||||
|
||||
import * as jsonFactoryABI from '@oceanprotocol/contracts/artifacts/development/SFactory.json'
|
||||
import * as jsonPoolABI from '@oceanprotocol/contracts/artifacts/development/SPool.json'
|
||||
|
||||
const Decimal = require('decimal.js')
|
||||
|
||||
/**
|
||||
* Provides a interface to Balancer BPool & BFactory
|
||||
|
||||
@ -857,4 +858,22 @@ export class Pool extends PoolFactory {
|
||||
}
|
||||
return price
|
||||
}
|
||||
|
||||
public async calcInGivenOut(
|
||||
tokenBalanceIn,
|
||||
tokenWeightIn,
|
||||
tokenBalanceOut,
|
||||
tokenWeightOut,
|
||||
tokenAmountOut,
|
||||
swapFee
|
||||
) {
|
||||
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)
|
||||
.times(foo)
|
||||
.div(Decimal(1).minus(Decimal(swapFee)))
|
||||
return tokenAmountIn
|
||||
}
|
||||
}
|
||||
|
@ -133,13 +133,18 @@ describe('Balancer flow', () => {
|
||||
const currentDtReserve = await Pool.getDTReserve(alice, alicePoolAddress)
|
||||
assert(currentDtReserve > 0)
|
||||
})
|
||||
it('Get dtToken pool reserve ', async () => {
|
||||
it('Get Ocean pool reserve ', async () => {
|
||||
const currentOceanReserve = await Pool.getOceanReserve(
|
||||
alice,
|
||||
alicePoolAddress
|
||||
)
|
||||
assert(currentOceanReserve > 0)
|
||||
})
|
||||
it('Get amount of Ocean needed to buy 1 dtToken', async () => {
|
||||
const requiredOcean = await Pool.getOceanNeeded(alice, alicePoolAddress, '1')
|
||||
assert(requiredOcean > 0)
|
||||
})
|
||||
|
||||
it('Bob should search for pools with this DT', async () => {
|
||||
const pools = await Pool.searchPoolforDT(bob, tokenAddress)
|
||||
assert(pools.length > 0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user