Skip to content

Commit

Permalink
feat: added serializable functions helper (#132)
Browse files Browse the repository at this point in the history
* feat: added serializable functions helper

* chore: format

---------

Co-authored-by: maxencerb <[email protected]>
  • Loading branch information
maxencerb and maxencerb authored Sep 6, 2024
1 parent 5d71c94 commit 692f50f
Show file tree
Hide file tree
Showing 26 changed files with 125 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-mayflies-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": patch
---

Added serialize helper functions for markets and offer list keys
4 changes: 2 additions & 2 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/1.7.2/schema.json",
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"files": {
"ignore": [
"_cjs",
Expand Down Expand Up @@ -61,7 +61,7 @@
"javascript": {
"formatter": {
"quoteStyle": "single",
"trailingComma": "all",
"trailingCommas": "all",
"semicolons": "asNeeded"
}
},
Expand Down
Binary file modified bun.lockb
Binary file not shown.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@
"clean": "rimraf src/_esm src/_cjs src/_types",
"format": "biome format . --write",
"lint": "biome check .",
"lint:fix": "bun run lint --apply",
"lint:fix": "bun run lint --write",
"prepublishOnly": "bun scripts/prepublishOnly.ts",
"test": "vitest -c ./test/vitest.config.ts dev",
"test:ci": "CI=true vitest -c ./test/vitest.config.ts --retry=3"
},
"devDependencies": {
"@biomejs/biome": "1.7.2",
"@types/bun": "latest",
"@biomejs/biome": "1.8.3",
"@types/bun": "^1.1.8",
"@viem/anvil": "^0.0.10",
"@vitest/coverage-v8": "^1.5.3",
"globby": "^14.0.0",
"rimraf": "^5.0.5",
"@vitest/coverage-v8": "^2.0.5",
"globby": "^14.0.2",
"rimraf": "^6.0.1",
"simple-git-hooks": "^2.11.1",
"viem": "^2.12.5",
"vitest": "^1.5.3"
"viem": "^2.21.2",
"vitest": "^2.0.5"
},
"peerDependencies": {
"typescript": "^5.0.0"
"typescript": "^5.5.4"
},
"simple-git-hooks": {
"pre-commit": "bun run format && bun run lint:fix"
},
"dependencies": {
"@changesets/cli": "^2.27.1"
"@changesets/cli": "^2.27.8"
}
}
3 changes: 2 additions & 1 deletion src/actions/book.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { describe, expect, inject, it } from 'vitest'
import { getClient } from '~test/src/client.js'
import { getMarkets } from '~test/src/markets.js'
import { getBook } from './book.js'

const { wethDAI } = inject('markets')
const { wethDAI } = getMarkets()
const params = inject('mangrove')

describe('Getting the book', () => {
Expand Down
7 changes: 4 additions & 3 deletions src/actions/kandel/populate.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { type Client, erc20Abi, parseEther, parseUnits } from 'viem'
import { parseEther, parseUnits } from 'viem'
import { describe, expect, inject, it } from 'vitest'
import { minVolume, validateKandelParams } from '~mgv/index.js'
import { validateKandelParams } from '~mgv/index.js'
import { BS } from '~mgv/lib/enums.js'
import { getClient } from '~test/src/client.js'
import { mintAndApprove } from '~test/src/contracts/index.js'
import { getMarkets } from '~test/src/markets.js'
import { getBook } from '../book.js'
import { simulateMarketOrderByVolumeAndMarket } from '../market-order.js'
import { simulateBind, simulateDeployRouter } from '../smart-router.js'
import { simulatePopulate } from './populate.js'
import { simulateSow } from './sow.js'

const { smartKandelSeeder, kandelSeeder } = inject('kandel')
const { wethUSDC } = inject('markets')
const { wethUSDC } = getMarkets()
const actionParams = inject('mangrove')
const client = getClient()

Expand Down
3 changes: 2 additions & 1 deletion src/actions/kandel/retract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { describe, expect, inject, it } from 'vitest'
import { validateKandelParams } from '~mgv/index.js'
import { getClient } from '~test/src/client.js'
import { mintAndApprove } from '~test/src/contracts/index.js'
import { getMarkets } from '~test/src/markets.js'
import { getBook } from '../book.js'
import { simulateBind, simulateDeployRouter } from '../smart-router.js'
import { simulatePopulate } from './populate.js'
import { simulateRetract } from './retract.js'
import { simulateSow } from './sow.js'

const { smartKandelSeeder, kandelSeeder } = inject('kandel')
const { wethUSDC } = inject('markets')
const { wethUSDC } = getMarkets()
const actionParams = inject('mangrove')
const client = getClient()

Expand Down
3 changes: 2 additions & 1 deletion src/actions/kandel/sow.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { isAddress } from 'viem'
import { describe, expect, inject, it } from 'vitest'
import { getClient } from '~test/src/client.js'
import { getMarkets } from '~test/src/markets.js'
import { simulateSow } from './sow.js'

const { smartKandelSeeder, kandelSeeder } = inject('kandel')
const { wethUSDC } = inject('markets')
const { wethUSDC } = getMarkets()
const client = getClient()

describe('sow Smart Kandel', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/actions/kandel/view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { validateKandelParams } from '~mgv/index.js'
import type { KandelParams, MarketParams } from '~mgv/index.js'
import { BS, Order } from '~mgv/lib/enums.js'
import { getClient } from '~test/src/client.js'
import { getMarkets } from '~test/src/markets.js'
import { getBook } from '../book.js'
import { simulateLimitOrder } from '../index.js'
import { simulateBind, simulateDeployRouter } from '../smart-router.js'
Expand All @@ -12,7 +13,7 @@ import { simulateSow } from './sow.js'
import { KandelStatus, getKandelState } from './view.js'

const { smartKandelSeeder } = inject('kandel')
const { wethUSDC } = inject('markets')
const { wethUSDC } = getMarkets()
const actionParams = inject('mangrove')
const client = getClient()

Expand Down
3 changes: 2 additions & 1 deletion src/actions/order/new.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import { 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 { getMarkets } from '~test/src/markets.js'
import { BS, Order } from '../../lib/enums.js'
import { getBook } from '../book.js'
import { getUserRouter } from '../smart-router.js'
import { getLimitOrderSteps, simulateLimitOrder } from './new.js'

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

describe('new order', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/actions/order/remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
removeOrderResultFromLogs,
} from '~mgv/lib/limit-order.js'
import { getClient } from '~test/src/client.js'
import { getMarkets } from '~test/src/markets.js'
import { getBook } from '../book.js'
import { simulateLimitOrder } from './new.js'
import { simulateRemoveOrder } from './remove.js'

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

describe('remove order', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/actions/order/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {
} from '~mgv/lib/limit-order.js'
import { tickFromVolumes } from '~mgv/lib/tick.js'
import { getClient } from '~test/src/client.js'
import { getMarkets } from '~test/src/markets.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 { wethUSDC } = getMarkets()
const params = inject('mangrove')

describe('update order', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/actions/order/view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { 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 { getMarkets } from '~test/src/markets.js'
import { BA, BS, Order } from '../../lib/enums.js'
import { getBook } from '../book.js'
import { getUserRouter } from '../smart-router.js'
Expand All @@ -11,7 +12,7 @@ import { getOrder, getOrders } from './view.js'

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

describe('view order', () => {
it('single order', async () => {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export {
export type {
MangroveActionsDefaultParams,
MarketParams,
SerializableMarketParams,
BuiltArgs,
BuiltArgsWithValue,
Book,
Expand All @@ -105,6 +106,7 @@ export type {
LocalConfig,
CompleteToken,
OLKey,
SerializableOLKey,
RpcOffer,
RpcOfferDetail,
RpcCompleteOffer,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/human-readable.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parseUnits } from 'viem'
import { describe, expect, inject, it } from 'vitest'
import { getMarkets } from '~test/src/markets.js'
import { BA } from './enums.js'
import {
amounts,
Expand All @@ -10,7 +11,7 @@ import {
} from './human-readable.js'
import { tickFromVolumes } from './tick.js'

const { wethUSDC } = inject('markets')
const { wethUSDC } = getMarkets()
const { USDC, WETH } = inject('tokens')

// price is quote/base
Expand Down
2 changes: 1 addition & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ export {

// utils

export { mask } from './utils.js'
export { mask, fromSerializable, toSerializable } from './utils.js'
3 changes: 2 additions & 1 deletion src/lib/kandel/distribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { parseAbi } from 'viem'
import { expect, it } from 'vitest'
import { describe, inject } from 'vitest'
import { getClient } from '~test/src/client.js'
import { getMarkets } from '~test/src/markets.js'
import { createGeometricDistribution } from './distribution.js'
import { getKandelPositionRawParams } from './params.js'

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

const client = getClient()

Expand Down
3 changes: 2 additions & 1 deletion src/lib/kandel/logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { isAddress } from 'viem'
import { describe, expect, inject, it } from 'vitest'
import { simulateSow } from '~mgv/actions/kandel/sow.js'
import { getClient } from '~test/src/client.js'
import { getMarkets } from '~test/src/markets.js'
import { hash } from '../ol-key.js'
import { getKandelsFromLogs } from './logs.js'

const { smartKandelSeeder } = inject('kandel')
const { wethUSDC } = inject('markets')
const { wethUSDC } = getMarkets()
const client = getClient()

describe('Kandel logs', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/kandel/params.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { describe, expect, inject, it } from 'vitest'
import { getBook } from '~mgv/actions/book.js'
import { getClient } from '~test/src/client.js'
import { getMarkets } from '~test/src/markets.js'
import {
humanPriceToRawPrice,
rawPriceToHumanPrice,
} from '../human-readable.js'
import { priceFromTick, tickFromPrice } from '../tick.js'
import { getKandelPositionRawParams, validateKandelParams } from './params.js'

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

const client = getClient()
Expand Down
3 changes: 2 additions & 1 deletion src/lib/market-order-simulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { simulateSow } from '~mgv/actions/kandel/sow.js'
import { validateKandelParams } from '~mgv/index.js'
import { getClient } from '~test/src/client.js'
import { mintAndApprove } from '~test/src/contracts/index.js'
import { getMarkets } from '~test/src/markets.js'
import type { Book } from '../types/index.js'
import { BS } from './enums.js'
import { marketOrderSimulation } from './market-order-simulation.js'
Expand All @@ -14,7 +15,7 @@ import { inboundFromOutbound, outboundFromInbound } from './tick.js'
const client = getClient()
const actionParams = inject('mangrove')
const kandelSeeder = inject('kandel')
const { wethUSDC } = inject('markets')
const { wethUSDC } = getMarkets()

const KANDEL_GASREQ = 128_000n

Expand Down
31 changes: 31 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
import type {
MarketParams,
OLKey,
SerializableMarketParams,
SerializableOLKey,
} from '../types/index.js'

export function mask(n: bigint): bigint {
return (1n << n) - 1n
}

export function toSerializable<
TType extends MarketParams | OLKey = MarketParams | OLKey,
>(
args: TType,
): TType extends MarketParams ? SerializableMarketParams : SerializableOLKey {
return {
...args,
tickSpacing: Number(args.tickSpacing),
} as unknown as TType extends MarketParams
? SerializableMarketParams
: SerializableOLKey
}

export function fromSerializable<
TType extends SerializableMarketParams | SerializableOLKey =
| SerializableMarketParams
| SerializableOLKey,
>(args: TType): TType extends SerializableMarketParams ? MarketParams : OLKey {
return {
...args,
tickSpacing: BigInt(args.tickSpacing),
} as unknown as TType extends SerializableMarketParams ? MarketParams : OLKey
}
12 changes: 12 additions & 0 deletions src/types/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ export type MarketParams = {
tickSpacing: bigint
}

/**
* A serializable version of a market
* @param base The base token
* @param quote The quote token
* @param tickSpacing The tick spacing
*/
export type SerializableMarketParams = {
base: Token
quote: Token
tickSpacing: number
}

/**
* List of args that are built by the transaction builders
*/
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export type {
MangroveActionsDefaultParams,
MarketParams,
SerializableMarketParams,
BuiltArgs,
BuiltArgsWithValue,
Book,
Expand All @@ -22,6 +23,7 @@ export type {
LocalConfig,
CompleteToken,
OLKey,
SerializableOLKey,
RpcOffer,
RpcOfferDetail,
RpcCompleteOffer,
Expand Down
12 changes: 12 additions & 0 deletions src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ export type OLKey = {
tickSpacing: bigint
}

/**
* An OLKey object that is serializable.
* @param outbound_tkn The address of the outbound token.
* @param inbound_tkn The address of the inbound token.
* @param tickSpacing The tick spacing.
*/
export type SerializableOLKey = {
outbound_tkn: Address
inbound_tkn: Address
tickSpacing: number
}

/**
* The offer object returned by the RPC endpoint.
*/
Expand Down
Loading

0 comments on commit 692f50f

Please sign in to comment.