Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bryzettler committed Sep 5, 2023
1 parent d8d5e48 commit cfcca85
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 4 deletions.
8 changes: 4 additions & 4 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ SPEC CHECKSUMS:
BEMCheckBox: 5ba6e37ade3d3657b36caecc35c8b75c6c2b1a4e
boost: 57d2868c099736d80fcd648bf211b4431e51a558
BVLinearGradient: 34a999fda29036898a09c6a6b728b0b4189e1a44
Charts: ce0768268078eee0336f122c3c4ca248e4e204c5
Charts: 354f86803d11d9c35de280587fef50d1af063978
DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54
EXApplication: d8f53a7eee90a870a75656280e8d4b85726ea903
EXBarCodeScanner: 8e23fae8d267dbef9f04817833a494200f1fce35
Expand All @@ -861,17 +861,17 @@ 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
MapboxMaps: cb76511b98d3b95c74b0771ed105bc69f30ace6b
MapboxMobileEvents: de50b3a4de180dd129c326e09cd12c8adaaa46d6
MultiplatformBleAdapter: 5a6a897b006764392f9cef785e4360f54fb9477d
OneSignalXCFramework: 81ceac017a290f23793443323090cfbe888f74ea
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
RCT-Folly: 85766c3226c7ec638f05ad7cb3cf6a268d6c4241
RCTRequired: bd6045fbd511da5efe6db89eecb21e4e36bd7cbf
RCTTypeSafety: c06d9f906faa69dd1c88223204c3a24767725fd8
React: b9ea33557ef1372af247f95d110fbdea114ed3b2
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
105 changes: 105 additions & 0 deletions src/storage/JupiterProvider.tsx
Original file line number Diff line number Diff line change
@@ -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<string, string[]>
interface IJupiterContextState {
api: DefaultApi
loaded: boolean
tokenMap: Map<string, TokenInfo>
routeMap: RouteMap
}

const JupiterContext = createContext<IJupiterContextState | null>(null)
export const JupiterProvider: React.FC = ({ children }) => {
const { cluster } = useSolana()
const [tokenMap, setTokenMap] = useState<Map<string, TokenInfo>>(new Map())
const [routeMap, setRouteMap] = useState<RouteMap>(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<string, string[]>()),
)

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 (
<JupiterContext.Provider
value={{
api,
routeMap,
tokenMap,
loaded,
}}
>
{children}
</JupiterContext.Provider>
)
}

export const useJupiter = () => {
const context = useContext(JupiterContext)

if (!context) {
throw new Error('useJupiter must be used within a JupiterProvider')
}

return context
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit cfcca85

Please sign in to comment.