Skip to content

Commit

Permalink
feat: Adapter, Reflexer
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpeluche committed Aug 13, 2023
1 parent 478c9ee commit 692ca43
Show file tree
Hide file tree
Showing 6 changed files with 156 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 @@ -222,6 +222,7 @@
"rage-trade",
"railgun",
"redacted",
"reflexer",
"reserve",
"ribbon-finance",
"rocket-pool",
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ import raft from '@adapters/raft'
import rageTrade from '@adapters/rage-trade'
import railgun from '@adapters/railgun'
import redacted from '@adapters/redacted'
import reflexer from '@adapters/reflexer'
import reserve from '@adapters/reserve'
import ribbonFinance from '@adapters/ribbon-finance'
import rocketPool from '@adapters/rocket-pool'
Expand Down Expand Up @@ -381,6 +382,7 @@ export const adapters: Adapter[] = [
rageTrade,
railgun,
redacted,
reflexer,
reserve,
ribbonFinance,
rocketPool,
Expand Down
80 changes: 80 additions & 0 deletions src/adapters/reflexer/ethereum/farm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import type { Balance, BalancesContext, BorrowBalance, LendBalance } from '@lib/adapter'
import type { Token } from '@lib/token'
import request, { gql } from 'graphql-request'

const WETH: Token = {
chain: 'ethereum',
address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
decimals: 18,
symbol: 'WETH',
}

const RAI: Token = {
chain: 'ethereum',
address: '0x03ab458634910aad20ef5f1c8ee96f1d6ac54919',
decimals: 18,
symbol: 'RAI',
}

interface ReflexerLendBalancesParams extends LendBalance {
proxy: `0x${string}`
safe: { safeHandler: `0x${string}`; safeId: number }
}

interface ReflexerBorrowBalancesParams extends BorrowBalance {
proxy: `0x${string}`
safe: { safeHandler: `0x${string}`; safeId: number }
}

const url = 'https://api.thegraph.com/subgraphs/name/reflexer-labs/rai-mainnet'

export async function getReflexerFarmBalancesWithProxies(ctx: BalancesContext): Promise<Balance[]> {
const query = gql`
query proxies {
user(id: "${ctx.address}") {
id
proxies {
id
}
safes {
safeHandler
safeId
debt
collateral
}
}
}
`

const res: any = await request(url, query)

const balances: Balance[] = res.user.safes.flatMap((safe: any) => {
const { safeHandler, safeId, collateral, debt } = safe

const lend: ReflexerLendBalancesParams = {
chain: 'ethereum',
address: WETH.address,
proxy: res.user.proxies[0].id,
safe: { safeHandler, safeId },
amount: BigInt(Math.round(collateral * Math.pow(10, WETH.decimals))),
underlyings: undefined,
rewards: undefined,
category: 'lend',
}

const borrow: ReflexerBorrowBalancesParams = {
chain: 'ethereum',
address: RAI.address,
proxy: res.user.proxies[0].id,
safe: { safeHandler, safeId },
amount: BigInt(Math.round(debt * Math.pow(10, RAI.decimals))),
underlyings: undefined,
rewards: undefined,
category: 'borrow',
}

return [lend, borrow]
})

return balances
}
35 changes: 35 additions & 0 deletions src/adapters/reflexer/ethereum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getReflexerFarmBalancesWithProxies } from '@adapters/reflexer/ethereum/farm'
import { getReflexerStakeBalances } from '@adapters/reflexer/ethereum/stake'
import type { BaseContext, Contract, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'

const RAI: Contract = {
chain: 'ethereum',
address: '0x03ab458634910aad20ef5f1c8ee96f1d6ac54919',
decimals: 18,
symbol: 'RAI',
}

const FLX_ETH: Contract = {
chain: 'ethereum',
address: '0x69c6c08b91010c88c95775b6fd768e5b04efc106',
token: '0xd6F3768E62Ef92a9798E5A8cEdD2b78907cEceF9',
underlyings: ['0x6243d8CEA23066d098a15582d81a598b4e8391F4', '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'],
}

export const getContracts = async (_ctx: BaseContext) => {
return {
contracts: { FLX_ETH, RAI },
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
FLX_ETH: getReflexerStakeBalances,
RAI: getReflexerFarmBalancesWithProxies,
})

return {
groups: [{ balances }],
}
}
28 changes: 28 additions & 0 deletions src/adapters/reflexer/ethereum/stake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Balance, BalancesContext, Contract } from '@lib/adapter'
import { call } from '@lib/call'
import { getUnderlyingBalances } from '@lib/uniswap/v2/pair'

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

export async function getReflexerStakeBalances(ctx: BalancesContext, staker: Contract): Promise<Balance[]> {
const userBalance = await call({ ctx, target: staker.address, params: [ctx.address], abi: abi.descendantBalanceOf })

return getUnderlyingBalances(ctx, [
{
...staker,
address: staker.token!,
amount: userBalance,
underlyings: staker.underlyings as Contract[],
rewards: undefined,
category: 'stake',
},
])
}
10 changes: 10 additions & 0 deletions src/adapters/reflexer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Adapter } from '@lib/adapter'

import * as ethereum from './ethereum'

const adapter: Adapter = {
id: 'reflexer',
ethereum,
}

export default adapter

0 comments on commit 692ca43

Please sign in to comment.