-
-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from codeSTACKr/fix-uploads-mints
Fix uploads mints
- Loading branch information
Showing
12 changed files
with
296 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,113 @@ | ||
const fetch = require("node-fetch"); | ||
const path = require("path"); | ||
const isLocal = typeof process.pkg === "undefined"; | ||
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath); | ||
const basePath = process.cwd(); | ||
const fs = require("fs"); | ||
|
||
const AUTH = 'YOUR API KEY HERE'; | ||
const CONTRACT_ADDRESS = 'YOUR CONTRACT ADDRESS HERE'; | ||
const MINT_TO_ADDRESS = 'YOUR WALLET ADDRESS HERE'; | ||
const CHAIN = 'rinkeby'; | ||
const TIMEOUT = 1000; // Milliseconds. This a timeout for errors only. If there is an error, it will wait then try again. 5000 = 5 seconds. | ||
|
||
const ipfsMetas = JSON.parse( | ||
fs.readFileSync(`${basePath}/build/json/_ipfsMetas.json`) | ||
); | ||
|
||
fs.writeFileSync(`${basePath}/build/minted.json`, ""); | ||
const writter = fs.createWriteStream(`${basePath}/build/minted.json`, { | ||
flags: "a", | ||
}); | ||
writter.write("["); | ||
nftCount = ipfsMetas.length; | ||
|
||
ipfsMetas.forEach((meta) => { | ||
let url = "https://api.nftport.xyz/v0/mints/customizable"; | ||
|
||
const mintInfo = { | ||
chain: CHAIN, | ||
contract_address: CONTRACT_ADDRESS, | ||
metadata_uri: meta.metadata_uri, | ||
mint_to_address: MINT_TO_ADDRESS, | ||
token_id: meta.custom_fields.edition, | ||
}; | ||
|
||
let options = { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: AUTH, | ||
}, | ||
body: JSON.stringify(mintInfo), | ||
}; | ||
|
||
fetch(url, options) | ||
.then((res) => res.json()) | ||
.then((json) => { | ||
writter.write(JSON.stringify(json, null, 2)); | ||
nftCount--; | ||
|
||
if (nftCount === 0) { | ||
writter.write("]"); | ||
writter.end(); | ||
} else { | ||
writter.write(",\n"); | ||
if (!fs.existsSync(path.join(`${basePath}/build`, "/minted"))) { | ||
fs.mkdirSync(path.join(`${basePath}/build`, "minted")); | ||
} | ||
|
||
async function main() { | ||
const ipfsMetas = JSON.parse( | ||
fs.readFileSync(`${basePath}/build/ipfsMetas/_ipfsMetas.json`) | ||
); | ||
|
||
for (const meta of ipfsMetas) { | ||
const mintFile = `${basePath}/build/minted/${meta.custom_fields.edition}.json`; | ||
|
||
try { | ||
fs.accessSync(mintFile); | ||
const mintedFile = fs.readFileSync(mintFile) | ||
if(mintedFile.length > 0) { | ||
const mintedMeta = JSON.parse(mintedFile) | ||
if(mintedMeta.mintData.response !== "OK") throw 'not minted' | ||
} | ||
console.log(`${meta.name} already minted`); | ||
} catch(err) { | ||
try { | ||
let mintData = await fetchWithRetry(meta) | ||
const combinedData = { | ||
metaData: meta, | ||
mintData: mintData | ||
} | ||
writeMintData(meta.custom_fields.edition, combinedData) | ||
console.log(`Minted: ${meta.name}!`); | ||
} catch(err) { | ||
console.log(`Catch: ${err}`) | ||
} | ||
} | ||
} | ||
} | ||
|
||
main(); | ||
|
||
function timer(ms) { | ||
return new Promise(res => setTimeout(res, ms)); | ||
} | ||
|
||
async function fetchWithRetry(meta) { | ||
await timer(TIMEOUT); | ||
return new Promise((resolve, reject) => { | ||
const fetch_retry = (_meta) => { | ||
let url = "https://api.nftport.xyz/v0/mints/customizable"; | ||
|
||
const mintInfo = { | ||
chain: CHAIN, | ||
contract_address: CONTRACT_ADDRESS, | ||
metadata_uri: _meta.metadata_uri, | ||
mint_to_address: MINT_TO_ADDRESS, | ||
token_id: _meta.custom_fields.edition, | ||
}; | ||
|
||
let options = { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: AUTH, | ||
}, | ||
body: JSON.stringify(mintInfo), | ||
}; | ||
|
||
return fetch(url, options).then(async (res) => { | ||
const status = res.status; | ||
|
||
if(status === 200) { | ||
return res.json(); | ||
} | ||
else { | ||
console.error(`ERROR STATUS: ${status}`) | ||
console.log('Retrying') | ||
await timer(TIMEOUT) | ||
fetch_retry(_meta) | ||
} | ||
}) | ||
.then(async (json) => { | ||
if(json.response === "OK"){ | ||
return resolve(json); | ||
} else { | ||
console.error(`NOK: ${json.error}`) | ||
console.log('Retrying') | ||
await timer(TIMEOUT) | ||
fetch_retry(_meta) | ||
} | ||
}) | ||
.catch(async (error) => { | ||
console.error(`CATCH ERROR: ${error}`) | ||
console.log('Retrying') | ||
await timer(TIMEOUT) | ||
fetch_retry(_meta) | ||
}); | ||
} | ||
return fetch_retry(meta); | ||
}); | ||
} | ||
|
||
console.log(`Minted: ${json.transaction_external_url}`); | ||
}) | ||
.catch((err) => console.error("error:" + err)); | ||
}); | ||
const writeMintData = (_edition, _data) => { | ||
fs.writeFileSync(`${basePath}/build/minted/${_edition}.json`, JSON.stringify(_data, null, 2)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,97 @@ | ||
const FormData = require("form-data"); | ||
const fetch = require("node-fetch"); | ||
|
||
const path = require("path"); | ||
const isLocal = typeof process.pkg === "undefined"; | ||
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath); | ||
const basePath = process.cwd(); | ||
const fs = require("fs"); | ||
|
||
const AUTH = 'YOUR API KEY HERE'; | ||
const TIMEOUT = 1000; // Milliseconds. Extend this if needed to wait for each upload. 1000 = 1 second. | ||
|
||
fs.readdirSync(`${basePath}/build/images`).forEach((file) => { | ||
const formData = new FormData(); | ||
const fileStream = fs.createReadStream(`${basePath}/build/images/${file}`); | ||
formData.append("file", fileStream); | ||
|
||
let url = "https://api.nftport.xyz/v0/files"; | ||
let options = { | ||
method: "POST", | ||
headers: { | ||
Authorization: AUTH, | ||
}, | ||
body: formData, | ||
}; | ||
|
||
fetch(url, options) | ||
.then((res) => res.json()) | ||
.then((json) => { | ||
const fileName = path.parse(json.file_name).name; | ||
let rawdata = fs.readFileSync(`${basePath}/build/json/${fileName}.json`); | ||
let metaData = JSON.parse(rawdata); | ||
|
||
metaData.file_url = json.ipfs_url; | ||
const allMetadata = []; | ||
|
||
async function main() { | ||
const files = fs.readdirSync(`${basePath}/build/images`); | ||
files.sort(function(a, b){ | ||
return a.split(".")[0] - b.split(".")[0]; | ||
}); | ||
for (const file of files) { | ||
const fileName = path.parse(file).name; | ||
let jsonFile = fs.readFileSync(`${basePath}/build/json/${fileName}.json`); | ||
let metaData = JSON.parse(jsonFile); | ||
if(!metaData.file_url.includes('https://')) { | ||
const response = await fetchWithRetry(file); | ||
metaData.file_url = response.ipfs_url; | ||
|
||
fs.writeFileSync( | ||
`${basePath}/build/json/${fileName}.json`, | ||
JSON.stringify(metaData, null, 2) | ||
); | ||
console.log(`${response.file_name} uploaded & ${fileName}.json updated!`); | ||
} else { | ||
console.log(`${fileName} already uploaded.`); | ||
} | ||
|
||
allMetadata.push(metaData); | ||
} | ||
fs.writeFileSync( | ||
`${basePath}/build/json/_metadata.json`, | ||
JSON.stringify(allMetadata, null, 2) | ||
); | ||
} | ||
|
||
main(); | ||
|
||
function timer(ms) { | ||
return new Promise(res => setTimeout(res, ms)); | ||
} | ||
|
||
async function fetchWithRetry(file) { | ||
await timer(TIMEOUT) | ||
return new Promise((resolve, reject) => { | ||
const fetch_retry = (_file) => { | ||
const formData = new FormData(); | ||
const fileStream = fs.createReadStream(`${basePath}/build/images/${_file}`); | ||
formData.append("file", fileStream); | ||
|
||
let url = "https://api.nftport.xyz/v0/files"; | ||
let options = { | ||
method: "POST", | ||
headers: { | ||
Authorization: AUTH, | ||
}, | ||
body: formData, | ||
}; | ||
|
||
return fetch(url, options).then(async (res) => { | ||
const status = res.status; | ||
|
||
console.log(`${json.file_name} uploaded & ${fileName}.json updated!`); | ||
}) | ||
.catch((err) => console.error("error:" + err)); | ||
}); | ||
if(status === 200) { | ||
return res.json(); | ||
} | ||
else { | ||
console.error(`ERROR STATUS: ${status}`) | ||
console.log('Retrying') | ||
await timer(TIMEOUT) | ||
fetch_retry(_file) | ||
} | ||
}) | ||
.then(async (json) => { | ||
if(json.response === "OK"){ | ||
return resolve(json); | ||
} else { | ||
console.error(`NOK: ${json.error}`) | ||
console.log('Retrying') | ||
await timer(TIMEOUT) | ||
fetch_retry(_file) | ||
} | ||
}) | ||
.catch(async (error) => { | ||
console.error(`CATCH ERROR: ${error}`) | ||
console.log('Retrying') | ||
await timer(TIMEOUT) | ||
fetch_retry(_file) | ||
}); | ||
} | ||
return fetch_retry(file); | ||
}); | ||
} |
Oops, something went wrong.