Skip to content

Commit

Permalink
Merge branch 'main' into feat/eng-365/openMarkets
Browse files Browse the repository at this point in the history
  • Loading branch information
anasbelbaz committed Sep 23, 2024
2 parents a701c86 + 4a775b1 commit 7b13c86
Show file tree
Hide file tree
Showing 42 changed files with 748 additions and 67 deletions.
4 changes: 2 additions & 2 deletions .github/actions/install-dependencies/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ runs:
- name: Set up Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.6
bun-version: 1.1.26

- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: 21
node-version: 22

- name: Set up Foundry
uses: foundry-rs/foundry-toolchain@v1
Expand Down
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"
}
}
54 changes: 54 additions & 0 deletions src/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
# @mangrovedao/mgv

## 0.9.10

### Patch Changes

- d939aeb: Added base and quote reserve to kandel view function

## 0.9.9

### Patch Changes

- 692f50f: Added serialize helper functions for markets and offer list keys

## 0.9.8

### Patch Changes

- 61623e3: Fixed market order simulation and added tests

## 0.9.7

### Patch Changes

- fd0b7bf: Reverse base quote order for WETH/weETH

## 0.9.6

### Patch Changes

- 48ee6b9: Added WETH/WBTC WBTC/USDT and weETH/WETH on arbitrum

## 0.9.5

### Patch Changes

- f838392: Added check for aave markets

## 0.9.4

### Patch Changes

- c88d3a1: Fix invalid param names on kandel

## 0.9.3

### Patch Changes

- 5c63a0c: Fixed USDC and USDT decimals on arbitrum

## 0.9.2

### Patch Changes

- 19a954c: Add arbitrum tokens and markets

## 0.9.1

### Patch Changes
Expand Down
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
114 changes: 114 additions & 0 deletions src/actions/kandel/aave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import type {
Address,
Client,
MulticallParameters,
ReadContractParameters,
} from 'viem'
import { multicall, readContract } from 'viem/actions'
import {
type CheckAaveAssetParams,
type aaveRouterCheckAssetABI,
checkAaveAssetParams,
} from '../../builder/kandel/aave.js'
import type { BuiltArgs, MarketParams } from '../../index.js'
import { getAction } from '../../utils/getAction.js'

type ReadSingleParams = ReadContractParameters<
typeof aaveRouterCheckAssetABI,
'checkAsset'
>

export type CheckAaveAssetArgs = CheckAaveAssetParams &
Omit<ReadSingleParams, BuiltArgs>

export async function checkAaveAsset(
client: Client,
aaveRouter: Address,
args: CheckAaveAssetArgs,
): Promise<boolean> {
return getAction(
client,
readContract,
'readContract',
)({
...(args as unknown as ReadSingleParams),
address: aaveRouter,
...checkAaveAssetParams(args),
})
}

export type CheckAaveAssetsArgs = { tokens: Address[] } & Omit<
MulticallParameters,
'contracts' | 'allowFailure'
>

export async function checkAaveAssets(
client: Client,
aaveRouter: Address,
args: CheckAaveAssetsArgs,
): Promise<boolean[]> {
return getAction(
client,
multicall,
'multicall',
)({
...args,
contracts: args.tokens.map((token) => ({
address: aaveRouter,
...checkAaveAssetParams({ token }),
})),
allowFailure: false,
})
}

export type CheckAaveMarketArgs = { market: MarketParams } & Omit<
MulticallParameters,
'contracts' | 'allowFailure'
>

export async function checkAaveMarket(
client: Client,
aaveRouter: Address,
args: CheckAaveMarketArgs,
): Promise<boolean> {
const tokens = [args.market.base.address, args.market.quote.address]
const available = await checkAaveAssets(client, aaveRouter, {
...args,
tokens,
})
return available.every((a) => a)
}

export type CheckAaveMarketsArgs = { markets: MarketParams[] } & Omit<
MulticallParameters,
'contracts' | 'allowFailure'
>

export async function checkAaveMarkets(
client: Client,
aaveRouter: Address,
args: CheckAaveMarketsArgs,
): Promise<MarketParams[]> {
const tokens = [
...new Set(
args.markets.flatMap((m) => [
m.base.address.toLowerCase() as Address,
m.quote.address.toLowerCase() as Address,
]),
),
]
const available = await checkAaveAssets(client, aaveRouter, {
...args,
tokens,
})
const tokensMap = tokens.reduce((acc, token, i) => {
acc.set(token, available[i] || false)
return acc
}, new Map<Address, boolean>())

return args.markets.filter(
(m) =>
tokensMap.get(m.base.address.toLowerCase() as Address) &&
tokensMap.get(m.quote.address.toLowerCase() as Address),
)
}
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
21 changes: 21 additions & 0 deletions src/actions/kandel/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
offeredVolumeParams,
provisionOfParams,
quoteParams,
reserveBalanceParams,
tickSpacingParams,
} from '../../builder/kandel/view.js'
import {
Expand Down Expand Up @@ -59,6 +60,8 @@ export type GetKandelStateResult = {
pricePoints: number
quoteAmount: bigint
baseAmount: bigint
reserveBalanceQuote: bigint
reserveBalanceBase: bigint
unlockedProvision: bigint
totalProvision: bigint
kandelStatus: KandelStatus
Expand All @@ -77,6 +80,8 @@ type KandelInitCallResult = {
}
baseAmount: bigint
quoteAmount: bigint
reserveBalanceQuote: bigint
reserveBalanceBase: bigint
unlockedProvision: bigint
// mid price from the book
midPrice: number
Expand All @@ -99,6 +104,8 @@ async function kandelInitCall(
params,
_quoteAmount,
_baseAmount,
_reserveBalanceQuote,
_reserveBalanceBase,
_base,
_quote,
_tickSpacing,
Expand Down Expand Up @@ -140,6 +147,14 @@ async function kandelInitCall(
address: kandel,
...offeredVolumeParams(BA.asks),
},
{
address: kandel,
...reserveBalanceParams(BA.bids),
},
{
address: kandel,
...reserveBalanceParams(BA.asks),
},
{
address: kandel,
...baseParams,
Expand Down Expand Up @@ -187,6 +202,10 @@ async function kandelInitCall(
const baseAmount = _baseAmount.status === 'success' ? _baseAmount.result : 0n
const quoteAmount =
_quoteAmount.status === 'success' ? _quoteAmount.result : 0n
const reserveBalanceQuote =
_reserveBalanceQuote.status === 'success' ? _reserveBalanceQuote.result : 0n
const reserveBalanceBase =
_reserveBalanceBase.status === 'success' ? _reserveBalanceBase.result : 0n

const asks =
bestAsk.status === 'success'
Expand Down Expand Up @@ -231,6 +250,8 @@ async function kandelInitCall(
: { gasprice: 0, gasreq: 0, stepSize: 0, pricePoints: 0 },
baseAmount: reversed ? quoteAmount : baseAmount,
quoteAmount: reversed ? baseAmount : quoteAmount,
reserveBalanceQuote: reversed ? reserveBalanceBase : reserveBalanceQuote,
reserveBalanceBase: reversed ? reserveBalanceQuote : reserveBalanceBase,
reversed,
unlockedProvision:
unlockedProvision.status === 'success' ? unlockedProvision.result : 0n,
Expand Down
Loading

0 comments on commit 7b13c86

Please sign in to comment.