Skip to content

Commit

Permalink
Feat/maxence/aave check (#124)
Browse files Browse the repository at this point in the history
* feat: test aave markets

* chore: changeset

* chore: format

---------

Co-authored-by: maxencerb <[email protected]>
  • Loading branch information
maxencerb and maxencerb authored Aug 14, 2024
1 parent e6a3c42 commit f838392
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-trees-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": patch
---

Added check for aave markets
114 changes: 114 additions & 0 deletions src/actions/kandel/aave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import type {
Address,
Client,
MulticallParameters,
ReadContractParameters,
} from 'viem'
import { multicall, readContract } from 'viem/actions'
import {
type CheckAaveAssetParams,
type aaveRouterCheckAssetABI,
checkAaveAssetParams,
} from '../../builder/kandel/aave.js'
import type { BuiltArgs, MarketParams } from '../../index.js'
import { getAction } from '../../utils/getAction.js'

type ReadSingleParams = ReadContractParameters<
typeof aaveRouterCheckAssetABI,
'checkAsset'
>

export type CheckAaveAssetArgs = CheckAaveAssetParams &
Omit<ReadSingleParams, BuiltArgs>

export async function checkAaveAsset(
client: Client,
aaveRouter: Address,
args: CheckAaveAssetArgs,
): Promise<boolean> {
return getAction(
client,
readContract,
'readContract',
)({
...(args as unknown as ReadSingleParams),
address: aaveRouter,
...checkAaveAssetParams(args),
})
}

export type CheckAaveAssetsArgs = { tokens: Address[] } & Omit<
MulticallParameters,
'contracts' | 'allowFailure'
>

export async function checkAaveAssets(
client: Client,
aaveRouter: Address,
args: CheckAaveAssetsArgs,
): Promise<boolean[]> {
return getAction(
client,
multicall,
'multicall',
)({
...args,
contracts: args.tokens.map((token) => ({
address: aaveRouter,
...checkAaveAssetParams({ token }),
})),
allowFailure: false,
})
}

export type CheckAaveMarketArgs = { market: MarketParams } & Omit<
MulticallParameters,
'contracts' | 'allowFailure'
>

export async function checkAaveMarket(
client: Client,
aaveRouter: Address,
args: CheckAaveMarketArgs,
): Promise<boolean> {
const tokens = [args.market.base.address, args.market.quote.address]
const available = await checkAaveAssets(client, aaveRouter, {
...args,
tokens,
})
return available.every((a) => a)
}

export type CheckAaveMarketsArgs = { markets: MarketParams[] } & Omit<
MulticallParameters,
'contracts' | 'allowFailure'
>

export async function checkAaveMarkets(
client: Client,
aaveRouter: Address,
args: CheckAaveMarketsArgs,
): Promise<MarketParams[]> {
const tokens = [
...new Set(
args.markets.flatMap((m) => [
m.base.address.toLowerCase() as Address,
m.quote.address.toLowerCase() as Address,
]),
),
]
const available = await checkAaveAssets(client, aaveRouter, {
...args,
tokens,
})
const tokensMap = tokens.reduce((acc, token, i) => {
acc.set(token, available[i] || false)
return acc
}, new Map<Address, boolean>())

return args.markets.filter(
(m) =>
tokensMap.get(m.base.address.toLowerCase() as Address) &&
tokensMap.get(m.quote.address.toLowerCase() as Address),
)
}
24 changes: 24 additions & 0 deletions src/builder/kandel/aave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { type Address, type ContractFunctionParameters, parseAbi } from 'viem'

export type CheckAaveAssetParams = {
token: Address
}

export const aaveRouterCheckAssetABI = parseAbi([
'function checkAsset(address token) public view returns (bool)',
])

export function checkAaveAssetParams(params: CheckAaveAssetParams) {
return {
abi: aaveRouterCheckAssetABI,
functionName: 'checkAsset',
args: [params.token],
} satisfies Omit<
ContractFunctionParameters<
typeof aaveRouterCheckAssetABI,
'view',
'checkAsset'
>,
'address'
>
}
2 changes: 2 additions & 0 deletions src/bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type {
KandelActions,
KandelSeederActions,
UserRouterActions,
AaveKandelActions,
} from './public/index.js'

export {
Expand All @@ -14,4 +15,5 @@ export {
kandelActions,
kandelSeederActions,
userRouterActions,
aaveKandelActions,
} from './public/index.js'
2 changes: 2 additions & 0 deletions src/bundle/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ export { mangroveActions, type MangroveActions } from './mangrove-actions.js'
export {
kandelActions,
kandelSeederActions,
aaveKandelActions,
type KandelActions,
type KandelSeederActions,
type AaveKandelActions,
} from './kandel-actions.js'
export {
type UserRouterActions,
Expand Down
26 changes: 26 additions & 0 deletions src/bundle/public/kandel-actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { type Address, type Client, zeroAddress } from 'viem'
import {
type CheckAaveAssetArgs,
type CheckAaveAssetsArgs,
type CheckAaveMarketArgs,
type CheckAaveMarketsArgs,
checkAaveAsset,
checkAaveAssets,
checkAaveMarket,
checkAaveMarkets,
} from '../../actions/kandel/aave.js'
import {
type SetLogicsArgs,
type SetLogicsResult,
Expand Down Expand Up @@ -91,3 +101,19 @@ export function kandelActions(
getKandelState(client, actionParams, market, kandel, args),
})
}

export type AaveKandelActions = {
checkAaveAsset: (args: CheckAaveAssetArgs) => Promise<boolean>
checkAaveAssets: (args: CheckAaveAssetsArgs) => Promise<boolean[]>
checkAaveMarket: (args: CheckAaveMarketArgs) => Promise<boolean>
checkAaveMarkets: (args: CheckAaveMarketsArgs) => Promise<MarketParams[]>
}

export function aaveKandelActions(aaveRouter: Address) {
return (client: Client): AaveKandelActions => ({
checkAaveAsset: (args) => checkAaveAsset(client, aaveRouter, args),
checkAaveAssets: (args) => checkAaveAssets(client, aaveRouter, args),
checkAaveMarket: (args) => checkAaveMarket(client, aaveRouter, args),
checkAaveMarkets: (args) => checkAaveMarkets(client, aaveRouter, args),
})
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export type {
KandelActions,
KandelSeederActions,
UserRouterActions,
AaveKandelActions,
} from './bundle/index.js'

export {
Expand All @@ -129,6 +130,7 @@ export {
kandelActions,
kandelSeederActions,
userRouterActions,
aaveKandelActions,
} from './bundle/index.js'

// --- addresses ---
Expand Down

0 comments on commit f838392

Please sign in to comment.