Skip to content

Commit

Permalink
fix tests, feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
satish-ravi committed May 13, 2024
1 parent 23d8add commit 794001f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
45 changes: 37 additions & 8 deletions src/earn/prepareTransactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TokenBalance } from 'src/tokens/slice'
import { Network, NetworkId } from 'src/transactions/types'
import { publicClient } from 'src/viem'
import { prepareTransactions } from 'src/viem/prepareTransactions'
import { encodeFunctionData } from 'viem'
import { Address, encodeFunctionData } from 'viem'

const mockFeeCurrency: TokenBalance = {
address: null,
Expand All @@ -22,13 +22,15 @@ const mockFeeCurrency: TokenBalance = {
isNative: true,
}

const mockTokenAddress: Address = '0x1234567890abcdef1234567890abcdef12345678'

const mockToken: TokenBalance = {
address: '0xusdc',
address: mockTokenAddress,
balance: new BigNumber(10),
decimals: 6,
priceUsd: null,
lastKnownPriceUsd: null,
tokenId: 'arbitrum-sepolia:0xusdc',
tokenId: `arbitrum-sepolia:${mockTokenAddress}`,
symbol: 'USDC',
name: 'USD Coin',
networkId: NetworkId['arbitrum-sepolia'],
Expand Down Expand Up @@ -92,7 +94,7 @@ describe('prepareTransactions', () => {
const expectedTransactions = [
{
from: '0x1234',
to: '0xusdc',
to: mockTokenAddress,
data: '0xencodedData',
},
{
Expand All @@ -109,7 +111,7 @@ describe('prepareTransactions', () => {
transactions: expectedTransactions,
})
expect(publicClient[Network.Arbitrum].readContract).toHaveBeenCalledWith({
address: '0xusdc',
address: mockTokenAddress,
abi: erc20.abi,
functionName: 'allowance',
args: ['0x1234', '0x5678'],
Expand All @@ -122,7 +124,7 @@ describe('prepareTransactions', () => {
expect(encodeFunctionData).toHaveBeenNthCalledWith(2, {
abi: aavePool,
functionName: 'supply',
args: ['0xusdc', BigInt(5e6), '0x1234', 0],
args: [mockTokenAddress, BigInt(5e6), '0x1234', 0],
})
expect(prepareTransactions).toHaveBeenCalledWith({
baseTransactions: expectedTransactions,
Expand Down Expand Up @@ -172,15 +174,15 @@ describe('prepareTransactions', () => {
transactions: expectedTransactions,
})
expect(publicClient[Network.Arbitrum].readContract).toHaveBeenCalledWith({
address: '0xusdc',
address: mockTokenAddress,
abi: erc20.abi,
functionName: 'allowance',
args: ['0x1234', '0x5678'],
})
expect(encodeFunctionData).toHaveBeenNthCalledWith(1, {
abi: aavePool,
functionName: 'supply',
args: ['0xusdc', BigInt(5e6), '0x1234', 0],
args: [mockTokenAddress, BigInt(5e6), '0x1234', 0],
})
expect(prepareTransactions).toHaveBeenCalledWith({
baseTransactions: expectedTransactions,
Expand Down Expand Up @@ -239,5 +241,32 @@ describe('prepareTransactions', () => {
})
).rejects.toThrow('Failed to simulate supply transaction')
})

it('throws if simulated transactions length does not match base transactions length', async () => {
mockFetch.mockResponseOnce(
JSON.stringify({
status: 'OK',
simulatedTransactions: [
{
status: 'success',
blockNumber: '1',
gasNeeded: 3000,
gasUsed: 2800,
gasPrice: '1',
},
],
})
)

await expect(
prepareSupplyTransactions({
amount: '5',
token: mockToken,
walletAddress: '0x1234',
feeCurrencies: [mockFeeCurrency],
poolContractAddress: '0x5678',
})
).rejects.toThrow('Expected 2 simulated transactions, got 1')
})
})
})
9 changes: 8 additions & 1 deletion src/earn/prepareTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function prepareSupplyTransactions({

if (!token.address || !isAddress(token.address)) {
// should never happen
throw new Error('Cannot use a token without address')
throw new Error(`Cannot use a token without address. Token id: ${token.tokenId}`)

Check warning on line 42 in src/earn/prepareTransactions.ts

View check run for this annotation

Codecov / codecov/patch

src/earn/prepareTransactions.ts#L42

Added line #L42 was not covered by tests
}

const approvedAllowanceForSpender = await publicClient[
Expand Down Expand Up @@ -97,6 +97,13 @@ export async function prepareSupplyTransactions({

// extract fee of the supply transaction and set gas fields
const { simulatedTransactions }: SimulatedTransactionResponse = await response.json()

if (simulatedTransactions.length !== baseTransactions.length) {
throw new Error(
`Expected ${baseTransactions.length} simulated transactions, got ${simulatedTransactions.length}, response: ${JSON.stringify(simulatedTransactions)}`
)
}

const supplySimulatedTx = simulatedTransactions[simulatedTransactions.length - 1]

if (supplySimulatedTx.status !== 'success') {
Expand Down

0 comments on commit 794001f

Please sign in to comment.