diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index a41728f7..d1363236 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -586,7 +586,63 @@ export class OceanPool extends Pool { } /** - * Sell datatoken + * Buy at least datatoken from a pool for a fixed Ocean amount + * @param {String} account + * @param {String} poolAddress + * @param {String} amount datatoken amount + * @param {String} oceanAmount Ocean Token amount payed + * @param {String} maxPrice Maximum price to pay + * @return {TransactionReceipt} + */ + public async buyDTWithExactOcean( + account: string, + poolAddress: string, + minimumdtAmountWanted: string, + OceanAmount: string, + maxPrice?: string + ): Promise { + if (this.oceanAddress == null) { + this.logger.error('ERROR: undefined ocean token contract address') + return null + } + const dtAddress = await this.getDTAddress(poolAddress) + if ( + parseFloat(minimumdtAmountWanted) > + parseFloat(await this.getDTMaxBuyQuantity(poolAddress)) + ) { + this.logger.error('ERROR: Buy quantity exceeds quantity allowed') + return null + } + const calcInGivenOut = await this.getOceanNeeded(poolAddress, minimumdtAmountWanted) + + if (parseFloat(calcInGivenOut) > parseFloat(OceanAmount)) { + this.logger.error('ERROR: Not enough Ocean Tokens') + return null + } + // TODO - check balances first + const txid = await super.approve( + account, + this.oceanAddress, + poolAddress, + this.web3.utils.toWei(OceanAmount) + ) + if (!txid) { + this.logger.error('ERROR: OCEAN approve failed') + return null + } + return this.swapExactAmountIn( + account, + poolAddress, + this.oceanAddress, + OceanAmount, + dtAddress, + minimumdtAmountWanted, + maxPrice + ) + } + + /** + * Sell a specific amount of datatoken to get some ocean tokens * @param {String} account * @param {String} poolAddress * @param {String} amount datatoken amount to be sold diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index d990048f..e364050f 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -213,13 +213,19 @@ describe('Balancer flow', () => { greatPool = pools[0] }) it('Bob should buy 2 DT ', async () => { - const maxPrice = parseFloat(currentDtPrice) * 2 await Pool.buyDT(bob, greatPool, '2', '4') const bobDtBalance = await datatoken.balance(tokenAddress, bob) const bobOceanBalance = await datatoken.balance(oceanTokenAddress, bob) assert(Number(bobDtBalance) > 0) assert(Number(bobOceanBalance) > 0) }) + it('Bob should spend 10 Oceans to buy some DT ', async () => { + await Pool.buyDTWithExactOcean(bob, greatPool, '1', '5') + const bobDtBalance = await datatoken.balance(tokenAddress, bob) + const bobOceanBalance = await datatoken.balance(oceanTokenAddress, bob) + assert(Number(bobDtBalance) > 0) + assert(Number(bobOceanBalance) > 0) + }) it('Bob should sell 1 DT ', async () => { const maxPrice = parseFloat(currentDtPrice) * 2 await Pool.sellDT(bob, greatPool, '1', '1')