Merge pull request #11 from PombeirP/feature/avoid-double-funding

Keep LRU list of last 10 funded issues to avoid double-funding
This commit is contained in:
Andy Tudhope 2018-03-20 17:24:54 +02:00 committed by GitHub
commit 10750a915a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 9 deletions

View File

@ -9,6 +9,8 @@
const config = require('./config') const config = require('./config')
const bot = require('./bot') const bot = require('./bot')
const crypto = require('crypto') const crypto = require('crypto')
const lru = require('lru-cache')
const previouslyFundedContracts = lru(10)
const express = require('express') const express = require('express')
const cors = require('cors') const cors = require('cors')
@ -35,8 +37,9 @@ app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) {
if (validation.correct) { if (validation.correct) {
setTimeout(async () => { setTimeout(async () => {
try { try {
await processRequest(req) if (await processRequest(req)) {
bot.info(`issue well funded: ${req.body.issue.url}`) bot.info(`Issue well funded: ${req.body.issue.url}`)
}
} catch (err) { } catch (err) {
bot.error(`Error processing request: ${req.body.issue.url}`) bot.error(`Error processing request: ${req.body.issue.url}`)
bot.error(err) bot.error(err)
@ -82,11 +85,22 @@ function validateRequest (req) {
async function processRequest (req) { async function processRequest (req) {
const to = bot.getAddress(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 amount = await bot.getAmount(req)
const gasPrice = await bot.getGasPrice() 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 const port = process.env.PORT || 8181

7
package-lock.json generated
View File

@ -1682,7 +1682,6 @@
"version": "4.1.2", "version": "4.1.2",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.2.tgz", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.2.tgz",
"integrity": "sha512-wgeVXhrDwAWnIF/yZARsFnMBtdFXOg1b8RIrhilp+0iDYN4mdQcNZElDZ0e4B64BhaxeQ5zN7PMyvu7we1kPeQ==", "integrity": "sha512-wgeVXhrDwAWnIF/yZARsFnMBtdFXOg1b8RIrhilp+0iDYN4mdQcNZElDZ0e4B64BhaxeQ5zN7PMyvu7we1kPeQ==",
"dev": true,
"requires": { "requires": {
"pseudomap": "1.0.2", "pseudomap": "1.0.2",
"yallist": "2.1.2" "yallist": "2.1.2"
@ -2072,8 +2071,7 @@
"pseudomap": { "pseudomap": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz",
"integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=", "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM="
"dev": true
}, },
"qs": { "qs": {
"version": "6.5.1", "version": "6.5.1",
@ -2608,8 +2606,7 @@
"yallist": { "yallist": {
"version": "2.1.2", "version": "2.1.2",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz",
"integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=", "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI="
"dev": true
} }
} }
} }

View File

@ -22,6 +22,7 @@
"express": "^4.15.2", "express": "^4.15.2",
"helmet": "^3.9.0", "helmet": "^3.9.0",
"lodash": "^4.17.4", "lodash": "^4.17.4",
"lru-cache": "^4.1.2",
"winston": "^3.0.0-rc3" "winston": "^3.0.0-rc3"
}, },
"devDependencies": { "devDependencies": {