diff --git a/src/peripheryManager.ts b/src/peripheryManager.ts index 39972ae..17d9b23 100644 --- a/src/peripheryManager.ts +++ b/src/peripheryManager.ts @@ -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', [ @@ -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')) diff --git a/test/PeripheryManager.spec.ts b/test/PeripheryManager.spec.ts index 0f70799..9459400 100644 --- a/test/PeripheryManager.spec.ts +++ b/test/PeripheryManager.spec.ts @@ -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) @@ -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