Skip to content

Commit

Permalink
feat: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxencerb committed May 22, 2024
1 parent 714b70b commit 3855f38
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
Empty file.
37 changes: 37 additions & 0 deletions src/lib/kandel/params.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, it, inject, expect } from 'vitest'
import { getKandelPositionRawParams } from './params.js'
import { priceFromTick, tickFromPrice } from '../tick.js'
import {
humanPriceToRawPrice,
rawPriceToHumanPrice,
} from '../human-readable.js'

const { wethUSDC } = inject('markets')

describe('kandel params', () => {
it('kandel position raw params', () => {
const params = getKandelPositionRawParams({
minPrice: 2500,
midPrice: 3000,
maxPrice: 3500,
pricePoints: 10n,
market: wethUSDC,
})

expect(params.pricePoints).toBe(10n)
expect(params.firstAskIndex).toBe(5n)

const baseQuoteTickIndex0 = tickFromPrice(
humanPriceToRawPrice(2500, wethUSDC),
wethUSDC.tickSpacing,
)
expect(params.baseQuoteTickIndex0).toBe(baseQuoteTickIndex0)
expect(params.baseQuoteTickOffset % wethUSDC.tickSpacing).toBe(0n)

const endTick =
params.baseQuoteTickIndex0 +
params.baseQuoteTickOffset * (params.pricePoints - 1n)
const endPrice = rawPriceToHumanPrice(priceFromTick(endTick), wethUSDC)
expect(endPrice).toApproximateEqual(3500)
})
})
8 changes: 5 additions & 3 deletions src/lib/kandel/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type PositionKandelParams = {
pricePoints: bigint
}

