Skip to content

Commit

Permalink
feat: Add orderLabel, updateOrderResultFromLogs, getDefaultLimitOrder…
Browse files Browse the repository at this point in the history
…Gasreq. (#55)

* feat: Add orderLabel, updateOrderResultFromLogs, getDefaultLimitOrderGasreq.

* chore: format

* feat: Refactor update order test and fix variable declaration.

* feat: Add set expiration result from logs params to limit order exports.

* feat: Add remove order functionality with deprovision flag.

* feat: Add logs result on update and remove unnecessary code.

---------

Co-authored-by: maxencerb <[email protected]>
  • Loading branch information
maxencerb and maxencerb authored May 23, 2024
1 parent 9765207 commit fe28644
Show file tree
Hide file tree
Showing 8 changed files with 503 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-maps-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": patch
---

Add result from logs on update, and remove
113 changes: 113 additions & 0 deletions src/actions/order/remove.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { parseUnits } from 'viem'
import { describe, expect, inject, it } from 'vitest'
import { BS, Order } from '~mgv/lib/enums.js'
import {
limitOrderResultFromLogs,
removeOrderResultFromLogs,
} from '~mgv/lib/limit-order.js'
import { getClient } from '~test/src/client.js'
import { getBook } from '../book.js'
import { simulateLimitOrder } from './new.js'
import { simulateRemoveOrder } from './remove.js'

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

describe('remove order', () => {
it('removes the order: no deprovisionning', async () => {
// create an order
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.offer).toBeDefined()
expect(result.offer!.id).toBe(1n)

const { request: removeRequest } = await simulateRemoveOrder(
client,
params,
wethUSDC,
{
bs: BS.buy,
offerId: 1n,
deprovision: false,
},
)
const removeTx = await client.writeContract(removeRequest)
const removeReceipt = await client.waitForTransactionReceipt({
hash: removeTx,
})
const removeResult = removeOrderResultFromLogs(params, wethUSDC, {
logs: removeReceipt.logs,
offerId: 1n,
bs: BS.buy,
})
expect(removeResult.success).toBeTruthy()
expect(removeResult.deprovision).toBeFalsy()
})

it('removes the order: deprovisionning', async () => {
// create an order
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.offer).toBeDefined()
expect(result.offer!.id).toBe(1n)

const { request: removeRequest } = await simulateRemoveOrder(
client,
params,
wethUSDC,
{
bs: BS.buy,
offerId: 1n,
deprovision: true,
},
)
const removeTx = await client.writeContract(removeRequest)
const removeReceipt = await client.waitForTransactionReceipt({
hash: removeTx,
})
const removeResult = removeOrderResultFromLogs(params, wethUSDC, {
logs: removeReceipt.logs,
offerId: 1n,
bs: BS.buy,
})
expect(removeResult.success).toBeTruthy()
expect(removeResult.deprovision).toBeTruthy()
})
})
139 changes: 139 additions & 0 deletions src/actions/order/update.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { parseUnits } from 'viem'
import { describe, expect, inject, it } from 'vitest'
import { BS, Order } from '~mgv/lib/enums.js'
import {
limitOrderResultFromLogs,
setExpirationResultFromLogs,
updateOrderResultFromLogs,
} from '~mgv/lib/limit-order.js'
import { tickFromVolumes } from '~mgv/lib/tick.js'
import { getClient } from '~test/src/client.js'
import { getBook } from '../book.js'
import { simulateLimitOrder } from './new.js'
import { simulateSetExpiration, simulateUpdateOrder } from './update.js'

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

describe('update order', () => {
it('updates an order', async () => {
// create an order
const book = await getBook(client, params, wethUSDC)

let baseAmount = parseUnits('1', wethUSDC.base.decimals)
let 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.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),
)

baseAmount *= 2n
quoteAmount *= 3n

const { request: updateRequest } = await simulateUpdateOrder(
client,
params,
wethUSDC,
{
baseAmount,
quoteAmount,
restingOrderGasreq: 250_000n,
bs: BS.buy,
book,
offerId: 1n,
},
)
const updateTx = await client.writeContract(updateRequest)
const updateReceipt = await client.waitForTransactionReceipt({
hash: updateTx,
})
const updateResult = updateOrderResultFromLogs(params, wethUSDC, {
logs: updateReceipt.logs,
bs: BS.buy,
offerId: 1n,
})
expect(updateResult.gives).toApproximateEqual(quoteAmount)
expect(updateResult.wants).toApproximateEqual(baseAmount)
expect(updateResult.tick).toBe(
tickFromVolumes(baseAmount, quoteAmount, wethUSDC.tickSpacing),
)
expect(updateResult.gasreq).toBe(250_000n)
})

