mirror of
https://github.com/stronk-dev/RandomChad.git
synced 2025-07-05 10:35:08 +02:00
Verify that checked ID is smaller than total supply
This commit is contained in:
parent
72a489ded8
commit
e762553faf
3
functions/.gitignore
vendored
3
functions/.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
|
.*
|
41
functions/modules/contract.js
Normal file
41
functions/modules/contract.js
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// Dependencies
|
||||||
|
const Web3 = require( 'web3' )
|
||||||
|
const functions = require( 'firebase-functions' )
|
||||||
|
const { infura } = functions.config()
|
||||||
|
|
||||||
|
// Contract data
|
||||||
|
const contractAddress = {
|
||||||
|
mainnet: '',
|
||||||
|
rinkeby: '0x2829ba9d76e675b8867E1707A9aB49B280D916c6'
|
||||||
|
}
|
||||||
|
const ABI = [
|
||||||
|
{
|
||||||
|
"inputs": [],
|
||||||
|
"name": "totalSupply",
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"internalType": "uint256",
|
||||||
|
"name": "",
|
||||||
|
"type": "uint256"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"stateMutability": "view",
|
||||||
|
"type": "function",
|
||||||
|
"constant": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
async function getTotalSupply( network='mainnet' ) {
|
||||||
|
|
||||||
|
// Initialise contract connection
|
||||||
|
const web3 = new Web3( `wss://${ network }.infura.io/ws/v3/${ infura.projectid }` )
|
||||||
|
const contract = new web3.eth.Contract( ABI, contractAddress[ network ] )
|
||||||
|
|
||||||
|
// Return the call promise which returns the total supply
|
||||||
|
return contract.methods.totalSupply().call()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getTotalSupply: getTotalSupply
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
const app = require( './express' )()
|
const app = require( './express' )()
|
||||||
const name = require( 'random-name' )
|
const name = require( 'random-name' )
|
||||||
const { db } = require( './firebase' )
|
const { db } = require( './firebase' )
|
||||||
|
const { getTotalSupply } = require( './contract' )
|
||||||
|
|
||||||
// ///////////////////////////////
|
// ///////////////////////////////
|
||||||
// Data sources
|
// Data sources
|
||||||
@ -64,6 +65,32 @@ app.get( '/rocketeer/:id', async ( req, res ) => {
|
|||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
if( !id ) return res.json( { error: `No ID specified in URL` } )
|
if( !id ) return res.json( { error: `No ID specified in URL` } )
|
||||||
|
|
||||||
|
// Chech if this is an illegal ID
|
||||||
|
try {
|
||||||
|
|
||||||
|
// Get the last know total supply
|
||||||
|
const { cachedTotalSupply } = await db.collection( 'meta' ).doc( 'contract' ).get().then( doc => doc.data() )
|
||||||
|
|
||||||
|
// If the requested ID is larger than that, check if the new total supply is more
|
||||||
|
if( cachedTotalSupply < id ) {
|
||||||
|
|
||||||
|
// Get net total supply through infura, if infura fails, return the cached value just in case
|
||||||
|
const totalSupply = await getTotalSupply().catch( f => cachedTotalSupply )
|
||||||
|
|
||||||
|
// Write new value to cache
|
||||||
|
await db.collection( 'meta' ).doc( 'contract' ).set( { cachedTotalSupply: totalSupply }, { merge: true } )
|
||||||
|
|
||||||
|
// If the requested ID is larger than total supply, exit
|
||||||
|
if( totalSupply < id ) return res.json( {
|
||||||
|
trace: 'total supply getter',
|
||||||
|
error: 'This Rocketeer does not yet exist.'
|
||||||
|
} )
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch( e ) {
|
||||||
|
return res.json( { trace: 'total supply getter', error: e.message || JSON.stringify( e ) } )
|
||||||
|
}
|
||||||
|
|
||||||
// Get existing rocketeer if it exists
|
// Get existing rocketeer if it exists
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
const app = require( './express' )()
|
const app = require( './express' )()
|
||||||
|
const { getTotalSupply } = require( './contract' )
|
||||||
|
|
||||||
// Specific Rocketeer instances
|
// Specific Rocketeer instances
|
||||||
app.get( '/rocketeer/:id', ( req, res ) => res.json( {
|
app.get( '/rocketeer/:id', async ( req, res ) => res.json( {
|
||||||
description: "A testnet Rocketeer",
|
description: "A testnet Rocketeer",
|
||||||
external_url: `https://openseacreatures.io/${ req.params.id }`,
|
external_url: `https://openseacreatures.io/${ req.params.id }`,
|
||||||
image: "https://rocketpool.net/images/rocket.png",
|
image: "https://rocketpool.net/images/rocket.png",
|
||||||
@ -14,7 +15,8 @@ app.get( '/rocketeer/:id', ( req, res ) => res.json( {
|
|||||||
|
|
||||||
|
|
||||||
// Collection data
|
// Collection data
|
||||||
app.get( '/collection', ( req, res ) => res.json( {
|
app.get( '/collection', async ( req, res ) => res.json( {
|
||||||
|
totalSupply: await getTotalSupply( 'rinkeby' ),
|
||||||
description: "A testnet collection",
|
description: "A testnet collection",
|
||||||
external_url: `https://openseacreatures.io/`,
|
external_url: `https://openseacreatures.io/`,
|
||||||
image: "https://rocketpool.net/images/rocket.png",
|
image: "https://rocketpool.net/images/rocket.png",
|
||||||
|
2556
functions/package-lock.json
generated
2556
functions/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -18,7 +18,8 @@
|
|||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"firebase-admin": "^9.12.0",
|
"firebase-admin": "^9.12.0",
|
||||||
"firebase-functions": "^3.11.0",
|
"firebase-functions": "^3.11.0",
|
||||||
"random-name": "^0.1.2"
|
"random-name": "^0.1.2",
|
||||||
|
"web3": "^1.6.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.15.8",
|
"@babel/core": "^7.15.8",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user