Skip to content

Commit

Permalink
Add kandel steps for classic kandel (#113)
Browse files Browse the repository at this point in the history
* feat: Add get steps for classic kandel

* chore: lint and format

* chore: changeset
  • Loading branch information
maxencerb authored Jul 22, 2024
1 parent f2b7ad7 commit 66994d1
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-coats-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": minor
---

Add get kandel steps for classic kandel
102 changes: 99 additions & 3 deletions src/actions/kandel/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -40,6 +42,100 @@ export async function getKandelSteps(
kandel: Address,
args: GetKandelStepsArgs,
): Promise<KandelSteps> {
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<MulticallParameters, 'contracts' | 'allowFailure'>

export async function getSmartKandelSteps(
client: Client,
market: MarketParams,
kandel: Address,
args: GetSmartKandelStepsArgs,
): Promise<SmartKandelSteps> {
const [admin, bound, logics, /*params,*/ baseAllowance, quoteAllowance] =
await getAction(
client,
Expand Down
13 changes: 13 additions & 0 deletions src/bundle/public/kandel-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,24 +37,33 @@ import type {
KandelSteps,
MangroveActionsDefaultParams,
MarketParams,
SmartKandelSteps,
} from '../../index.js'

export type KandelSeederActions = {
getKandelSteps: (args: GetKandelStepsArgs) => Promise<KandelSteps>
getSmartKandelSteps: (
args: GetSmartKandelStepsArgs,
) => Promise<SmartKandelSteps>
simulateSow: (args?: SowArgs | undefined) => Promise<SimulateSowResult>
}

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),
})
}

export type KandelActions = {
getKandelSteps: (args: GetKandelStepsArgs) => Promise<KandelSteps>
getSmartKandelSteps: (
args: GetSmartKandelStepsArgs,
) => Promise<SmartKandelSteps>
simulateSetLogics: (args: SetLogicsArgs) => Promise<SetLogicsResult>
simulatePopulate: (args: PopulateArgs) => Promise<PopulateResult>
simulatePopulateChunk: (
Expand All @@ -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) =>
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export type {
NewOfferSteps,
AmplifiedOrderSteps,
KandelSteps,
SmartKandelSteps,
GlobalConfig,
LocalConfig,
CompleteToken,
Expand Down
1 change: 1 addition & 0 deletions src/types/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ export type {
NewOfferSteps,
AmplifiedOrderSteps,
KandelSteps,
SmartKandelSteps,
} from './steps.js'
8 changes: 8 additions & 0 deletions src/types/actions/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type {
NewOfferSteps,
AmplifiedOrderSteps,
KandelSteps,
SmartKandelSteps,
} from './actions/index.js'

export type {
Expand Down

0 comments on commit 66994d1

Please sign in to comment.