-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add orderLabel, updateOrderResultFromLogs, getDefaultLimitOrder…
…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
Showing
8 changed files
with
503 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.