Skip to content

Commit ae8d6e7

Browse files
authored
Merge pull request #20 from AmbireTech/update-ambire-common-and-portfolio-network-structure
Update ambire-common + portfolio logic related to structure of network args
2 parents c48e59e + da0063d commit ae8d6e7

File tree

5 files changed

+113
-15
lines changed

5 files changed

+113
-15
lines changed

lib/types/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { TokenResult } from 'ambire-common/dist/src/libs/portfolio/interfaces'
22

33
export type PortfolioLibToken = Pick<
44
TokenResult,
5-
'symbol' | 'address' | 'networkId' | 'decimals' | 'amount' | 'priceIn'
5+
'symbol' | 'address' | 'chainId' | 'decimals' | 'amount' | 'priceIn'
66
>
77
export type NetworkPortfolioLibResponse = {
88
tokens: PortfolioLibToken[]

lib/utils/portfolio.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fetch from 'node-fetch'
22
import { networks } from 'ambire-common/dist/src/consts/networks'
3+
import { Network } from 'ambire-common/dist/src/interfaces/network'
34
import { getRpcProvider } from 'ambire-common/dist/src/services/provider/getRpcProvider'
45
import { Portfolio } from 'ambire-common/dist/src/libs/portfolio'
56
import { llmMockProcess } from './llm/mockedAI'
@@ -14,9 +15,9 @@ import {
1415

1516
export async function getPortfolioForNetwork(
1617
address: string,
17-
networkId: string
18+
networkId: string | bigint
1819
): Promise<NetworkPortfolioLibResponse> {
19-
const network = networks.find((n: any) => n.id === networkId)
20+
const network = networks.find((n: Network) => n.chainId === networkId || n.name === networkId)
2021
if (!network) throw new Error(`Failed to find ${networkId} in configured networks`)
2122

2223
const provider = getRpcProvider(network.rpcUrls, network.chainId)
@@ -34,13 +35,14 @@ export async function getPortfolioVelcroV3(address: string): Promise<PortfolioFo
3435
const output: PortfolioForNetwork[] = []
3536

3637
const responses = await Promise.all(
37-
networks.map((network) => getPortfolioForNetwork(address, network.id))
38+
networks.map((network) => getPortfolioForNetwork(address, network.chainId))
3839
)
3940

4041
for (const resp of responses) {
4142
const tokens = resp.tokens
4243
.filter((t) => t.amount > 0n)
4344
.map((t) => {
45+
const network = networks.find((n) => n.chainId === t.chainId) as Network
4446
const balance = Number(t.amount) / Math.pow(10, t.decimals)
4547
const priceUSD = (t.priceIn.find((p) => p.baseCurrency === 'usd') || { price: 0 })
4648
.price
@@ -49,7 +51,7 @@ export async function getPortfolioVelcroV3(address: string): Promise<PortfolioFo
4951
symbol: t.symbol,
5052
balance,
5153
balanceUSD: balance * priceUSD,
52-
network: t.networkId,
54+
network: network.name,
5355
address: t.address
5456
}
5557
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
},
4747
"dependencies": {
4848
"@google/generative-ai": "^0.21.0",
49-
"ambire-common": "github:AmbireTech/ambire-common#677651a49c49cf49ff7864bd1bd6dc32b580c789",
49+
"ambire-common": "github:AmbireTech/ambire-common#378bba59e18430bfcc411504ca3dbc607f32d214",
5050
"openai": "^4.85.4",
5151
"tslib": "^2.8.1",
5252
"zod": "^3.24.2"

test/unit/portfolio.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const mockedNetworkPortfolioResult: NetworkPortfolioLibResponse = {
1919
{
2020
symbol: 'USDC',
2121
address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
22-
networkId: 'ethereum',
22+
chainId: 1n,
2323
decimals: 6,
2424
amount: 50000000n,
2525
priceIn: [
@@ -55,7 +55,7 @@ const mockedLlmOutput: LlmProcessOutput = {
5555
jest.mock('ambire-common/dist/src/consts/networks', () => {
5656
const actual = jest.requireActual('ambire-common/dist/src/consts/networks')
5757
return {
58-
networks: actual.networks.filter((n: any) => ['ethereum'].includes(n.id))
58+
networks: actual.networks.filter((n: any) => ['Ethereum'].includes(n.name))
5959
}
6060
})
6161

@@ -75,7 +75,7 @@ describe('Portfolio unit tests', () => {
7575
})
7676

7777
test('should successfully get portfolio for address on ethereum', async () => {
78-
const res = await getPortfolioForNetwork(TEST_WALLET, 'ethereum')
78+
const res = await getPortfolioForNetwork(TEST_WALLET, 'Ethereum')
7979

8080
expect(res).toHaveProperty('tokens')
8181
expect(res.tokens).toHaveLength(1)
@@ -88,13 +88,13 @@ describe('Portfolio unit tests', () => {
8888
expect(res).toHaveLength(1)
8989
expect(res[0]).toHaveProperty('network')
9090
expect(res[0]).toHaveProperty('tokens')
91-
expect(res[0].network).toEqual('ethereum')
91+
expect(res[0].network).toEqual('Ethereum')
9292
expect(res[0].tokens).toHaveLength(1)
9393

9494
const mockedLibToken = mockedNetworkPortfolioResult.tokens[0]
9595
expect(res[0].tokens[0].symbol).toEqual(mockedLibToken.symbol)
9696
expect(res[0].tokens[0].address).toEqual(mockedLibToken.address)
97-
expect(res[0].tokens[0].network).toEqual(mockedLibToken.networkId)
97+
expect(res[0].tokens[0].network).toEqual('Ethereum')
9898

9999
const expectedBalance =
100100
Number(mockedLibToken.amount) / Math.pow(10, mockedLibToken.decimals)

yarn.lock

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
resolved "https://registry.yarnpkg.com/@adraffy/ens-normalize/-/ens-normalize-1.10.1.tgz#63430d04bd8c5e74f8d7d049338f1cd9d4f02069"
88
integrity sha512-96Z2IP3mYmF1Xg2cDm8f1gWGf/HUVedQ3FMifV4kG/PQ4yEP51xDtRAEfhVNt5f/uzpNkZHwWQuUcu6D6K+Ekw==
99

10+
"@adraffy/ens-normalize@^1.10.1":
11+
version "1.11.0"
12+
resolved "https://registry.yarnpkg.com/@adraffy/ens-normalize/-/ens-normalize-1.11.0.tgz#42cc67c5baa407ac25059fcd7d405cc5ecdb0c33"
13+
integrity sha512-/3DDPKHqqIqxUULp8yP4zODUY1i+2xvVWsv8A79xGWdCAG+8sb0hRh0Rk2QyOJUnnbyPUAZYcpBuRe3nS2OIUg==
14+
1015
"@ampproject/remapping@^2.2.0":
1116
version "2.3.0"
1217
resolved "https://registry.yarnpkg.com/@ampproject/remapping/-/remapping-2.3.0.tgz#ed441b6fa600072520ce18b43d2c8cc8caecc7f4"
@@ -1000,18 +1005,42 @@
10001005
"@jridgewell/resolve-uri" "^3.1.0"
10011006
"@jridgewell/sourcemap-codec" "^1.4.14"
10021007

1008+
"@lifi/types@^17.7.1":
1009+
version "17.16.0"
1010+
resolved "https://registry.yarnpkg.com/@lifi/types/-/types-17.16.0.tgz#bbbc582740b6cc7df7ad19120fdf1db2d7dc4915"
1011+
integrity sha512-jaJ1VGcfWLSjhQN65bIBSGs18K8U98avb8AlFwJvcROk237ueq6lD7Sd0OheAUohodI+v3udZ9KfrBp3SLghig==
1012+
dependencies:
1013+
viem "^2.24.3"
1014+
1015+
"@noble/ciphers@^1.3.0":
1016+
version "1.3.0"
1017+
resolved "https://registry.yarnpkg.com/@noble/ciphers/-/ciphers-1.3.0.tgz#f64b8ff886c240e644e5573c097f86e5b43676dc"
1018+
integrity sha512-2I0gnIVPtfnMw9ee9h1dJG7tp81+8Ob3OJb3Mv37rx5L40/b0i7djjCVvGOVqc9AEIQyvyu1i6ypKdFw8R8gQw==
1019+
10031020
10041021
version "1.2.0"
10051022
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.2.0.tgz#92d7e12e4e49b23105a2555c6984d41733d65c35"
10061023
integrity sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==
10071024
dependencies:
10081025
"@noble/hashes" "1.3.2"
10091026

1027+
"@noble/[email protected]", "@noble/curves@^1.6.0", "@noble/curves@~1.9.0":
1028+
version "1.9.1"
1029+
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.9.1.tgz#9654a0bc6c13420ae252ddcf975eaf0f58f0a35c"
1030+
integrity sha512-k11yZxZg+t+gWvBbIswW0yoJlu8cHOC7dhunwOzoWH/mXGBiYyR4YY6hAEK/3EUs4UpB8la1RfdRpeGsFHkWsA==
1031+
dependencies:
1032+
"@noble/hashes" "1.8.0"
1033+
10101034
10111035
version "1.3.2"
10121036
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.2.tgz#6f26dbc8fbc7205873ce3cee2f690eba0d421b39"
10131037
integrity sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==
10141038

1039+
"@noble/[email protected]", "@noble/hashes@^1.5.0", "@noble/hashes@~1.8.0":
1040+
version "1.8.0"
1041+
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.8.0.tgz#cee43d801fcef9644b11b8194857695acd5f815a"
1042+
integrity sha512-jCs9ldd7NwzpgXDIf6P3+NrHh9/sD6CQdxHyjQI+h/6rDNo88ypBxxz45UDuZHz9r3tNz7N/VInSVoVdtXEI4A==
1043+
10151044
"@nodelib/[email protected]":
10161045
version "2.1.5"
10171046
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
@@ -1033,6 +1062,28 @@
10331062
"@nodelib/fs.scandir" "2.1.5"
10341063
fastq "^1.6.0"
10351064

1065+
"@scure/base@~1.2.5":
1066+
version "1.2.5"
1067+
resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.2.5.tgz#f9d1b232425b367d0dcb81c96611dcc651d58671"
1068+
integrity sha512-9rE6EOVeIQzt5TSu4v+K523F8u6DhBsoZWPGKlnCshhlDhy0kJzUX4V+tr2dWmzF1GdekvThABoEQBGBQI7xZw==
1069+
1070+
"@scure/[email protected]", "@scure/bip32@^1.5.0":
1071+
version "1.7.0"
1072+
resolved "https://registry.yarnpkg.com/@scure/bip32/-/bip32-1.7.0.tgz#b8683bab172369f988f1589640e53c4606984219"
1073+
integrity sha512-E4FFX/N3f4B80AKWp5dP6ow+flD1LQZo/w8UnLGYZO674jS6YnYeepycOOksv+vLPSpgN35wgKgy+ybfTb2SMw==
1074+
dependencies:
1075+
"@noble/curves" "~1.9.0"
1076+
"@noble/hashes" "~1.8.0"
1077+
"@scure/base" "~1.2.5"
1078+
1079+
"@scure/[email protected]", "@scure/bip39@^1.4.0":
1080+
version "1.6.0"
1081+
resolved "https://registry.yarnpkg.com/@scure/bip39/-/bip39-1.6.0.tgz#475970ace440d7be87a6086cbee77cb8f1a684f9"
1082+
integrity sha512-+lF0BbLiJNwVlev4eKelw1WWLaiKXw7sSl8T6FvBlWkdX+94aGJ4o8XjUdlyhTCjd8c+B3KT3JfS8P0bLRNU6A==
1083+
dependencies:
1084+
"@noble/hashes" "~1.8.0"
1085+
"@scure/base" "~1.2.5"
1086+
10361087
"@sinclair/typebox@^0.27.8":
10371088
version "0.27.8"
10381089
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.27.8.tgz#6667fac16c436b5434a387a34dedb013198f6e6e"
@@ -1374,6 +1425,11 @@
13741425
"@typescript-eslint/types" "8.23.0"
13751426
eslint-visitor-keys "^4.2.0"
13761427

1428+
[email protected], abitype@^1.0.6:
1429+
version "1.0.8"
1430+
resolved "https://registry.yarnpkg.com/abitype/-/abitype-1.0.8.tgz#3554f28b2e9d6e9f35eb59878193eabd1b9f46ba"
1431+
integrity sha512-ZeiI6h3GnW06uYDLx0etQtX/p8E24UaHHBj57RSjK7YBFe7iuVn07EDpOeP451D06sF27VOz9JJPlIKJmXgkEg==
1432+
13771433
abort-controller@^3.0.0:
13781434
version "3.0.0"
13791435
resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392"
@@ -1443,16 +1499,18 @@ ajv@^6.12.4:
14431499
json-schema-traverse "^0.4.1"
14441500
uri-js "^4.2.2"
14451501

1446-
"ambire-common@github:AmbireTech/ambire-common#677651a49c49cf49ff7864bd1bd6dc32b580c789":
1447-
version "2.44.0"
1448-
resolved "https://codeload.github.com/AmbireTech/ambire-common/tar.gz/677651a49c49cf49ff7864bd1bd6dc32b580c789"
1502+
"ambire-common@github:AmbireTech/ambire-common#378bba59e18430bfcc411504ca3dbc607f32d214":
1503+
version "2.55.0"
1504+
resolved "https://codeload.github.com/AmbireTech/ambire-common/tar.gz/378bba59e18430bfcc411504ca3dbc607f32d214"
14491505
dependencies:
1506+
"@lifi/types" "^17.7.1"
14501507
aes-js "^3.1.2"
14511508
dotenv "^16.3.1"
14521509
eth-crypto "^2.6.0"
14531510
eth-rpc-errors "^4.0.3"
14541511
ethers "^6.8.0"
14551512
events "^3.3.0"
1513+
js-yaml "^4.1.0"
14561514
scrypt-js "^3.0.1"
14571515
uuid "9.0.0"
14581516

@@ -2438,7 +2496,7 @@ event-target-shim@^5.0.0:
24382496
resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789"
24392497
integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==
24402498

2441-
eventemitter3@^5.0.1:
2499+
eventemitter3@5.0.1, eventemitter3@^5.0.1:
24422500
version "5.0.1"
24432501
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-5.0.1.tgz#53f5ffd0a492ac800721bb42c66b841de96423c4"
24442502
integrity sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==
@@ -3032,6 +3090,11 @@ isexe@^2.0.0:
30323090
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
30333091
integrity sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==
30343092

3093+
3094+
version "1.0.7"
3095+
resolved "https://registry.yarnpkg.com/isows/-/isows-1.0.7.tgz#1c06400b7eed216fbba3bcbd68f12490fc342915"
3096+
integrity sha512-I1fSfDCZL5P0v33sVqeTDSpcstAg/N+wF5HS033mogOVIp4B+oHC7oOCsA3axAbBSGTJ8QubbNmnIRN/h8U7hg==
3097+
30353098
istanbul-lib-coverage@^3.0.0, istanbul-lib-coverage@^3.2.0:
30363099
version "3.2.2"
30373100
resolved "https://registry.yarnpkg.com/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.2.tgz#2d166c4b0644d43a39f04bf6c2edd1e585f31756"
@@ -3918,6 +3981,20 @@ optionator@^0.9.3:
39183981
type-check "^0.4.0"
39193982
word-wrap "^1.2.5"
39203983

3984+
3985+
version "0.7.1"
3986+
resolved "https://registry.yarnpkg.com/ox/-/ox-0.7.1.tgz#fb23a770dd966c051ad916d4e2e655a6f995e1cf"
3987+
integrity sha512-+k9fY9PRNuAMHRFIUbiK9Nt5seYHHzSQs9Bj+iMETcGtlpS7SmBzcGSVUQO3+nqGLEiNK4598pHNFlVRaZbRsg==
3988+
dependencies:
3989+
"@adraffy/ens-normalize" "^1.10.1"
3990+
"@noble/ciphers" "^1.3.0"
3991+
"@noble/curves" "^1.6.0"
3992+
"@noble/hashes" "^1.5.0"
3993+
"@scure/bip32" "^1.5.0"
3994+
"@scure/bip39" "^1.4.0"
3995+
abitype "^1.0.6"
3996+
eventemitter3 "5.0.1"
3997+
39213998
p-limit@^2.2.0:
39223999
version "2.3.0"
39234000
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.3.0.tgz#3dd33c647a214fdfffd835933eb086da0dc21db1"
@@ -4743,6 +4820,20 @@ vary@~1.1.2:
47434820
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
47444821
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==
47454822

4823+
viem@^2.24.3:
4824+
version "2.30.1"
4825+
resolved "https://registry.yarnpkg.com/viem/-/viem-2.30.1.tgz#22cdd653c57b4ff342524afd6974b08b302a7875"
4826+
integrity sha512-CkoS5Vv6kiRGmRF2xO2z275Gu90vTrKZHf/ckYXxP2J94UvCnFvUcbRdfit6uebj1I8nFwkGlkkOMuOZDHyO4w==
4827+
dependencies:
4828+
"@noble/curves" "1.9.1"
4829+
"@noble/hashes" "1.8.0"
4830+
"@scure/bip32" "1.7.0"
4831+
"@scure/bip39" "1.6.0"
4832+
abitype "1.0.8"
4833+
isows "1.0.7"
4834+
ox "0.7.1"
4835+
ws "8.18.2"
4836+
47464837
walker@^1.0.8:
47474838
version "1.0.8"
47484839
resolved "https://registry.yarnpkg.com/walker/-/walker-1.0.8.tgz#bd498db477afe573dc04185f011d3ab8a8d7653f"
@@ -4821,6 +4912,11 @@ [email protected]:
48214912
resolved "https://registry.yarnpkg.com/ws/-/ws-8.17.1.tgz#9293da530bb548febc95371d90f9c878727d919b"
48224913
integrity sha512-6XQFvXTkbfUOZOKKILFG1PDK2NDQs4azKQl26T0YS5CxqWLgXajbPZ+h4gZekJyRqFU8pvnbAbbs/3TgRPy+GQ==
48234914

4915+
4916+
version "8.18.2"
4917+
resolved "https://registry.yarnpkg.com/ws/-/ws-8.18.2.tgz#42738b2be57ced85f46154320aabb51ab003705a"
4918+
integrity sha512-DMricUmwGZUVr++AEAe2uiVM7UoO9MAVZMDu05UQOaUII0lp+zOzLLU4Xqh/JvTqklB1T4uELaaPBKyjE1r4fQ==
4919+
48244920
y18n@^5.0.5:
48254921
version "5.0.8"
48264922
resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.8.tgz#7f4934d0f7ca8c56f95314939ddcd2dd91ce1d55"

0 commit comments

Comments
 (0)