diff --git a/package-lock.json b/package-lock.json index bac55478..10f580e4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2837,6 +2837,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", diff --git a/package.json b/package.json index c3f53a02..9175fc15 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 4868a5ca..4cc990bb 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -330,4 +330,41 @@ export class OceanPool extends Pool { } return result } + + public async getOceanNeeded( + account: string, + poolAddress: string, + dtRequired: string + ): Promise { + 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 + ) + } } diff --git a/src/balancer/balancerlib.ts b/src/balancer/balancerlib.ts index 7ca8111a..7e1666aa 100644 --- a/src/balancer/balancerlib.ts +++ b/src/balancer/balancerlib.ts @@ -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 + } } diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index a6a62e0c..0c328a34 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -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)