Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update, Smardex #1051

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/adapters/smardex/arbitrum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { getUserPendingSDEX } from '@adapters/smardex/common/farm'
import { getSmardexMasterChefPoolsContracts } from '@adapters/smardex/common/pool'
import type { BaseContext, Contract, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'
import { getMasterChefPoolsBalances } from '@lib/masterchef/masterChefBalance'
import { getPairsContracts } from '@lib/uniswap/v2/factory'
import { getPairsBalances } from '@lib/uniswap/v2/pair'

const SDEX: Contract = {
chain: 'arbitrum',
address: '0xabd587f2607542723b17f14d00d99b987c29b074',
decimals: 18,
symbol: 'SDEX',
}

const masterChef: Contract = {
chain: 'arbitrum',
address: '0x53D165DF0414bD02E91747775450934BF2257f69',
}

export const getContracts = async (ctx: BaseContext, props: any) => {
const offset = props.pairOffset || 0
const limit = 100

const [pools, { pairs, allPairsLength }] = await Promise.all([
getSmardexMasterChefPoolsContracts(ctx, masterChef),
getPairsContracts({
ctx,
factoryAddress: '0x41A00e3FbE7F479A99bA6822704d9c5dEB611F22',
offset,
limit,
}),
])

return {
contracts: {
pools,
pairs,
},
revalidate: 60 * 60,
revalidateProps: {
pairOffset: Math.min(offset + limit, allPairsLength),
},
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
pairs: getPairsBalances,
pools: (...args) =>
getMasterChefPoolsBalances(...args, {
masterChefAddress: masterChef.address,
rewardToken: SDEX,
getUserPendingRewards: (...args) => getUserPendingSDEX(...args),
}),
})

return {
groups: [{ balances }],
}
}
61 changes: 61 additions & 0 deletions src/adapters/smardex/base/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { getUserPendingSDEX } from '@adapters/smardex/common/farm'
import { getSmardexMasterChefPoolsContracts } from '@adapters/smardex/common/pool'
import type { BaseContext, Contract, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'
import { getMasterChefPoolsBalances } from '@lib/masterchef/masterChefBalance'
import { getPairsContracts } from '@lib/uniswap/v2/factory'
import { getPairsBalances } from '@lib/uniswap/v2/pair'

const SDEX: Contract = {
chain: 'base',
address: '0xFd4330b0312fdEEC6d4225075b82E00493FF2e3f',
decimals: 18,
symbol: 'SDEX',
}

const masterChef: Contract = {
chain: 'base',
address: '0xa5D378c05192E3f1F365D6298921879C4D51c5a3',
}

export const getContracts = async (ctx: BaseContext, props: any) => {
const offset = props.pairOffset || 0
const limit = 100

const [pools, { pairs, allPairsLength }] = await Promise.all([
getSmardexMasterChefPoolsContracts(ctx, masterChef),
getPairsContracts({
ctx,
factoryAddress: '0xdd4536dD9636564D891c919416880a3e250f975A',
offset,
limit,
}),
])

return {
contracts: {
pools,
pairs,
},
revalidate: 60 * 60,
revalidateProps: {
pairOffset: Math.min(offset + limit, allPairsLength),
},
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
pairs: getPairsBalances,
pools: (...args) =>
getMasterChefPoolsBalances(...args, {
masterChefAddress: masterChef.address,
rewardToken: SDEX,
getUserPendingRewards: (...args) => getUserPendingSDEX(...args),
}),
})

return {
groups: [{ balances }],
}
}
61 changes: 61 additions & 0 deletions src/adapters/smardex/bsc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { getUserPendingSDEX } from '@adapters/smardex/common/farm'
import { getSmardexMasterChefPoolsContracts } from '@adapters/smardex/common/pool'
import type { BaseContext, Contract, GetBalancesHandler } from '@lib/adapter'
import { resolveBalances } from '@lib/balance'
import { getMasterChefPoolsBalances } from '@lib/masterchef/masterChefBalance'
import { getPairsContracts } from '@lib/uniswap/v2/factory'
import { getPairsBalances } from '@lib/uniswap/v2/pair'

const SDEX: Contract = {
chain: 'bsc',
address: '0xFdc66A08B0d0Dc44c17bbd471B88f49F50CdD20F',
decimals: 18,
symbol: 'SDEX',
}

const masterChef: Contract = {
chain: 'bsc',
address: '0xb891Aeb2130805171796644a2af76Fc7Ff25a0b9',
}

export const getContracts = async (ctx: BaseContext, props: any) => {
const offset = props.pairOffset || 0
const limit = 100

const [pools, { pairs, allPairsLength }] = await Promise.all([
getSmardexMasterChefPoolsContracts(ctx, masterChef),
getPairsContracts({
ctx,
factoryAddress: '0xA8EF6FEa013034E62E2C4A9Ec1CDb059fE23Af33',
offset,
limit,
}),
])

return {
contracts: {
pools,
pairs,
},
revalidate: 60 * 60,
revalidateProps: {
pairOffset: Math.min(offset + limit, allPairsLength),
},
}
}

