Skip to content

Commit

Permalink
refactor: tests utils (#107)
Browse files Browse the repository at this point in the history
* config

* rename factory

* use type import

* ci: readd icusd

* refactor: signer mgmt

* use new get test rpc provider

* update base tests

* clean up

* refactor: alchemy urls

* move zeroex to separate file

* remove some quote tokens

* remove localhost provider const

* update arbitrum tests

* test: make curve adapter test more resilient

* fix providers using dotenv

* add biome json

* skip dai test for icusd
  • Loading branch information
janndriessen authored Jan 3, 2025
1 parent 3dff730 commit 570f67b
Show file tree
Hide file tree
Showing 37 changed files with 257 additions and 415 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
# - run: npm run test:btc2xeth
# - run: npm run test:eth2xbtc
# - run: npm run test:base
# - run: npm run test:icusd
- run: npm run test:icusd
- run: npm run test:hyeth
# - run: npm run test:btc2x
# - run: npm run test:eth2x
Expand Down
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
12 changes: 12 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"formatter": {
"enabled": false
},
"linter": {
"enabled": false
},
"organizeImports": {
"enabled": true
}
}
26 changes: 11 additions & 15 deletions src/flashmint/builders/hyeth.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { BigNumber } from '@ethersproject/bignumber'
import { getTokenByChainAndSymbol } from '@indexcoop/tokenlists'

import { EthAddress } from 'constants/addresses'
import { ChainId } from 'constants/chains'
import { Contracts } from 'constants/contracts'
import {
LocalhostProvider,
LocalhostProviderUrl,
QuoteTokens,
} from 'tests/utils'
import { getLocalHostProviderUrl, getTestRpcProvider } from 'tests/utils'
import { getFlashMintHyEthContract } from 'utils/contracts'
import { wei } from 'utils/numbers'
import { Exchange } from 'utils/swap-data'
Expand All @@ -18,17 +15,16 @@ import {
FlashMintHyEthTransactionBuilder,
} from './hyeth'

const provider = LocalhostProvider
const rpcUrl = LocalhostProviderUrl

const { hyeth, usdc, weth } = QuoteTokens

const FlashMintHyEthAddress = Contracts[ChainId.Mainnet].FlashMintHyEthV3
const chainId = ChainId.Mainnet
const FlashMintHyEthAddress = Contracts[chainId].FlashMintHyEthV3
const eth = EthAddress
const indexToken = hyeth
const indexToken = getTokenByChainAndSymbol(chainId, 'hyETH')
const usdc = getTokenByChainAndSymbol(chainId, 'USDC')
const weth = getTokenByChainAndSymbol(chainId, 'WETH')

