Skip to content

Commit 48e2287

Browse files
committedSep 16, 2021
Add github actions workflow for uploading to IPFS and fix uploader
1 parent 6d8d522 commit 48e2287

File tree

5 files changed

+127
-63
lines changed

5 files changed

+127
-63
lines changed
 

‎.github/workflows/upload-ipfs.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Upload IPFS
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
8+
jobs:
9+
upload-to-ipfs:
10+
name: dip-uploader
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout repo
14+
uses: actions/checkout@v2
15+
16+
- name: Rename dip to upper case
17+
run: cd content/dips && bash toUpper.sh || true && cd ../..
18+
19+
- name: Set up Node.js
20+
uses: actions/setup-node@v1
21+
with:
22+
node-version: 12.x
23+
24+
- name: Install dependencies
25+
run: npm install
26+
27+
- name: Parse Markdown
28+
run: npm run parse-markdown
29+
30+
- name: Upload to Pinata and the Graph IPFS
31+
env:
32+
PINATA_KEY: ${{ secrets.PINATA_KEY }}
33+
PINATA_SECRET: ${{ secrets.PINATA_SECRET }}
34+
run: npm run upload:pinata-thegraph
35+
36+
- name: Commit changes
37+
uses: EndBug/add-and-commit@v6
38+
with:
39+
author_name: GitHub Actions
40+
author_email: 41898282+github-actions[bot]@users.noreply.github.com
41+
message: '[PIPELINE] Committed IPFS uploads'
42+
add: '.'

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

‎dip-uploader.js

+80-52
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,91 @@
22
const bs58 = require('bs58');
33
const fs = require('fs')
44
const fetch = require('node-fetch')
5-
const { exec, spawn } = require('child_process');
5+
const { execSync } = require('child_process');
6+
const { exit } = require('process');
67

7-
const { PINATA_KEY, PINATA_SECRET} = process.env;
8+
const { PINATA_KEY, PINATA_SECRET } = process.env;
89

910
const pinataEndpoint = 'https://api.pinata.cloud/pinning/pinJSONToIPFS'
1011

12+
async function delay(ms) {
13+
return new Promise(resolve => setTimeout(resolve, ms));
14+
}
1115

