hackathon-registration-dapp/server/main.js

100 lines
5.7 KiB
JavaScript
Raw Normal View History

2018-10-03 12:45:56 +00:00
const IPFS = require('ipfs')
const OrbitDB = require('orbit-db')
const Web3 = require('web3');
const EventEmitter = require('events');
const contractAddress = "0xA49e638dD086E38Dac737C9346909f62DDf7d387";
const connectionURL = `ws://localhost:8546`;
const abiDefinition = [{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"codeUsed","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x39c6fe16"},{"constant":false,"inputs":[{"name":"_newController","type":"address"}],"name":"changeController","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x3cebb823"},{"constant":false,"inputs":[{"name":"_proof","type":"bytes32[]"},{"name":"code","type":"bytes32"},{"name":"_leaf","type":"bytes32"},{"name":"_dest","type":"address"}],"name":"send","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x6c1101c9"},{"constant":true,"inputs":[],"name":"sntAmount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x7f58fa14"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"sentToAddress","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x81e8706d"},{"constant":false,"inputs":[],"name":"boom","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0xa169ce09"},{"constant":false,"inputs":[{"name":"_ethAmount","type":"uint256"},{"name":"_sntAmount","type":"uint256"},{"name":"_root","type":"bytes32"}],"name":"updateSettings","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0xa4438334"},{"constant":true,"inputs":[],"name":"SNT","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xc55a02a0"},{"constant":true,"inputs":[],"name":"ethAmount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xc98166c0"},{"constant":true,"inputs":[],"name":"root","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xebf0c717"},{"constant":true,"inputs":[],"name":"controller","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xf77c4791"},{"inputs":[{"name":"_tokenAddress","type":"address"},{"name":"_ethAmount","type":"uint256"},{"name":"_sntAmount","type":"uint256"},{"name":"_root","type":"bytes32"}],"payable":true,"stateMutability":"payable","type":"constructor","signature":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"dest","type":"address"},{"indexed":false,"name":"leaf","type":"bytes32"},{"indexed":false,"name":"ethAmount","type":"uint256"},{"indexed":false,"name":"sntAmount","type":"uint256"}],"name":"AddressFunded","type":"event","signature":"0xd692fa6f47b47bb192b78e1ec163f648fc83f1f87f1caf3dd4640106e052a3f2"}];
const events = new EventEmitter();
const ipfs = new IPFS({
EXPERIMENTAL: {
pubsub: true
}
})
let orbitdb;
let web3;
let contract;
ipfs.on('ready', async () => {
console.log("Connected to IPFS");
orbitdb = new OrbitDB(ipfs)
web3 = new Web3(new Web3.providers.WebsocketProvider(connectionURL, {headers: {Origin: "embark"}}));
web3.eth.net.isListening()
.then(async () => {
console.log("Connected to WEB3: '" + connectionURL + "'");
contract = new web3.eth.Contract(abiDefinition, contractAddress);
const accounts = await web3.eth.getAccounts();
web3.eth.defaultAccount = accounts[0];
events.emit('web3:connected')
})
.catch(error => {
console.error(error);
process.exit();
});
});
const start = async () => {
console.log("Connecting to data stores...");
const transactionStore = await orbitdb.keyvalue('rramos-transactions-adadasdasd');
const fundRequestStore = await orbitdb.log('rramos-funds-requests-adadasdasd');
let lastCodeProcessed;
let running = false;
setInterval(() => {
if(running) return;
console.log("Processing requests...");
running = true;
let conditions = {}
if(lastCodeProcessed){
conditions = ({gt: lastCodeProcessed});
}
// Query the database.
const all = fundRequestStore.iterator(conditions)
.collect()
.map(async (e) => {
const record = e.payload.value;
lastCodeProcessed = record.code;
const transaction = transactionStore.get(record.code);
if(!transaction) return;
const validRequest = await contract.methods.validRequest(record.proof, record.code, record.address).call();
if(!validRequest){
console.warn(record.address + "-" + record.code + " : INVALID REQUEST");
return;
}
// Add code to db to indicate that we're processing it
await transactionStore.put(record.code, record);
// Execute the contract function
const receipt = await contract.methods.processRequest(record.proof, record.code, record.address).send();
// Update the db with the transaction hash
record.transactionHash = receipt.transactionHash;
await transactionStore.put(record.code, record);
});
// Wait for all promises to be resolved
Promise.all(results).then((completed) => {
running = false;
});
}, 20000);
}
events.on('web3:connected', start);