describe('FlashMintHyEthTransactionBuilder()', () => {
const contract = getFlashMintHyEthContract(provider)
const rpcUrl = getLocalHostProviderUrl(chainId)
const contract = getFlashMintHyEthContract(getTestRpcProvider(chainId))

test('returns null for invalid request (no input token)', async () => {
const buildRequest = createBuildRequest()
Expand All @@ -49,7 +45,7 @@ describe('FlashMintHyEthTransactionBuilder()', () => {
test('returns null for invalid request (indexTokenAmount = 0)', async () => {
const buildRequest = createBuildRequest()
// isMinting: true
buildRequest.outputTokenAmount = BigNumber.from(0)
buildRequest.outputTokenAmount = wei(0)
const builder = new FlashMintHyEthTransactionBuilder(rpcUrl)
const tx = await builder.build(buildRequest)
expect(tx).toBeNull()
Expand All @@ -58,7 +54,7 @@ describe('FlashMintHyEthTransactionBuilder()', () => {
test('returns null for invalid request (inputTokenAmount = 0)', async () => {
const buildRequest = createBuildRequest()
// isMinting: true
buildRequest.inputTokenAmount = BigNumber.from(0)
buildRequest.inputTokenAmount = wei(0)
const builder = new FlashMintHyEthTransactionBuilder(rpcUrl)
const tx = await builder.build(buildRequest)
expect(tx).toBeNull()
Expand Down
23 changes: 6 additions & 17 deletions src/flashmint/builders/leveraged.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,27 @@ import {
inputSwapData,
outputSwapData,
} from 'constants/swapdata'
import {
LocalhostProvider,
LocalhostProviderUrl,
QuoteTokens,
} from 'tests/utils'
import { getLocalHostProviderUrl, getTestRpcProvider } from 'tests/utils'
import { getFlashMintLeveragedContractForToken } from 'utils/contracts'
import { wei } from 'utils/numbers'
import {
FlashMintLeveragedBuildRequest,
LeveragedTransactionBuilder,
} from './leveraged'
import { getTokenByChainAndSymbol } from '@indexcoop/tokenlists'

const chainId = 1
const provider = LocalhostProvider
const rpcUrl = LocalhostProviderUrl

const { iceth, usdc } = QuoteTokens

const eth = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
const indexToken = iceth
const usdcAddress = usdc.address
const indexToken = getTokenByChainAndSymbol(chainId, 'icETH')
const usdcAddress = getTokenByChainAndSymbol(chainId, 'USDC').address

describe('LeveragedTransactionBuilder()', () => {
const contract = getFlashMintLeveragedContractForToken(
indexToken.symbol,
provider,
getTestRpcProvider(chainId),
chainId
)

beforeEach((): void => {
jest.setTimeout(10000000)
})
const rpcUrl = getLocalHostProviderUrl(chainId)

test('returns null for invalid request (no index token)', async () => {
const buildRequest = createBuildRequest()
Expand Down
16 changes: 8 additions & 8 deletions src/flashmint/builders/nav.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import {
USDC,
WETH,
} from 'constants/tokens'
import { LocalhostProvider, LocalhostProviderUrl } from 'tests/utils'
import { getFlashMintNavContract } from 'utils/contracts'
import { wei } from 'utils/numbers'
import { Exchange } from 'utils'

import {
FlashMintNavBuildRequest,
FlashMintNavTransactionBuilder,
} from 'flashmint/builders/nav'
import { getLocalHostProviderUrl, getTestRpcProvider } from 'tests/utils'
import { getFlashMintNavContract } from 'utils/contracts'
import { wei } from 'utils/numbers'
import { Exchange } from 'utils'

const provider = LocalhostProvider
const rpcUrl = LocalhostProviderUrl
const chainId = ChainId.Mainnet
const provider = getTestRpcProvider(chainId)
const rpcUrl = getLocalHostProviderUrl(chainId)

const eth = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
const usdcAddress = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831'

const FlashMintNavAddress = Contracts[ChainId.Mainnet].FlashMintNav
const FlashMintNavAddress = Contracts[chainId].FlashMintNav

describe('FlashMintNavTransactionBuilder()', () => {
const contract = getFlashMintNavContract(provider)
Expand Down
23 changes: 6 additions & 17 deletions src/flashmint/builders/zeroex.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
import { BigNumber } from '@ethersproject/bignumber'

import { FlashMintZeroExMainnetAddress } from 'constants/contracts'
import {
LocalhostProvider,
LocalhostProviderUrl,
QuoteTokens,
} from 'tests/utils'
import { getLocalHostProviderUrl, getTestRpcProvider } from 'tests/utils'
import { getFlashMintZeroExContractForToken } from 'utils/contracts'
import { getIssuanceModule } from 'utils/issuanceModules'
import { wei } from 'utils/numbers'
import { FlashMintZeroExBuildRequest, ZeroExTransactionBuilder } from './zeroex'
import { getTokenByChainAndSymbol } from '@indexcoop/tokenlists'

const chainId = 1
const provider = LocalhostProvider
const rpcUrl = LocalhostProviderUrl

const { dseth, usdc } = QuoteTokens

const eth = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
const indexToken = dseth
const usdcAddress = usdc.address
const indexToken = getTokenByChainAndSymbol(chainId, 'dsETH')
const usdcAddress = getTokenByChainAndSymbol(chainId, 'USDC').address

describe('ZeroExTransactionBuilder()', () => {
const contract = getFlashMintZeroExContractForToken(
indexToken.symbol,
provider,
getTestRpcProvider(chainId),
chainId
)
const issuanceModule = getIssuanceModule(indexToken.symbol, chainId)

beforeEach((): void => {
jest.setTimeout(10000000)
})
const rpcUrl = getLocalHostProviderUrl(chainId)

test('returns null for invalid request (no index token)', async () => {
const buildRequest = createBuildRequest()
Expand Down
22 changes: 8 additions & 14 deletions src/quote/flashmint/leveraged/provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { ChainId } from 'constants/chains'
import {
collateralDebtSwapData,
Expand All @@ -14,11 +13,14 @@ import {
} from 'tests/utils'

import { LeveragedQuoteProvider } from './provider'
import { getTokenByChainAndSymbol } from '@indexcoop/tokenlists'

const rpcUrl = getLocalHostProviderUrl(ChainId.Mainnet)
const swapQuoteProvider = getZeroExSwapQuoteProvider(ChainId.Mainnet)
const chainId = ChainId.Mainnet
const rpcUrl = getLocalHostProviderUrl(chainId)
const swapQuoteProvider = getZeroExSwapQuoteProvider(chainId)

const { eth, iceth } = QuoteTokens
const { eth } = QuoteTokens
const iceth = getTokenByChainAndSymbol(chainId, 'icETH')

describe('LeveragedQuoteProvider()', () => {
test('returns static swap data for 🧊ETH - minting', async () => {
Expand All @@ -27,11 +29,7 @@ describe('LeveragedQuoteProvider()', () => {
const request = {
isMinting: true,
inputToken: eth,
outputToken: {
symbol: indexToken.symbol,
decimals: 18,
address: indexToken.address!,
},
outputToken: indexToken,
indexTokenAmount,
slippage: 0.5,
}
Expand All @@ -53,11 +51,7 @@ describe('LeveragedQuoteProvider()', () => {
const indexTokenAmount = wei(1)
const request = {
isMinting: false,
inputToken: {
symbol: indexToken.symbol,
decimals: 18,
address: indexToken.address!,
},
inputToken: indexToken,
outputToken: eth,
indexTokenAmount,
slippage: 0.5,
Expand Down
53 changes: 7 additions & 46 deletions src/quote/flashmint/zeroEx/provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dotenv/config'
import { getTokenByChainAndSymbol } from '@indexcoop/tokenlists'

import { ChainId } from 'constants/chains'
import {
Expand All @@ -10,53 +10,14 @@ import { wei } from 'utils'

import { ZeroExQuoteProvider } from './provider'

const rpcUrl = getLocalHostProviderUrl(ChainId.Mainnet)
const swapQuoteProvider = getZeroExSwapQuoteProvider(ChainId.Mainnet)

const { dpi, dseth, eth, mvi } = QuoteTokens
const chainId = ChainId.Mainnet
const rpcUrl = getLocalHostProviderUrl(chainId)
const swapQuoteProvider = getZeroExSwapQuoteProvider(chainId)

describe('ZeroExQuoteProvider', () => {
beforeEach((): void => {
jest.setTimeout(100000)
})

test.skip('returns a quote for minting dsETH', async () => {
const indexTokenAmount = wei(1)
const inputToken = eth
const outputToken = dseth
const quoteProvider = new ZeroExQuoteProvider(rpcUrl, swapQuoteProvider)
const quote = await quoteProvider.getQuote({
isMinting: true,
inputToken,
outputToken,
indexTokenAmount,
slippage: 0.5,
})
if (!quote) fail()
expect(quote.componentQuotes.length).toBeGreaterThan(0)
expect(quote.indexTokenAmount).toEqual(indexTokenAmount)
expect(quote.inputOutputTokenAmount).toBeDefined()
expect(quote.inputOutputTokenAmount.gt(0)).toBe(true)
})

test.skip('returns a quote for redeeming dsETH', async () => {
const indexTokenAmount = wei(1)
const inputToken = dseth
const outputToken = eth
const quoteProvider = new ZeroExQuoteProvider(rpcUrl, swapQuoteProvider)
const quote = await quoteProvider.getQuote({
isMinting: false,
inputToken,
outputToken,
indexTokenAmount,
slippage: 0.5,
})
if (!quote) fail()
expect(quote.componentQuotes.length).toBeGreaterThan(0)
expect(quote.indexTokenAmount).toEqual(indexTokenAmount)
expect(quote.inputOutputTokenAmount).toBeDefined()
expect(quote.inputOutputTokenAmount.gt(0)).toBe(true)
})
const dpi = getTokenByChainAndSymbol(chainId, 'DPI')
const { eth } = QuoteTokens
const mvi = getTokenByChainAndSymbol(chainId, 'MVI')

/**
* Skipping tests for DPI and MVI - as they often lead to 0x API rate limits
Expand Down
6 changes: 5 additions & 1 deletion src/quote/provider/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ const provider = getRpcProvider(rpcUrl)
const zeroexSwapQuoteProvider = getZeroExSwapQuoteProvider(chainId)

const FlashMintHyEthAddress = Contracts[ChainId.Mainnet].FlashMintHyEthV3
const { eth, eth2x, hyeth, iceth, usdc } = QuoteTokens
const { eth } = QuoteTokens
const eth2x = getTokenByChainAndSymbol(chainId, 'ETH2X')
const hyeth = getTokenByChainAndSymbol(chainId, 'hyETH')
const iceth = getTokenByChainAndSymbol(chainId, 'icETH')
const usdc = getTokenByChainAndSymbol(chainId, 'USDC')

describe('FlashMintQuoteProvider()', () => {
test('throws if token is unsupported', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/quote/swap/adapters/adapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { ETH, stETH, WETH } from 'constants/tokens'
import { AlchemyProviderUrl } from 'tests/utils'
import { getAlchemyProviderUrl } from 'tests/utils'

import { IndexSwapQuoteProvider } from './adapter'
import { Exchange } from 'utils'

const rpcUrl = AlchemyProviderUrl
const rpcUrl = getAlchemyProviderUrl(1)

// ETH/stETH
const curvePool = '0xdc24316b9ae028f1497c275eb9192a3ea0f67022'
Expand Down
Loading

0 comments on commit 570f67b

Please sign in to comment.