mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Merge pull request #454 from oceanprotocol/feature/buyDTWithExactOcean
add buyDTWithExactOcean function
This commit is contained in:
commit
56d4fc249a
@ -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<TransactionReceipt> {
|
||||
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
|
||||
@ -1166,4 +1222,113 @@ export class OceanPool extends Pool {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private async computeSlippage(
|
||||
poolAddress: string,
|
||||
tokenInBalance: string,
|
||||
tokenInWeight: string,
|
||||
tokenOutBalance: string,
|
||||
tokenOutWeight: string,
|
||||
newTokenInBalance: string,
|
||||
newTokenOutBalance: string,
|
||||
swapfee: string
|
||||
) {
|
||||
const initialPrice = await super.calcSpotPrice(
|
||||
poolAddress,
|
||||
tokenInBalance,
|
||||
tokenInWeight,
|
||||
tokenOutBalance,
|
||||
tokenOutWeight,
|
||||
swapfee
|
||||
)
|
||||
|
||||
const newPrice = await super.calcSpotPrice(
|
||||
poolAddress,
|
||||
newTokenInBalance,
|
||||
tokenInWeight,
|
||||
newTokenOutBalance,
|
||||
tokenOutWeight,
|
||||
swapfee
|
||||
)
|
||||
const slippage = (parseFloat(newPrice) * 100) / parseFloat(initialPrice) - 100
|
||||
return String(slippage)
|
||||
}
|
||||
|
||||
/* Get slippage for buying some datatokens while spending exactly oceanAmount ocean tokens */
|
||||
public async computeBuySlippage(
|
||||
poolAddress: string,
|
||||
oceanAmount: string
|
||||
): Promise<string> {
|
||||
const dtAddress = await this.getDTAddress(poolAddress)
|
||||
const dtWeight = await super.getDenormalizedWeight(poolAddress, dtAddress)
|
||||
const oceanWeight = await super.getDenormalizedWeight(poolAddress, this.oceanAddress)
|
||||
const dtReserve = await super.getReserve(poolAddress, dtAddress)
|
||||
const oceanReserve = await super.getReserve(poolAddress, dtAddress)
|
||||
const swapFee = await super.getSwapFee(poolAddress)
|
||||
const dtReceived = await super.calcOutGivenIn(
|
||||
poolAddress,
|
||||
oceanReserve,
|
||||
oceanWeight,
|
||||
dtReserve,
|
||||
dtWeight,
|
||||
oceanAmount,
|
||||
swapFee
|
||||
)
|
||||
const newDtReserve = new BigNumber(this.web3.utils.toWei(dtReserve)).minus(
|
||||
this.web3.utils.toWei(dtReceived)
|
||||
)
|
||||
const newOceanReserve = new BigNumber(this.web3.utils.toWei(oceanReserve)).plus(
|
||||
this.web3.utils.toWei(oceanAmount)
|
||||
)
|
||||
const slippage = await this.computeSlippage(
|
||||
poolAddress,
|
||||
oceanReserve,
|
||||
oceanWeight,
|
||||
dtReserve,
|
||||
dtWeight,
|
||||
this.web3.utils.fromWei(newOceanReserve.toString()),
|
||||
this.web3.utils.fromWei(newDtReserve.toString()),
|
||||
swapFee
|
||||
)
|
||||
return slippage
|
||||
}
|
||||
|
||||
/* Get slippage for selling an exact amount of datatokens to get some ocean tokens */
|
||||
public async computeSellSlippage(
|
||||
poolAddress: string,
|
||||
dtAmount: string
|
||||
): Promise<string> {
|
||||
const dtAddress = await this.getDTAddress(poolAddress)
|
||||
const dtWeight = await super.getDenormalizedWeight(poolAddress, dtAddress)
|
||||
const oceanWeight = await super.getDenormalizedWeight(poolAddress, this.oceanAddress)
|
||||
const dtReserve = await super.getReserve(poolAddress, dtAddress)
|
||||
const oceanReserve = await super.getReserve(poolAddress, dtAddress)
|
||||
const swapFee = await super.getSwapFee(poolAddress)
|
||||
const oceanReceived = await super.calcOutGivenIn(
|
||||
poolAddress,
|
||||
dtReserve,
|
||||
dtWeight,
|
||||
oceanReserve,
|
||||
oceanWeight,
|
||||
dtAmount,
|
||||
swapFee
|
||||
)
|
||||
const newDtReserve = new BigNumber(this.web3.utils.toWei(dtReserve)).plus(
|
||||
this.web3.utils.toWei(dtAmount)
|
||||
)
|
||||
const newOceanReserve = new BigNumber(this.web3.utils.toWei(oceanReserve)).minus(
|
||||
this.web3.utils.toWei(oceanReceived)
|
||||
)
|
||||
const slippage = await this.computeSlippage(
|
||||
poolAddress,
|
||||
dtReserve,
|
||||
dtWeight,
|
||||
oceanReserve,
|
||||
oceanWeight,
|
||||
this.web3.utils.fromWei(newDtReserve.toString()),
|
||||
this.web3.utils.fromWei(newOceanReserve.toString()),
|
||||
swapFee
|
||||
)
|
||||
return slippage
|
||||
}
|
||||
}
|
||||
|
@ -975,6 +975,33 @@ export class Pool extends PoolFactory {
|
||||
return price
|
||||
}
|
||||
|
||||
public async calcSpotPrice(
|
||||
poolAddress: string,
|
||||
tokenBalanceIn: string,
|
||||
tokenWeightIn: string,
|
||||
tokenBalanceOut: string,
|
||||
tokenWeightOut: string,
|
||||
swapFee: string
|
||||
): Promise<string> {
|
||||
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
|
||||
let amount = '0'
|
||||
try {
|
||||
const result = await pool.methods
|
||||
.calcSpotPrice(
|
||||
this.web3.utils.toWei(tokenBalanceIn),
|
||||
this.web3.utils.toWei(tokenWeightIn),
|
||||
this.web3.utils.toWei(tokenBalanceOut),
|
||||
this.web3.utils.toWei(tokenWeightOut),
|
||||
this.web3.utils.toWei(swapFee)
|
||||
)
|
||||
.call()
|
||||
amount = this.web3.utils.fromWei(result)
|
||||
} catch (e) {
|
||||
this.logger.error('ERROR: Failed to call calcSpotPrice')
|
||||
}
|
||||
return amount
|
||||
}
|
||||
|
||||
public async calcInGivenOut(
|
||||
poolAddress: string,
|
||||
tokenBalanceIn: string,
|
||||
|
@ -115,13 +115,13 @@ describe('Balancer flow', () => {
|
||||
assert(Pool !== null)
|
||||
})
|
||||
|
||||
it('Alice mints 1000 tokens', async () => {
|
||||
it('Alice mints 2000 tokens', async () => {
|
||||
await datatoken.mint(tokenAddress, alice, tokenAmount)
|
||||
})
|
||||
it('Alice mints 1000 Ocean tokens', async () => {
|
||||
it('Alice mints 2000 Ocean tokens', async () => {
|
||||
await oceandatatoken.mint(oceanTokenAddress, alice, tokenAmount)
|
||||
})
|
||||
it('Alice transfers 200 ocean token to Bob', async () => {
|
||||
it('Alice transfers 500 ocean token to Bob', async () => {
|
||||
await datatoken.transfer(oceanTokenAddress, bob, transferAmount, alice)
|
||||
})
|
||||
it('Alice creates a new OceanPool pool', async () => {
|
||||
@ -212,22 +212,37 @@ describe('Balancer flow', () => {
|
||||
assert(pools.length > 0)
|
||||
greatPool = pools[0]
|
||||
})
|
||||
it('Bob should buy 2 DT ', async () => {
|
||||
const maxPrice = parseFloat(currentDtPrice) * 2
|
||||
await Pool.buyDT(bob, greatPool, '2', '4')
|
||||
it('Bob should buy 1 DT ', async () => {
|
||||
await Pool.buyDT(bob, greatPool, '1', '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 sell 1 DT ', async () => {
|
||||
const maxPrice = parseFloat(currentDtPrice) * 2
|
||||
await Pool.sellDT(bob, greatPool, '1', '1')
|
||||
it('Bob should spend 2 Oceans to buy at least 0.1 DT ', async () => {
|
||||
await Pool.buyDTWithExactOcean(bob, greatPool, '0.1', '2')
|
||||
const bobDtBalance = await datatoken.balance(tokenAddress, bob)
|
||||
const bobOceanBalance = await datatoken.balance(oceanTokenAddress, bob)
|
||||
assert(Number(bobDtBalance) === 1)
|
||||
assert(Number(bobDtBalance) > 0)
|
||||
assert(Number(bobOceanBalance) > 0)
|
||||
})
|
||||
it('Bob should get slippage for buying some DT with 5 Ocean Tokens ', async () => {
|
||||
const slippage = await Pool.computeBuySlippage(greatPool, '5')
|
||||
assert(Number(slippage) > 0)
|
||||
})
|
||||
it('Bob should get slippage for selling 1 DT', async () => {
|
||||
const slippage = await Pool.computeSellSlippage(greatPool, '1')
|
||||
assert(Number(slippage) > 0)
|
||||
})
|
||||
it('Bob should sell 1 DT ', async () => {
|
||||
const bobDtBalance = await datatoken.balance(tokenAddress, bob)
|
||||
const bobOceanBalance = await datatoken.balance(oceanTokenAddress, bob)
|
||||
await Pool.sellDT(bob, greatPool, '1', '0.1')
|
||||
const newbobDtBalance = await datatoken.balance(tokenAddress, bob)
|
||||
const newbobOceanBalance = await datatoken.balance(oceanTokenAddress, bob)
|
||||
assert(Number(newbobDtBalance) < Number(bobDtBalance))
|
||||
assert(Number(newbobOceanBalance) > Number(bobOceanBalance))
|
||||
})
|
||||
it('Bob should get maximum DT liquidity that he can add to pool ', async () => {
|
||||
const maxDT = await Pool.getDTMaxAddLiquidity(greatPool)
|
||||
if (consoleDebug) console.error('maxDT:' + maxDT)
|
||||
@ -249,7 +264,7 @@ describe('Balancer flow', () => {
|
||||
await Pool.addDTLiquidity(
|
||||
bob,
|
||||
greatPool,
|
||||
String(Math.min(parseFloat(maxDT), parseFloat(bobDtBalance)))
|
||||
String(Math.min(parseFloat(maxDT), parseFloat(bobDtBalance) / 2))
|
||||
)
|
||||
|
||||
const newbobDtBalance = await datatoken.balance(tokenAddress, bob)
|
||||
@ -307,7 +322,7 @@ describe('Balancer flow', () => {
|
||||
await Pool.removeDTLiquidity(
|
||||
bob,
|
||||
greatPool,
|
||||
String(Math.min(parseFloat(maxDT), parseFloat('0.75'))),
|
||||
String(Math.min(parseFloat(maxDT), parseFloat('0.1'))),
|
||||
poolShares
|
||||
)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user