1
0
mirror of https://github.com/oceanprotocol/faucet.git synced 2024-11-22 01:36:57 +01:00

added db config

This commit is contained in:
“ciscodacunha” 2020-10-14 15:50:17 +02:00
parent b6b07c8d38
commit 19c7d04a5c
4 changed files with 80 additions and 35 deletions

11
package-lock.json generated
View File

@ -2162,6 +2162,11 @@
"version": "6.5.2", "version": "6.5.2",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz",
"integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" "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=" "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
}, },
"uuid": { "uuid": {
"version": "3.4.0", "version": "8.3.1",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.1.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==" "integrity": "sha512-FOmRr+FmWEIG8uhZv6C2bTgEVXsHk08kE7mPlrBbEe+c3r9pjceVPgupIfNIhc4yx55H69OXANrUaSuu9eInKg=="
}, },
"varint": { "varint": {
"version": "5.0.0", "version": "5.0.0",

View File

@ -16,6 +16,7 @@
"mongodb": "^3.6.2", "mongodb": "^3.6.2",
"truffle-hdwallet-provider": "^1.0.17", "truffle-hdwallet-provider": "^1.0.17",
"url": "^0.11.0", "url": "^0.11.0",
"uuid": "^8.3.1",
"web3": "^1.2.11" "web3": "^1.2.11"
} }
} }

View File

@ -1,5 +1,6 @@
const MongoClient = require('mongodb').MongoClient; const MongoClient = require('mongodb').MongoClient;
require('dotenv').config() require('dotenv').config()
var client = null
// Connection URL // Connection URL
@ -9,7 +10,7 @@ console.log("URL - ", url)
// Use connect method to connect to the server // Use connect method to connect to the server
async function connection() { async function connection() {
try { try {
let client = await MongoClient.connect(url, { useUnifiedTopology: true }) client = await MongoClient.connect(url, { useUnifiedTopology: true })
console.log("Connected successfully to server"); console.log("Connected successfully to server");
return client return client
} catch (err) { } 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 }

View File

@ -4,7 +4,8 @@ const url = require('url')
const HDWalletProvider = require('truffle-hdwallet-provider') const HDWalletProvider = require('truffle-hdwallet-provider')
const Web3 = require('web3') const Web3 = require('web3')
const abi = require('./abi/token') const abi = require('./abi/token')
const { connection } = require('./db') const { connection, insert, find } = require('./db')
var client = null
require('dotenv').config() require('dotenv').config()
const infura_apikey = process.env.INFURA_NODE_ID const infura_apikey = process.env.INFURA_NODE_ID
@ -71,43 +72,52 @@ app.get('/', (req, res) => {
res.send(template()) res.send(template())
}) })
app.get('/send', (req, res) => { app.get('/send', async (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
const from = process.env.FROM try {
const to = query.address
const value = web3.utils.toWei('1', 'ether')
//create token instance from abi and contract address let ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress
const tokenInstance = new web3.eth.Contract(abi, process.env.TOKEN_CONTRACT_ADDRESS) 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) { const from = process.env.FROM
if (!error) { const to = query.address
console.log(txHash) const value = web3.utils.toWei(process.env.TOKEN_AMOUNT, 'ether')
res.send(
template('', 'Great, test OCEANs are on the way!', txHash)
)
} else {
console.error(err)
}
})
//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 //insert ip address into db
.sendTransaction({ from, to, value }) await insert({ ip: ipAddress, wallet: to, lastUpdatedOn: Date.now() }, (result) => console.log(result))
.then(tx => {
console.log(tx) //create token instance from abi and contract address
res.send( const tokenInstance = new web3.eth.Contract(abi, process.env.TOKEN_CONTRACT_ADDRESS)
template('', 'Great, coins are on the way!', tx.transactionHash)
) tokenInstance.methods.transfer(to, value).send({ from }, function (error, txHash) {
}) if (!error) {
.catch(err => res.send(template(err))) */ 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 const port = process.env.PORT || 4000
app.listen(port, async () => { app.listen(port, async () => {
await connection() client = await connection()
console.log('Listening on port - ', port) console.log('Listening on port - ', port)
}) })