Skip to content

Commit

Permalink
fix: Update MangroveOrderStart event ABI and handle MangroveOrder
Browse files Browse the repository at this point in the history
… related data in `limit-order` module. (#53)

* fix: Update `MangroveOrderStart` event ABI and handle `MangroveOrder` related data in `limit-order` module.

* chore: format

* feat: Reorder imports and update dependencies in new order test file.

* fix: Fixed limit order wrong address.

---------

Co-authored-by: maxencerb <[email protected]>
  • Loading branch information
maxencerb and maxencerb authored May 23, 2024
1 parent 3bf2805 commit cbbc841
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-rules-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": patch
---

Fixed limit order wrong address
12 changes: 2 additions & 10 deletions src/actions/book.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,12 @@ import { getClient } from '~test/src/client.js'
import { getBook } from './book.js'

const { wethDAI } = inject('markets')
const { mangrove, reader, order } = inject('mangrove')
const params = inject('mangrove')

describe('Getting the book', () => {
it('should get the configs', async () => {
const client = getClient()
const book = await getBook(
client,
{
mgv: mangrove,
mgvReader: reader,
mgvOrder: order,
},
wethDAI,
)
const book = await getBook(client, params, wethDAI)
expect(book.marketConfig).toEqual({
monitor: '0x0000000000000000000000000000000000000000',
useOracle: false,
Expand Down
186 changes: 186 additions & 0 deletions src/actions/order/new.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import {
erc20Abi,
isAddress,
isAddressEqual,
maxUint128,
maxUint256,
parseUnits,
zeroAddress,
} from 'viem'
import { afterEach, describe, expect, inject, it } from 'vitest'
import { limitOrderResultFromLogs } from '~mgv/lib/limit-order.js'
import { tickFromVolumes } from '~mgv/lib/tick.js'
import { getClient } from '~test/src/client.js'
import { BS, Order } from '../../lib/enums.js'
import { getBook } from '../book.js'
import { getLimitOrderSteps, getUserRouter, simulateLimitOrder } from './new.js'

const params = inject('mangrove')
const { wethUSDC } = inject('markets')
const client = getClient()

describe('new order', () => {
afterEach(async () => {
await client.reset()
})

it('get user router', async () => {
const router = await getUserRouter(client, params, {
user: client.account.address,
})
expect(isAddress(router)).toBeTruthy()
expect(!isAddressEqual(router, zeroAddress)).toBeTruthy()
})

it('getting the steps: not done', async () => {
const router = await getUserRouter(client, params, {
user: client.account.address,
})
const steps = await getLimitOrderSteps(client, wethUSDC, {
user: client.account.address,
userRouter: router,
bs: BS.buy,
})
expect(steps.length).toBe(1)
expect(steps[0]).toStrictEqual({
type: 'erc20Approval',
params: {
amount: maxUint256,
from: client.account.address,
spender: router,
token: wethUSDC.quote,
},
done: false,
})
})

it('getting the steps: done', async () => {
const router = await getUserRouter(client, params, {
user: client.account.address,
})
const tx = await client.writeContract({
address: wethUSDC.quote.address,
abi: erc20Abi,
functionName: 'approve',
args: [router, maxUint256],
})
await client.waitForTransactionReceipt({ hash: tx })
const steps = await getLimitOrderSteps(client, wethUSDC, {
user: client.account.address,
userRouter: router,
bs: BS.buy,
})
expect(steps.length).toBe(1)
expect(steps[0]).toStrictEqual({
type: 'erc20Approval',
params: {
amount: maxUint256,
from: client.account.address,
spender: router,
token: wethUSDC.quote,
},
done: true,
})
})

it('geeting the steps: done (minimum)', async () => {
const router = await getUserRouter(client, params, {
user: client.account.address,
})
const tx = await client.writeContract({
address: wethUSDC.quote.address,
abi: erc20Abi,
functionName: 'approve',
args: [router, maxUint128],
})
await client.waitForTransactionReceipt({ hash: tx })
const steps = await getLimitOrderSteps(client, wethUSDC, {
user: client.account.address,
userRouter: router,
bs: BS.buy,
})
expect(steps.length).toBe(1)
expect(steps[0]).toStrictEqual({
type: 'erc20Approval',
params: {
amount: maxUint256,
from: client.account.address,
spender: router,
token: wethUSDC.quote,
},
done: true,
})
})

it('creating an order', async () => {
const book = await getBook(client, params, wethUSDC)

const baseAmount = parseUnits('1', wethUSDC.base.decimals)
const quoteAmount = parseUnits('3000', wethUSDC.quote.decimals)

const { request } = await simulateLimitOrder(client, params, wethUSDC, {
baseAmount,
quoteAmount,
restingOrderGasreq: 250_000n,
bs: BS.buy,
book,
orderType: Order.PO,
})
const tx = await client.writeContract(request)
const receipt = await client.waitForTransactionReceipt({ hash: tx })
const result = limitOrderResultFromLogs(params, wethUSDC, {
logs: receipt.logs,
user: client.account.address,
bs: BS.buy,
})
expect(result.takerGave).toBe(0n)
expect(result.takerGot).toBe(0n)
expect(result.feePaid).toBe(0n)
expect(result.bounty).toBe(0n)
expect(result.takerGivesLogic).toBeUndefined()
expect(result.takerWantsLogic).toBeUndefined()
expect(result.offer).toBeDefined()
expect(result.offer!.id).toBe(1n)
expect(result.offer!.gives).toApproximateEqual(quoteAmount)
expect(result.offer!.wants).toApproximateEqual(baseAmount)
expect(result.offer!.tick).toBe(
-tickFromVolumes(quoteAmount, baseAmount, wethUSDC.tickSpacing),
)
expect(result.offer!.gasprice).toBe(book.marketConfig.gasprice)
expect(result.offer!.gasreq).toBe(250_000n)
expect(result.offer!.expiry).toBeUndefined()
})

it('creating an order with expiry and logics', async () => {
const book = await getBook(client, params, wethUSDC)

const baseAmount = parseUnits('1', wethUSDC.base.decimals)
const quoteAmount = parseUnits('3000', wethUSDC.quote.decimals)

const expiryDate = BigInt(Math.floor(Date.now() / 1000) + 1000)

const { request } = await simulateLimitOrder(client, params, wethUSDC, {
baseAmount,
quoteAmount,
restingOrderGasreq: 250_000n,
bs: BS.buy,
book,
orderType: Order.PO,
// random addresses
takerGivesLogic: params.mgv,
takerWantsLogic: params.mgv,
expiryDate,
})
const tx = await client.writeContract(request)
const receipt = await client.waitForTransactionReceipt({ hash: tx })
const result = limitOrderResultFromLogs(params, wethUSDC, {
logs: receipt.logs,
user: client.account.address,
bs: BS.buy,
})
expect(result.takerGivesLogic).toAddressEqual(params.mgv)
expect(result.takerWantsLogic).toAddressEqual(params.mgv)
expect(result.offer).toBeDefined()
expect(result.offer!.expiry).toBe(expiryDate)
})
})
2 changes: 1 addition & 1 deletion src/actions/order/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,6 @@ export async function simulateLimitOrder(
)({
...(args as unknown as SimulationParams),
...limitOrderParams(marketParams, args),
address: actionParams.mgv,
address: actionParams.mgvOrder,
})
}
4 changes: 2 additions & 2 deletions src/actions/order/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function simulateRawRemoveOrder(
'simulateContract',
)({
...(args as unknown as SimulationParams),
address: actionParams.mgv,
address: actionParams.mgvOrder,
...rawRemoveOrderParams(args),
})
return { provision: result, request }
Expand All @@ -69,7 +69,7 @@ export async function simulateRemoveOrder(
'simulateContract',
)({
...(args as unknown as SimulationParams),
address: actionParams.mgv,
address: actionParams.mgvOrder,
...removeOrderParams(market, args),
})
return { provision: result, request }
Expand Down
10 changes: 5 additions & 5 deletions src/actions/order/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function simulateUpdateOrderByTick(
'simulateContract',
)({
...(args as unknown as SimulationParams),
address: actionParams.mgv,
address: actionParams.mgvOrder,
...updateOrderByTickParams(args),
})
return { request }
Expand All @@ -82,7 +82,7 @@ export async function simulateUpdateOrderByVolume(
'simulateContract',
)({
...(args as unknown as SimulationParams),
address: actionParams.mgv,
address: actionParams.mgvOrder,
...updateOrderByVolumeParams(args),
})
return { request }
Expand All @@ -104,7 +104,7 @@ export async function simulateUpdateOrder(
'simulateContract',
)({
...(args as unknown as SimulationParams),
address: actionParams.mgv,
address: actionParams.mgvOrder,
...updateOrderParams(market, args),
})
return { request }
Expand Down Expand Up @@ -137,7 +137,7 @@ export async function simulateSetRawExpiration(
'simulateContract',
)({
...(args as unknown as SetExpirationSimulationParams),
address: actionParams.mgv,
address: actionParams.mgvOrder,
...rawSetExpirationParams(args),
})
return { request }
Expand All @@ -159,7 +159,7 @@ export async function simulateSetExpiration(
'simulateContract',
)({
...(args as unknown as SetExpirationSimulationParams),
address: actionParams.mgv,
address: actionParams.mgvOrder,
...setExpirationParams(market, args),
})
return { request }
Expand Down
8 changes: 4 additions & 4 deletions src/lib/kandel/distribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createGeometricDistribution } from './distribution.js'
import { getKandelPositionRawParams } from './params.js'

const { kandelLib, smartKandelSeeder } = inject('kandel')
const { routerProxyFactory, mangrove } = inject('mangrove')
const { routerProxyFactory, mgv } = inject('mangrove')
const { wethUSDC } = inject('markets')

const client = getClient()
Expand All @@ -19,7 +19,7 @@ const kandelLibAbi = parseAbi([

describe('distribution', () => {
it('checks kandel deployment', async () => {
const [factory, mgv] = await client.multicall({
const [factory, _mgv] = await client.multicall({
contracts: [
{
address: smartKandelSeeder,
Expand All @@ -35,8 +35,8 @@ describe('distribution', () => {
})
expect(factory.status).toEqual('success')
expect(factory.result).toAddressEqual(routerProxyFactory)
expect(mgv.status).toEqual('success')
expect(mgv.result).toAddressEqual(mangrove)
expect(_mgv.status).toEqual('success')
expect(_mgv.result).toAddressEqual(mgv)
})

it('checks kandel distribution', async () => {
Expand Down
Loading

0 comments on commit cbbc841

Please sign in to comment.