Skip to content

Commit

Permalink
feat: Adapter, Retro (#763)
Browse files Browse the repository at this point in the history
* feat: Adapter, Retro [WIP]

* Unwrap Underlyings

* remove comment
  • Loading branch information
0xpeluche authored Aug 21, 2023
1 parent 813e4fe commit ecabfbb
Show file tree
Hide file tree
Showing 5 changed files with 151 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 @@ -229,6 +229,7 @@
"redacted",
"reflexer",
"reserve",
"retro",
"ribbon-finance",
"rocket-pool",
"rook",
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ import railgun from '@adapters/railgun'
import redacted from '@adapters/redacted'
import reflexer from '@adapters/reflexer'
import reserve from '@adapters/reserve'
import retro from '@adapters/retro'
import ribbonFinance from '@adapters/ribbon-finance'
import rocketPool from '@adapters/rocket-pool'
import rook from '@adapters/rook'
Expand Down Expand Up @@ -395,6 +396,7 @@ export const adapters: Adapter[] = [
redacted,
reflexer,
reserve,
retro,
ribbonFinance,
rocketPool,
rook,
Expand Down
10 changes: 10 additions & 0 deletions src/adapters/retro/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Adapter } from '@lib/adapter'

import * as polygon from './polygon'

const adapter: Adapter = {
id: 'retro',
polygon,
}

export default adapter
109 changes: 109 additions & 0 deletions src/adapters/retro/polygon/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { getPoolsBalances } from '@adapters/uniswap-v3/common/pools'
import type { Balance, BalancesContext, Contract, FarmBalance } from '@lib/adapter'
import { mapSuccessFilter } from '@lib/array'
import { multicall } from '@lib/multicall'
import type { Token } from '@lib/token'

const abi = {
calculateRedeemOutputs: {
inputs: [{ internalType: 'uint256', name: '_amount', type: 'uint256' }],
name: 'calculateRedeemOutputs',
outputs: [{ internalType: 'uint256[]', name: '', type: 'uint256[]' }],
stateMutability: 'view',
type: 'function',
},
} as const

const CASH: Token = {
chain: 'polygon',
address: '0x5d066d022ede10efa2717ed3d79f22f949f8c175',
symbol: 'CASH',
decimals: 18,
}

const redeemableContract: Contract = {
chain: 'polygon',
address: '0x2d62f6d8288994c7900e9c359f8a72e84d17bfba',
}

const DAI: Token = {
chain: 'polygon',
address: '0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063',
decimals: 18,
symbol: 'DAI',
}

const USDT: Token = {
chain: 'polygon',
address: '0xc2132D05D31c914a87C6611C10748AEb04B58e8F',
decimals: 6,
symbol: 'USDT',
}

const USDC: Token = {
chain: 'polygon',
address: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
decimals: 6,
symbol: 'USDC',
}

interface CashBalances extends FarmBalance {
cashAmount?: bigint
}

export async function getRetroBalances(
ctx: BalancesContext,
nonFungiblePositionManager: Contract,
factory: Contract,
): Promise<Balance[]> {
const balances: Balance[] = await getPoolsBalances(ctx, nonFungiblePositionManager, factory)
const fmtBalances: CashBalances[] = []

let i = 0
while (i < balances.length) {
const balance = balances[i]

if (balance.underlyings && balance.underlyings.length > 0) {
for (const underlying of balance.underlyings) {
if (underlying.address.toLowerCase() === CASH.address) {
fmtBalances.push({ ...(balance as CashBalances), cashAmount: (underlying as Contract).amount })
balances.splice(i, 1)
i--
break
}
}
}
i++
}

const underlyingsBalancesRes = await multicall({
ctx,
calls: fmtBalances.map(
(balance) => ({ target: redeemableContract.address, params: [balance.cashAmount!] }) as const,
),
abi: abi.calculateRedeemOutputs,
})

const fmtUnderlyings = (output: readonly bigint[]) => {
const tokens = [DAI, USDT, USDC]

return output.map((amount, index) => ({
...tokens[index],
amount,
}))
}

const fmtCash: Contract[][] = mapSuccessFilter(underlyingsBalancesRes, (res) => fmtUnderlyings(res.output))

for (const [index, fmtBalance] of fmtBalances.entries()) {
const fmtUnderlyingsWithoutCash = fmtBalance.underlyings?.filter(
(underlying) => underlying.address.toLowerCase() !== CASH.address,
)

if (fmtUnderlyingsWithoutCash) {
fmtBalance.underlyings = [...fmtUnderlyingsWithoutCash, ...fmtCash[index]]
}
}

return [...balances, ...fmtBalances]
}
29 changes: 29 additions & 0 deletions src/adapters/retro/polygon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getRetroBalances } from '@adapters/retro/polygon/balance'
import type { Contract, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

export const factory: Contract = {
chain: 'ethereum',
address: '0x91e1b99072f238352f59e58de875691e20dc19c1',
}

export const nonFungiblePositionManager: Contract = {
chain: 'ethereum',
address: '0x8aac493fd8c78536ef193882aeffeaa3e0b8b5c5',
}

export const getContracts = async () => {
return {
contracts: { nonFungiblePositionManager },
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
nonFungiblePositionManager: (...args) => getRetroBalances(...args, factory),
})

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

0 comments on commit ecabfbb

Please sign in to comment.