From 359108fcf71bbb2c35a305bbd5b4610341e61117 Mon Sep 17 00:00:00 2001 From: sleyter93 <96137983+sleyter93@users.noreply.github.com> Date: Fri, 28 Jun 2024 11:08:08 -0500 Subject: [PATCH] profiles (#146) Added new profile env for differentiation between DAO and WALLET --- .env => .env.dao | 3 ++- .env.wallet | 10 ++++++++++ .gitignore | 1 - Dockerfile | 4 ++-- README.md | 3 ++- src/controller/httpsAPI.ts | 3 ++- src/index.ts | 29 ++++++++++++++++++++++------- 7 files changed, 40 insertions(+), 13 deletions(-) rename .env => .env.dao (78%) create mode 100644 .env.wallet diff --git a/.env b/.env.dao similarity index 78% rename from .env rename to .env.dao index a0f06a6..357d5c1 100644 --- a/.env +++ b/.env.dao @@ -6,4 +6,5 @@ NODE_URL=https://public-node.testnet.rsk.co NODE_MAINNET_URL=https://public-node.rsk.co CYPHER_ESTIMATE_FEE_URL=https://api.blockcypher.com/v1/btc/test3 CYPHER_ESTIMATE_FEE_MAINNET_URL=https://api.blockcypher.com/v1/btc/main -# API_URL=https://rootstock-testnet.blockscout.com/api \ No newline at end of file +API_URL=https://rootstock-testnet.blockscout.com/api +API_MAINNET_URL=https://rootstock.blockscout.com/api \ No newline at end of file diff --git a/.env.wallet b/.env.wallet new file mode 100644 index 0000000..ac3d704 --- /dev/null +++ b/.env.wallet @@ -0,0 +1,10 @@ +COIN_MARKET_CAP_KEY= +DEFAULT_CONVERT_FIAT=USD +BLOCKBOOK_URL=http://ip-10-10-130-64.ec2.internal:19130 +BLOCKBOOK_MAINNET_URL=http://ip-10-10-130-76.ec2.internal:9130 +NODE_URL=https://public-node.testnet.rsk.co +NODE_MAINNET_URL=https://public-node.rsk.co +CYPHER_ESTIMATE_FEE_URL=https://api.blockcypher.com/v1/btc/test3 +CYPHER_ESTIMATE_FEE_MAINNET_URL=https://api.blockcypher.com/v1/btc/main +API_URL=https://be.explorer.testnet.rootstock.io/api +API_MAINNET_URL=https://be.explorer.rootstock.io/api \ No newline at end of file diff --git a/.gitignore b/.gitignore index 1a0c36a..7dd02e6 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,4 @@ node_modules/ # others .vscode -.env* .DS_Store \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 35e16a9..b02bf0a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:16 +FROM node:18-slim WORKDIR /app @@ -9,7 +9,7 @@ RUN npm i COPY ./src ./src COPY ./tsconfig.json ./ -COPY ./.env ./ +COPY ./.env.* ./ RUN npm run build diff --git a/README.md b/README.md index cf30917..ea1d489 100644 --- a/README.md +++ b/README.md @@ -122,4 +122,5 @@ npm run start:prod We provide two different indexer integrations to get balances, transactions, events: - RSKExplorerAPI(Default) - BlockscoutAPI -You should only replace the RSKExplorerAPI class in src/index.ts and set blockscout url in .env +To use RSKExplorerAPI, you should set profile environment variable into PROFILE=wallet +To use BlockscoutAPI, you should set profile environment variable into PROFILE=dao diff --git a/src/controller/httpsAPI.ts b/src/controller/httpsAPI.ts index 7e0b964..138b181 100644 --- a/src/controller/httpsAPI.ts +++ b/src/controller/httpsAPI.ts @@ -62,7 +62,8 @@ export class HttpsAPI { const whilelist = ['https://dapp.testnet.dao.rif.technology', 'https://dapp.mainnet.dao.rif.technology', - 'https://rif-wallet-services.testnet.rifcomputing.net'] + 'https://rif-wallet-services.testnet.rifcomputing.net', + 'https://dao-backend.testnet.rifcomputing.net'] this.app.use(cors({ origin: (origin, callback) => { if (!origin || whilelist.indexOf(origin) !== -1) { diff --git a/src/index.ts b/src/index.ts index 364eb71..3245a98 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import 'dotenv/config' +import dotenv from 'dotenv' import express from 'express' import axios from 'axios' import http from 'http' @@ -14,15 +14,32 @@ import BitcoinCore from './service/bitcoin/BitcoinCore' import { ethers } from 'ethers' import { AddressService } from './service/address/AddressService' import { RSKExplorerAPI } from './rskExplorerApi' +import { BlockscoutAPI } from './blockscoutApi' async function main () { + const profile = process.env.PROFILE || 'wallet' + + dotenv.config({ + path: `.env.${profile}` + }) + + const createInstance = (apiUrl: string, chainId: number, _axios: typeof axios, id: string) => { + switch (profile) { + case 'wallet': + return new RSKExplorerAPI(apiUrl, chainId, _axios, id) + case 'dao': + return new BlockscoutAPI(apiUrl, chainId, _axios, id) + default: + throw new Error(`Unknown environment: ${profile}`) + } + } + const environment = { // TODO: remove these defaults NETWORKS: [ { ID: '31', - API_URL: (process.env.API_URL as string) || - 'https://be.explorer.testnet.rootstock.io/api', + API_URL: process.env.API_URL as string, CHAIN_ID: parseInt(process.env.CHAIN_ID as string) || 31, BLOCKBOOK_URL: process.env.BLOCKBOOK_URL, NODE_URL: process.env.NODE_URL, @@ -30,8 +47,7 @@ async function main () { }, { ID: '30', - API_URL: (process.env.API_MAINNET_URL as string) || - 'https://be.explorer.rootstock.io/api', + API_URL: (process.env.API_MAINNET_URL as string), CHAIN_ID: parseInt(process.env.CHAIN_MAINNET_ID as string) || 30, BLOCKBOOK_URL: process.env.BLOCKBOOK_MAINNET_URL, NODE_URL: process.env.NODE_MAINNET_URL, @@ -51,8 +67,7 @@ async function main () { const bitcoinMapping: BitcoinDatasource = {} const nodeProvider: RSKNodeProvider = {} environment.NETWORKS.forEach(network => { - dataSourceMapping[network.ID] = new RSKExplorerAPI(network.API_URL, network.CHAIN_ID, axios, network.ID) - // dataSourceMapping[network.ID] = new BlockscoutAPI(network.API_URL, network.CHAIN_ID, axios, network.ID) + dataSourceMapping[network.ID] = createInstance(network.API_URL, network.CHAIN_ID, axios, network.ID) bitcoinMapping[network.ID] = new BitcoinCore({ BLOCKBOOK_URL: network.BLOCKBOOK_URL, CYPHER_ESTIMATE_FEE_URL: network.CYPHER_ESTIMATE_FEE_URL