export const getBalances: GetBalancesHandler<typeof getContracts> = async (ctx, contracts) => {
const balances = await resolveBalances<typeof getContracts>(ctx, contracts, {
pairs: getPairsBalances,
pools: (...args) =>
getMasterChefPoolsBalances(...args, {
masterChefAddress: masterChef.address,
rewardToken: SDEX,
getUserPendingRewards: (...args) => getUserPendingSDEX(...args),
}),
})

return {
groups: [{ balances }],
}
}
74 changes: 74 additions & 0 deletions src/adapters/smardex/common/farm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { BalancesContext, Contract } from '@lib/adapter'
import { mapSuccessFilter } from '@lib/array'
import type { GetUsersInfosParams } from '@lib/masterchef/masterChefBalance'
import { multicall } from '@lib/multicall'

const abi = {
userInfo: {
inputs: [
{ internalType: 'uint256', name: '', type: 'uint256' },
{ internalType: 'address', name: '', type: 'address' },
],
name: 'userInfo',
outputs: [
{ internalType: 'uint256', name: 'amount', type: 'uint256' },
{ internalType: 'uint256', name: 'rewardDebt', type: 'uint256' },
],
stateMutability: 'view',
type: 'function',
},
pendingReward: {
inputs: [
{ internalType: 'uint256', name: '_campaignID', type: 'uint256' },
{ internalType: 'address', name: '_user', type: 'address' },
],
name: 'pendingReward',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
} as const

export async function getUserInfosSDEX(
ctx: BalancesContext,
{ masterChefAddress, pools, getUserBalance }: GetUsersInfosParams,
) {
const poolsInfos = await multicall({
ctx,
calls: pools.map((pool) => ({ target: masterChefAddress, params: [pool.pid, ctx.address] }) as const),
abi: abi.userInfo,
})

return mapSuccessFilter(poolsInfos, (res: any, index) => {
const pool = pools[index]
const underlyings = pool.underlyings as Contract[]
const rewards = pool.rewards as Contract[]
const userBalance = Array.isArray(res.output) ? getUserBalance!({ userBalance: res.output }) : res.output

return {
...pool,
amount: userBalance,
underlyings,
rewards,
category: 'farm',
}
})
}

export async function getUserPendingSDEX(
ctx: BalancesContext,
{ masterChefAddress, pools, rewardToken }: GetUsersInfosParams,
) {
const userPendingRewards = await multicall({
ctx,
calls: pools.map((pool) => ({ target: masterChefAddress, params: [pool.pid, ctx.address] }) as const),
abi: abi.pendingReward,
})

return mapSuccessFilter(userPendingRewards, (res: any, index) => {
const pool = pools[index]
const reward = rewardToken || (pool.rewards?.[0] as Contract)

return [{ ...reward, amount: res.output }]
})
}
59 changes: 59 additions & 0 deletions src/adapters/smardex/common/pool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { BaseContext, Contract } from '@lib/adapter'
import { mapSuccessFilter, rangeBI } from '@lib/array'
import { call } from '@lib/call'
import { getMasterChefPoolsContracts, type GetPoolsInfosParams } from '@lib/masterchef/masterChefContract'
import { multicall } from '@lib/multicall'

const abi = {
campaignInfo: {
inputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
name: 'campaignInfo',
outputs: [
{ internalType: 'contract IERC20', name: 'stakingToken', type: 'address' },
{ internalType: 'contract IERC20', name: 'rewardToken', type: 'address' },
{ internalType: 'uint256', name: 'startBlock', type: 'uint256' },
{ internalType: 'uint256', name: 'lastRewardBlock', type: 'uint256' },
{ internalType: 'uint256', name: 'accRewardPerShare', type: 'uint256' },
{ internalType: 'uint256', name: 'totalStaked', type: 'uint256' },
{ internalType: 'uint256', name: 'totalRewards', type: 'uint256' },
],
stateMutability: 'view',
type: 'function',
},
campaignInfoLen: {
inputs: [],
name: 'campaignInfoLen',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
} as const

export async function getSmardexMasterChefPoolsContracts(ctx: BaseContext, masterChef: Contract): Promise<Contract[]> {
return getMasterChefPoolsContracts(ctx, {
masterChefAddress: masterChef.address,
getAllPoolLength: (...args) => getAllPoolLength(...args),
getPoolInfos: (...args) => getPoolInfos(...args),
})
}

async function getAllPoolLength(ctx: BaseContext, masterChefAddress: `0x${string}`) {
return call({ ctx, target: masterChefAddress, abi: abi.campaignInfoLen })
}

export async function getPoolInfos(
ctx: BaseContext,
{ masterChefAddress, poolLength, getLpToken }: GetPoolsInfosParams,
) {
const poolInfos = await multicall({
ctx,
calls: rangeBI(0n, poolLength).map((idx) => ({ target: masterChefAddress, params: [idx] }) as const),
abi: abi.campaignInfo,
})

return mapSuccessFilter(poolInfos, (res) => {
const lpToken = Array.isArray(res.output) ? getLpToken!({ lpToken: res.output }) : res.output

return { chain: ctx.chain, address: lpToken, pid: res.input.params![0] }
})
}
Loading
Loading