Skip to content

Commit

Permalink
refactor: update tests, mock responses
Browse files Browse the repository at this point in the history
  • Loading branch information
enesozturk committed May 6, 2024
1 parent a4b55c1 commit 448ad84
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 86 deletions.
23 changes: 0 additions & 23 deletions packages/core/src/controllers/SwapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,36 +673,13 @@ export const SwapController = {
}
},

getToTokenValues() {
const { toTokenAddress, toTokenAmount } = this.getParams()

if (!toTokenAddress) {
return {
toTokenAmount: '0',
toTokenPriceInUSD: 0
}
}

const toTokenPrice = state.tokensPriceMap[toTokenAddress] || '0'
const toTokenPriceInUSD = NumberUtil.bigNumber(toTokenPrice).toNumber()

return {
toTokenAmount,
toTokenPriceInUSD
}
},

setTransactionDetails(transaction: TransactionParams | undefined) {
const { toTokenAddress, toTokenDecimals } = this.getParams()

if (!transaction || !toTokenAddress || !toTokenDecimals) {
return
}

const { toTokenAmount, toTokenPriceInUSD } = this.getToTokenValues()

state.toTokenAmount = toTokenAmount
state.toTokenPriceInUSD = toTokenPriceInUSD
state.gasPriceInUSD = SwapCalculationUtil.getGasPriceInUSD(
state.networkPrice,
transaction.gas,
Expand Down
99 changes: 55 additions & 44 deletions packages/core/tests/controllers/SwapController.test.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,77 @@
import { beforeAll, beforeEach, describe, expect, it } from 'vitest'
import { AccountController, SwapController, type SwapTokenWithBalance } from '../../index.js'
import { prices, tokenInfo } from '../mocks/SwapController.js'
import { beforeAll, describe, expect, it, vi } from 'vitest'
import {
AccountController,
BlockchainApiController,
NetworkController,
SwapController,
type NetworkControllerClient
} from '../../index.js'
import {
balanceResponse,
gasPriceResponse,
networkTokenPriceResponse,
tokensResponse
} from '../mocks/SwapController.js'
import { INITIAL_GAS_LIMIT } from '../../src/controllers/SwapController.js'
import { SwapCalculationUtil } from '../../src/utils/SwapCalculationUtil.js'
import { SwapApiUtil } from '../../src/utils/SwapApiUtil.js'

// - Mocks ---------------------------------------------------------------------
const mockTransaction = {
data: '0x11111',
gas: BigInt(INITIAL_GAS_LIMIT),
gasPrice: BigInt(10000000000),
to: '0x222',
toAmount: '1',
value: BigInt(1)
}
const caipNetwork = { id: 'eip155:137', name: 'Polygon' } as const
const client: NetworkControllerClient = {
switchCaipNetwork: async _caipNetwork => Promise.resolve(),
getApprovedCaipNetworksData: async () =>
Promise.resolve({ approvedCaipNetworkIds, supportsAllNetworks: false })
}
const caipAddress = 'eip155:1:0x123'
const gasLimit = BigInt(INITIAL_GAS_LIMIT)
const gasFee = BigInt(455966887160)

const networkTokenAddress = 'eip155:137:0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'

// MATIC
const networkToken = tokenInfo[0] as SwapTokenWithBalance
// FOX
const toToken = tokenInfo[1] as SwapTokenWithBalance

// - Helpers
function initializeSwapState(_sourceToken: SwapTokenWithBalance, _toToken: SwapTokenWithBalance) {
SwapController.state.tokensPriceMap = prices
SwapController.state.networkPrice = prices[networkTokenAddress].toString()
SwapController.state.networkBalanceInUSD = networkToken.quantity.numeric
SwapController.state.gasPriceInUSD = SwapCalculationUtil.getGasPriceInUSD(
SwapController.state.networkPrice,
gasLimit,
gasFee
)
SwapController.setSourceToken(_sourceToken)
SwapController.state.sourceTokenPriceInUSD = _sourceToken.price
SwapController.setToToken(_toToken)
SwapController.state.toTokenPriceInUSD = _toToken.price
SwapController.state.toTokenAmount = SwapCalculationUtil.getToTokenAmount({
sourceToken: _sourceToken,
sourceTokenAmount: '1',
sourceTokenPrice: _sourceToken.price,
toToken: _toToken,
toTokenPrice: _toToken.price
})
}
const networkTokenAddress = 'eip155:137:0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
// AVAX
const toTokenAddress = 'eip155:137:0x2c89bbc92bd86f8075d1decc58c7f4e0107f286b'

// - Setup ---------------------------------------------------------------------
beforeAll(() => {
beforeAll(async () => {
// -- Set Account and
NetworkController.setClient(client)
await NetworkController.switchActiveNetwork(caipNetwork)
AccountController.setCaipAddress(caipAddress)
})

beforeEach(() => {
initializeSwapState(networkToken, toToken)
vi.spyOn(BlockchainApiController, 'getBalance').mockResolvedValue(balanceResponse)
vi.spyOn(BlockchainApiController, 'fetchTokenPrice').mockResolvedValue(networkTokenPriceResponse)
vi.spyOn(SwapApiUtil, 'getTokenList').mockResolvedValue(tokensResponse)
vi.spyOn(SwapApiUtil, 'fetchGasPrice').mockResolvedValue(gasPriceResponse)
vi.spyOn(SwapController, 'getTransaction').mockResolvedValue(mockTransaction)

await SwapController.initializeState()

const toToken = SwapController.state.myTokensWithBalance?.[1]
SwapController.setToToken(toToken)
})

// -- Tests --------------------------------------------------------------------
describe('SwapController', () => {
it('should set sourceToken as expected', () => {
expect(SwapController.state.sourceToken?.address).toEqual(networkToken.address)
expect(SwapController.state.sourceToken?.address).toEqual(networkTokenAddress)
})

it('should set toToken as expected', () => {
expect(SwapController.state.toToken?.address).toEqual(toToken.address)
expect(SwapController.state.toToken?.address).toEqual(toTokenAddress)
expect(SwapController.state.toTokenPriceInUSD).toEqual(38.0742530944)
})

it('should calculate swap values as expected', () => {
expect(SwapController.state.toTokenAmount).toEqual('6.725738471695571914')
expect(SwapController.state.toTokenPriceInUSD).toEqual(0.10315220553291868)
it('should calculate swap values as expected', async () => {
await SwapController.swapTokens()

expect(SwapController.state.gasPriceInUSD).toEqual(0.0010485260814)
expect(SwapController.state.priceImpact).toEqual(0.898544263592072)
expect(SwapController.state.maxSlippage).toEqual(0.00019274639331006023)
})

it('should reset values as expected', () => {
Expand Down
75 changes: 65 additions & 10 deletions packages/core/tests/mocks/SwapController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export const tokenInfo = [
export const tokensResponse = [
{
name: 'Matic Token',
symbol: 'MATIC',
chainId: 'eip155:137',
address:
'eip155:137:0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' as `${string}:${string}:${string}`,
value: 15.945686877137186,
price: 0.6990173876,
decimals: 18,
Expand All @@ -15,8 +16,8 @@ export const tokenInfo = [
{
name: 'ShapeShift FOX',
symbol: 'FOX',
chainId: 'eip155:137',
address: 'eip155:137:0x65a05db8322701724c197af82c9cae41195b0aa8',
address:
'eip155:137:0x65a05db8322701724c197af82c9cae41195b0aa8' as `${string}:${string}:${string}`,
value: 0.818151429070586,
price: 0.10315220553291868,
decimals: 18,
Expand All @@ -29,8 +30,8 @@ export const tokenInfo = [
{
name: 'Tether USD',
symbol: 'USDT',
chainId: 'eip155:137',
address: 'eip155:137:0xc2132d05d31c914a87c6611c10748aeb04b58e8f',
address:
'eip155:137:0xc2132d05d31c914a87c6611c10748aeb04b58e8f' as `${string}:${string}:${string}`,
value: 0.8888156632489365,
price: 0.9995840116762155,
decimals: 6,
Expand All @@ -42,8 +43,62 @@ export const tokenInfo = [
}
]

export const prices = {
'eip155:137:0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee': 0.93508248,
'eip155:137:0x65a05db8322701724c197af82c9cae41195b0aa8': 0.10315220553291868,
'eip155:137:0xc2132d05d31c914a87c6611c10748aeb04b58e8f': 0.9995840116762155
export const networkTokenPriceResponse = {
fungibles: [
{
name: 'Matic Token',
symbol: 'MATIC',
price: '0.6990173876',
iconUrl: 'https://token-icons.s3.amazonaws.com/0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0.png'
}
]
}

export const balanceResponse = {
balances: [
{
name: 'Matic Token',
symbol: 'MATIC',
chainId: 'eip155:137',
value: 10.667935172031754,
price: 0.7394130944,
quantity: {
decimals: '18',
numeric: '14.427571343848456409'
},
iconUrl: 'https://token-icons.s3.amazonaws.com/0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0.png'
},
{
name: 'Wrapped AVAX',
symbol: 'AVAX',
chainId: 'eip155:137',
address: 'eip155:137:0x2c89bbc92bd86f8075d1decc58c7f4e0107f286b',
value: 3.751852120639868,
price: 38.0742530944,
quantity: {
decimals: '18',
numeric: '0.098540399764051957'
},
iconUrl: 'https://token-icons.s3.amazonaws.com/0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7.png'
},
{
name: 'Tether USD',
symbol: 'USDT',
chainId: 'eip155:137',
address: 'eip155:137:0xc2132d05d31c914a87c6611c10748aeb04b58e8f',
value: 2.3040319252130432,
price: 1.0010962048,
quantity: {
decimals: '6',
numeric: '2.301509'
},
iconUrl: 'https://token-icons.s3.amazonaws.com/0xdac17f958d2ee523a2206206994597c13d831ec7.png'
}
]
}

export const gasPriceResponse = {
standard: '60000000128',
fast: '150000000128',
instant: '195000000166'
}
16 changes: 7 additions & 9 deletions packages/core/tests/utils/SwapCalculationUtil.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { describe, expect, it } from 'vitest'
import { SwapCalculationUtil } from '../../src/utils/SwapCalculationUtil.js'
import { INITIAL_GAS_LIMIT } from '../../src/controllers/SwapController.js'
import { prices, tokenInfo } from '../mocks/SwapController.js'
import { networkTokenPriceResponse, tokensResponse } from '../mocks/SwapController.js'
import type { SwapTokenWithBalance } from '../../src/utils/TypeUtil.js'
import { NumberUtil } from '@web3modal/common'

// - Mocks ---------------------------------------------------------------------
const gasLimit = BigInt(INITIAL_GAS_LIMIT)
const gasFee = BigInt(455966887160)

const sourceTokenAddress = 'eip155:137:0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'

const sourceToken = tokenInfo[0] as SwapTokenWithBalance
const sourceToken = tokensResponse[0] as SwapTokenWithBalance
const sourceTokenAmount = '1'
const toToken = tokenInfo[1] as SwapTokenWithBalance
const toToken = tokensResponse[1] as SwapTokenWithBalance

const networkPrice = prices[sourceTokenAddress].toString()
const networkPrice = networkTokenPriceResponse.fungibles[0]?.price || '0'

// -- Tests --------------------------------------------------------------------
describe('SwapCalculationUtil', () => {
Expand All @@ -24,7 +22,7 @@ describe('SwapCalculationUtil', () => {
const gasPriceInUSD = SwapCalculationUtil.getGasPriceInUSD(networkPrice, gasLimit, gasFee)

expect(gasPriceInEther).toEqual(0.068395033074)
expect(gasPriceInUSD).toEqual(0.06395499714651795)
expect(gasPriceInUSD).toEqual(0.04780931734420308)
})

it('should return insufficient balance as expected', () => {
Expand Down Expand Up @@ -60,7 +58,7 @@ describe('SwapCalculationUtil', () => {
toTokenAmount,
toTokenPriceInUSD: toToken.price
})
expect(priceImpact).equal(9.974076865161582)
expect(priceImpact).equal(7.646854717783376)
})

it('should get to token amount with same decimals including provider fee as expected', () => {
Expand All @@ -75,7 +73,7 @@ describe('SwapCalculationUtil', () => {
})

it('should get to token amount with different decimals including provider fee as expected', () => {
const newToToken = tokenInfo[2] as SwapTokenWithBalance
const newToToken = tokensResponse[2] as SwapTokenWithBalance

const toTokenAmount = SwapCalculationUtil.getToTokenAmount({
sourceToken,
Expand Down

0 comments on commit 448ad84

Please sign in to comment.