Skip to content

Commit

Permalink
fix(#36): updates create pool with native to use correct amount
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexangelj committed Mar 14, 2022
1 parent c615f4b commit bffc378
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/peripheryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,12 @@ export abstract class PeripheryManager extends SelfPermit {
)

// if curve should be created
let createData: string | undefined = undefined

if (options.createPool) {
invariant(!options.fromMargin, 'Cannot pay from margin when creating, set fromMargin to false.')
calldatas.push(PeripheryManager.encodeCreate(pool, options.delLiquidity))
createData = PeripheryManager.encodeCreate(pool, options.delLiquidity)
calldatas.push(createData)
} else {
calldatas.push(
PeripheryManager.INTERFACE.encodeFunctionData('allocate', [
Expand All @@ -429,7 +432,18 @@ export abstract class PeripheryManager extends SelfPermit {
const wrapped = options.useNative.wrapped
invariant(pool.risky.equals(wrapped) || pool.stable.equals(wrapped), 'No Weth')

const wrappedAmount = pool.risky.equals(wrapped) ? options.delRisky.raw : options.delStable.raw
let wrappedAmount: BigNumber

if (options.createPool && typeof createData !== 'undefined') {
const decoded = PeripheryManager.INTERFACE.decodeFunctionData('create', createData)
const riskyPerLp: BigNumber = decoded[decoded.length - 2]
const liquidity: BigNumber = decoded[decoded.length - 1]
const amount: BigNumber = riskyPerLp.mul(liquidity).div(Engine.PRECISION.raw) // weth token per liquidity * liquidity / 1e18

wrappedAmount = pool.risky.equals(wrapped) ? amount : options.delStable.raw
} else {
wrappedAmount = pool.risky.equals(wrapped) ? options.delRisky.raw : options.delStable.raw
}

if (wrappedAmount.gte(0)) {
calldatas.push(PeripheryManager.INTERFACE.encodeFunctionData('refundETH'))
Expand Down
61 changes: 61 additions & 0 deletions test/PeripheryManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PeripheryManager } from '../src/peripheryManager'

import { AddressOne } from './shared/constants'
import { usePool, usePoolWithDecimals, useWethPool } from './shared/fixture'
import { Engine } from '../src/entities/engine'

function decode(frag: string, data: any) {
return PeripheryManager.INTERFACE.decodeFunctionData(frag, data)
Expand Down Expand Up @@ -462,6 +463,66 @@ describe('Periphery Manager', function() {
expect(value).toBe('0x00')
})

it('successful when creating pool using native', async function() {
const recipient = from
const fromMargin = false
const createPool = true
const delRisky = parseWei(0.3, wethPool.risky.decimals)
const delStable = parseWei(3, wethPool.stable.decimals)
const delLiquidity = parseWei(1, 18)

const { calldata, value } = PeripheryManager.allocateCallParameters(wethPool, {
recipient,
fromMargin,
delRisky,
delStable,
delLiquidity,
createPool,
useNative,
slippageTolerance
})

const decimals = pool.risky.decimals
const reference = pool.referencePriceOfRisky ?? pool.reportedPriceOfRisky
const riskyPerLp = reference
? parseWei(
Swaps.getRiskyReservesGivenReferencePrice(
pool.strike.float,
pool.sigma.float,
pool.tau.years,
reference.float
),
decimals
)
: undefined
if (!riskyPerLp) throw Error('Risky per lp is undefined')

const createData = [
wethPool.risky.address,
wethPool.stable.address,
wethPool.strike.raw,
wethPool.sigma.raw,
wethPool.maturity.raw,
wethPool.gamma.raw,
riskyPerLp.raw,
delLiquidity.raw
]

const multicall = decode('multicall', calldata)

const createDecoded = decode('create', multicall.data[0])
createData.forEach((item, i) => expect(item.toString()).toStrictEqual(createDecoded[i].toString()))

const refundETHDecoded = decode('refundETH', multicall.data[1])
expect(refundETHDecoded).toBeDefined()
expect(value).toBe(
riskyPerLp
.mul(delLiquidity)
.div(Engine.PRECISION)
.raw.toHexString()
)
})

it('fails if delRisky is 0', async function() {
const recipient = from
const fromMargin = false
Expand Down

0 comments on commit bffc378

Please sign in to comment.