2018-10-03 13:36:13 +00:00
|
|
|
import React, { Fragment } from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
2018-10-03 12:45:56 +00:00
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
2018-10-03 13:36:13 +00:00
|
|
|
import web3 from 'Embark/web3'
|
2018-10-03 14:13:40 +00:00
|
|
|
import merkleData from './merkle';
|
2018-10-04 14:09:29 +00:00
|
|
|
import { sha3 } from 'ethereumjs-util';
|
|
|
|
import merkle from 'merkle-tree-solidity';
|
2018-10-03 16:03:22 +00:00
|
|
|
import SNTGiveaway from 'Embark/contracts/SNTGiveaway';
|
2018-10-03 18:28:33 +00:00
|
|
|
import SNT from 'Embark/contracts/SNT';
|
2018-10-05 01:55:45 +00:00
|
|
|
import axios from 'axios';
|
2018-10-03 18:28:33 +00:00
|
|
|
|
2018-10-03 16:03:22 +00:00
|
|
|
window.SNTGiveaway = SNTGiveaway;
|
2018-10-03 18:28:33 +00:00
|
|
|
window.SNT = SNT;
|
2018-10-03 16:03:22 +00:00
|
|
|
|
2018-10-03 13:36:13 +00:00
|
|
|
class App extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-10-04 14:09:29 +00:00
|
|
|
this.state = {
|
|
|
|
error: false,
|
2018-10-04 15:21:43 +00:00
|
|
|
errorMessage: '',
|
2018-10-05 01:55:45 +00:00
|
|
|
ready: false,
|
2018-10-06 18:50:15 +00:00
|
|
|
showENSLink: false,
|
|
|
|
intervalCheck: false,
|
|
|
|
showError: false
|
2018-10-04 14:09:29 +00:00
|
|
|
};
|
2018-10-03 13:36:13 +00:00
|
|
|
}
|
2018-10-03 14:13:40 +00:00
|
|
|
|
|
|
|
componentDidMount(){
|
|
|
|
EmbarkJS.onReady(async (error) => {
|
2018-10-04 15:21:43 +00:00
|
|
|
|
2018-10-03 14:13:40 +00:00
|
|
|
if(error){
|
2018-10-04 15:21:43 +00:00
|
|
|
this.setState({error: true, errorMessage: "Error loading the DAPP - Contact your nearest Status Core Developer"});
|
|
|
|
console.error(error);
|
2018-10-03 14:13:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:55:45 +00:00
|
|
|
try {
|
|
|
|
await this.start();
|
|
|
|
} catch (error) {
|
2018-10-04 15:21:43 +00:00
|
|
|
this.setState({error: true, errorMessage: "Error loading the DAPP - Contact your nearest Status Core Developer"});
|
2018-10-05 01:55:45 +00:00
|
|
|
console.error(error);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-03 14:13:40 +00:00
|
|
|
});
|
2018-10-04 15:21:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 18:50:15 +00:00
|
|
|
async redirectIfProcessed(code, intervalCheck){
|
2018-10-04 17:31:50 +00:00
|
|
|
const sentToAddress = await SNTGiveaway.methods.sentToAddress(web3.eth.defaultAccount).call();
|
|
|
|
const usedCode = await SNTGiveaway.methods.codeUsed( '0x' + code,).call();
|
2018-10-05 02:31:39 +00:00
|
|
|
if(sentToAddress && usedCode){
|
2018-10-06 18:50:15 +00:00
|
|
|
this.setState({showENSLink: true, intervalCheck});
|
|
|
|
setTimeout(() => {
|
|
|
|
// TODO: set ENS url
|
|
|
|
window.location = "http://www.google.com";
|
|
|
|
}, 7000);
|
2018-10-04 17:31:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:55:45 +00:00
|
|
|
async start(){
|
|
|
|
const code = location.search.replace("?", ''); // QR code value, i.e.: a8cd4b33bc
|
2018-10-04 15:21:43 +00:00
|
|
|
const accounts = await web3.eth.getAccounts();
|
|
|
|
|
|
|
|
web3.eth.defaultAccount = accounts[0];
|
|
|
|
|
|
|
|
if(!code) {
|
|
|
|
this.setState({error: true, errorMessage: "Code is required"});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const merkleTree = new merkle(merkleData.elements);
|
2018-10-04 17:31:50 +00:00
|
|
|
const hashedCode = sha3('0x' + code);
|
2018-10-04 15:21:43 +00:00
|
|
|
|
2018-10-04 17:31:50 +00:00
|
|
|
let proof;
|
|
|
|
try {
|
|
|
|
proof = merkleTree.getProof(hashedCode);
|
|
|
|
} catch(error){
|
|
|
|
this.setState({error: true, errorMessage: "Invalid Code"});
|
|
|
|
console.log(error);
|
|
|
|
return;
|
2018-10-04 15:21:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 01:55:45 +00:00
|
|
|
await this.redirectIfProcessed(code);
|
2018-10-04 15:21:43 +00:00
|
|
|
|
2018-10-05 01:55:45 +00:00
|
|
|
if(!this.state.showENSLink){
|
|
|
|
const validRequest = await SNTGiveaway.methods.validRequest(proof, '0x' + code, web3.eth.defaultAccount).call();
|
|
|
|
if(!validRequest){
|
|
|
|
this.setState({error: true, errorMessage: "Invalid Code"});
|
|
|
|
console.error("Request is not valid according to contract");
|
|
|
|
return;
|
2018-10-04 17:31:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 01:55:45 +00:00
|
|
|
// Create / Open a database
|
2018-10-06 18:50:15 +00:00
|
|
|
const response = await axios.get('http://138.68.78.11:3000/isProcessing/' + code); // TODO: extract to config
|
2018-10-05 01:55:45 +00:00
|
|
|
if(!response.data.result){
|
|
|
|
const record = {
|
|
|
|
code,
|
|
|
|
address: web3.eth.defaultAccount,
|
|
|
|
proof: proof.map(x => '0x' + x.toString('hex'))
|
|
|
|
}
|
2018-10-04 17:31:50 +00:00
|
|
|
|
2018-10-06 18:50:15 +00:00
|
|
|
const response = await axios.post('http://138.68.78.11:3000/requestFunds/', record); // TODO:
|
2018-10-05 02:31:39 +00:00
|
|
|
if(response.data.error){
|
|
|
|
this.setState({error: true, errorMessage: response.data.message});
|
|
|
|
}
|
2018-10-04 15:21:43 +00:00
|
|
|
|
2018-10-05 01:55:45 +00:00
|
|
|
// TODO: avoid people spamming with localstorage
|
|
|
|
}
|
|
|
|
|
|
|
|
setInterval(async () => {
|
2018-10-06 18:50:15 +00:00
|
|
|
await this.redirectIfProcessed(code, true);
|
2018-10-05 01:55:45 +00:00
|
|
|
}, 10000);
|
|
|
|
}
|
2018-10-04 17:31:50 +00:00
|
|
|
|
|
|
|
|
2018-10-04 15:21:43 +00:00
|
|
|
this.setState({ready: true});
|
2018-10-03 14:13:40 +00:00
|
|
|
}
|
2018-10-06 18:50:15 +00:00
|
|
|
|
|
|
|
showErrorMessage(){
|
|
|
|
this.setState({showErrorMessage: !this.state.showErrorMessage});
|
|
|
|
}
|
2018-10-03 13:36:13 +00:00
|
|
|
|
|
|
|
render(){
|
2018-10-06 18:50:15 +00:00
|
|
|
const {error, errorMessage, ready, showENSLink, showErrorMessage, intervalCheck} = this.state;
|
|
|
|
|
|
|
|
let className = "blue";
|
|
|
|
if(error) className = "red";
|
|
|
|
if(!ready && !error) className = "blue";
|
|
|
|
if(ready && !error && showENSLink && !intervalCheck) className = "yellow";
|
|
|
|
if(ready && !error && showENSLink && intervalCheck) className = "green";
|
|
|
|
|
|
|
|
let messageTitle;
|
|
|
|
let messageBody;
|
|
|
|
switch(className){
|
|
|
|
case "blue":
|
|
|
|
messageTitle = "Transaction is being processed";
|
|
|
|
messageBody = <Fragment><p>You will shortly receive some ETH and SNT, just for attending #CryptoLife! Use it to register an ENS Name and buy food.</p><p>We hope you have a wonderful time bringing crypto closer to life with us.</p></Fragment>
|
|
|
|
break;
|
|
|
|
case "red":
|
|
|
|
messageTitle = "Shit bro, there was an error";
|
|
|
|
messageBody = <p>Please ask a volunteer for help in person.</p>;
|
|
|
|
break;
|
|
|
|
case "yellow":
|
2018-10-06 21:22:28 +00:00
|
|
|
messageTitle = "You've already been funded!";
|
2018-10-06 18:50:15 +00:00
|
|
|
messageBody = <p>We're redirecting you to ENS to register your own username! Ask a volunteer for help if you get lost.</p>
|
|
|
|
break;
|
|
|
|
case "green":
|
2018-10-06 21:22:28 +00:00
|
|
|
messageTitle = "Great success! Such future!";
|
2018-10-06 18:50:15 +00:00
|
|
|
messageBody = <p>We're redirecting you to ENS to register your own username! Ask a volunteer for help if you get lost.</p>
|
|
|
|
break;
|
|
|
|
}
|
2018-10-04 17:31:50 +00:00
|
|
|
|
2018-10-06 18:50:15 +00:00
|
|
|
return <div className={className + " container"}>
|
|
|
|
<div id="image">
|
2018-10-04 17:31:50 +00:00
|
|
|
|
2018-10-06 18:50:15 +00:00
|
|
|
</div>
|
|
|
|
<div className="message-title" onClick={(e) => this.showErrorMessage()}>
|
|
|
|
{messageTitle}
|
|
|
|
</div>
|
|
|
|
<div className="message-body">
|
|
|
|
{messageBody}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{ error && showErrorMessage && <div className="errorMessage">{errorMessage}</div> }
|
|
|
|
</div>
|
2018-10-03 13:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 12:45:56 +00:00
|
|
|
|
2018-10-03 13:36:13 +00:00
|
|
|
ReactDOM.render(<App></App>, document.getElementById('app'));
|