diff --git a/ios/Podfile.lock b/ios/Podfile.lock index a85de240a..109a1773f 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -841,7 +841,7 @@ SPEC CHECKSUMS: BEMCheckBox: 5ba6e37ade3d3657b36caecc35c8b75c6c2b1a4e boost: 57d2868c099736d80fcd648bf211b4431e51a558 BVLinearGradient: 34a999fda29036898a09c6a6b728b0b4189e1a44 - Charts: ce0768268078eee0336f122c3c4ca248e4e204c5 + Charts: 354f86803d11d9c35de280587fef50d1af063978 DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54 EXApplication: d8f53a7eee90a870a75656280e8d4b85726ea903 EXBarCodeScanner: 8e23fae8d267dbef9f04817833a494200f1fce35 @@ -861,9 +861,9 @@ SPEC CHECKSUMS: FBLazyVector: f1897022b53abf1469d6ad692ee2c69f57d967f3 FBReactNativeSpec: 627fd07f1b9d498c9fa572e76d7f1a6b1ee9a444 fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9 - glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b + glog: 791fe035093b84822da7f0870421a25839ca7870 helium-react-native-sdk: 32c0a7e3abc733a7f3d291013b2db31475fc6980 - hermes-engine: 0784cadad14b011580615c496f77e0ae112eed75 + hermes-engine: 7a53ccac09146018a08239c5425625fdb79a6162 libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913 MapboxCommon: fdf7fd31c90b7b607cd9c63e37797f023c01d860 MapboxCoreMaps: 24270c7c6b8cb71819fc2f3c549db9620ee4d019 @@ -871,7 +871,7 @@ SPEC CHECKSUMS: MapboxMobileEvents: de50b3a4de180dd129c326e09cd12c8adaaa46d6 MultiplatformBleAdapter: 5a6a897b006764392f9cef785e4360f54fb9477d OneSignalXCFramework: 81ceac017a290f23793443323090cfbe888f74ea - RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1 + RCT-Folly: 85766c3226c7ec638f05ad7cb3cf6a268d6c4241 RCTRequired: bd6045fbd511da5efe6db89eecb21e4e36bd7cbf RCTTypeSafety: c06d9f906faa69dd1c88223204c3a24767725fd8 React: b9ea33557ef1372af247f95d110fbdea114ed3b2 diff --git a/package.json b/package.json index 3e5464d4d..510369a2b 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "@helium/treasury-management-sdk": "0.1.2", "@helium/voter-stake-registry-sdk": "0.1.2", "@helium/wallet-link": "4.10.0", + "@jup-ag/api": "^6.0.6", "@ledgerhq/hw-app-helium": "6.29.1", "@ledgerhq/react-native-hid": "6.28.4", "@ledgerhq/react-native-hw-transport-ble": "6.27.2", diff --git a/src/storage/JupiterProvider.tsx b/src/storage/JupiterProvider.tsx new file mode 100644 index 000000000..7c1cfc349 --- /dev/null +++ b/src/storage/JupiterProvider.tsx @@ -0,0 +1,105 @@ +import { Configuration, DefaultApi } from '@jup-ag/api' +import { TokenInfo, TokenListProvider } from '@solana/spl-token-registry' +import React, { + createContext, + useContext, + useEffect, + useMemo, + useState, +} from 'react' +import { useSolana } from 'src/solana/SolanaProvider' + +type RouteMap = Map +interface IJupiterContextState { + api: DefaultApi + loaded: boolean + tokenMap: Map + routeMap: RouteMap +} + +const JupiterContext = createContext(null) +export const JupiterProvider: React.FC = ({ children }) => { + const { cluster } = useSolana() + const [tokenMap, setTokenMap] = useState>(new Map()) + const [routeMap, setRouteMap] = useState(new Map()) + const [loaded, setLoaded] = useState(false) + const api = useMemo(() => { + const config = new Configuration({ + basePath: 'https://quote-api.jup.ag/v6', + }) + return new DefaultApi(config) + }, []) + + useEffect(() => { + ;(async () => { + const [tokens, indexedRouteMapResult] = await Promise.all([ + new TokenListProvider().resolve(), + api.indexedRouteMapGet(), + ]) + + const tokenList = tokens.filterByClusterSlug(cluster).getList() + const { indexedRouteMap = {}, mintKeys = [] } = indexedRouteMapResult + + setTokenMap( + tokenList.reduce((map, item) => { + map.set(item.address, item) + return map + }, new Map()), + ) + + setRouteMap( + Object.keys(indexedRouteMap).reduce((map, key) => { + map.set( + mintKeys[Number(key)], + indexedRouteMap[key].map((index) => mintKeys[index]), + ) + return map + }, new Map()), + ) + + setLoaded(true) + })() + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []) + + /* const getQuote = useCallback( + () => + api.quoteGet({ + inputMint: '', + outputMint: '', + amount: 100000, + slippageBps: 20, + platformFeeBps: 20, + }), + [api], + ) */ + + /* const swap = useCallback(() => { + api.swapPost({ + + }) + }, [api]) */ + + return ( + + {children} + + ) +} + +export const useJupiter = () => { + const context = useContext(JupiterContext) + + if (!context) { + throw new Error('useJupiter must be used within a JupiterProvider') + } + + return context +} diff --git a/yarn.lock b/yarn.lock index 3dfabc494..c05ecf360 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2992,6 +2992,11 @@ "@jridgewell/resolve-uri" "3.1.0" "@jridgewell/sourcemap-codec" "1.4.14" +"@jup-ag/api@^6.0.6": + version "6.0.6" + resolved "https://registry.yarnpkg.com/@jup-ag/api/-/api-6.0.6.tgz#f0f838aaf8ca5964d7cb9ff24535599bf4d03813" + integrity sha512-wGp3FjT+WNdd5TzsWaKr2a2rQ2FgIyaV6sq8L76R8KVYxH7TfCY/BZwhcij6YhxHGjxrF8j7qI6ZPpmUTICpnA== + "@koale/useworker@^4.0.2": version "4.0.2" resolved "https://registry.yarnpkg.com/@koale/useworker/-/useworker-4.0.2.tgz#cb540a2581cd6025307c3ca6685bc60748773e58"