Skip to content
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
10 changes: 4 additions & 6 deletions lib/utils/portfolio.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch from 'node-fetch'
import { networks } from 'ambire-common/dist/src/consts/networks'
import { networks as commonNetworks } from 'ambire-common/dist/src/consts/networks'
import { Network } from 'ambire-common/dist/src/interfaces/network'
import { Fetch } from 'ambire-common/dist/src/interfaces/fetch'
import { getRpcProvider } from 'ambire-common/dist/src/services/provider/getRpcProvider'
Expand All @@ -17,12 +17,9 @@ import {

export async function getPortfolioForNetwork(
address: string,
networkId: string | bigint,
network: Network,
customFetch?: Fetch
): Promise<NetworkPortfolioLibResponse> {
const network = networks.find((n: Network) => n.chainId === networkId || n.name === networkId)
if (!network) throw new Error(`Failed to find ${networkId} in configured networks`)

const provider = getRpcProvider(network.rpcUrls, network.chainId)
const portfolio = new Portfolio(
customFetch || fetch,
Expand All @@ -36,12 +33,13 @@ export async function getPortfolioForNetwork(

export async function getPortfolioVelcroV3(
address: string,
networks: Network[] = commonNetworks,
customFetch?: Fetch
): Promise<PortfolioForNetwork[]> {
const output: PortfolioForNetwork[] = []

const responses = await Promise.all(
networks.map((network) => getPortfolioForNetwork(address, network.chainId, customFetch))
networks.map((network) => getPortfolioForNetwork(address, network, customFetch))
)

for (const resp of responses) {
Expand Down
11 changes: 4 additions & 7 deletions test/unit/portfolio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
processAddress
} from '../../lib/utils/portfolio'
import { simplePrompt } from '../../lib/utils/prompts'
import { networks as commonNetworks } from 'ambire-common/dist/src/consts/networks'
import { Network } from 'ambire-common/dist/src/interfaces/network'

const TEST_WALLET = '0x69bfD720Dd188B8BB04C4b4D24442D3c15576D10'
const USDC_PRICE = 0.99
Expand Down Expand Up @@ -68,14 +70,9 @@ jest.mock('ambire-common/dist/src/libs/portfolio', () => {
})

describe('Portfolio unit tests', () => {
test('should throw an error trying to get portfolio on non-existing network', async () => {
await expect(getPortfolioForNetwork(TEST_WALLET, 'nethereum')).rejects.toThrow(
'Failed to find nethereum in configured networks'
)
})

test('should successfully get portfolio for address on ethereum', async () => {
const res = await getPortfolioForNetwork(TEST_WALLET, 'Ethereum')
const network = commonNetworks.find((n: Network) => n.name === 'Ethereum') as Network
const res = await getPortfolioForNetwork(TEST_WALLET, network)

expect(res).toHaveProperty('tokens')
expect(res.tokens).toHaveLength(1)
Expand Down