Skip to content

Commit

Permalink
feat:Adapter,Karak (#1399)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpeluche authored Apr 12, 2024
1 parent 25842f5 commit 0869d5d
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
"izumi-finance",
"jaypeggers",
"jones-dao",
"karak",
"keep3r-network",
"kelp-dao",
"keom-protocol",
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ import ironBank from '@adapters/iron-bank'
import izumiFinance from '@adapters/izumi-finance'
import jaypeggers from '@adapters/jaypeggers'
import jonesDao from '@adapters/jones-dao'
import karak from '@adapters/karak'
import keep3rNetwork from '@adapters/keep3r-network'
import kelpDao from '@adapters/kelp-dao'
import keomProtocol from '@adapters/keom-protocol'
Expand Down Expand Up @@ -608,6 +609,7 @@ export const adapters: Adapter[] = [
izumiFinance,
jaypeggers,
jonesDao,
karak,
keep3rNetwork,
kelpDao,
keomProtocol,
Expand Down
30 changes: 30 additions & 0 deletions src/adapters/karak/arbitrum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getKarakStakeBalances } from '@adapters/karak/common/balance'
import { getKarakTokens } from '@adapters/karak/common/contract'
import type { AdapterConfig, BaseContext, Contract, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

const supervisor: Contract = {
chain: 'arbitrum',
address: '0x399f22ae52a18382a67542b3de9bed52b7b9a4ad',
}

export const getContracts = async (ctx: BaseContext) => {
const staker = await getKarakTokens(ctx, supervisor)
return {
contracts: { staker },
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
staker: getKarakStakeBalances,
})

return {
groups: [{ balances }],
}
}

export const config: AdapterConfig = {
startDate: 1712620800,
}
47 changes: 47 additions & 0 deletions src/adapters/karak/common/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { Balance, BalancesContext, Contract } from '@lib/adapter'
import { call } from '@lib/call'
import type { Category } from '@lib/category'
import { isNotNullish } from '@lib/type'

const abi = {
getDeposits: {
inputs: [{ internalType: 'address', name: 'staker', type: 'address' }],
name: 'getDeposits',
outputs: [
{ internalType: 'contract IVault[]', name: 'vaults', type: 'address[]' },
{ internalType: 'contract IERC20[]', name: 'tokens', type: 'address[]' },
{ internalType: 'uint256[]', name: 'assets', type: 'uint256[]' },
{ internalType: 'uint256[]', name: 'shares', type: 'uint256[]' },
],
stateMutability: 'view',
type: 'function',
},
} as const

export async function getKarakStakeBalances(ctx: BalancesContext, staker: Contract): Promise<Balance[]> {
const underlyings = staker.underlyings as Contract[]
if (!underlyings) return []

const [_vaults, tokens, assets, _shares] = await call({
ctx,
target: staker.address,
params: [ctx.address],
abi: abi.getDeposits,
})

return tokens
.map((token, index) => {
const amount = assets[index]
const underlying = underlyings.find((underlying) => underlying.address.toLowerCase() === token.toLowerCase())
if (!underlying) return null

return {
...underlying,
amount,
underlyings: undefined,
rewards: undefined,
category: 'stake' as Category,
}
})
.filter(isNotNullish)
}
36 changes: 36 additions & 0 deletions src/adapters/karak/common/contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { BaseContext, Contract } from '@lib/adapter'
import { mapSuccessFilter } from '@lib/array'
import { call } from '@lib/call'
import { multicall } from '@lib/multicall'

const abi = {
depositToken: {
inputs: [],
name: 'depositToken',
outputs: [{ internalType: 'contract IERC20', name: '', type: 'address' }],
stateMutability: 'view',
type: 'function',
},
asset: {
inputs: [],
name: 'asset',
outputs: [{ internalType: 'address', name: '', type: 'address' }],
stateMutability: 'view',
type: 'function',
},
getVaults: {
inputs: [],
name: 'getVaults',
outputs: [{ internalType: 'contract IVault[]', name: '', type: 'address[]' }],
stateMutability: 'view',
type: 'function',
},
} as const

export async function getKarakTokens(ctx: BaseContext, supervisor: Contract): Promise<Contract> {
const vaults = await call({ ctx, target: supervisor.address, abi: abi.getVaults })
const tokens = await multicall({ ctx, calls: vaults.map((vault) => ({ target: vault })), abi: abi.depositToken })

const underlyings = mapSuccessFilter(tokens, (res) => res.output)
return { ...supervisor, underlyings }
}
30 changes: 30 additions & 0 deletions src/adapters/karak/ethereum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getKarakStakeBalances } from '@adapters/karak/common/balance'
import { getKarakTokens } from '@adapters/karak/common/contract'
import type { AdapterConfig, BaseContext, Contract, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

const supervisor: Contract = {
chain: 'ethereum',
address: '0x54e44dbb92dba848ace27f44c0cb4268981ef1cc',
}

export const getContracts = async (ctx: BaseContext) => {
const staker = await getKarakTokens(ctx, supervisor)
return {
contracts: { staker },
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
staker: getKarakStakeBalances,
})

return {
groups: [{ balances }],
}
}

export const config: AdapterConfig = {
startDate: 1712620800,
}
12 changes: 12 additions & 0 deletions src/adapters/karak/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Adapter } from '@lib/adapter'

import * as ethereum from './ethereum'
import * as arbitrum from './arbitrum'

const adapter: Adapter = {
id: 'karak',
ethereum: ethereum,
arbitrum: arbitrum,
}

export default adapter

0 comments on commit 0869d5d

Please sign in to comment.