1
0
mirror of https://github.com/oceanprotocol/faucet.git synced 2024-12-22 09:13:13 +01:00

working backend

This commit is contained in:
“ciscodacunha” 2020-10-12 16:42:30 +02:00
commit b6b07c8d38
7 changed files with 4147 additions and 0 deletions

9
.env.example Normal file
View File

@ -0,0 +1,9 @@
TOKEN_CONTRACT_ADDRESS="0xxxxxxxxxxx"
SEED_PHRASE="12 or 24 words of seed phrase"
FROM="0xxxxxxxxxxxx"
INFURA_NODE_ID="1234554"
DB_NAME="test"
DB_USER="admin"
DB_PASSWORD="password"
DB_HOST="localhost"
DB_PORT="31956"

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
node_modules/
.env
dist/
build/

3494
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

21
package.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "faucet",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongodb": "^3.6.2",
"truffle-hdwallet-provider": "^1.0.17",
"url": "^0.11.0",
"web3": "^1.2.11"
}
}

486
src/abi/token.json Normal file
View File

@ -0,0 +1,486 @@
[
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function",
"signature": "0x06fdde03"
},
{
"constant": false,
"inputs": [
{
"name": "spender",
"type": "address"
},
{
"name": "value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
"signature": "0x095ea7b3"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function",
"signature": "0x18160ddd"
},
{
"constant": false,
"inputs": [
{
"name": "from",
"type": "address"
},
{
"name": "to",
"type": "address"
},
{
"name": "value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
"signature": "0x23b872dd"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function",
"signature": "0x313ce567"
},
{
"constant": true,
"inputs": [],
"name": "cap",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function",
"signature": "0x355274ea"
},
{
"constant": false,
"inputs": [
{
"name": "spender",
"type": "address"
},
{
"name": "addedValue",
"type": "uint256"
}
],
"name": "increaseAllowance",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
"signature": "0x39509351"
},
{
"constant": false,
"inputs": [
{
"name": "to",
"type": "address"
},
{
"name": "value",
"type": "uint256"
}
],
"name": "mint",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
"signature": "0x40c10f19"
},
{
"constant": true,
"inputs": [
{
"name": "owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function",
"signature": "0x70a08231"
},
{
"constant": false,
"inputs": [],
"name": "renounceOwnership",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
"signature": "0x715018a6"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function",
"signature": "0x8da5cb5b"
},
{
"constant": true,
"inputs": [],
"name": "isOwner",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function",
"signature": "0x8f32d59b"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function",
"signature": "0x95d89b41"
},
{
"constant": false,
"inputs": [
{
"name": "account",
"type": "address"
}
],
"name": "addMinter",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
"signature": "0x983b2d56"
},
{
"constant": false,
"inputs": [],
"name": "renounceMinter",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
"signature": "0x98650275"
},
{
"constant": false,
"inputs": [
{
"name": "spender",
"type": "address"
},
{
"name": "subtractedValue",
"type": "uint256"
}
],
"name": "decreaseAllowance",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
"signature": "0xa457c2d7"
},
{
"constant": false,
"inputs": [
{
"name": "to",
"type": "address"
},
{
"name": "value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
"signature": "0xa9059cbb"
},
{
"constant": true,
"inputs": [
{
"name": "account",
"type": "address"
}
],
"name": "isMinter",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function",
"signature": "0xaa271e1a"
},
{
"constant": true,
"inputs": [
{
"name": "owner",
"type": "address"
},
{
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function",
"signature": "0xdd62ed3e"
},
{
"constant": false,
"inputs": [
{
"name": "newOwner",
"type": "address"
}
],
"name": "transferOwnership",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
"signature": "0xf2fde38b"
},
{
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor",
"signature": "constructor"
},
{
"payable": true,
"stateMutability": "payable",
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "account",
"type": "address"
}
],
"name": "MinterAdded",
"type": "event",
"signature": "0x6ae172837ea30b801fbfcdd4108aa1d5bf8ff775444fd70256b44e6bf3dfc3f6"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "account",
"type": "address"
}
],
"name": "MinterRemoved",
"type": "event",
"signature": "0xe94479a9f7e1952cc78f2d6baab678adc1b772d936c6583def489e524cb66692"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event",
"signature": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": true,
"name": "spender",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event",
"signature": "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "previousOwner",
"type": "address"
},
{
"indexed": true,
"name": "newOwner",
"type": "address"
}
],
"name": "OwnershipTransferred",
"type": "event",
"signature": "0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0"
},
{
"constant": false,
"inputs": [],
"name": "kill",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
"signature": "0x41c0e1b5"
}
]

20
src/db.js Normal file
View File

@ -0,0 +1,20 @@
const MongoClient = require('mongodb').MongoClient;
require('dotenv').config()
// Connection URL
let password = encodeURIComponent(process.env.DB_PASSWORD)
const url = `mongodb://${process.env.DB_USER}:${password}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`
console.log("URL - ", url)
// Use connect method to connect to the server
async function connection() {
try {
let client = await MongoClient.connect(url, { useUnifiedTopology: true })
console.log("Connected successfully to server");
return client
} catch (err) {
console.error(err)
}
}
module.exports = { connection }

113
src/server.js Normal file
View File

@ -0,0 +1,113 @@
const express = require('express')
const bodyParser = require('body-parser')
const url = require('url')
const HDWalletProvider = require('truffle-hdwallet-provider')
const Web3 = require('web3')
const abi = require('./abi/token')
const { connection } = require('./db')
require('dotenv').config()
const infura_apikey = process.env.INFURA_NODE_ID
const provider = new HDWalletProvider(
process.env.SEED_PHRASE,
'https://rinkeby.infura.io/v3/' + infura_apikey
)
const web3 = new Web3(provider)
const app = express()
app.use(bodyParser.urlencoded({ extended: false }))
const template = (err, message, transactionHash) => `
<html>
<head>
<title>Rinkeby OCEAN Faucet</title>
</head>
<body>
<h1>Rinkeby test OCEAN Faucet</h1>
<h4>Give me your address and I'll give you .001 ether!</h4>
<form action="/send">
<label>My Address:</label>
<input name="address" />
<button>Submit</button>
<span style="display: none" class="loader loader--style1" title="0">
<svg version="1.1" id="loader-1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="40px" height="40px" viewBox="0 0 40 40" enable-background="new 0 0 40 40" xml:space="preserve">
<path opacity="0.2" fill="#000" d="M20.201,5.169c-8.254,0-14.946,6.692-14.946,14.946c0,8.255,6.692,14.946,14.946,14.946
s14.946-6.691,14.946-14.946C35.146,11.861,28.455,5.169,20.201,5.169z M20.201,31.749c-6.425,0-11.634-5.208-11.634-11.634
c0-6.425,5.209-11.634,11.634-11.634c6.425,0,11.633,5.209,11.633,11.634C31.834,26.541,26.626,31.749,20.201,31.749z"/>
<path fill="#000" d="M26.013,10.047l1.654-2.866c-2.198-1.272-4.743-2.012-7.466-2.012h0v3.312h0
C22.32,8.481,24.301,9.057,26.013,10.047z">
<animateTransform attributeType="xml"
attributeName="transform"
type="rotate"
from="0 20 20"
to="360 20 20"
dur="0.5s"
repeatCount="indefinite"/>
</path>
</svg>
</span>
</form>
<h3 style="color: red">${err || ''}</h3>
<h3>${message || ''}</h3>
<p>
${transactionHash
? `If you're curious, here is your transaction id: ${transactionHash}`
: ''}
</p>
</body>
<script>
var button = document.querySelector('button')
button.addEventListener('click', function() {
button.style.display = 'none'
document.querySelector('.loader').style.display = 'inline'
})
</script>
</html>
`
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
const from = process.env.FROM
const to = query.address
const value = web3.utils.toWei('1', 'ether')
//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)
}
})
/*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))) */
})
const port = process.env.PORT || 4000
app.listen(port, async () => {
await connection()
console.log('Listening on port - ', port)
})