Skip to content

Commit

Permalink
feat: Added addresses to the default context. (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxencerb authored May 28, 2024
1 parent 8c6cfde commit a459e3a
Show file tree
Hide file tree
Showing 23 changed files with 413 additions and 61 deletions.
9 changes: 9 additions & 0 deletions .changeset/four-fireants-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@mangrovedao/mgv": minor
---

Added the smart router client

The `getUserRouter` functions were moved from the order subfolders to `smart-router.ts` files

Please enter a summary for your changes.
5 changes: 5 additions & 0 deletions .changeset/quiet-clocks-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": minor
---

Added addresses to the default context
17 changes: 15 additions & 2 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export type {
GetLimitOrderStepsParams,
GetLimitOrderStepsArgs,
GetUserRouterArgs,
SimulateRawLimitOrderArgs,
SimulateLimitOrderArgs,
SimulateLimitOrderResult,
Expand All @@ -25,7 +24,6 @@ export type {

export {
getLimitOrderSteps,
getUserRouter,
simulateRawLimitOrder,
simulateLimitOrder,
getUpdateOrderSteps,
Expand Down Expand Up @@ -89,3 +87,18 @@ export {
simulateMarketOrderByVolumeAndMarket,
waitForMarketOrderResult,
} from './market-order.js'

export type {
GetUserRouterArgs,
IsBoundArgs,
DeployRouterArgs,
DeployRouterResult,
BindArgs,
BindResult,
} from './smart-router.js'

export {
getUserRouter,
isBound,
simulateDeployRouter,
} from './smart-router.js'
Empty file.
16 changes: 5 additions & 11 deletions src/actions/kandel/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@ import {
isAddressEqual,
maxUint128,
maxUint256,
parseAbi,
zeroAddress,
} from 'viem'
import { multicall } from 'viem/actions'
import { getLogicsParams } from '../../builder/kandel/logic.js'
import { adminParams, isBoundParams } from '../../builder/smart-router.js'
// import { getParamsParams } from '../../builder/kandel/populate.js'
import { tokenAllowanceParams } from '../../builder/tokens.js'
import type { KandelSteps, MarketParams } from '../../index.js'
import { getKandelGasReq } from '../../lib/kandel/params.js'
import { getAction } from '../../utils/getAction.js'
import type { OverlyingResult } from '../balances.js'

export const routerABI = parseAbi([
'function admin() public view returns (address current)',
'function isBound(address mkr) public view returns (bool)',
])

// => Deploy user router instance if not exist
// => Create kandel instance
// => Bind the router with the kandel
Expand Down Expand Up @@ -55,14 +50,13 @@ export async function getKandelSteps(
contracts: [
{
address: args.userRouter,
abi: routerABI,
functionName: 'admin',
...adminParams,
},
{
address: args.userRouter,
abi: routerABI,
functionName: 'isBound',
args: [kandel],
...isBoundParams({
maker: kandel,
}),
},
{
address: kandel,
Expand Down
2 changes: 0 additions & 2 deletions src/actions/order/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
export type {
GetLimitOrderStepsParams,
GetLimitOrderStepsArgs,
GetUserRouterArgs,
SimulateRawLimitOrderArgs,
SimulateLimitOrderArgs,
SimulateLimitOrderResult,
} from './new.js'

export {
getLimitOrderSteps,
getUserRouter,
simulateRawLimitOrder,
simulateLimitOrder,
} from './new.js'
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 @@ -13,7 +13,8 @@ import { tickFromVolumes } from '~mgv/lib/tick.js'
import { getClient } from '~test/src/client.js'
import { BS, Order } from '../../lib/enums.js'
import { getBook } from '../book.js'
import { getLimitOrderSteps, getUserRouter, simulateLimitOrder } from './new.js'
import { getUserRouter } from '../smart-router.js'
import { getLimitOrderSteps, simulateLimitOrder } from './new.js'

const params = inject('mangrove')
const { wethUSDC } = inject('markets')
Expand Down
21 changes: 0 additions & 21 deletions src/actions/order/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import {
} from 'viem'
import { readContract, simulateContract } from 'viem/actions'
import {
type GetUserRouterParams,
type LimitOrderParams,
type RawLimitOrderParams,
getUserRouterParams,
type limitOrderABI,
limitOrderParams,
rawLimitOrderParams,
Expand Down Expand Up @@ -84,25 +82,6 @@ export async function getLimitOrderSteps(
]
}

export type GetUserRouterArgs = GetUserRouterParams &
Omit<ReadContractParameters<typeof limitOrderABI, 'router'>, BuiltArgs>

export async function getUserRouter(
client: Client,
actionParams: MangroveActionsDefaultParams,
args: GetUserRouterArgs,
) {
return getAction(
client,
readContract,
'readContract',
)({
...getUserRouterParams(args),
address: actionParams.mgvOrder,
...args,
})
}

type LimitOrderSimulationParams = SimulationParams<typeof limitOrderABI, 'take'>

export type SimulateRawLimitOrderArgs = Prettify<
Expand Down
88 changes: 88 additions & 0 deletions src/actions/smart-router.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { isAddress } from 'viem'
import { describe, expect, inject, it } from 'vitest'
import { getClient } from '~test/src/client.js'
import { adminParams } from '../builder/smart-router.js'
import {
getUserRouter,
isBound,
simulateBind,
simulateDeployRouter,
} from './smart-router.js'

const client = getClient()
const params = inject('mangrove')

describe('smart router', () => {
it('gets router address', async () => {
const router = await getUserRouter(client, params, {
user: client.account.address,
})
expect(isAddress(router)).toBeTruthy()
})

it('deploys router', async () => {
const { router, created, request } = await simulateDeployRouter(
client,
params,
{
user: client.account.address,
},
)

const routerExpected = await getUserRouter(client, params, {
user: client.account.address,
})

expect(isAddress(router)).toBeTruthy()
expect(created).toBeTruthy()
expect(router).toAddressEqual(routerExpected)

const tx = await client.writeContract(request)
await client.waitForTransactionReceipt({
hash: tx,
})

const admin = await client.readContract({
address: router,
...adminParams,
})

expect(admin).toAddressEqual(client.account.address)
})

it('binds router', async () => {
const { request, router } = await simulateDeployRouter(client, params, {
user: client.account.address,
})

let tx = await client.writeContract(request)
await client.waitForTransactionReceipt({
hash: tx,
})

const isBoundToMangroveOrder = await isBound(client, router, {
maker: params.mgvOrder,
})

expect(isBoundToMangroveOrder).toBeTruthy()

const isBoundToMangrove = await isBound(client, router, {
maker: params.mgv,
})

expect(isBoundToMangrove).toBeFalsy()

const { request: request2 } = await simulateBind(client, router, {
target: params.mgv,
})
tx = await client.writeContract(request2)
await client.waitForTransactionReceipt({
hash: tx,
})

const isBoundToMangroveAfter = await isBound(client, router, {
maker: params.mgv,
})
expect(isBoundToMangroveAfter).toBeTruthy()
})
})
129 changes: 129 additions & 0 deletions src/actions/smart-router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import type { Address, Client, SimulateContractReturnType } from 'viem'
import {
type ReadContractParameters,
readContract,
simulateContract,
} from 'viem/actions'
import {
type BindParams,
type DeployRouterParams,
type GetUserRouterParams,
type IsBoundParams,
bindParams,
deployRouterParams,
getUserRouterParams,
isBoundParams,
type routerProxyFactoryABI,
type smartRouterABI,
} from '../builder/smart-router.js'
import type {
BuiltArgs,
MangroveActionsDefaultParams,
} from '../types/actions/index.js'
import type { SimulationParams } from '../types/actions/simulation.js'
import { getAction } from '../utils/getAction.js'

export type GetUserRouterArgs = GetUserRouterParams &
Omit<
ReadContractParameters<typeof routerProxyFactoryABI, 'computeProxyAddress'>,
BuiltArgs
>

export async function getUserRouter(
client: Client,
actionParams: MangroveActionsDefaultParams,
args: GetUserRouterArgs,
) {
return getAction(
client,
readContract,
'readContract',
)({
...getUserRouterParams(actionParams, args),
address: actionParams.routerProxyFactory,
...args,
})
}

export type IsBoundArgs = IsBoundParams &
Omit<ReadContractParameters<typeof smartRouterABI, 'isBound'>, BuiltArgs>

export async function isBound(
client: Client,
router: Address,
args: IsBoundArgs,
) {
return getAction(
client,
readContract,
'readContract',
)({
...args,
...isBoundParams(args),
address: router,
})
}

type DeployRouterSimulationParams = SimulationParams<
typeof routerProxyFactoryABI,
'instantiate'
>

export type DeployRouterArgs = DeployRouterParams &
Omit<DeployRouterSimulationParams, BuiltArgs>

export type DeployRouterResult = {
router: Address
created: boolean
request: SimulateContractReturnType<
typeof routerProxyFactoryABI,
'instantiate'
>['request']
}

export async function simulateDeployRouter(
client: Client,
actionParams: MangroveActionsDefaultParams,
args: DeployRouterArgs,
) {
const {
request,
result: [router, created],
} = await getAction(
client,
simulateContract,
'simulateContract',
)({
...(args as unknown as DeployRouterSimulationParams),
address: actionParams.routerProxyFactory,
...deployRouterParams(actionParams, args),
})
return {
router,
created,
request,
}
}

type BindSimulationParams = SimulationParams<typeof smartRouterABI, 'bind'>
export type BindArgs = BindParams & Omit<BindSimulationParams, BuiltArgs>
export type BindResult = SimulateContractReturnType<
typeof smartRouterABI,
'bind'
>

export async function simulateBind(
client: Client,
router: Address,
args: BindArgs,
): Promise<BindResult> {
return getAction(
client,
simulateContract,
'simulateContract',
)({
...(args as unknown as BindSimulationParams),
address: router,
...bindParams(args),
})
}
2 changes: 2 additions & 0 deletions src/addresses/mangrove/blast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export const blastMangrove = {
mgv: '0xb1a49C54192Ea59B233200eA38aB56650Dfb448C',
mgvOrder: '0x83251E7F36a51c5238C9aa0c6Bb7cc209b32d80e',
mgvReader: '0x26fD9643Baf1f8A44b752B28f0D90AEBd04AB3F8',
smartRouter: '0x270C0Bc25795DceF15Bb6D374670eBe9eCf76998',
routerProxyFactory: '0x79C3F23F210e30F6AA9072F0ddFd2D180C8B7131',
} as const satisfies MangroveActionsDefaultParams
Loading

0 comments on commit a459e3a

Please sign in to comment.