Skip to content

Commit

Permalink
fix polygon mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
matteenm committed Nov 4, 2024
1 parent da286b8 commit fb8a716
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
30 changes: 30 additions & 0 deletions src/constants/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,33 @@ export type GenericMappedTokenData = { [key: string]: string | undefined }
export type PolygonMappedTokenData = { [key: string]: PolygonMappedToken }

export type MappedTokenData = { [key: string]: MappedToken }

export interface PolygonWrappedToken {
wrappedTokenAddress: string
wrappedNetworkId?: number
tags: string[]
originChainBridgeAdapter?: string
wrappedChainBridgeAdapter?: string
}

export interface PolygonTokenMapping {
chainId: number
name: string
symbol: string
decimals: number
originTokenAddress: string
originNetworkId: number
tags: string[]
wrappedTokens: PolygonWrappedToken[]
logoURI: string
}

export interface PolygonTokenListResponse {
name: string
version: number
logoURI: string
description: string
tags: Record<string, { name: string; description: string }>
timestamp: string
tokens: PolygonTokenMapping[]
}
32 changes: 20 additions & 12 deletions src/providers/PolygonMappingProvider.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import axios from 'axios'
import { MappingProvider } from './MappingProvider'
import { PolygonMappedTokenData } from '../constants/types'
import {
PolygonMappedTokenData,
PolygonTokenListResponse,
PolygonWrappedToken,
} from '../constants/types'

// called from https://mapper.polygon.technology
const url =
'https://api-polygon-tokens.polygon.technology/api/v1/info/all-mappings'
const access_token = '504afd90-3228-4df9-9d88-9b4d70646101'
'https://api-polygon-tokens.polygon.technology/tokenlists/mapped.tokenlist.json'

const POS_BRIDGE_TAG = 'pos'

/**
* The Polygon team manually maintains the mapping via user submissions at
* https://mapper.polygon.technology.
*
* This provider provides the l1->l2(Polygon) token mappings.
*/
export class PolygonMappingProvider implements MappingProvider {
async provide(): Promise<PolygonMappedTokenData> {
const response = await axios.get(url, {
headers: { 'x-access-token': access_token },
})
const response = await axios.get<PolygonTokenListResponse>(url)
const tokens: PolygonMappedTokenData = {}

for (const token of response.data) {
if (token.isPos) {
tokens[token.rootToken.toLowerCase()] = token
for (const token of response.data.tokens) {
const posToken = token.wrappedTokens.find(
(wrapped: PolygonWrappedToken) => wrapped.tags.includes(POS_BRIDGE_TAG)
)

if (posToken) {
tokens[token.originTokenAddress.toLowerCase()] = {
rootToken: token.originTokenAddress,
childToken: posToken.wrappedTokenAddress,
isPos: true,
}
}
}
return tokens
Expand Down

0 comments on commit fb8a716

Please sign in to comment.