12-
jsonAips = JSON.parse(fs.readFileSync('./content/ipfs-aips/all-aips.json').toString());
13-
Object.keys(jsonAips).forEach((id) => {
14-
delete Object.assign(jsonAips[id], {'description': jsonAips[id]['content'] })['content'];
15-
fetch(pinataEndpoint, {
16-
method: 'POST',
17-
body: JSON.stringify({
18-
pinataOptions: { cidVersion: 0 },
19-
pinataContent: jsonAips[id],
20-
}),
21-
headers: {
22-
'Content-Type': 'application/json',
23-
pinata_api_key: PINATA_KEY,
24-
pinata_secret_api_key: PINATA_SECRET,
25-
},
26-
})
27-
.then(res => {
28-
return res.json()
29-
}).then(result => {
30-
if (result.error) throw { message: result.error }
31-
const hash = result.IpfsHash
32-
const encodedHash = `0x${
33-
bs58
34-
.decode(hash)
35-
.slice(2)
36-
.toString('hex')
37-
}`
38-
jsonAips[id].ipfsHash = hash
39-
jsonAips[id].encodeIpfsHash = encodedHash
40-
console.log('✅ Success!')
41-
console.log(` IPFS hash: ${hash}`)
42-
console.log(` Encoded IPFS hash (for proposal creation): ${encodedHash}`)
43-
console.log(` See the file here: https://gateway.pinata.cloud/ipfs/${hash}`)
44-
fs.writeFileSync(
45-
`./content/ipfs-aips/${id}-Ipfs-hashes.json`,
46-
JSON.stringify({aip: id, hash, encodedHash}, null, 2)
47-
)
48-
fs.writeFileSync(
49-
`./content/ipfs-aips/all-aips.json`,
50-
JSON.stringify(jsonAips, null, 2)
51-
)
52-
exec(`curl https://gateway.pinata.cloud/ipfs/${hash} > tmp && curl -sF file='@./tmp' https://api.thegraph.com/ipfs/api/v0/add`, (err, stdout, stderr) => {
53-
if(err) {
54-
throw new Error(stderr)
16+
const rawJsonDips = JSON.parse(fs.readFileSync('./content/ipfs-dips/all-dips.json').toString());
17+
const jsonDips = Object.values(rawJsonDips);
18+
19+
async function main() {
20+
const dipIds = Object.keys(jsonDips).sort(((a, b) => a.split('-')[1] - b.split('-')[1]));
21+
22+
for (let x = 0; x < dipIds.length; x++) {
23+
const id = dipIds[x];
24+
25+
delete Object.assign(jsonDips[id], { 'description': jsonDips[id]['content'] })['content'];
26+
27+
try {
28+
const res = await fetch(pinataEndpoint, {
29+
method: 'POST',
30+
body: JSON.stringify({
31+
pinataOptions: { cidVersion: 0 },
32+
pinataContent: jsonDips[id],
33+
}),
34+
headers: {
35+
'Content-Type': 'application/json',
36+
pinata_api_key: PINATA_KEY,
37+
pinata_secret_api_key: PINATA_SECRET,
38+
},
39+
})
40+
41+
if (!res.ok) {
42+
throw Error(await res.text());
5543
}
56-
console.log(stdout)
57-
});
58-
}).catch((err) => {
59-
console.log('error:', err);
60-
})
61-
})
44+
45+
const result = await res.json()
46+
47+
if (result.error) throw { message: result.error }
48+
49+
const hash = result.IpfsHash
50+
const encodedHash = `0x${bs58
51+
.decode(hash)
52+
.slice(2)
53+
.toString('hex')
54+
}`
55+
jsonDips[id].ipfsHash = hash
56+
jsonDips[id].encodeIpfsHash = encodedHash
57+
console.log(`${jsonDips[id].title}: ✅ Success!`)
58+
console.log(` IPFS hash: ${hash}`)
59+
console.log(` Encoded IPFS hash (for proposal creation): ${encodedHash}`)
60+
console.log(` See the file here: https://gateway.pinata.cloud/ipfs/${hash}`)
61+
fs.writeFileSync(
62+
`./content/ipfs-dips/${jsonDips[id].basename}-Ipfs-hashes.json`,
63+
JSON.stringify({ dip: id, hash, encodedHash }, null, 2)
64+
)
65+
fs.writeFileSync(
66+
`./content/ipfs-dips/all-dips.json`,
67+
JSON.stringify(jsonDips, null, 2)
68+
)
69+
await delay(250);
70+
execSync(`curl -s https://gateway.pinata.cloud/ipfs/${hash} > tmp && curl -sF file='@./tmp' https://api.thegraph.com/ipfs/api/v0/add`, (err, stdout, stderr) => {
71+
if (err) {
72+
throw new Error(stderr)
73+
}
74+
console.log(stdout)
75+
});
76+
await delay(250);
77+
} catch (error) {
78+
console.error(`Error during main loop: ${error}`)
79+
throw error;
80+
}
81+
}
82+
}
6283

6384

64-
85+
(async () => {
86+
try {
87+
await main();
88+
} catch (e) {
89+
console.error(`Exiting [dip-uploader] process due next error: \n ${error}`)
90+
exit(1)
91+
}
92+
})();

‎package-lock.json

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

‎package.json

+3-10
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,18 @@
4949
"gh-pages": "^2.2.0",
5050
"prettier": "2.0.4"
5151
},
52-
"homepage": "https://github.com/dYdX/dip",
52+
"homepage": "https://github.com/dydxfoundation/dip",
5353
"keywords": [
5454
"gatsby"
5555
],
5656
"main": "n/a",
5757
"repository": {
5858
"type": "git",
59-
"url": "git+https://github.com/dYdX/dip"
59+
"url": "git+https://github.com/dydxfoundation/dip"
6060
},
6161
"scripts": {
62-
"build": "gatsby build --prefix-paths",
63-
"develop": "gatsby develop",
6462
"parse-markdown": "m2j ./content/dips/*.md -c -o ./content/ipfs-dips/all-dips.json",
6563
"upload:pinata-thegraph": "node dip-uploader.js && rm tmp",
66-
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
67-
"start": "npm run develop",
68-
"serve": "gatsby serve --prefix-paths",
69-
"clean": "gatsby clean",
70-
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1",
71-
"deploy": "gatsby build --prefix-paths && gh-pages -d public"
64+
"format": "prettier --write \"**/*.{js,jsx,json,md}\""
7265
}
7366
}

0 commit comments

Comments
 (0)
Please sign in to comment.