Skip to content

Commit

Permalink
feat(kandel): Fix value in populate (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxencerb authored Jun 5, 2024
1 parent 2b78573 commit 90827c9
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-horses-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": patch
---

Fixed values for populate
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/1.5.3/schema.json",
"$schema": "https://biomejs.dev/schemas/1.7.2/schema.json",
"files": {
"ignore": [
"_cjs",
Expand Down
Empty file.
72 changes: 72 additions & 0 deletions src/actions/kandel/populate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { parseEther, parseUnits } from 'viem'
import { describe, expect, inject, it } from 'vitest'
import { validateKandelParams } from '~mgv/index.js'
import { getClient } from '~test/src/client.js'
import { getBook } from '../book.js'
import { simulateBind, simulateDeployRouter } from '../smart-router.js'
import { simulatePopulate } from './populate.js'
import { simulateSow } from './sow.js'

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

describe('populate', () => {
it('populates', async () => {
const { request: sowReq, result: kandel } = await simulateSow(
client,
wethUSDC,
smartKandelSeeder,
{
account: client.account.address,
},
)
const hash = await client.writeContract(sowReq)
await client.waitForTransactionReceipt({ hash })

const book = await getBook(client, actionParams, wethUSDC)

const { params, isValid, minProvision } = validateKandelParams({
minPrice: 2500,
midPrice: 3000,
maxPrice: 3500,
pricePoints: 5n,
market: wethUSDC,
baseAmount: parseEther('1'),
quoteAmount: parseUnits('3000', 18),
stepSize: 1n,
gasreq: 350_000n,
factor: 3,
asksLocalConfig: book.asksConfig,
bidsLocalConfig: book.bidsConfig,
marketConfig: book.marketConfig,
})

expect(isValid).toBe(true)

const { request: deployRouterReq, router } = await simulateDeployRouter(
client,
actionParams,
{
user: client.account.address,
},
)
const routerTx = await client.writeContract(deployRouterReq)
await client.waitForTransactionReceipt({ hash: routerTx })

const { request: bindReq } = await simulateBind(client, router, {
target: kandel,
})
const bindTx = await client.writeContract(bindReq)
await client.waitForTransactionReceipt({ hash: bindTx })

const { request } = await simulatePopulate(client, kandel, {
...params,
account: client.account.address,
value: minProvision,
})
const hash2 = await client.writeContract(request)
await client.waitForTransactionReceipt({ hash: hash2 })
})
})
6 changes: 3 additions & 3 deletions src/actions/kandel/populate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
populateChunkFromOffsetParams,
populateFromOffsetParams,
} from '../../builder/kandel/populate.js'
import type { BuiltArgsWithValue } from '../../index.js'
import type { BuiltArgs } from '../../index.js'
import type { SimulationParams } from '../../types/actions/simulation.js'
import { getAction } from '../../utils/getAction.js'

Expand All @@ -16,7 +16,7 @@ type SimulationPopulateParams = SimulationParams<
'populateFromOffset'
>
export type PopulateArgs = PopulateFromOffsetParams &
Omit<SimulationPopulateParams, BuiltArgsWithValue>
Omit<SimulationPopulateParams, BuiltArgs>

export type PopulateResult = SimulateContractReturnType<
typeof populateABI,
Expand Down Expand Up @@ -45,7 +45,7 @@ type SimulationPopulateChunckParams = SimulationParams<
>

export type PopulateChunkArgs = PopulateChunkFromOffsetParams &
Omit<SimulationPopulateChunckParams, BuiltArgsWithValue>
Omit<SimulationPopulateChunckParams, BuiltArgs>

export type PopulateChunkResult = SimulateContractReturnType<
typeof populateABI,
Expand Down
23 changes: 23 additions & 0 deletions src/actions/kandel/sow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isAddress } from 'viem'
import { describe, expect, inject, it } from 'vitest'
import { getClient } from '~test/src/client.js'
import { simulateSow } from './sow.js'

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

describe('sow', () => {
it('sows', async () => {
const { request, result } = await simulateSow(
client,
wethUSDC,
smartKandelSeeder,
{},
)
expect(isAddress(result)).toBe(true)
const tx = await client.writeContract(request)
const receipt = await client.waitForTransactionReceipt({ hash: tx })
expect(receipt.status).toBe('success')
})
})
9 changes: 7 additions & 2 deletions src/lib/kandel/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export function countBidsAndAsks(distribution: Distribution) {
let nAsks = 0n
for (let i = 0; i < distribution.asks.length; i++) {
if (distribution.asks[i]!.gives !== 0n) nAsks++
}
for (let i = 0; i < distribution.bids.length; i++) {
if (distribution.bids[i]!.gives !== 0n) nBids++
}
return {
Expand Down Expand Up @@ -179,9 +181,12 @@ export function validateKandelParams(

const minBaseAmount = minAsk * nAsks
const minQuoteAmount = minBid * nBids

const minProvision =
((gasreq + asksLocalConfig.offer_gasbase) * nAsks +
(gasreq + bidsLocalConfig.offer_gasbase) * nBids) *
((gasreq + asksLocalConfig.offer_gasbase) *
BigInt(distribution.asks.length) +
(gasreq + bidsLocalConfig.offer_gasbase) *
BigInt(distribution.bids.length)) *
marketConfig.gasprice *
BigInt(1e6)

Expand Down

0 comments on commit 90827c9

Please sign in to comment.