function getKandelPositionRawParams(
export function getKandelPositionRawParams(
params: RawKandelPositionParams,
): PositionKandelParams {
const { market, pricePoints } = params
Expand Down Expand Up @@ -90,10 +90,11 @@ export type ValidateParamsResult = {
minBaseAmount: bigint
minQuoteAmount: bigint
minProvision: bigint
distribution: Distribution
isValid: boolean
}

function countBidsAndAsks(distribution: Distribution) {
export function countBidsAndAsks(distribution: Distribution) {
let nBids = 0n
let nAsks = 0n
for (let i = 0; i < distribution.asks.length; i++) {
Expand All @@ -106,7 +107,7 @@ function countBidsAndAsks(distribution: Distribution) {
}
}

function changeGives(
export function changeGives(
distribution: Distribution,
bidGives: bigint,
askGives: bigint,
Expand Down Expand Up @@ -204,5 +205,6 @@ export function validateKandelParams(
minQuoteAmount,
minProvision,
isValid,
distribution,
}
}
16 changes: 16 additions & 0 deletions test/src/contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ export async function deployERC20(
})
}

export async function deploySmartKandel(
mgv: Address,
kandelGasreq: bigint,
routerProxyFactory: Address,
routerImplementation: Address,
kandelLibBytecode: Hex,
smartKandelSeederBytecode: Hex
): Promise<{kandelLib: Address, smartKandelSeeder: Address}> {
const libTx = await globalTestClient.deployContract({
account: globalTestClient.account,
chain: globalTestClient.chain,
bytecode: kandelLibBytecode,
abi: parseAbi(["constructor()"]),
})
}

export async function deployMangroveCore(bytecode: Hex): Promise<Address> {
const gasprice = await globalTestClient.getGasPrice();
const res = await globalTestClient.deployContract({
Expand Down
3 changes: 3 additions & 0 deletions test/src/contracts/kandellib.bytecode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Hex } from "viem";

export const kandellibBytecode: Hex = "0x610ca061003a600b82828239805160001a60731461002d57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600436106100355760003560e01c8063fbbd4a651461003a575b600080fd5b61004d6100483660046109f1565b610063565b60405161005a9190610aa1565b60405180910390f35b60408051808201909152606080825260208201526000198514158061008a57506000198414155b6100d65760405162461bcd60e51b81526020600482015260186024820152774b616e64656c2f626f746847697665735661726961626c6560401b60448201526064015b60405180910390fd5b6000806100e4600285610af9565b6100ef600286610b23565b6100f99190610b37565b9050808811610109576000610113565b6101138189610b4a565b915060006101218587610b4a565b90508083111561012f578092505b5061013d9050600284610b23565b6101479088610b37565b965082871015610155578296505b808a10156101605750885b8a87101561016c578a96505b60008a881061017c576000610186565b610186888c610b4a565b828d1061019457600061019e565b61019e8d84610b4a565b6101a89190610b37565b9050806001600160401b038111156101c2576101c2610b5d565b6040519080825280602002602001820160405280156101fb57816020015b6101e86109d0565b8152602001906001900390816101e05790505b506020840152806001600160401b0381111561021957610219610b5d565b60405190808252806020026020018201604052801561025257816020015b61023f6109d0565b8152602001906001900390816102375790505b508352508a6000610263828b610b73565b61026d908c610ba3565b61027690610bcb565b905060005b838310156103625760405180606001604052808481526020018381526020016000198b146102a9578a6102b3565b6102b3848b61047c565b815250856020015182815181106102cc576102cc610be7565b602002602001018190525060006102e6600185898b6104ae565b90506040518060600160405280828152602001828e6103059190610b73565b8f6103109190610ba3565b81526000602090910152865180518490811061032e5761032e610be7565b60209081029190910101526103438c84610bfd565b925061034e82610c24565b9150508261035b90610c24565b925061027b565b89925061036f838c610b73565b610379908d610ba3565b91505b8c83101561046b5760405180606001604052808481526020018381526020016000198a146103aa57896103b4565b6103b4848c61047c565b905285518051839081106103ca576103ca610be7565b602002602001018190525060006103e4600085898b6104ae565b90506040518060600160405280828152602001828e6104039190610b73565b8f61040e9190610ba3565b61041790610bcb565b815260200160008152508660200151838151811061043757610437610be7565b602090810291909101015261044c8c84610ba3565b925061045782610c24565b9150508261046490610c24565b925061037c565b505050509998505050505050505050565b6000808061049161048c86610bcb565b61050b565b9092509050806104a18584610c3d565b901c925050505b92915050565b600060018560018111156104c4576104c4610c54565b036104ef576104d38385610b37565b90508181106104ea576104e7600183610b4a565b90505b610503565b828410610503576105008385610b4a565b90505b949350505050565b6000806105178361057d565b909250905060eb83901b60007ed89e8c075c4155dd3213326cbbcf86fd1f63f5c205dd2ad2cf44f9349b39e180830782139083050390607e1982850101908113156105655793841c9361056f565b8060000385901b94505b81607f039350505050915091565b600080600080841261058f5783610598565b61059884610bcb565b9050620d89e88111156105e75760405162461bcd60e51b81526020600482015260176024820152766d67762f6162735469636b2f6f75744f66426f756e647360481b60448201526064016100cd565b6000600182161561060a576ffff97272373d413259a46990580e21399350610612565b600160801b93505b600282161561063c576080610637856ffff2e50f5f656932ef12357cf3c7fdcb610c3d565b901c93505b6004821615610666576080610661856fffe5caca7e10e4e61c3624eaa0941ccf610c3d565b901c93505b600882161561069057608061068b856fffcb9843d60f6159c9db58835c926643610c3d565b901c93505b60108216156106ba5760806106b5856fff973b41fa98c081472e6896dfb254bf610c3d565b901c93505b60208216156106e45760806106df856fff2ea16466c96a3843ec78b326b52860610c3d565b901c93505b604082161561070e576080610709856ffe5dee046a99a2a811c461f1969c3052610c3d565b901c93505b6080821615610738576080610733856ffcbe86c7900a88aedcffc83b479aa3a3610c3d565b901c93505b61010082161561076357608061075e856ff987a7253ac413176f2b074cf7815e53610c3d565b901c93505b61020082161561078e576080610789856ff3392b0822b70005940c7a398e4b70f2610c3d565b901c93505b6104008216156107b95760806107b4856fe7159475a2c29b7443b29c7fa6e889d8610c3d565b901c93505b6108008216156107e45760806107df856fd097f3bdfd2022b8845ad8f792aa5825610c3d565b901c93505b61100082161561080f57608061080a856fa9f746462d870fdf8a65dc1f90e061e4610c3d565b901c93505b612000821615610847576080610835856fe1b0d342ada5437121767bec575e65ed610c3d565b901c9350610844600182610ba3565b90505b61400082161561087f57608061086d856fc6f84d7e5f423f66048c541550bf3e96610c3d565b901c935061087c600282610ba3565b90505b6180008216156108b75760806108a5856f9aa508b5b7a84e1c677de54f3e99bc8f610c3d565b901c93506108b4600482610ba3565b90505b620100008216156108f05760806108de856fbad5f1bdb70232cd33865244bdcc089c610c3d565b901c93506108ed600982610ba3565b90505b62020000821615610929576080610917856f885b9613d7e87aa498106fb7fa5edd37610c3d565b901c9350610926601282610ba3565b90505b62040000821615610962576080610950856f9142e0723efb884889d1f447715afacd610c3d565b901c935061095f602582610ba3565b90505b6208000082161561099b576080610989856fa4d9a773d61316918f140bd96e8e6814610c3d565b901c9350610998604b82610ba3565b90505b60008513156109bc576001848560000304019350806109b990610bcb565b90505b6109c7816080610ba3565b92505050915091565b60405180606001604052806000815260200160008152602001600081525090565b60008060008060008060008060006101208a8c031215610a1057600080fd5b505087359960208901359950604089013598606081013598506080810135975060a0810135965060c0810135955060e08101359450610100013592509050565b600081518084526020808501945080840160005b83811015610a965781518051885283810151848901526040908101519088015260609096019590820190600101610a64565b509495945050505050565b602081526000825160406020840152610abd6060840182610a50565b90506020840151601f19848303016040850152610ada8282610a50565b95945050505050565b634e487b7160e01b600052601260045260246000fd5b600082610b0857610b08610ae3565b500690565b634e487b7160e01b600052601160045260246000fd5b600082610b3257610b32610ae3565b500490565b808201808211156104a8576104a8610b0d565b818103818111156104a8576104a8610b0d565b634e487b7160e01b600052604160045260246000fd5b80820260008212600160ff1b84141615610b8f57610b8f610b0d565b81810583148215176104a8576104a8610b0d565b8082018281126000831280158216821582161715610bc357610bc3610b0d565b505092915050565b6000600160ff1b8201610be057610be0610b0d565b5060000390565b634e487b7160e01b600052603260045260246000fd5b8181036000831280158383131683831282161715610c1d57610c1d610b0d565b5092915050565b600060018201610c3657610c36610b0d565b5060010190565b80820281158282048414176104a8576104a8610b0d565b634e487b7160e01b600052602160045260246000fdfea264697066735822122075593e68f72d4bc201c22e348fa34c029c4bd0ff63fb069a0294e61aefc29e6a64736f6c63430008140033"
3 changes: 3 additions & 0 deletions test/src/contracts/smart-kandel-seeder.bytecode.ts

Large diffs are not rendered by default.

0 comments on commit 3855f38

Please sign in to comment.