Skip to content

Commit

Permalink
feat: Add classic kandelSeeder to tests. (#104)
Browse files Browse the repository at this point in the history
* feat: Add classic kandelSeeder to tests.

* chore: format

---------

Co-authored-by: maxencerb <[email protected]>
  • Loading branch information
maxencerb and maxencerb authored Jun 26, 2024
1 parent 36c298f commit f1a3844
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 8 deletions.
67 changes: 64 additions & 3 deletions src/actions/kandel/populate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ 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 { mintAndApprove } from '~test/src/contracts/index.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 { smartKandelSeeder, kandelSeeder } = inject('kandel')
const { wethUSDC } = inject('markets')
const actionParams = inject('mangrove')
const client = getClient()

describe('populate', () => {
describe('populate smart kandel', () => {
it('populates', async () => {
const { request: sowReq, result: kandel } = await simulateSow(
client,
Expand All @@ -34,7 +35,7 @@ describe('populate', () => {
pricePoints: 5n,
market: wethUSDC,
baseAmount: parseEther('1'),
quoteAmount: parseUnits('3000', 18),
quoteAmount: parseUnits('3000', 6),
stepSize: 1n,
gasreq: 350_000n,
factor: 3,
Expand Down Expand Up @@ -70,3 +71,63 @@ describe('populate', () => {
await client.waitForTransactionReceipt({ hash: hash2 })
})
})

describe('populate kandel', () => {
it('populates', async () => {
const { request: sowReq, result: kandel } = await simulateSow(
client,
wethUSDC,
kandelSeeder,
{
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: 2990,
midPrice: 3000,
maxPrice: 3010,
pricePoints: 5n,
market: wethUSDC,
baseAmount: parseEther('10'),
quoteAmount: parseUnits('30000', 6),
stepSize: 1n,
gasreq: 350_000n,
factor: 3,
asksLocalConfig: book.asksConfig,
bidsLocalConfig: book.bidsConfig,
marketConfig: book.marketConfig,
deposit: true,
})

expect(isValid).toBe(true)

// mint tokens and give approval to kandel
await mintAndApprove(
client,
wethUSDC.base.address,
client.account.address,
params.baseAmount || 0n,
kandel,
)
await mintAndApprove(
client,
wethUSDC.quote.address,
client.account.address,
params.quoteAmount || 0n,
kandel,
)

const { request } = await simulatePopulate(client, kandel, {
...params,
account: client.account.address,
value: minProvision,
})
const hash2 = await client.writeContract(request)
await client.waitForTransactionReceipt({ hash: hash2 })
})
})
19 changes: 17 additions & 2 deletions src/actions/kandel/sow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { describe, expect, inject, it } from 'vitest'
import { getClient } from '~test/src/client.js'
import { simulateSow } from './sow.js'

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

describe('sow', () => {
describe('sow Smart Kandel', () => {
it('sows', async () => {
const { request, result } = await simulateSow(
client,
Expand All @@ -21,3 +21,18 @@ describe('sow', () => {
expect(receipt.status).toBe('success')
})
})

describe('Sow Kandel', () => {
it('sows', async () => {
const { request, result } = await simulateSow(
client,
wethUSDC,
kandelSeeder,
{},
)
expect(isAddress(result)).toBe(true)
const tx = await client.writeContract(request)
const receipt = await client.waitForTransactionReceipt({ hash: tx })
expect(receipt.status).toBe('success')
})
})
6 changes: 6 additions & 0 deletions src/lib/kandel/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ export type RawKandelParams = RawKandelPositionParams & {
asksLocalConfig: LocalConfig
bidsLocalConfig: LocalConfig
marketConfig: GlobalConfig
deposit?: boolean | undefined
}

export type KandelParams = PositionKandelParams & {
stepSize: bigint
askGives: bigint
bidGives: bigint
gasreq: bigint
baseAmount?: bigint | undefined
quoteAmount?: bigint | undefined
}

export type ValidateParamsResult = {
Expand Down Expand Up @@ -140,6 +143,7 @@ export function validateKandelParams(
asksLocalConfig,
bidsLocalConfig,
marketConfig,
deposit = false,
} = params

let distribution = createGeometricDistribution({
Expand Down Expand Up @@ -202,6 +206,8 @@ export function validateKandelParams(
bidGives,
gasreq,
pricePoints,
baseAmount: deposit ? baseAmount : undefined,
quoteAmount: deposit ? quoteAmount : undefined,
},
rawParams: {
...params,
Expand Down
59 changes: 58 additions & 1 deletion src/lib/ol-key.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import type { OLKey } from '../types/lib.js'
import { flip } from './ol-key.js'
import { flip, hash } from './ol-key.js'

describe('flip', () => {
it('flips the OLKey', () => {
Expand All @@ -16,4 +16,61 @@ describe('flip', () => {
tickSpacing: 100n,
})
})

it('hashes the olKey', () => {
const markets = [
{
id: '0x456cb2ca75d08a5c881c4b083a6dd4a469f35d14bd7f2279e8ea8141378ad840',
outbound_tkn: '0x999f220296b5843b2909cc5f8b4204aaca5341d8',
inbound_tkn: '0x4300000000000000000000000000000000000004',
tickSpacing: 1n,
},
{
id: '0x8c421ef7d31aa292802bc4360219b78a45e38a40cc76d04f3f0a9fbc914cbd79',
outbound_tkn: '0x4300000000000000000000000000000000000003',
inbound_tkn: '0x5d3a1ff2b6bab83b63cd9ad0787074081a52ef34',
tickSpacing: 1n,
},
{
id: '0x9478fa0733344acd896e7f5ffa9ee03ccab653020a2898458323ddd6b53593df',
outbound_tkn: '0x4300000000000000000000000000000000000003',
inbound_tkn: '0x4300000000000000000000000000000000000004',
tickSpacing: 1n,
},
{
id: '0x9646f837c173ebace85f4331500010baada3c402b39fe0b42cb6a981ae4c86d0',
outbound_tkn: '0x5d3a1ff2b6bab83b63cd9ad0787074081a52ef34',
inbound_tkn: '0x4300000000000000000000000000000000000003',
tickSpacing: 1n,
},
{
id: '0x970524a7911bd72feac6c2e9065d469de667eeb53c26dd876351b6c9eaea9a30',
outbound_tkn: '0x9a50953716ba58e3d6719ea5c437452ac578705f',
inbound_tkn: '0x4300000000000000000000000000000000000004',
tickSpacing: 1n,
},
{
id: '0xa0d8dad9aa31ad878a583f223811d7a37f06a6f2eefaf72f6b60ef43419f9cdd',
outbound_tkn: '0x4300000000000000000000000000000000000004',
inbound_tkn: '0x4300000000000000000000000000000000000003',
tickSpacing: 1n,
},
{
id: '0xa1dbe0263a07f9407dbe0f2b0ccfae710ce701cca7bfba9fe42227998e859403',
outbound_tkn: '0x4300000000000000000000000000000000000004',
inbound_tkn: '0x9a50953716ba58e3d6719ea5c437452ac578705f',
tickSpacing: 1n,
},
{
id: '0xdff65bb4101d518f43ad16acfea56d37bce9a65b8ecc0de94a3e238f68a3516d',
outbound_tkn: '0x4300000000000000000000000000000000000004',
inbound_tkn: '0x999f220296b5843b2909cc5f8b4204aaca5341d8',
tickSpacing: 1n,
},
] as const

for (const market of markets) {
expect(hash(market)).toEqual(market.id)
}
})
})
12 changes: 11 additions & 1 deletion test/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { globalTestClient } from '~test/src/client.js'
import { accounts } from './src/constants.js'
import {
deployERC20,
deployKandelSeeder,
deployMangroveCore,
deployMangroveOrder,
deployMangroveReader,
Expand All @@ -15,6 +16,7 @@ import {
openMarket,
setMulticall,
} from './src/contracts/index.js'
import { kandelSeederBytecode } from './src/contracts/kandel-seeder.bytecode.js'
import { kandellibBytecode } from './src/contracts/kandellib.bytecode.js'
import { getMangroveBytecodes } from './src/contracts/mangrove.js'
import { smartKandelSeederBytecode } from './src/contracts/smart-kandel-seeder.bytecode.js'
Expand Down Expand Up @@ -79,6 +81,13 @@ export default async function ({ provide }: GlobalSetupContext) {
smartKandelSeederBytecode,
)

const kandelSeeder = await deployKandelSeeder(
mangrove,
128_000n,
kandelLib,
kandelSeederBytecode,
)

provide('tokens', { WETH, USDC, DAI })
provide('mangrove', {
mgv: mangrove,
Expand All @@ -89,7 +98,7 @@ export default async function ({ provide }: GlobalSetupContext) {
multicall,
tickSpacing: 60n,
})
provide('kandel', { kandelLib, smartKandelSeeder })
provide('kandel', { kandelLib, kandelSeeder, smartKandelSeeder })

// open markets

Expand Down Expand Up @@ -163,6 +172,7 @@ declare module 'vitest' {
}
kandel: {
kandelLib: Address
kandelSeeder: Address
smartKandelSeeder: Address
}
}
Expand Down
52 changes: 51 additions & 1 deletion test/src/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { globalTestClient } from "../client.js";
import { accounts } from "../constants.js";
import { Address, Client, Hex, parseAbi } from "viem";
import { writeContract } from "viem/actions";
import { writeContract, waitForTransactionReceipt } from "viem/actions";
import { ERC20_ABI, ERC20_BYTECODE } from "./erc20.js";
import type { OLKey } from "~mgv/types/lib.js";
import { flip } from "~mgv/lib/ol-key.js";
Expand Down Expand Up @@ -32,6 +32,28 @@ export async function deployERC20(
})
}

export async function deployKandelSeeder(
mgv: Address,
kandelGasreq: bigint,
kandelLib: Address,
kandelSeederBytecode: Hex
): Promise<Address> {
const seederTx = await globalTestClient.deployContract({
account: globalTestClient.account,
chain: globalTestClient.chain,
bytecode: kandelSeederBytecode.replace(/__\$[a-fA-F0-9]{34}\$__/g, kandelLib.slice(2)),
abi: parseAbi([
"constructor(address mgv, uint gasreq)",
]),
args: [mgv, kandelGasreq],
} as any);
const seederReceipt = await globalTestClient.waitForTransactionReceipt({
hash: seederTx,
});
const seederAddress = seederReceipt.contractAddress;
return seederAddress;
}

export async function deploySmartKandel(
mgv: Address,
kandelGasreq: bigint,
Expand Down Expand Up @@ -219,4 +241,32 @@ export async function mint(
functionName: "mint",
args: [to, amount],
} as any);
await waitForTransactionReceipt(client, { hash: res });
}

export async function approve(
client: Client,
token: Address,
owner: Address,
spender: Address,
amount: bigint
) {
const res = await writeContract(client, {
address: token,
abi: ERC20_ABI,
functionName: "approve",
args: [spender, amount],
} as any);
await waitForTransactionReceipt(client, { hash: res });
}

export async function mintAndApprove(
client: Client,
token: Address,
to: Address,
amount: bigint,
spender: Address
) {
await mint(client, token, to, amount)
await approve(client, token, to, spender, amount)
}
3 changes: 3 additions & 0 deletions test/src/contracts/kandel-seeder.bytecode.ts

Large diffs are not rendered by default.

0 comments on commit f1a3844

Please sign in to comment.