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

chore: add aaveArbUsdcTokenId to network config #5397

Merged
merged 16 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
45 changes: 43 additions & 2 deletions src/earn/poolInfo.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import aavePool from 'src/abis/AavePoolV3'
import { fetchAavePoolInfo } from 'src/earn/poolInfo'
import { fetchAavePoolInfo, fetchAavePoolUserBalance } from 'src/earn/poolInfo'
import { Network } from 'src/transactions/types'
import { publicClient } from 'src/viem'
import networkConfig from 'src/web3/networkConfig'
import { getContract } from 'viem'

describe('poolInfo', () => {
jest.mock('viem', () => ({
...jest.requireActual('viem'),
getContract: jest.fn(),
}))

describe('fetchAavePoolInfo', () => {
it('fetches poolInfo from contract', async () => {
jest.spyOn(publicClient[Network.Arbitrum], 'readContract').mockResolvedValue({
currentLiquidityRate: BigInt(1e27 * 0.036),
Expand All @@ -21,3 +27,38 @@ describe('poolInfo', () => {
})
})
})

describe('fetchAavePoolUserBalance', () => {
it('fetches user balance from contract', async () => {
const mockReadContract = jest
.spyOn(publicClient[Network.Arbitrum], 'readContract')
.mockResolvedValue({
aTokenAddress: '0xaToken',
})
const mockContractInstance = {
read: {
balanceOf: jest.fn().mockResolvedValue(BigInt(10750000)),
decimals: jest.fn().mockResolvedValue(6),
},
}

// @ts-ignore
jest.mocked(getContract).mockReturnValue(mockContractInstance)

const result = await fetchAavePoolUserBalance({
assetAddress: '0x1234',
walletAddress: '0x5678',
})

expect(mockReadContract).toHaveBeenCalledWith({
abi: aavePool,
address: networkConfig.arbAavePoolV3ContractAddress,
functionName: 'getReserveData',
args: ['0x1234'],
})

expect(mockContractInstance.read.balanceOf).toHaveBeenCalledWith(['0x5678'])
expect(mockContractInstance.read.decimals).toHaveBeenCalled()
expect(result).toEqual({ balanceInDecimal: '10.75' })
})
})
50 changes: 49 additions & 1 deletion src/earn/poolInfo.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import BigNumber from 'bignumber.js'
import AavePool from 'src/abis/AavePoolV3'
import erc20 from 'src/abis/IERC20'
import { Network } from 'src/transactions/types'
import Logger from 'src/utils/Logger'
import { ensureError } from 'src/utils/ensureError'
import { publicClient } from 'src/viem'
import networkConfig from 'src/web3/networkConfig'
import { Address } from 'viem'
import { Address, formatUnits, getContract } from 'viem'

const TAG = 'earn/poolInfo'

const COMPOUND_PERIOD = 365 * 24 * 60 * 60 // 1 year in seconds

export async function fetchAavePoolInfo(assetAddress: Address) {
try {
if (!assetAddress) {
throw new Error('No asset address provided')

Check warning on line 18 in src/earn/poolInfo.ts

View check run for this annotation

Codecov / codecov/patch

src/earn/poolInfo.ts#L18

Added line #L18 was not covered by tests
}
Logger.debug(TAG, 'Fetching Aave pool info for asset', assetAddress)
const result = await publicClient[Network.Arbitrum].readContract({
abi: AavePool,
Expand All @@ -32,3 +36,47 @@
throw err
}
}

/** @beta - Exclude from Knip dep check */
MuckT marked this conversation as resolved.
Show resolved Hide resolved
export async function fetchAavePoolUserBalance({
assetAddress,
walletAddress,
}: {
assetAddress: Address
walletAddress: Address
}) {
try {
if (!assetAddress) {
MuckT marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('No asset address provided')

Check warning on line 50 in src/earn/poolInfo.ts

View check run for this annotation

Codecov / codecov/patch

src/earn/poolInfo.ts#L50

Added line #L50 was not covered by tests
}
if (!walletAddress) {
MuckT marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('No wallet address provided')

Check warning on line 53 in src/earn/poolInfo.ts

View check run for this annotation

Codecov / codecov/patch

src/earn/poolInfo.ts#L53

Added line #L53 was not covered by tests
}
Logger.debug(TAG, 'Fetching Aave pool user balance', { assetAddress, walletAddress })
const result = await publicClient[Network.Arbitrum].readContract({
MuckT marked this conversation as resolved.
Show resolved Hide resolved
abi: AavePool,
address: networkConfig.arbAavePoolV3ContractAddress,
functionName: 'getReserveData',
args: [assetAddress],
})

const { aTokenAddress } = result as { aTokenAddress: Address }
const aaveUSDCContract = getContract({
abi: erc20.abi,
address: aTokenAddress,
client: {
public: publicClient[Network.Arbitrum],
},
})

const balance = await aaveUSDCContract.read.balanceOf([walletAddress])
const decimals = await aaveUSDCContract.read.decimals()
const balanceInDecimal = formatUnits(balance, decimals)

return { balanceInDecimal }
} catch (error) {
const err = ensureError(error)
Logger.error(TAG, 'Failed to fetch Aave pool user balance', err)
throw err

Check warning on line 80 in src/earn/poolInfo.ts

View check run for this annotation

Codecov / codecov/patch

src/earn/poolInfo.ts#L78-L80

Added lines #L78 - L80 were not covered by tests
}
}
Loading