Skip to content
This repository has been archived by the owner on Jan 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 from Dhaiwat10/restrictions
Browse files Browse the repository at this point in the history
Add withdrawal restrictions (#1)
  • Loading branch information
Dhaiwat10 authored Apr 19, 2021
2 parents 3816cc2 + 8373543 commit 91d7dde
Show file tree
Hide file tree
Showing 7 changed files with 744 additions and 17 deletions.
9 changes: 8 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
ROPSTEN_PRIVATE_KEY=
INFURA_API_KEY=
NEXT_PUBLIC_CONTRACT_ADDRESS=
CALLER_SECRET=
CALLER_SECRET=
FIREBASE_API_KEY=
FIREBASE_AUTH_DOMAIN=
FIREBASE_PROJECT_ID=
FIREBASE_STORAGE_BUCKET=
FIREBASE_MESSAGING_ID=
FIREBASE_APP_ID=
FIREBASE_MEASUREMENT_ID=
1 change: 1 addition & 0 deletions ADDRESS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0xEccA7BF722455A94b9e759247aB86E2e1559c871
663 changes: 651 additions & 12 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"dependencies": {
"axios": "^0.21.1",
"firebase": "^8.4.1",
"next": "^10.1.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
41 changes: 41 additions & 0 deletions src/lib/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { firebase } from './firebase';

export const validateRequest = async (address: string): Promise<boolean> => {
try {
const ref = firebase.database().ref('addresses/');
const currentTime = Date.now();

const snapshot = await ref.child(address).get();

if (snapshot.exists()) {
// Previous record exists

// Validate request
if (currentTime - snapshot.val() < 8.64e7) {
// Cooldown not over yet
return false;
} else {
// Cooldown over
return true;
}
} else {
// Allow withdrawal
return true;
}
} catch (error) {
console.log(error);
return false;
}
};

export const createRecord = async (address: string): Promise<boolean> => {
try {
const currentTime = Date.now();
const ref = firebase.database().ref('addresses/' + address);
await ref.set(currentTime);
return true;
} catch (error) {
console.log(error);
return false;
}
};
19 changes: 19 additions & 0 deletions src/lib/firebase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import firebase from 'firebase/app';
import 'firebase/firebase-database';

const firebaseConfig = {
apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
projectId: process.env.FIREBASE_PROJECT_ID,
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.FIREBASE_MESSAGING_ID,
appId: process.env.FIREBASE_APP_ID,
measurementId: process.env.FIREBASE_MEASUREMENT_ID,
};

if (typeof window !== 'undefined' && !firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
firebase.database();
}

export { firebase };
27 changes: 23 additions & 4 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FormEvent, useEffect, useState } from 'react';
import { Button, Container, Form, Input, Message } from 'semantic-ui-react';
import { verifyEthAddress } from '../util';
import axios from 'axios';
import { createRecord, validateRequest } from '../lib/db';

function Home(): React.ReactNode {
const [value, setValue] = useState('');
Expand All @@ -24,9 +25,26 @@ function Home(): React.ReactNode {
};

const requestFunds = async () => {
setSuccess(false);
setError('');
setLoading(true);
try {
const validity = await validateRequest(value);
if (!validity) {
setLoading(false);
setError(
'You can make only 1 request in a day. Please try again later.'
);
return;
}
await axios.post('/api', { address: value });
const recordCreated = await createRecord(value);

if (!recordCreated) {
// TODO: Creating the record failed, probably report this
console.log('Couldnt create the record');
}

setSuccess(true);
} catch (e) {
console.log(e);
Expand Down Expand Up @@ -64,8 +82,8 @@ function Home(): React.ReactNode {
}, []);

return (
<Container>
<Form success={success} error={!!error} onSubmit={onSubmit}>
<Container style={{ height: '100vh' }}>
<Form style={{ marginTop: 100 }} success={success} error={!!error} onSubmit={onSubmit}>
<h1>Ropsten Faucet</h1>
<Form.Field error={touched && !isValid}>
<label>Your Ropsten wallet address</label>
Expand All @@ -83,13 +101,14 @@ function Home(): React.ReactNode {
content="0.1 ether will soon be transferred to your wallet."
/>
)}
<Button loading={loading} primary>
<Button style={{ marginBottom: 10 }} loading={loading} primary>
Request Ether
</Button>
</Form>
{contractBalance ? (
<span>
Contract balance: {parseInt(contractBalance || '') / Math.pow(10, 18)}{' '}
Contract balance:{' '}
{(parseInt(contractBalance || '') / Math.pow(10, 18), 2).toFixed(2)}{' '}
ether
</span>
) : null}
Expand Down

1 comment on commit 91d7dde

@vercel
Copy link

@vercel vercel bot commented on 91d7dde Apr 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.