diff --git a/constants/contracts.js b/constants/contracts.js deleted file mode 100644 index d886213..0000000 --- a/constants/contracts.js +++ /dev/null @@ -1,6 +0,0 @@ -const addresses = { - dai: '0x7Af456bf0065aADAB2E6BEc6DaD3731899550b84', - usdt: '0x74a3dbd5831f45CD0F3002Bb87a59B7C15b1B5E6', - usdc: '0x265566D4365d80152515E800ca39424300374A83', -} -module.exports = {addresses} diff --git a/index.js b/index.js index 489aebe..b52b360 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,11 @@ +const {program} = require('commander'); const express = require('express') const path = require('path') const cors = require('cors') const bodyParser = require('body-parser'); const {initContract, claimTestToken} = require('./utils/contract') const {validClaimArgs} = require('./utils/address') +const fs = require("fs"); const log = console.log.bind(console) const port = 3089 const app = express() @@ -30,6 +32,13 @@ app.get('/', (req, res) => { res.sendFile(path.join('index.html')); }) +app.get('/tokens', (req, res) => { + log('receive a request get /tokens: ') + res.json({ + tokens: allTokens + }); +}) + app.post('/api/claimTestToken', (req, res) => { const params = req.body log('receive a request: ', params) @@ -49,9 +58,18 @@ app.post('/api/claimTestToken', (req, res) => { ) }) +const defaultConfig = './config.json'; + +let allTokens = []; const main = async () => { - await initContract() + const {config} = program + .option('-c, --config ', 'config path', defaultConfig) + .parse(process.argv) + .opts() + const { tokens } = JSON.parse(fs.readFileSync(config).toString()); + await initContract(tokens) + allTokens = Object.assign(tokens, []); app.listen(port, () => { log(`Faucet server listening at http://localhost:${port}`) diff --git a/package.json b/package.json index 131f1ee..4646499 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "license": "ISC", "dependencies": { "body-parser": "^1.19.0", + "commander": "^9.4.0", "cors": "^2.8.5", "dotenv": "^8.2.0", "ethers": "^5.0.23", diff --git a/utils/contract.js b/utils/contract.js index 2a1754c..e430d69 100644 --- a/utils/contract.js +++ b/utils/contract.js @@ -1,16 +1,15 @@ require('dotenv').config() const ethers = require("ethers"); const erc20Abi = require("../constants/abi/erc20.json") -const {addresses} = require("../constants/contracts") let provider, wallet const contracts = {} -const initContract = async () => { +const initContract = async (tokens) => { provider = await ethers.getDefaultProvider(process.env.RINKEBY_API); wallet = new ethers.Wallet(process.env.RINKEBY_DEPLOYER_PRIVATE_KEY, provider) - Object.entries(addresses).forEach(([tokenName, tokenAddress]) => { - contracts[tokenAddress] = ethers.ContractFactory.getContract(tokenAddress, erc20Abi, wallet) + tokens.map((token) => { + contracts[token.address] = ethers.ContractFactory.getContract(token.address, erc20Abi, wallet) }) } diff --git a/yarn.lock b/yarn.lock index 3ee9d65..ff100e6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -392,6 +392,11 @@ bytes@3.1.0: resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6" integrity sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg== +commander@^9.4.0: + version "9.4.0" + resolved "https://registry.npmmirror.com/commander/-/commander-9.4.0.tgz#bc4a40918fefe52e22450c111ecd6b7acce6f11c" + integrity sha512-sRPT+umqkz90UA8M1yqYfnHlZA7fF6nSphDtxeywPZ49ysjxDQybzk13CL+mXekDRG92skbcqCLVovuCusNmFw== + content-disposition@0.5.3: version "0.5.3" resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.3.tgz#e130caf7e7279087c5616c2007d0485698984fbd"