diff --git a/index.js b/index.js index 6aedf52..cb8f8a8 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,8 @@ const config = require('./config') const bot = require('./bot') const crypto = require('crypto') +const lru = require('lru-cache') +const previouslyFundedContracts = lru(10) const express = require('express') const cors = require('cors') @@ -35,8 +37,9 @@ app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) { if (validation.correct) { setTimeout(async () => { try { - await processRequest(req) - bot.info(`issue well funded: ${req.body.issue.url}`) + if (await processRequest(req)) { + bot.info(`Issue well funded: ${req.body.issue.url}`) + } } catch (err) { bot.error(`Error processing request: ${req.body.issue.url}`) bot.error(err) @@ -82,11 +85,22 @@ function validateRequest (req) { async function processRequest (req) { const to = bot.getAddress(req) + + const previousHash = previouslyFundedContracts.get(to) + if (previousHash) { + bot.info(`Issue has been funded before (tx hash=${previousHash}), ignoring`) + return null + } + const amount = await bot.getAmount(req) const gasPrice = await bot.getGasPrice() - const hash = await bot.sendTransaction(to, amount, gasPrice) + const transaction = await bot.sendTransaction(to, amount, gasPrice) - bot.logTransaction(hash) + previouslyFundedContracts.set(to, transaction.hash) + + bot.logTransaction(transaction) + + return transaction } const port = process.env.PORT || 8181 diff --git a/package-lock.json b/package-lock.json index 6005bc6..c0b0677 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1682,7 +1682,6 @@ "version": "4.1.2", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.2.tgz", "integrity": "sha512-wgeVXhrDwAWnIF/yZARsFnMBtdFXOg1b8RIrhilp+0iDYN4mdQcNZElDZ0e4B64BhaxeQ5zN7PMyvu7we1kPeQ==", - "dev": true, "requires": { "pseudomap": "1.0.2", "yallist": "2.1.2" @@ -2072,8 +2071,7 @@ "pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", - "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=", - "dev": true + "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=" }, "qs": { "version": "6.5.1", @@ -2608,8 +2606,7 @@ "yallist": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", - "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=", - "dev": true + "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=" } } } diff --git a/package.json b/package.json index 712a21e..752de00 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "express": "^4.15.2", "helmet": "^3.9.0", "lodash": "^4.17.4", + "lru-cache": "^4.1.2", "winston": "^3.0.0-rc3" }, "devDependencies": {