Skip to content

Commit

Permalink
Firebase Migration (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklabh authored Aug 9, 2022
1 parent 1bfc9bf commit 6e97378
Show file tree
Hide file tree
Showing 13 changed files with 1,201 additions and 94 deletions.
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "facuet-bot"
}
}
25 changes: 25 additions & 0 deletions .github/workflows/firebase-hosting-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on merge
'on':
push:
branches:
- main
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_FACUET_BOT }}'
channelId: live
projectId: facuet-bot
- name: Deploy Firebase Function
uses: w9jds/firebase-action@master
with:
args: deploy --only functions
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
16 changes: 16 additions & 0 deletions .github/workflows/firebase-hosting-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on PR
'on': pull_request
jobs:
build_and_preview:
if: '${{ github.event.pull_request.head.repo.full_name == github.repository }}'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_FACUET_BOT }}'
projectId: facuet-bot
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,6 @@ lib
.db/*
!.db/.gitkeep

# End of https://www.toptal.com/developers/gitignore/api/node,visualstudiocode,yarn
.firebase/

# End of https://www.toptal.com/developers/gitignore/api/node,visualstudiocode,yarn
31 changes: 31 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"functions": {
"source": ".",
"predeploy": "npm --prefix \"$RESOURCE_DIR\" run build"
},
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "app"
}
]
},
"emulators": {
"functions": {
"port": 5001
},
"hosting": {
"port": 5000
},
"ui": {
"enabled": true
}
}
}
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
},
"main": "build/index.js",
"scripts": {
"serve:firebase": "npm run build && firebase emulators:start --only functions",
"shell:firebase": "npm run build && firebase functions:shell",
"start": "yarn run serve",
"serve": "node build/index.js",
"dev": "ts-node-dev -r dotenv/config src/index.ts",
"build": "tsc --project tsconfig.json",
"lint": "eslint '*/**/*.{js,ts}' --quiet --fix && prettier -w .",
"lint:check": "eslint '*/**/*.{js,ts}' && prettier -c .",
"lint": "eslint '*/**/*.{js,ts}' --quiet --fix",
"lint:check": "eslint '*/**/*.{js,ts}'",
"test": "NODE_ENV=test echo \"Test not implemented\"!"
},
"engines": {
"node": ">=16.6.x"
"node": "16"
},
"license": "MIT",
"devDependencies": {
Expand Down Expand Up @@ -69,6 +71,8 @@
"discord-api-types": "^0.30.0",
"discord.js": "^13.6.0",
"express": "^4.17.3",
"firebase-admin": "^11.0.0",
"firebase-functions": "^3.22.0",
"google-recaptcha": "^1.1.0"
}
}
12 changes: 12 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Astar Faucet Bot</title>
</head>
<body>
Astar Faucet Bot
</body>
</html>
57 changes: 0 additions & 57 deletions src/app.ts

This file was deleted.

26 changes: 20 additions & 6 deletions src/clients/astar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ interface ChainProperty {
}

export class FaucetApi {
private _started: boolean;
private _mnemonic: string;
private _faucetAccount: KeyringPair;
private _provider: WsProvider;
private _api: ApiPromise;
Expand All @@ -54,11 +56,13 @@ export class FaucetApi {
provider: this._provider,
});

this._mnemonic = options.mnemonic;

//this._keyring = new Keyring({ type: 'sr25519', ss58Format: ASTAR_SS58_FORMAT });
// create a random account if no mnemonic was provided
this._faucetAccount = this._keyring.addFromUri(options.mnemonic || mnemonicGenerate(), {
name: 'Astar Faucet',
});
// // create a random account if no mnemonic was provided
// this._faucetAccount = this._keyring.addFromUri(options.mnemonic || mnemonicGenerate(), {
// name: 'Astar Faucet',
// });
}

public get faucetAccount() {
Expand All @@ -84,6 +88,10 @@ export class FaucetApi {
}

public async start() {
if (this._started) {
return this;
}

this._api = await this._api.isReady;

const chainProperties = await this._api.rpc.system.properties();
Expand All @@ -102,14 +110,20 @@ export class FaucetApi {

const chainName = (await this._api.rpc.system.chain()).toString();

console.log(`connected to ${chainName} with account ${this.faucetAccount.address}`);

this._chainProperty = {
tokenSymbols,
tokenDecimals,
chainName,
};
this._keyring.setSS58Format(ss58Format);
this._started = true;

// create a random account if no mnemonic was provided
this._faucetAccount = this._keyring.addFromUri(this._mnemonic || mnemonicGenerate(), {
name: 'Astar Faucet',
});

console.log(`connected to ${chainName} with account ${this.faucetAccount.address}`);

return this;
}
Expand Down
18 changes: 15 additions & 3 deletions src/clients/express.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import cors from 'cors';
import express from 'express';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { verifyRecaptcha } from '../helpers';
import { NetworkApis, Network, FaucetInfo } from '../types';
import * as functions from 'firebase-functions';

const whitelist = ['http://localhost:8080', 'http://localhost:8081', 'https://portal.astar.network'];

const recaptchaSecret = process.env.GOOGLE_RECAPTCHA_SECRET;
const recaptchaSecret = process.env.GOOGLE_RECAPTCHA_SECRET || functions.config().google.recaptcha_secret;
if (!recaptchaSecret) {
throw Error('Secret key for recaptcha is not defined');
}
Expand All @@ -14,7 +16,7 @@ if (!recaptchaSecret) {
* Handles client request via Express.js. These are usually for custom endpoints or OAuth and app installation.
* We didn't hook this up to any database, so for out-of-the-box usage, you can hard-code the guild ID and other credentials in a .env file
*/
export const expressApp = async (apis: NetworkApis) => {
export const expressApp = (apis: NetworkApis) => {
const app = express();
app.use(express.json());
app.use(cors());
Expand All @@ -41,6 +43,10 @@ export const expressApp = async (apis: NetworkApis) => {

const { astarApi, shidenApi, shibuyaApi } = apis;

app.get('/healthcheck', async (req, res) => {
return res.status(200).json({ status: 'ok' });
});

app.post('/:network/drip', async (req, res) => {
try {
// todo: refactor to make this generic instead of hard coding
Expand All @@ -66,12 +72,15 @@ export const expressApp = async (apis: NetworkApis) => {
// i know this is not a clean solution :(
switch (network) {
case Network.astar:
await astarApi.start();
hash = await astarApi.drip(address);
break;
case Network.shiden:
await shidenApi.start();
hash = await shidenApi.drip(address);
break;
default:
await shibuyaApi.start();
hash = await shibuyaApi.drip(address);
break;
}
Expand All @@ -96,6 +105,7 @@ export const expressApp = async (apis: NetworkApis) => {
switch (network) {
case Network.astar:
//const { timestamps, faucet } = await getFaucetInfo({ network, address });
await astarApi.start();
balance = await astarApi.getBalance();
faucetInfo = {
timestamps: astarApi.faucetRequestTime(address),
Expand All @@ -108,6 +118,7 @@ export const expressApp = async (apis: NetworkApis) => {

case Network.shiden:
//const { timestamps, faucet } = await getFaucetInfo({ network, address });
await shidenApi.start();
balance = await shidenApi.getBalance();
faucetInfo = {
timestamps: shidenApi.faucetRequestTime(address),
Expand All @@ -119,6 +130,7 @@ export const expressApp = async (apis: NetworkApis) => {
break;
default:
//const { timestamps, faucet } = await getFaucetInfo({ network, address });
await shibuyaApi.start();
balance = await shibuyaApi.getBalance();
faucetInfo = {
timestamps: shibuyaApi.faucetRequestTime(address),
Expand All @@ -139,6 +151,6 @@ export const expressApp = async (apis: NetworkApis) => {
}
});

app.listen(port, () => console.log(`App listening at port ${port}`));
// app.listen(port, () => console.log(`App listening at port ${port}`));
return app;
};
6 changes: 3 additions & 3 deletions src/config/appConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

"network": {
"shibuya": {
"endpoint": "wss://rpc.shibuya.astar.network",
"endpoint": "wss://shibuya-rpc.dwellir.com",
"amount": 10,
"requestTimeout": 3600,
"types": {
Expand All @@ -65,7 +65,7 @@
}
},
"shiden": {
"endpoint": "wss://rpc.shiden.astar.network",
"endpoint": "wss://shiden.api.onfinality.io/public-ws",
"amount": 0.002,
"requestTimeout": 172800,
"types": {
Expand All @@ -89,7 +89,7 @@
}
},
"astar": {
"endpoint": "wss://rpc.astar.network",
"endpoint": "wss://astar.api.onfinality.io/public-ws",
"amount": 0.002,
"requestTimeout": 172800,
"types": {
Expand Down
Loading

0 comments on commit 6e97378

Please sign in to comment.