it('updates an order: expiry', async () => {
// create an order
const book = await getBook(client, params, wethUSDC)

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

let expiry = BigInt(Math.floor(Date.now() / 1000) + 60 * 60 * 24)

const { request } = await simulateLimitOrder(client, params, wethUSDC, {
baseAmount,
quoteAmount,
restingOrderGasreq: 250_000n,
bs: BS.buy,
book,
orderType: Order.PO,
expiryDate: expiry,
})
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.offer).toBeDefined()
expect(result.offer!.id).toBe(1n)
expect(result.offer!.expiry).toBe(expiry)

baseAmount *= 2n
quoteAmount *= 3n

expiry += 60n * 60n * 24n

const { request: updateRequest } = await simulateSetExpiration(
client,
params,
wethUSDC,
{
bs: BS.buy,
offerId: 1n,
expiryDate: expiry,
},
)
const updateTx = await client.writeContract(updateRequest)
const updateReceipt = await client.waitForTransactionReceipt({
hash: updateTx,
})
const updateResult = setExpirationResultFromLogs(params, wethUSDC, {
logs: updateReceipt.logs,
bs: BS.buy,
offerId: 1n,
})

expect(updateResult).toBe(expiry)
})
})
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
export type {
MarketOrderSimulationParams,
MarketOrderResultFromLogsParams,
RawLimitOrderResultFromLogsParams,
LimitOrderResultFromLogsParams,
LimitOrderResult,
GetDefaultLimitOrderGasreqParams,
RawUpdateOrderResultFromLogsParams,
UpdateOrderResultFromLogsParams,
AmountsToHumanPriceParams,
AmountsParams,
AmountsOutput,
Expand All @@ -17,6 +21,11 @@ export type {
RawKandelParams,
KandelParams,
ValidateParamsResult,
RawSetExpirationResultFromLogsParams,
SetExpirationResultFromLogsParams,
RawRemoveOrderResultFromLogsParams,
RemoveOrderResult,
RemoveOrderResultFromLogsParams,
} from './lib/index.js'

export {
Expand All @@ -41,11 +50,20 @@ export {
marketOrderSimulation,
marketOrderResultFromLogs,
limitOrderResultFromLogs,
orderLabel,
rawUpdateOrderResultFromLogs,
updateOrderResultFromLogs,
ParseUpdateOrderLogsError,
CreateDistributionError,
createGeometricDistribution,
seederEventsABI,
getKandelsFromLogs,
validateKandelParams,
getDefaultLimitOrderGasreq,
rawSetExpirationResultFromLogs,
setExpirationResultFromLogs,
rawRemoveOrderResultFromLogs,
removeOrderResultFromLogs,
} from './lib/index.js'

// --- Types ---
Expand Down
14 changes: 14 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,27 @@ export type {
LimitOrderResultFromLogsParams,
LimitOrderResult,
GetDefaultLimitOrderGasreqParams,
RawUpdateOrderResultFromLogsParams,
UpdateOrderResultFromLogsParams,
RawSetExpirationResultFromLogsParams,
SetExpirationResultFromLogsParams,
RawRemoveOrderResultFromLogsParams,
RemoveOrderResult,
RemoveOrderResultFromLogsParams,
} from './limit-order.js'

export {
rawLimitOrderResultFromLogs,
limitOrderResultFromLogs,
getDefaultLimitOrderGasreq,
orderLabel,
rawUpdateOrderResultFromLogs,
updateOrderResultFromLogs,
ParseUpdateOrderLogsError,
rawSetExpirationResultFromLogs,
setExpirationResultFromLogs,
rawRemoveOrderResultFromLogs,
removeOrderResultFromLogs,
} from './limit-order.js'

// local
Expand Down
Loading

0 comments on commit fe28644

Please sign in to comment.