Skip to content

Instantly share code, notes, and snippets.

@mikeymop
Last active June 11, 2022 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeymop/5d437ab4d418acc58dfa8d8a3b4ff15e to your computer and use it in GitHub Desktop.
Save mikeymop/5d437ab4d418acc58dfa8d8a3b4ff15e to your computer and use it in GitHub Desktop.
const fetch = require('node-fetch');
var testString1 = "137/erc721:0xa90a110c81e9f2aacd08ffc6d24871908612be6f/8774";
var testString2 = "1/erc721:0xa90a110c81e9f2aacd08ffc6d24871908612be6f/8774";
var testString3 = "137/erc1155:0xa90a110c81e9f2aacd08ffc6d24871908612be6f/8774"
var testString4 = "1/erc1155:0xa90a110c81e9f2aacd08ffc6d24871908612be6f/8774"
var badStringH = "137/erc721:0x723e481aef940fc7454850a83c54ca1ee770209c/291";
var badStringJ = "137/erc721:0xa6d6d5fbeb58cd8c1006fff02fd73ac8b0a9c0f2/16";
/** Returns void or the ipfs location of nft image.
* Modifies the following DBM vars:
* - pfpErr {true | false} - Whether there was an error or not
* - pfpURL {string | void} - The uri of the nft, ipfs:// or https://
* - pfpSent {true | false} - The image was stored directly in the nft.
* This function sent had the bot send the message already no further action needed.
*/
const resolveNFTUri = async (str) => {
log = (msg) => console.log("getNFT: \n", msg);
console.log('Str is: ', str);
str = str[0];
log('Finding nft uri for: ', str);
const getChainId = (str) => {
const chainEnum = {
1: 'eth',
25: 'cro',
56: 'bnb',
137: 'polygon',
250: 'fantom',
43114: 'avax',
1666600000: 'one',
};
const re = /\d+(?=\/)/;
const res = str.match(re);
return chainEnum[res[0]];
}
/** When given a social.picture.value returns the token type as a string */
const getTokenValue = (str) => {
const re = /(erc721|erc1155)/
return str.match(re)[0];
}
/** When given a social.picture.value returns the eth-like address as a string */
const getAddress = (str) => {
var re = /0x[a-zA-Z0-9]{40}/;
return str.match(re)[0];
}
const getFullTokenId = (str) => {
var [, tokenId] = str.split(':');
console.log('got tokenId of: ', tokenId);
return tokenId;
}
/** Some NFTs may have image data stored directly on the token.
* In this case we dont need to transofmr an ipfs url, and can send
* the data to discord directly.
* See: https://stackoverflow.com/a/65676660
*/
const directWriteMessage = async (str, addr) => {
try {
// const { DiscordJS, Actions, Images } = this.getDBM();
console.log("Image stored directly in asset. Converting...");
console.log("Getting image body for: ", str);
if (str.split('+')[0].split('/')[1] === 'svg') {
console.log("Got an svg, fancy!");
const nftString = new Buffer.from(str.split(',')[1], 'base64');
const path = 'resources/' + addr + '.jpg';
svg2img(nftString, { format: 'jpg', 'quality': 75 }, (error, buffer) => {
fs.writeFileSync(path, buffer); // default jpeg quality is 75
});
}
} catch (e) {
throw e;
}
};
var chainId = getChainId(str);
var tokenId = getTokenValue(str);
var addr = getAddress(str);
var fullAddr = getFullTokenId(str);
var path = '/api/v2/nft/' + fullAddr + '?chain=' + chainId + '&format=decimal';
console.log('Calling: https://deep-index.moralis.io' + path);
try {
const result = await fetch('https://deep-index.moralis.io' + path, {
method: 'GET',
headers: {
'X-API-Key': apiKey,
'Accept': 'application/json'
}
});
if (result?.status && result.status !== 200) {
throw new Error('No nft was found.');
}
// console.log('json', result);
const json = await result.json();
const metadata = JSON.parse(json.metadata);
console.log("Metadata: ", metadata.image);
if (metadata.image === null || metadata.image === undefined) {
throw new Error(
"No image metadata present, are there issues with the UD api?"
+ metadata
)
}
if (metadata.image.split("/")[0] === 'data:image') {
console.log("Svg embedded directly in nft, evaluating...");
await directWriteMessage(metadata.image, addr);
}
// this.storeValue(metadata.image, 2, "pfpURL", cache) // tried this more recently
} catch (e) {
console.log("rNI error: ", e.message);
// store(true, 2, "pfpErr", cache);
}
};
(async () => {
// channel.send("Looking for your nft...");
await resolveNFTUri([badStringJ]);
// Actions.callNextAction(cache);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment