Skip to content

Commit 92cbc7d

Browse files
authored
Merge pull request #30 from AmbireTech/feat/allow-custom-networks-for-get-portoflio
Allow custom networks when getting portfolio
2 parents fd12e68 + c7b45eb commit 92cbc7d

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

lib/utils/portfolio.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fetch from 'node-fetch'
2-
import { networks } from 'ambire-common/dist/src/consts/networks'
2+
import { networks as commonNetworks } from 'ambire-common/dist/src/consts/networks'
33
import { Network } from 'ambire-common/dist/src/interfaces/network'
44
import { Fetch } from 'ambire-common/dist/src/interfaces/fetch'
55
import { getRpcProvider } from 'ambire-common/dist/src/services/provider/getRpcProvider'
@@ -17,12 +17,9 @@ import {
1717

1818
export async function getPortfolioForNetwork(
1919
address: string,
20-
networkId: string | bigint,
20+
network: Network,
2121
customFetch?: Fetch
2222
): Promise<NetworkPortfolioLibResponse> {
23-
const network = networks.find((n: Network) => n.chainId === networkId || n.name === networkId)
24-
if (!network) throw new Error(`Failed to find ${networkId} in configured networks`)
25-
2623
const provider = getRpcProvider(network.rpcUrls, network.chainId)
2724
const portfolio = new Portfolio(
2825
customFetch || fetch,
@@ -36,12 +33,13 @@ export async function getPortfolioForNetwork(
3633

3734
export async function getPortfolioVelcroV3(
3835
address: string,
36+
networks: Network[] = commonNetworks,
3937
customFetch?: Fetch
4038
): Promise<PortfolioForNetwork[]> {
4139
const output: PortfolioForNetwork[] = []
4240

4341
const responses = await Promise.all(
44-
networks.map((network) => getPortfolioForNetwork(address, network.chainId, customFetch))
42+
networks.map((network) => getPortfolioForNetwork(address, network, customFetch))
4543
)
4644

4745
for (const resp of responses) {

test/unit/portfolio.test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
processAddress
1212
} from '../../lib/utils/portfolio'
1313
import { simplePrompt } from '../../lib/utils/prompts'
14+
import { networks as commonNetworks } from 'ambire-common/dist/src/consts/networks'
15+
import { Network } from 'ambire-common/dist/src/interfaces/network'
1416

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

7072
describe('Portfolio unit tests', () => {
71-
test('should throw an error trying to get portfolio on non-existing network', async () => {
72-
await expect(getPortfolioForNetwork(TEST_WALLET, 'nethereum')).rejects.toThrow(
73-
'Failed to find nethereum in configured networks'
74-
)
75-
})
76-
7773
test('should successfully get portfolio for address on ethereum', async () => {
78-
const res = await getPortfolioForNetwork(TEST_WALLET, 'Ethereum')
74+
const network = commonNetworks.find((n: Network) => n.name === 'Ethereum') as Network
75+
const res = await getPortfolioForNetwork(TEST_WALLET, network)
7976

8077
expect(res).toHaveProperty('tokens')
8178
expect(res.tokens).toHaveLength(1)

0 commit comments

Comments
 (0)