|
2 | 2 | const bs58 = require('bs58');
|
3 | 3 | const fs = require('fs')
|
4 | 4 | const fetch = require('node-fetch')
|
5 |
| -const { exec, spawn } = require('child_process'); |
| 5 | +const { execSync } = require('child_process'); |
| 6 | +const { exit } = require('process'); |
6 | 7 |
|
7 |
| -const { PINATA_KEY, PINATA_SECRET} = process.env; |
| 8 | +const { PINATA_KEY, PINATA_SECRET } = process.env; |
8 | 9 |
|
9 | 10 | const pinataEndpoint = 'https://api.pinata.cloud/pinning/pinJSONToIPFS'
|
10 | 11 |
|
| 12 | +async function delay(ms) { |
| 13 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 14 | +} |
11 | 15 |
|
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()); |
55 | 43 | }
|
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 | +} |
62 | 83 |
|
63 | 84 |
|
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 | +})(); |
0 commit comments