Verify that checked ID is smaller than total supply

This commit is contained in:
Mentor Palokaj 2021-10-14 15:12:36 +02:00
parent 72a489ded8
commit e762553faf
6 changed files with 2605 additions and 31 deletions

View File

@ -1 +1,2 @@
node_modules/
node_modules/
.*

View 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
}

View File

@ -1,6 +1,7 @@
const app = require( './express' )()
const name = require( 'random-name' )
const { db } = require( './firebase' )
const { getTotalSupply } = require( './contract' )
// ///////////////////////////////
// Data sources
@ -64,6 +65,32 @@ app.get( '/rocketeer/:id', async ( req, res ) => {
const { id } = req.params
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
try {

View File

@ -1,7 +1,8 @@
const app = require( './express' )()
const { getTotalSupply } = require( './contract' )
// Specific Rocketeer instances
app.get( '/rocketeer/:id', ( req, res ) => res.json( {
app.get( '/rocketeer/:id', async ( req, res ) => res.json( {
description: "A testnet Rocketeer",
external_url: `https://openseacreatures.io/${ req.params.id }`,
image: "https://rocketpool.net/images/rocket.png",
@ -14,7 +15,8 @@ app.get( '/rocketeer/:id', ( req, res ) => res.json( {
// 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",
external_url: `https://openseacreatures.io/`,
image: "https://rocketpool.net/images/rocket.png",

File diff suppressed because it is too large Load Diff

View File

@ -18,7 +18,8 @@
"express": "^4.17.1",
"firebase-admin": "^9.12.0",
"firebase-functions": "^3.11.0",
"random-name": "^0.1.2"
"random-name": "^0.1.2",
"web3": "^1.6.0"
},
"devDependencies": {
"@babel/core": "^7.15.8",