From 66994d1252f73993ff4af4bb58c6d88ece25877f Mon Sep 17 00:00:00 2001 From: Maxence Raballand Date: Mon, 22 Jul 2024 17:11:25 +0200 Subject: [PATCH] Add kandel steps for classic kandel (#113) * feat: Add get steps for classic kandel * chore: lint and format * chore: changeset --- .changeset/angry-coats-dress.md | 5 ++ src/actions/kandel/steps.ts | 102 +++++++++++++++++++++++++++- src/bundle/public/kandel-actions.ts | 13 ++++ src/index.ts | 1 + src/types/actions/index.ts | 1 + src/types/actions/steps.ts | 8 +++ src/types/index.ts | 1 + 7 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 .changeset/angry-coats-dress.md diff --git a/.changeset/angry-coats-dress.md b/.changeset/angry-coats-dress.md new file mode 100644 index 0000000..3e06a52 --- /dev/null +++ b/.changeset/angry-coats-dress.md @@ -0,0 +1,5 @@ +--- +"@mangrovedao/mgv": minor +--- + +Add get kandel steps for classic kandel diff --git a/src/actions/kandel/steps.ts b/src/actions/kandel/steps.ts index 328e9e1..245ad33 100644 --- a/src/actions/kandel/steps.ts +++ b/src/actions/kandel/steps.ts @@ -10,9 +10,12 @@ import { 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 type { + KandelSteps, + MarketParams, + SmartKandelSteps, +} from '../../index.js' import { getKandelGasReq } from '../../lib/kandel/params.js' import { getAction } from '../../utils/getAction.js' import type { OverlyingResult } from '../balances.js' @@ -25,7 +28,6 @@ import type { OverlyingResult } from '../balances.js' // => populate the kandel export type GetKandelStepsParams = { - userRouter: Address user: Address baseOverlying?: OverlyingResult | undefined quoteOverlying?: OverlyingResult | undefined @@ -40,6 +42,100 @@ export async function getKandelSteps( kandel: Address, args: GetKandelStepsArgs, ): Promise { + const [baseAllowance, quoteAllowance] = await getAction( + client, + multicall, + 'multicall', + )({ + ...args, + contracts: [ + { + ...tokenAllowanceParams({ + owner: args.user, + spender: kandel, + token: + args.baseOverlying?.available && args.baseOverlying.overlying + ? args.baseOverlying.overlying.address + : market.base.address, + }), + }, + { + ...tokenAllowanceParams({ + owner: args.user, + spender: kandel, + token: + args.quoteOverlying?.available && args.quoteOverlying.overlying + ? args.quoteOverlying.overlying.address + : market.quote.address, + }), + }, + ], + allowFailure: true, + }) + + return [ + { + type: 'sowKandel', + params: { + market, + }, + done: !isAddressEqual(kandel, zeroAddress), + }, + { + type: 'erc20Approval', + params: { + token: + args.baseOverlying?.available && args.baseOverlying.overlying + ? args.baseOverlying.overlying + : market.base, + from: args.user, + spender: kandel, + amount: maxUint256, + }, + done: + baseAllowance.status === 'success' && baseAllowance.result > maxUint128, + }, + { + type: 'erc20Approval', + params: { + token: + args.quoteOverlying?.available && args.quoteOverlying.overlying + ? args.quoteOverlying.overlying + : market.quote, + from: args.user, + spender: kandel, + amount: maxUint256, + }, + done: + quoteAllowance.status === 'success' && + quoteAllowance.result > maxUint128, + }, + { + type: 'populate', + params: { + gasreq: 128_000n, + }, + done: false, + }, + ] +} + +export type GetSmartKandelStepsParams = { + userRouter: Address + user: Address + baseOverlying?: OverlyingResult | undefined + quoteOverlying?: OverlyingResult | undefined +} + +export type GetSmartKandelStepsArgs = GetSmartKandelStepsParams & + Omit + +export async function getSmartKandelSteps( + client: Client, + market: MarketParams, + kandel: Address, + args: GetSmartKandelStepsArgs, +): Promise { const [admin, bound, logics, /*params,*/ baseAllowance, quoteAllowance] = await getAction( client, diff --git a/src/bundle/public/kandel-actions.ts b/src/bundle/public/kandel-actions.ts index 6fa28d9..38898b9 100644 --- a/src/bundle/public/kandel-actions.ts +++ b/src/bundle/public/kandel-actions.ts @@ -24,7 +24,9 @@ import { } from '../../actions/kandel/sow.js' import { type GetKandelStepsArgs, + type GetSmartKandelStepsArgs, getKandelSteps, + getSmartKandelSteps, } from '../../actions/kandel/steps.js' import { type GetKandelStateArgs, @@ -35,10 +37,14 @@ import type { KandelSteps, MangroveActionsDefaultParams, MarketParams, + SmartKandelSteps, } from '../../index.js' export type KandelSeederActions = { getKandelSteps: (args: GetKandelStepsArgs) => Promise + getSmartKandelSteps: ( + args: GetSmartKandelStepsArgs, + ) => Promise simulateSow: (args?: SowArgs | undefined) => Promise } @@ -46,6 +52,8 @@ export function kandelSeederActions(market: MarketParams, seeder: Address) { return (client: Client): KandelSeederActions => ({ getKandelSteps: (args: GetKandelStepsArgs) => getKandelSteps(client, market, zeroAddress, args), + getSmartKandelSteps: (args: GetSmartKandelStepsArgs) => + getSmartKandelSteps(client, market, zeroAddress, args), simulateSow: (args?: SowArgs | undefined) => simulateSow(client, market, seeder, args), }) @@ -53,6 +61,9 @@ export function kandelSeederActions(market: MarketParams, seeder: Address) { export type KandelActions = { getKandelSteps: (args: GetKandelStepsArgs) => Promise + getSmartKandelSteps: ( + args: GetSmartKandelStepsArgs, + ) => Promise simulateSetLogics: (args: SetLogicsArgs) => Promise simulatePopulate: (args: PopulateArgs) => Promise simulatePopulateChunk: ( @@ -69,6 +80,8 @@ export function kandelActions( ) { return (client: Client): KandelActions => ({ getKandelSteps: (args) => getKandelSteps(client, market, kandel, args), + getSmartKandelSteps: (args) => + getSmartKandelSteps(client, market, kandel, args), simulateSetLogics: (args) => simulateSetLogics(client, kandel, args), simulatePopulate: (args) => simulatePopulate(client, kandel, args), simulatePopulateChunk: (args) => diff --git a/src/index.ts b/src/index.ts index 0028da7..eb432d3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -100,6 +100,7 @@ export type { NewOfferSteps, AmplifiedOrderSteps, KandelSteps, + SmartKandelSteps, GlobalConfig, LocalConfig, CompleteToken, diff --git a/src/types/actions/index.ts b/src/types/actions/index.ts index a53708c..ceefa91 100644 --- a/src/types/actions/index.ts +++ b/src/types/actions/index.ts @@ -65,4 +65,5 @@ export type { NewOfferSteps, AmplifiedOrderSteps, KandelSteps, + SmartKandelSteps, } from './steps.js' diff --git a/src/types/actions/steps.ts b/src/types/actions/steps.ts index 91bd29a..62c1a44 100644 --- a/src/types/actions/steps.ts +++ b/src/types/actions/steps.ts @@ -47,7 +47,15 @@ export type AmplifiedOrderSteps = readonly [ ERC20ApprovalStep, ERC20ApprovalStep, ] + export type KandelSteps = readonly [ + SowKandelStep, + ERC20ApprovalStep, + ERC20ApprovalStep, + PopulateKandelSteps, +] + +export type SmartKandelSteps = readonly [ SowKandelStep, DeployRouterStep, BindStep, diff --git a/src/types/index.ts b/src/types/index.ts index fbc18de..b9d171b 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -14,6 +14,7 @@ export type { NewOfferSteps, AmplifiedOrderSteps, KandelSteps, + SmartKandelSteps, } from './actions/index.js' export type {