Skip to content

Commit c6df72d

Browse files
committed
Switch Pinata to Infura
1 parent b3393df commit c6df72d

File tree

7 files changed

+39
-75
lines changed

7 files changed

+39
-75
lines changed

components/Editor/SubAdapterTest.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import styled from 'styled-components'
33
import { Adapter } from '@cryptostats/sdk'
44
import QueryForm from './QueryForm'
55
import { useEditorState } from 'hooks/editor-state'
6+
import { IPFS_GATEWAY } from 'resources/constants'
67

78
const Container = styled.div``
89

@@ -54,7 +55,7 @@ const SubAdapterTest: React.FC<SubAdapterPreviewProps> = ({ subadapter, openByDe
5455
return (
5556
<Header onClick={() => setOpen(true)}>
5657
{metadata.icon?.cid && (
57-
<Icon size="small" src={`https://gateway.pinata.cloud/ipfs/${metadata.icon.cid}`} />
58+
<Icon size="small" src={`${IPFS_GATEWAY}/ipfs/${metadata.icon.cid}`} />
5859
)}
5960
{name || subadapter.id}
6061
{subtitle ? ` - ${subtitle}` : null}
@@ -68,7 +69,7 @@ const SubAdapterTest: React.FC<SubAdapterPreviewProps> = ({ subadapter, openByDe
6869
<Container>
6970
<Header onClick={() => setOpen(false)}>
7071
{metadata.icon?.cid && (
71-
<Icon size="small" src={`https://gateway.pinata.cloud/ipfs/${metadata.icon.cid}`} />
72+
<Icon size="small" src={`${IPFS_GATEWAY}/ipfs/${metadata.icon.cid}`} />
7273
)}
7374
{name || subadapter.id}
7475
{subtitle ? ` - ${subtitle}` : null}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"@graphprotocol/graph-cli": "^0.30.4",
1515
"@graphprotocol/graph-ts": "^0.27.0",
1616
"@monaco-editor/react": "^4.2.1",
17-
"@pinata/sdk": "^1.1.23",
1817
"@types/react-modal": "^3.12.1",
1918
"@types/react-syntax-highlighter": "^13.5.2",
2019
"@web3-react/core": "^6.1.9",

pages/api/graph/upload-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
1313
file = Buffer.from(file as string, 'base64')
1414
}
1515

16-
const cid = await saveToIPFS(file, req.body.name)
16+
const cid = await saveToIPFS(file)
1717

1818
res.json({ success: true, cid })
1919
}

pages/api/upload-image.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
1-
import * as fs from 'fs'
21
import { NextApiRequest, NextApiResponse } from 'next'
3-
import pinataSDK from '@pinata/sdk'
4-
5-
const filePath = '/tmp/upload.bin'
6-
7-
async function saveToIPFS(stream: any, name: string, type: string): Promise<string> {
8-
if (!process.env.PINATA_KEY || !process.env.PINATA_SECRET) {
9-
throw new Error('Pinata key missing')
10-
}
11-
12-
const pinata = pinataSDK(process.env.PINATA_KEY, process.env.PINATA_SECRET)
13-
const fileStream = fs.createWriteStream(filePath)
14-
stream.pipe(fileStream)
15-
const response = await pinata.pinFromFS(filePath, {
16-
pinataMetadata: { name, type, category: 'image' },
17-
})
18-
19-
return response.IpfsHash
20-
}
2+
import { getInfuraNode } from 'utils/ipfs-upload'
213

224
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
235
if (req.method !== 'POST') {
@@ -30,7 +12,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
3012
throw new Error('Upload must include name/type')
3113
}
3214

33-
const cid = await saveToIPFS(req, name.toString(), type.toString())
15+
const infuraNode = getInfuraNode()
16+
const response = await infuraNode.add(req)
17+
const cid = response.path
18+
3419
console.log('Uploaded image', cid)
3520
res.json({ cid })
3621
}

resources/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const IPFS_GATEWAY = 'https://gateway.pinata.cloud/'
1+
export const IPFS_GATEWAY = 'https://cryptostats.infura-ipfs.io'

utils/ipfs-upload.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
import * as fs from 'fs'
2-
import pinataSDK from '@pinata/sdk'
31
import { create } from 'ipfs-http-client'
42

5-
const filePath = '/tmp/upload.txt'
6-
7-
export async function saveToIPFS(file: string | Buffer, name: string): Promise<string> {
8-
if (!process.env.PINATA_KEY || !process.env.PINATA_SECRET) {
9-
throw new Error('Pinata key missing')
3+
export function getInfuraNode() {
4+
if (!process.env.INFURA_IPFS_USER || !process.env.INFURA_IPFS_KEY) {
5+
throw new Error('Infura key missing')
106
}
117

8+
const infuraAuthKey = Buffer.from(
9+
`${process.env.INFURA_IPFS_USER}:${process.env.INFURA_IPFS_KEY}`
10+
).toString('base64')
11+
const infuraNode = create({
12+
host: 'ipfs.infura.io',
13+
port: 5001,
14+
protocol: 'https',
15+
headers: {
16+
Authorization: 'Basic ' + infuraAuthKey,
17+
},
18+
})
19+
return infuraNode
20+
}
21+
22+
export async function saveToIPFS(file: string | Buffer): Promise<string> {
1223
let failedUpload: string | null = null
1324
const failHandler = (name: string) => () => {
1425
if (failedUpload) {
@@ -18,28 +29,17 @@ export async function saveToIPFS(file: string | Buffer, name: string): Promise<s
1829
return { IpfsHash: null, path: null }
1930
}
2031

21-
const pinata = pinataSDK(process.env.PINATA_KEY, process.env.PINATA_SECRET)
22-
fs.writeFileSync(filePath, file)
23-
const pinataPromise = pinata
24-
.pinFromFS(filePath, {
25-
pinataMetadata: {
26-
name,
27-
// @ts-ignore
28-
keyvalues: {
29-
type: 'module',
30-
},
31-
},
32-
})
33-
.catch(failHandler('pinata'))
32+
const infuraNode = getInfuraNode()
33+
const infuraPromise = infuraNode.add(file)
3434

3535
const graphNode = create('https://api.thegraph.com/ipfs/api/v0' as any)
3636
const graphPromise = graphNode.add(file)
3737

3838
const csNode = create('https://ipfs.cryptostats.community' as any)
3939
const csPromise = csNode.add(file).catch(failHandler('CryptoStats'))
4040

41-
const [pinataResult, graphResult, csResult] = await Promise.all([
42-
pinataPromise,
41+
const [infuraResult, graphResult, csResult] = await Promise.all([
42+
infuraPromise,
4343
graphPromise,
4444
csPromise,
4545
])
@@ -48,12 +48,12 @@ export async function saveToIPFS(file: string | Buffer, name: string): Promise<s
4848
console.warn(`2 out of 3 uploads successful, upload to ${failedUpload} failed`)
4949
}
5050

51-
if (pinataResult.IpfsHash && graphResult.path && pinataResult.IpfsHash !== graphResult.path) {
52-
throw new Error(`Mismatched CIDs: ${pinataResult.IpfsHash} & ${graphResult.path}`)
51+
if (infuraResult.path && graphResult.path && infuraResult.path !== graphResult.path) {
52+
throw new Error(`Mismatched CIDs: ${infuraResult.path} & ${graphResult.path}`)
5353
}
54-
if (pinataResult.IpfsHash && csResult.path && pinataResult.IpfsHash !== csResult.path) {
55-
throw new Error(`Mismatched CIDs: ${pinataResult.IpfsHash} & ${csResult.path}`)
54+
if (infuraResult.path && csResult.path && infuraResult.path !== csResult.path) {
55+
throw new Error(`Mismatched CIDs: ${infuraResult.path} & ${csResult.path}`)
5656
}
5757

58-
return pinataResult.IpfsHash || graphResult.path!
58+
return infuraResult.path || graphResult.path!
5959
}

yarn.lock

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,17 +1863,6 @@
18631863
resolved "https://registry.yarnpkg.com/@pedrouid/environment/-/environment-1.0.1.tgz#858f0f8a057340e0b250398b75ead77d6f4342ec"
18641864
integrity sha512-HaW78NszGzRZd9SeoI3JD11JqY+lubnaOx7Pewj5pfjqWXOEATpeKIFb9Z4t2WBUK2iryiXX3lzWwmYWgUL0Ug==
18651865

1866-
"@pinata/sdk@^1.1.23":
1867-
version "1.1.23"
1868-
resolved "https://registry.yarnpkg.com/@pinata/sdk/-/sdk-1.1.23.tgz#91d7e289a29f514ee35d8d2ad62f8ef891b57ea0"
1869-
integrity sha512-ZIQI6acEu2m3tuLW9UCUNVgqbHc5U+t657ukunamV0E8XcW639vgtGa5rd5C/9mVIxoWpp5CJ9q7FA1OT88huQ==
1870-
dependencies:
1871-
axios "^0.21.1"
1872-
base-path-converter "^1.0.2"
1873-
form-data "^2.3.3"
1874-
is-ipfs "^0.6.0"
1875-
recursive-fs "^1.1.2"
1876-
18771866
"@popperjs/core@^2.9.2":
18781867
version "2.11.0"
18791868
resolved "https://registry.yarnpkg.com/@popperjs/core/-/core-2.11.0.tgz#6734f8ebc106a0860dff7f92bf90df193f0935d7"
@@ -3011,11 +3000,6 @@ balanced-match@^1.0.0:
30113000
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
30123001
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==
30133002

3014-
base-path-converter@^1.0.2:
3015-
version "1.0.2"
3016-
resolved "https://registry.yarnpkg.com/base-path-converter/-/base-path-converter-1.0.2.tgz#e80b4b4f31c7b1561e632158e00774b6f2f27978"
3017-
integrity sha512-51R8JiuXadknn6ouVUteOhDpmI3G5u5GqjruL7bPJpfxUHVgosaO5uPAvRP4FeR4VyyH4sSvsN78Ci6ouoRYqQ==
3018-
30193003
base-x@^3.0.2, base-x@^3.0.8:
30203004
version "3.0.8"
30213005
resolved "https://registry.yarnpkg.com/base-x/-/base-x-3.0.8.tgz#1e1106c2537f0162e8b52474a557ebb09000018d"
@@ -4521,7 +4505,7 @@ forever-agent@~0.6.1:
45214505
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
45224506
integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=
45234507

4524-
form-data@^2.2.0, form-data@^2.3.3:
4508+
form-data@^2.2.0:
45254509
version "2.5.1"
45264510
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.5.1.tgz#f2cbec57b5e59e23716e128fe44d4e5dd23895f4"
45274511
integrity sha512-m21N3WOmEEURgk6B9GLOE4RuWOFf28Lhh9qGYeNlGq4VDXUlJy2th2slBNU8Gp8EzloYZOibZJ7t5ecIrFSjVA==
@@ -5616,7 +5600,7 @@ is-ip@^3.1.0:
56165600
dependencies:
56175601
ip-regex "^4.0.0"
56185602

5619-
is-ipfs@^0.6.0, is-ipfs@~0.6.1:
5603+
is-ipfs@~0.6.1:
56205604
version "0.6.3"
56215605
resolved "https://registry.yarnpkg.com/is-ipfs/-/is-ipfs-0.6.3.tgz#82a5350e0a42d01441c40b369f8791e91404c497"
56225606
integrity sha512-HyRot1dvLcxImtDqPxAaY1miO6WsiP/z7Yxpg2qpaLWv5UdhAPtLvHJ4kMLM0w8GSl8AFsVF23PHe1LzuWrUlQ==
@@ -8309,11 +8293,6 @@ recompose@^0.30.0:
83098293
react-lifecycles-compat "^3.0.2"
83108294
symbol-observable "^1.0.4"
83118295

8312-
recursive-fs@^1.1.2:
8313-
version "1.1.2"
8314-
resolved "https://registry.yarnpkg.com/recursive-fs/-/recursive-fs-1.1.2.tgz#1d752e2f1a65d25fb6964109a9dbf83a335f90f0"
8315-
integrity sha512-QPFEt5EwzwlHoqYsZc+NkUSyDTQf1Hvq7c/kpQJHi77OSCAiDXI3wfB0J04ZG+ekGHmv37mdR8MDPEshD3/RlQ==
8316-
83178296
redent@^3.0.0:
83188297
version "3.0.0"
83198298
resolved "https://registry.yarnpkg.com/redent/-/redent-3.0.0.tgz#e557b7998316bb53c9f1f56fa626352c6963059f"

0 commit comments

Comments
 (0)