{"version":3,"file":"fetch-positions.mjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/fetch-positions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,wCAAoC;AA2D/D,gDAAgD;AAChD,MAAM,CAAC,MAAM,sBAAsB,GAAG,yCAAyC,CAAC;AAEhF,MAAM,mBAAmB,GAAG,IAAK,CAAC;AAClC,MAAM,WAAW,GAAG,CAAC,CAAC;AAEtB;;;;GAIG;AACH,MAAM,UAAU,oBAAoB;IAClC,OAAO,KAAK,EAAE,cAAsB,EAAmC,EAAE;QACvE,MAAM,qBAAqB,GAAG,MAAM,gBAAgB,CAClD,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,sBAAsB,cAAc,cAAc,EAAE,CAAC,EACpE,mBAAmB,EACnB,WAAW,CACZ,CAAC;QAEF,IAAI,qBAAqB,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;YACzC,MAAM,IAAI,KAAK,CACb,yCAAyC,qBAAqB,CAAC,MAAM,EAAE,CACxE,CAAC;QACJ,CAAC;QAED,OAAO,CAAC,MAAM,qBAAqB,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC;IACnD,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { timeoutWithRetry } from '../utils/timeout-with-retry';\n\nexport type DefiPositionResponse = AdapterResponse<{\n  tokens: ProtocolToken[];\n}>;\n\ntype ProtocolDetails = {\n  chainId: number;\n  protocolId: string;\n  productId: string;\n  protocolDisplayName: string;\n  name: string;\n  description: string;\n  iconUrl: string;\n  siteUrl: string;\n  positionType: PositionType;\n  metadata?: {\n    groupPositions?: boolean;\n  };\n};\n\ntype AdapterResponse<ProtocolResponse> =\n  | (ProtocolDetails & {\n      chainName: string;\n    } & (\n        | (ProtocolResponse & { success: true })\n        | (AdapterErrorResponse & { success: false })\n      ))\n  | (AdapterErrorResponse & { success: false });\n\ntype AdapterErrorResponse = {\n  error: {\n    message: string;\n  };\n};\n\nexport type PositionType = 'supply' | 'borrow' | 'stake' | 'reward';\n\nexport type ProtocolToken = Balance & {\n  type: 'protocol';\n  tokenId?: string;\n};\n\nexport type Underlying = Balance & {\n  type: 'underlying' | 'underlying-claimable';\n  iconUrl: string;\n};\n\nexport type Balance = {\n  address: string;\n  name: string;\n  symbol: string;\n  decimals: number;\n  balanceRaw: string;\n  balance: number;\n  price?: number;\n  tokens?: Underlying[];\n};\n\n// TODO: Update with prod API URL when available\nexport const DEFI_POSITIONS_API_URL = 'https://defiadapters.api.cx.metamask.io';\n\nconst EIGHT_SECONDS_IN_MS = 8_000;\nconst MAX_RETRIES = 1;\n\n/**\n * Builds a function that fetches DeFi positions for a given account address\n *\n * @returns A function that fetches DeFi positions for a given account address\n */\nexport function buildPositionFetcher() {\n  return async (accountAddress: string): Promise<DefiPositionResponse[]> => {\n    const defiPositionsResponse = await timeoutWithRetry(\n      () => fetch(`${DEFI_POSITIONS_API_URL}/positions/${accountAddress}`),\n      EIGHT_SECONDS_IN_MS,\n      MAX_RETRIES,\n    );\n\n    if (defiPositionsResponse.status !== 200) {\n      throw new Error(\n        `Unable to fetch defi positions - HTTP ${defiPositionsResponse.status}`,\n      );\n    }\n\n    return (await defiPositionsResponse.json()).data;\n  };\n}\n"]}