Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 67c3bd4

Browse files
committedDec 30, 2022
use use-cardano
1 parent f991169 commit 67c3bd4

16 files changed

+88
-210
lines changed
 

‎.env.template

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
BLOCKFROST_PROJECT_ID_TESTNET=testnetMySecretBlockFrostProjectId
2+
BLOCKFROST_PROJECT_ID_PREVIEW=previewMySecretBlockFrostProjectId
23
BLOCKFROST_PROJECT_ID_MAINNET=mainnetMySecretBlockFrostProjectId

‎hooks/use-image.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef, useState } from "react"
1+
import { useEffect, useState } from "react"
22

33
const useImage = (src?: string) => {
44
const [loaded, setLoaded] = useState(false)
@@ -8,7 +8,6 @@ const useImage = (src?: string) => {
88
if (!src) return
99

1010
var loadedImage = new Image()
11-
loadedImage.crossOrigin = "Anonymous"
1211

1312
loadedImage.onload = function () {
1413
setImage(this as HTMLImageElement)

‎hooks/use-lucid/use-has-nami-extension.ts

-21
This file was deleted.

‎hooks/use-lucid/use-lucid.ts

-39
This file was deleted.

‎hooks/use-lucid/use-network-id.ts

-26
This file was deleted.

‎hooks/use-lucid/use-wallet-api.ts

-16
This file was deleted.

‎hooks/use-tooligans-assets.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { sortBy } from "lodash"
1+
import { isNil, sortBy } from "lodash"
22
import { fromUnit, Lucid } from "lucid-cardano"
33
import { useCallback, useEffect, useState } from "react"
4+
import { utility } from "use-cardano"
45

56
import { Responses } from "@blockfrost/blockfrost-js"
67

@@ -17,7 +18,7 @@ const useTooligansAssets = (lucid?: Lucid, networkId?: number): Responses["asset
1718
const [tooligansAssets, setTooligansAssets] = useState<Responses["asset"][]>([])
1819

1920
const fetchTooligans = useCallback(async () => {
20-
if (!lucid?.wallet) return
21+
if (!lucid?.wallet || isNil(networkId)) return
2122

2223
const utxos = await lucid.wallet.getUtxos()
2324

@@ -39,9 +40,9 @@ const useTooligansAssets = (lucid?: Lucid, networkId?: number): Responses["asset
3940

4041
const assetsWithMetadata: Responses["asset"][] = await Promise.all(
4142
utxoAssets.map((a) =>
42-
fetch(`/api/blockfrost/${networkId}/assets/${a.fullyQualifiedAssetName}`).then((r) =>
43-
r.json()
44-
)
43+
fetch(
44+
`/api/blockfrost/${utility.toNetworkName(networkId)}/assets/${a.fullyQualifiedAssetName}`
45+
).then((r) => r.json())
4546
)
4647
)
4748

‎hooks/use-tooligans-images.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const useTooligansImages = (tooligansAssets: Responses["asset"][]): Image[] => {
88
const [loaded, setLoaded] = useState(false)
99
const [tooligansImages, setTooligansImages] = useState<Image[]>([])
1010

11+
console.log(tooligansAssets)
12+
1113
useEffect(() => {
1214
if (loaded || tooligansAssets.length <= 0) return
1315

@@ -16,9 +18,7 @@ const useTooligansImages = (tooligansAssets: Responses["asset"][]): Image[] => {
1618
Promise.all(
1719
tooligansAssets.map((tooligan) =>
1820
asyncImageLoading(
19-
tooligan?.onchain_metadata?.image
20-
?.toString()
21-
?.replace("ipfs://", "https://ipfs.blockfrost.dev/ipfs/"),
21+
tooligan?.onchain_metadata?.image?.toString()?.replace("ipfs://", "/tooligan-image/"),
2222
makeTransparent
2323
)
2424
)

‎lib/async-image-loading.ts

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const asyncImageLoading = (src?: string, onLoad?: OnLoad): Promise<Image> =>
88
if (!src) return resolve(null)
99

1010
const image = new Image()
11-
image.crossOrigin = "Anonymous"
1211

1312
image.onload = function () {
1413
if (onLoad) {

‎next.config.js

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ const nextConfig = {
1111

1212
return config
1313
},
14+
// NOTE: Avoid CORS problems with IPFS
15+
rewrites: async () => [
16+
{
17+
source: "/tooligan-image/:path*",
18+
destination: "https://ipfs.blockfrost.dev/ipfs/:path*",
19+
},
20+
],
1421
}
1522

1623
module.exports = nextConfig

‎package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
},
1515
"dependencies": {
1616
"gameloop": "^1.1.4",
17+
"http-proxy": "^1.18.1",
1718
"lodash": "^4.17.21",
18-
"lucid-cardano": "^0.7.2",
19+
"lucid-cardano": "^0.8.3",
1920
"next": "13.0.0",
20-
"next-http-proxy-middleware": "^1.2.5",
2121
"react": "18.2.0",
2222
"react-dom": "18.2.0",
23-
"typescript": "^4.7.4"
23+
"typescript": "^4.7.4",
24+
"use-cardano": "1.0.0-beta.19",
25+
"use-cardano-blockfrost-proxy": "^1.0.0"
2426
},
2527
"devDependencies": {
2628
"@blockfrost/blockfrost-js": "^5.0.0",

‎pages/_app.tsx

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1+
import "use-cardano/styles/use-cardano.css"
12
import "../styles/styles.css"
23

4+
import { CardanoProvider, CardanoToaster, UseCardanoOptions } from "use-cardano"
5+
36
import type { AppProps } from "next/app"
47

8+
const options: UseCardanoOptions = {
9+
node: {
10+
provider: "blockfrost-proxy",
11+
proxyUrl: "/api/blockfrost",
12+
},
13+
}
14+
515
export default function App({ Component, pageProps }: AppProps) {
6-
return <Component {...pageProps} />
16+
return (
17+
<CardanoProvider options={options}>
18+
<Component {...pageProps} />
19+
20+
<CardanoToaster />
21+
</CardanoProvider>
22+
)
723
}

‎pages/api/blockfrost/[[...all]].ts

+1-49
Original file line numberDiff line numberDiff line change
@@ -1,49 +1 @@
1-
import { NextApiHandler } from 'next';
2-
import httpProxyMiddleware from 'next-http-proxy-middleware';
3-
4-
const getTarget = (url?: string) => {
5-
if (url?.startsWith("/api/blockfrost/0")) return "https://cardano-testnet.blockfrost.io/api/v0"
6-
if (url?.startsWith("/api/blockfrost/1")) return "https://cardano-mainnet.blockfrost.io/api/v0"
7-
return null
8-
}
9-
10-
const getProjectId = (url?: string) => {
11-
if (url?.startsWith("/api/blockfrost/0")) return process.env.BLOCKFROST_PROJECT_ID_TESTNET
12-
if (url?.startsWith("/api/blockfrost/1")) return process.env.BLOCKFROST_PROJECT_ID_MAINNET
13-
return null
14-
}
15-
16-
const blockfrostProxy: NextApiHandler = async (req, res) => {
17-
const target = getTarget(req.url)
18-
const PROJECT_ID = getProjectId(req.url)
19-
20-
try {
21-
if (!target || !PROJECT_ID) throw new Error("Invalid target or project id")
22-
23-
const response = await httpProxyMiddleware(req, res, {
24-
target,
25-
headers: {
26-
PROJECT_ID,
27-
},
28-
pathRewrite: [
29-
{
30-
patternStr: "^/api/blockfrost/0",
31-
replaceStr: "",
32-
},
33-
{
34-
patternStr: "^/api/blockfrost/1",
35-
replaceStr: "",
36-
},
37-
],
38-
})
39-
40-
return response
41-
} catch (e) {
42-
console.error("Blockfrost proxy error", e)
43-
44-
// NOTE(Alan): Not sure if this is compatible with Lucid / the Blockfrost provider
45-
return res.status(400).end()
46-
}
47-
}
48-
49-
export default blockfrostProxy
1+
export { blockfrostProxy as default } from "use-cardano-blockfrost-proxy"

‎pages/index.tsx

+9-22
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import { LevelSelect } from "components/LevelSelect"
44
import { defaultLevel, levels } from "data/levels"
55
import { useCanvas } from "hooks/use-canvas"
66
import { useImage } from "hooks/use-image"
7-
import { useHasNamiExtension } from "hooks/use-lucid/use-has-nami-extension"
8-
import { useLucid } from "hooks/use-lucid/use-lucid"
97
import { useTooligans } from "hooks/use-tooligans"
10-
import { useEffect, useMemo, useState } from "react"
8+
import { useMemo, useState } from "react"
9+
import { CardanoWalletSelector, useCardano } from "use-cardano"
1110

1211
import styles from "../styles/index.module.css"
1312

@@ -26,30 +25,18 @@ const Index = () => {
2625
const canvas = useCanvas()
2726
const { image: ballImage } = useImage("/images/soccer-ball.png")
2827

29-
const hasNamiExtension = useHasNamiExtension()
30-
const { lucid, networkId } = useLucid()
31-
const { tooligans, setTooligans } = useTooligans(lucid, networkId)
32-
const [selectedTooligan, setSelectedTooligan] = useState<string>()
28+
const { lucid, networkId } = useCardano()
3329

34-
// strict equals to avoid undefined
35-
if (hasNamiExtension === false)
36-
return (
37-
<div className={styles.container}>
38-
<div className={styles.left} />
39-
<div>
40-
<h1 className={styles.namiTitle}>
41-
This game currently only works with the Nami extension installed.
42-
</h1>
43-
</div>
44-
<div className={styles.right} />
45-
</div>
46-
)
30+
const { tooligans, setTooligans } = useTooligans(lucid, networkId)
4731

48-
// not initialized yet
49-
if (!lucid) return null
32+
const [selectedTooligan, setSelectedTooligan] = useState<string>()
5033

5134
return (
5235
<>
36+
<div className={styles.connectContainer}>
37+
<CardanoWalletSelector />
38+
</div>
39+
5340
<a href="https://github.com/GGAlanSmithee/tooligans-game">
5441
{/* eslint-disable-next-line @next/next/no-img-element */}
5542
<img

‎pnpm-lock.yaml

+32-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎styles/index.module.css

+6
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,9 @@
9191
text-align: center;
9292
margin-top: 4rem;
9393
}
94+
95+
.connectContainer {
96+
position: absolute;
97+
left: 1rem;
98+
top: 1rem;
99+
}

0 commit comments

Comments
 (0)
Please sign in to comment.