-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(companion-app): map internal chain id to debank chain id (#767)
Fixes #766
- Loading branch information
1 parent
f3e4bb0
commit 40853ff
Showing
5 changed files
with
64 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { getDeBankApiKey } from '@zodiac/env' | ||
import type { z, ZodTypeAny } from 'zod' | ||
|
||
type ApiOptions<Schema extends ZodTypeAny> = { | ||
schema: Schema | ||
data?: Record<string, string | number | boolean> | ||
} | ||
|
||
export const api = async <Schema extends ZodTypeAny>( | ||
endpoint: `/${string}`, | ||
{ schema, data = {} }: ApiOptions<Schema>, | ||
) => { | ||
const url = new URL(`/v1${endpoint}`, 'https://pro-openapi.debank.com') | ||
|
||
Object.entries(data).forEach(([key, value]) => | ||
url.searchParams.set(key, value.toString()), | ||
) | ||
|
||
const response = await fetch(url, { | ||
headers: { AccessKey: getDeBankApiKey() }, | ||
}) | ||
|
||
const json = await response.json() | ||
return schema.parse(json) as z.infer<Schema> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { invariantResponse } from '@epic-web/invariant' | ||
import type { ChainId } from 'ser-kit' | ||
import { chainListSchema } from '../types' | ||
import { api } from './api' | ||
|
||
export const getDeBankChainId = async (chainId: ChainId) => { | ||
const chains = await api('/chain/list', { schema: chainListSchema }) | ||
|
||
const chain = chains.find((chain) => chain.community_id === chainId) | ||
|
||
invariantResponse( | ||
chain != null, | ||
`Could not find a chain on DeBank with community_id "${chainId}"`, | ||
) | ||
|
||
return chain.id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters