-
Notifications
You must be signed in to change notification settings - Fork 11
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 #296 from williamchong/feature/likernft
Feature/likernft
- Loading branch information
Showing
32 changed files
with
3,164 additions
and
673 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
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,5 +1,6 @@ | ||
const config = {}; | ||
|
||
config.COSMOS_PRIVATE_KEY = Buffer.from('1111111111111111111111111111111111111111111111111111111111111111', 'hex'); | ||
config.LIKER_NFT_PRIVATE_KEY = Buffer.from('1111111111111111111111111111111111111111111111111111111111111111', 'hex'); | ||
|
||
module.exports = config; |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ValidationError } from '../util/ValidationError'; | ||
import { | ||
getISCNIdByClassId, getCurrentClassIdByISCNId, getISCNPrefixDocName, | ||
} from '../util/api/likernft'; | ||
|
||
export const fetchISCNIdAndClassId = async (req, res, next) => { | ||
try { | ||
let { iscn_id: iscnId, class_id: classId } = req.query; | ||
if (!iscnId && !classId) throw new ValidationError('MISSING_ISCN_OR_CLASS_ID'); | ||
|
||
if (!iscnId) { | ||
iscnId = await getISCNIdByClassId(classId); | ||
} | ||
if (!classId) { | ||
classId = await getCurrentClassIdByISCNId(iscnId); | ||
} | ||
res.locals.iscnId = iscnId; | ||
res.locals.classId = classId; | ||
res.locals.iscnPrefix = getISCNPrefixDocName(iscnId); | ||
next(); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
export default fetchISCNIdAndClassId; |
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Router } from 'express'; | ||
import axios from 'axios'; | ||
import { ValidationError } from '../../util/ValidationError'; | ||
import { getISCNDocByClassId } from '../../util/api/likernft'; | ||
import { fetchISCNIdAndClassId } from '../../middleware/likernft'; | ||
import { COSMOS_LCD_INDEXER_ENDPOINT } from '../../../config/config'; | ||
|
||
const router = Router(); | ||
|
||
router.get( | ||
'/history', | ||
fetchISCNIdAndClassId, | ||
async (req, res, next) => { | ||
try { | ||
const { nft_id: nftId } = req.query; | ||
const { classId } = res.locals; | ||
if (nftId && !classId) { | ||
throw new ValidationError('PLEASE_DEFINE_CLASS_ID'); | ||
} | ||
let list = []; | ||
const doc = await getISCNDocByClassId(classId); | ||
let queryObj = await doc.ref.collection('transaction'); | ||
if (nftId) queryObj = queryObj.where('nftId', '==', nftId); | ||
const query = await queryObj.orderBy('timestamp', 'desc').get(); | ||
list = query.docs.map(d => ({ txHash: d.id, ...(d.data() || {}) })); | ||
res.json({ | ||
list, | ||
}); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}, | ||
); | ||
|
||
router.get( | ||
'/events', | ||
fetchISCNIdAndClassId, | ||
async (_, res, next) => { | ||
try { | ||
const { classId } = res.locals; | ||
const [ | ||
{ data: newClassData }, | ||
{ data: mintData }, | ||
{ data: sendData }, | ||
] = await Promise.all([ | ||
axios.get(`${COSMOS_LCD_INDEXER_ENDPOINT}/cosmos/tx/v1beta1/txs?events=likechain.likenft.v1.EventNewClass.class_id=%27%22${classId}%22%27`), | ||
axios.get(`${COSMOS_LCD_INDEXER_ENDPOINT}/cosmos/tx/v1beta1/txs?events=likechain.likenft.v1.EventMintNFT.class_id=%27%22${classId}%22%27`), | ||
axios.get(`${COSMOS_LCD_INDEXER_ENDPOINT}/cosmos/tx/v1beta1/txs?events=cosmos.nft.v1beta1.EventSend.class_id=%27%22${classId}%22%27`), | ||
]); | ||
let list = []; | ||
list = list.concat(newClassData.tx_responses || []); | ||
let set = new Set(list.map(t => t.txhash)); | ||
list = list.concat((mintData.tx_responses || []).filter(t => !set.has(t.txhash))); | ||
set = new Set(list.map(t => t.txhash)); | ||
list = list.concat((sendData.tx_responses || []).filter(t => !set.has(t.txhash))); | ||
list = list.map((d) => { | ||
const { | ||
height, | ||
txhash, | ||
code, | ||
logs, | ||
tx, | ||
timestamp, | ||
} = d; | ||
return { | ||
height, | ||
txhash, | ||
code, | ||
logs, | ||
tx, | ||
timestamp, | ||
}; | ||
}).sort((a, b) => b.timestamp - a.timestamp); | ||
res.json({ | ||
list, | ||
}); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}, | ||
); | ||
|
||
export default router; |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as fs from 'fs'; | ||
import { Router } from 'express'; | ||
|
||
const router = Router(); | ||
|
||
fs.readdirSync(__dirname).forEach((file) => { | ||
const name = file.split('.')[0]; | ||
if (!name || name === 'index') return; | ||
router.use(require(`./${name}`).default); // eslint-disable-line import/no-dynamic-require,global-require | ||
}); | ||
|
||
export default router; |
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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { Router } from 'express'; | ||
import axios from 'axios'; | ||
import { ONE_DAY_IN_S, API_EXTERNAL_HOSTNAME } from '../../constant'; | ||
import { likeNFTCollection, iscnInfoCollection } from '../../util/firebase'; | ||
import { filterLikeNFTMetadata } from '../../util/ValidationHelper'; | ||
import { getISCNIdByClassId } from '../../util/api/likernft'; | ||
import { | ||
getLikerNFTDynamicData, getBasicImage, getCombinedImage, getResizedImage, | ||
} from '../../util/api/likernft/metadata'; | ||
import { getNFTISCNData, getNFTClassDataById, getNFTOwner } from '../../util/cosmos/nft'; | ||
import { fetchISCNIdAndClassId } from '../../middleware/likernft'; | ||
import { getISCNPrefix } from '../../util/cosmos/iscn'; | ||
import { LIKER_NFT_TARGET_ADDRESS } from '../../../config/config'; | ||
import { ValidationError } from '../../util/ValidationError'; | ||
import { sleep } from '../../util/misc'; | ||
|
||
const router = Router(); | ||
|
||
router.get( | ||
'/metadata', | ||
fetchISCNIdAndClassId, | ||
async (_, res, next) => { | ||
try { | ||
const { classId, iscnId, iscnPrefix } = res.locals; | ||
const classDocRef = likeNFTCollection.doc(iscnPrefix).collection('class').doc(classId); | ||
|
||
const classDoc = await classDocRef.get(); | ||
const classData = classDoc.data(); | ||
if (!classData) { | ||
res.status(404).send('NFT_DATA_NOT_FOUND'); | ||
return; | ||
} | ||
const [{ owner: iscnOwner, data: iscnData }, chainData, dynamicData] = await Promise.all([ | ||
getNFTISCNData(iscnId).catch((err) => { console.error(err); return {}; }), | ||
getNFTClassDataById(classId).catch(err => console.error(err)), | ||
getLikerNFTDynamicData(classId, classData).catch(err => console.error(err)), | ||
]); | ||
if (!iscnData) throw new ValidationError('ISCN_NOT_FOUND'); | ||
if (!chainData) throw new ValidationError('NFT_CLASS_NOT_FOUND'); | ||
if (!dynamicData) throw new ValidationError('NFT_CLASS_NOT_REGISTERED'); | ||
|
||
res.set('Cache-Control', `public, max-age=${60}, s-maxage=${60}, stale-if-error=${ONE_DAY_IN_S}`); | ||
res.json(filterLikeNFTMetadata({ | ||
iscnId: getISCNPrefix(iscnId), | ||
iscnOwner, | ||
iscnStakeholders: iscnData.stakeholders, | ||
...(classData.metadata || {}), | ||
...chainData, | ||
...dynamicData, | ||
})); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}, | ||
); | ||
|
||
router.get( | ||
'/metadata/owners', | ||
fetchISCNIdAndClassId, | ||
async (_, res, next) => { | ||
try { | ||
const { classId, iscnPrefix } = res.locals; | ||
const nftQuery = await likeNFTCollection.doc(iscnPrefix) | ||
.collection('class').doc(classId) | ||
.collection('nft') | ||
.where('ownerWallet', '!=', LIKER_NFT_TARGET_ADDRESS) | ||
.get(); | ||
const nftIds = nftQuery.docs.map(n => n.id); | ||
const ownerMap = { | ||
// don't include api holded wallet | ||
// LIKER_NFT_TARGET_ADDRESS: apiOwnedNFTIds, | ||
}; | ||
const owners = await Promise.all(nftIds.map(id => getNFTOwner(classId, id))); | ||
owners.forEach((owner, index) => { | ||
ownerMap[owner] = ownerMap[owner] || []; | ||
ownerMap[owner].push(nftIds[index]); | ||
}); | ||
res.set('Cache-Control', `public, max-age=${60}, s-maxage=${60}, stale-if-error=${ONE_DAY_IN_S}`); | ||
res.json(ownerMap); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}, | ||
); | ||
|
||
router.get( | ||
'/metadata/image/class_(:classId)(.png)?', | ||
async (req, res, next) => { | ||
try { | ||
const { classId } = req.params; | ||
const iscnId = await getISCNIdByClassId(classId); | ||
let iscnData = await iscnInfoCollection.doc(encodeURIComponent(iscnId)).get(); | ||
if (!iscnData.exists) { | ||
await axios.post( | ||
`https://${API_EXTERNAL_HOSTNAME}/like/info`, | ||
{ iscnId }, | ||
); | ||
await sleep(1000); | ||
iscnData = await iscnInfoCollection.doc(encodeURIComponent(iscnId)).get(); | ||
} | ||
let image = ''; | ||
let title = ''; | ||
if (iscnData.exists) { | ||
({ image, title } = iscnData.data()); | ||
} | ||
const basicImage = await getBasicImage(image, title); | ||
const resizedImage = getResizedImage(); | ||
const combinedImage = await getCombinedImage(); | ||
res.set('Cache-Control', `public, max-age=${60}, s-maxage=${60}, stale-if-error=${ONE_DAY_IN_S}`); | ||
basicImage.pipe(resizedImage).pipe(combinedImage).pipe(res); | ||
return; | ||
} catch (err) { | ||
next(err); | ||
} | ||
}, | ||
); | ||
|
||
export default router; |
Oops, something went wrong.