mirror of
https://github.com/oceanprotocol/faucet.git
synced 2024-11-21 17:26:57 +01:00
added db config
This commit is contained in:
parent
b6b07c8d38
commit
19c7d04a5c
11
package-lock.json
generated
11
package-lock.json
generated
@ -2162,6 +2162,11 @@
|
||||
"version": "6.5.2",
|
||||
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz",
|
||||
"integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA=="
|
||||
},
|
||||
"uuid": {
|
||||
"version": "3.4.0",
|
||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
|
||||
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
|
||||
}
|
||||
}
|
||||
},
|
||||
@ -2707,9 +2712,9 @@
|
||||
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
|
||||
},
|
||||
"uuid": {
|
||||
"version": "3.4.0",
|
||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
|
||||
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
|
||||
"version": "8.3.1",
|
||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.1.tgz",
|
||||
"integrity": "sha512-FOmRr+FmWEIG8uhZv6C2bTgEVXsHk08kE7mPlrBbEe+c3r9pjceVPgupIfNIhc4yx55H69OXANrUaSuu9eInKg=="
|
||||
},
|
||||
"varint": {
|
||||
"version": "5.0.0",
|
||||
|
@ -16,6 +16,7 @@
|
||||
"mongodb": "^3.6.2",
|
||||
"truffle-hdwallet-provider": "^1.0.17",
|
||||
"url": "^0.11.0",
|
||||
"uuid": "^8.3.1",
|
||||
"web3": "^1.2.11"
|
||||
}
|
||||
}
|
||||
|
33
src/db.js
33
src/db.js
@ -1,5 +1,6 @@
|
||||
const MongoClient = require('mongodb').MongoClient;
|
||||
require('dotenv').config()
|
||||
var client = null
|
||||
|
||||
// Connection URL
|
||||
|
||||
@ -9,7 +10,7 @@ console.log("URL - ", url)
|
||||
// Use connect method to connect to the server
|
||||
async function connection() {
|
||||
try {
|
||||
let client = await MongoClient.connect(url, { useUnifiedTopology: true })
|
||||
client = await MongoClient.connect(url, { useUnifiedTopology: true })
|
||||
console.log("Connected successfully to server");
|
||||
return client
|
||||
} catch (err) {
|
||||
@ -17,4 +18,32 @@ async function connection() {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { connection }
|
||||
async function insert(records, callback) {
|
||||
// Get the documents collection
|
||||
console.log("client - ", client)
|
||||
let db = client.db(process.env.DB_NAME)
|
||||
const collection = db.collection('records');
|
||||
// Insert some documents
|
||||
try {
|
||||
await collection.insertOne(records, function (err, result) {
|
||||
console.log("Inserted into database");
|
||||
callback(result);
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
async function find(query, callback) {
|
||||
// Get the documents collection
|
||||
let db = client.db(process.env.DB_NAME)
|
||||
const collection = db.collection('records');
|
||||
collection.find(query).toArray(function (err, docs) {
|
||||
assert.equal(err, null);
|
||||
console.log("Found the following records");
|
||||
console.log(docs);
|
||||
callback(docs);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { connection, insert }
|
@ -4,7 +4,8 @@ const url = require('url')
|
||||
const HDWalletProvider = require('truffle-hdwallet-provider')
|
||||
const Web3 = require('web3')
|
||||
const abi = require('./abi/token')
|
||||
const { connection } = require('./db')
|
||||
const { connection, insert, find } = require('./db')
|
||||
var client = null
|
||||
require('dotenv').config()
|
||||
|
||||
const infura_apikey = process.env.INFURA_NODE_ID
|
||||
@ -71,43 +72,52 @@ app.get('/', (req, res) => {
|
||||
res.send(template())
|
||||
})
|
||||
|
||||
app.get('/send', (req, res) => {
|
||||
console.log(`ip address - `, req.headers['x-forwarded-for'] || req.connection.remoteAddress)
|
||||
const url_parts = url.parse(req.url, true)
|
||||
const query = url_parts.query
|
||||
app.get('/send', async (req, res) => {
|
||||
|
||||
const from = process.env.FROM
|
||||
const to = query.address
|
||||
const value = web3.utils.toWei('1', 'ether')
|
||||
try {
|
||||
|
||||
//create token instance from abi and contract address
|
||||
const tokenInstance = new web3.eth.Contract(abi, process.env.TOKEN_CONTRACT_ADDRESS)
|
||||
let ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress
|
||||
console.log(`ip address - `, ipAddress)
|
||||
const url_parts = url.parse(req.url, true)
|
||||
const query = url_parts.query
|
||||
|
||||
tokenInstance.methods.transfer(to, value).send({ from }, function (error, txHash) {
|
||||
if (!error) {
|
||||
console.log(txHash)
|
||||
res.send(
|
||||
template('', 'Great, test OCEANs are on the way!', txHash)
|
||||
)
|
||||
} else {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
const from = process.env.FROM
|
||||
const to = query.address
|
||||
const value = web3.utils.toWei(process.env.TOKEN_AMOUNT, 'ether')
|
||||
|
||||
//check if this user is in cool down period
|
||||
await find({ $or: [{ "ip": ipAddress }, { "wallet": query.address }] }, shouldTransfer => {
|
||||
if (shouldTransfer) {
|
||||
res.send(
|
||||
template('', "You have to wait 24 hours between faucet requests", undefined)
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
/*web3.eth
|
||||
.sendTransaction({ from, to, value })
|
||||
.then(tx => {
|
||||
console.log(tx)
|
||||
res.send(
|
||||
template('', 'Great, coins are on the way!', tx.transactionHash)
|
||||
)
|
||||
})
|
||||
.catch(err => res.send(template(err))) */
|
||||
//insert ip address into db
|
||||
await insert({ ip: ipAddress, wallet: to, lastUpdatedOn: Date.now() }, (result) => console.log(result))
|
||||
|
||||
//create token instance from abi and contract address
|
||||
const tokenInstance = new web3.eth.Contract(abi, process.env.TOKEN_CONTRACT_ADDRESS)
|
||||
|
||||
tokenInstance.methods.transfer(to, value).send({ from }, function (error, txHash) {
|
||||
if (!error) {
|
||||
console.log(txHash)
|
||||
res.send(
|
||||
template('', 'Great, test OCEANs are on the way!', txHash)
|
||||
)
|
||||
} else {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
|
||||
const port = process.env.PORT || 4000
|
||||
app.listen(port, async () => {
|
||||
await connection()
|
||||
client = await connection()
|
||||
console.log('Listening on port - ', port)
|
||||
})
|
Loading…
Reference in New Issue
Block a user