Skip to content

Commit 0844a36

Browse files
committed
add custom metadata and provider services
1 parent 9406799 commit 0844a36

File tree

6 files changed

+105
-50
lines changed

6 files changed

+105
-50
lines changed

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,5 @@
7171
"typescript": "^5.7.2",
7272
"vite-tsconfig-paths": "^5.1.3",
7373
"vitest": "^2.1.6"
74-
},
75-
"pnpm": {
76-
"patchedDependencies": {
77-
78-
}
7974
}
8075
}

patches/@[email protected]

Lines changed: 0 additions & 37 deletions
This file was deleted.

pnpm-lock.yaml

Lines changed: 2 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common/utils/tokenList.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ const cache: Record<number, TokenListItem[]> = {
2626
export default function getTokenList(chainId: number): TokenListItem[] {
2727
return cache[chainId]
2828
}
29+
30+
export function getAllTokenLists() {
31+
return cache
32+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { getAllTokenLists } from "@/common/utils/tokenList"
2+
import type {
3+
ChainId,
4+
FieldsRequirements,
5+
SupportInChain,
6+
TimeString,
7+
TokenAddress,
8+
} from "@balmy/sdk"
9+
10+
import type {
11+
BaseTokenMetadata,
12+
IMetadataSource,
13+
MetadataInput,
14+
MetadataResult,
15+
} from "@balmy/sdk/dist/services/metadata/types"
16+
import { type Address, isAddressEqual } from "viem"
17+
18+
export class TokenlistMetadataSource
19+
implements IMetadataSource<BaseTokenMetadata>
20+
{
21+
async getMetadata<
22+
Requirements extends FieldsRequirements<BaseTokenMetadata>,
23+
>(params: {
24+
tokens: MetadataInput[]
25+
config?: { timeout?: TimeString }
26+
}) {
27+
const result: Record<ChainId, Record<TokenAddress, BaseTokenMetadata>> = {}
28+
const allTokens = getAllTokenLists()
29+
for (const { chainId, token } of params.tokens) {
30+
const tokenListItem = allTokens[chainId]?.find((t) =>
31+
isAddressEqual(t.addressInfo, token as Address),
32+
)
33+
if (tokenListItem) {
34+
if (!result[chainId]) result[chainId] = {}
35+
result[chainId][token] = {
36+
decimals: tokenListItem.decimals,
37+
symbol: tokenListItem.symbol,
38+
}
39+
}
40+
}
41+
42+
return result as Record<
43+
ChainId,
44+
Record<TokenAddress, MetadataResult<BaseTokenMetadata, Requirements>>
45+
>
46+
}
47+
48+
supportedProperties() {
49+
const properties: SupportInChain<BaseTokenMetadata> = {
50+
symbol: "present",
51+
decimals: "present",
52+
}
53+
return Object.fromEntries(
54+
Object.keys(getAllTokenLists()).map((chainId) => [chainId, properties]),
55+
)
56+
}
57+
}

src/swapService/strategies/strategyBalmySDK.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {
22
type BuildParams,
3+
type Chain,
34
type QuoteRequest,
45
type QuoteResponse,
56
type QuoteResponseWithTx,
67
type SourceId,
78
buildSDK,
9+
getAllChains,
810
} from "@balmy/sdk"
911
import { buildFetchService } from "@balmy/sdk/dist/sdk/builders/fetch-builder"
1012
import { buildProviderService } from "@balmy/sdk/dist/sdk/builders/provider-builder"
@@ -36,6 +38,7 @@ import {
3638
quoteToRoute,
3739
} from "../utils"
3840
import { CustomSourceList } from "./balmySDK/customSourceList"
41+
import { TokenlistMetadataSource } from "./balmySDK/tokenlistMetadataSource"
3942

4043
const DAO_MULTISIG = "0xcAD001c30E96765aC90307669d578219D4fb1DCe"
4144
const DEFAULT_TIMEOUT = "30000"
@@ -126,6 +129,21 @@ export class StrategyBalmySDK {
126129
},
127130
},
128131
},
132+
metadata: {
133+
source: {
134+
type: "custom",
135+
instance: new TokenlistMetadataSource(),
136+
},
137+
},
138+
provider: {
139+
source: {
140+
type: "public-rpcs",
141+
rpcsPerChain: combinePublicAndPrivateRPCs(),
142+
config: {
143+
type: "fallback",
144+
},
145+
},
146+
},
129147
} as BuildParams
130148
this.sdk = buildSDK(buildParams)
131149
this.match = match
@@ -500,3 +518,27 @@ export class StrategyBalmySDK {
500518
}
501519
}
502520
}
521+
522+
function combinePublicAndPrivateRPCs() {
523+
const rpcs = Object.fromEntries(
524+
getAllChains()
525+
.filter(
526+
(chain): chain is Chain & { publicRPCs: string[] } =>
527+
chain.publicRPCs.length > 0,
528+
)
529+
.map(({ chainId, publicRPCs }) => [chainId, publicRPCs]),
530+
)
531+
532+
const envRPCs = Object.entries(process.env).filter(([key]) =>
533+
/^RPC_URL_/.test(key),
534+
)
535+
536+
for (const [key, val] of envRPCs) {
537+
if (typeof val !== "string") return
538+
const chainId = Number(key.split("_").at(-1))
539+
if (!rpcs[chainId]) rpcs[chainId] = []
540+
if (!rpcs[chainId].includes(val)) rpcs[chainId].unshift(val)
541+
}
542+
543+
return rpcs
544+
}

0 commit comments

Comments
 (0)