mirror of
https://github.com/status-im/autobounty.git
synced 2025-03-02 22:10:41 +00:00
Implement capability to fund contract with tokens
This commit is contained in:
parent
ed558feb7b
commit
ab24498d53
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@ -14,7 +14,7 @@
|
||||
"debug"
|
||||
],
|
||||
"env": {
|
||||
"WEBHOOK_SECRET": "fa7689e3"
|
||||
"WEBHOOK_SECRET": ""
|
||||
},
|
||||
"port": 9229
|
||||
},
|
||||
|
97
bot/index.js
97
bot/index.js
@ -2,6 +2,7 @@ const winston = require('winston');
|
||||
|
||||
const ethers = require('ethers');
|
||||
const Wallet = ethers.Wallet;
|
||||
const Contract = ethers.Contract;
|
||||
const providers = ethers.providers;
|
||||
const utils = ethers.utils;
|
||||
|
||||
@ -9,6 +10,8 @@ const prices = require('./prices');
|
||||
const config = require('../config');
|
||||
const github = require('./github');
|
||||
|
||||
const contractAddressString = 'Contract address:';
|
||||
|
||||
|
||||
const logger = winston.createLogger({
|
||||
level: 'info',
|
||||
@ -34,7 +37,7 @@ const needsFunding = function (req) {
|
||||
|
||||
const hasAddress = function (req) {
|
||||
const commentBody = req.body.comment.body;
|
||||
if (commentBody.search('Contract address:') === -1) {
|
||||
if (commentBody.search(contractAddressString) === -1) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
@ -43,34 +46,15 @@ const hasAddress = function (req) {
|
||||
|
||||
const getAddress = function (req) {
|
||||
const commentBody = req.body.comment.body;
|
||||
return commentBody.substring(commentBody.search("Contract address:") + 19, commentBody.search("Contract address:") + 61)
|
||||
}
|
||||
|
||||
const getLabelMock = function (req) {
|
||||
return new Promise((resolve, reject) => {
|
||||
github.getLabels(req)
|
||||
.then(labels => {
|
||||
const bountyLabels = labels.filter(name => config.bountyLabels.hasOwnProperty(name));
|
||||
if (bountyLabels.length === 1) {
|
||||
resolve(bountyLabels[0]);
|
||||
} else {
|
||||
reject('Error getting bounty labels: ' + bountyLabels);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err)
|
||||
});
|
||||
});
|
||||
const index = commentBody.search(contractAddressString) + 19
|
||||
return commentBody.substring(index, index + 42)
|
||||
}
|
||||
|
||||
const getLabel = function (req) {
|
||||
if (config.debug) {
|
||||
return getLabelMock(req);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
github.getLabels(req)
|
||||
.then(labels => {
|
||||
const bountyLabels = labels.filter(name => config.bountyLabels.hasOwnProperty(name));
|
||||
const bountyLabels = labels.filter(label => config.bountyLabels.hasOwnProperty(label.name));
|
||||
if (bountyLabels.length === 1) {
|
||||
resolve(bountyLabels[0]);
|
||||
} else {
|
||||
@ -86,7 +70,6 @@ const getAmountMock = function (req) {
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve(10);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
const getAmount = function (req) {
|
||||
@ -98,7 +81,7 @@ const getAmount = function (req) {
|
||||
.then(function (values) {
|
||||
let label = values[0];
|
||||
let tokenPrice = values[1];
|
||||
let amountToPayDollar = config.priceHour * config.bountyLabels[label];
|
||||
let amountToPayDollar = config.priceHour * config.bountyLabels[label.name];
|
||||
resolve(amountToPayDollar / tokenPrice);
|
||||
})
|
||||
.catch((err) => {
|
||||
@ -128,10 +111,7 @@ const error = function (errorMessage) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const sendTransaction = function (to, amount, gasPrice) {
|
||||
|
||||
const sendTransaction = async function (to, amount, gasPrice) {
|
||||
var chainId = providers.Provider.chainId.ropsten;
|
||||
var chainName = providers.networks.ropsten;
|
||||
|
||||
@ -144,28 +124,53 @@ const sendTransaction = function (to, amount, gasPrice) {
|
||||
const provider = ethers.providers.getDefaultProvider(chainName);
|
||||
|
||||
wallet.provider = provider;
|
||||
if (config.token === 'ETH') {
|
||||
const transaction = {
|
||||
gasLimit: config.gasLimit,
|
||||
gasPrice: gasPrice,
|
||||
to: to,
|
||||
value: amount,
|
||||
chainId: chainId
|
||||
};
|
||||
|
||||
return await wallet.sendTransaction(transaction);
|
||||
} else {
|
||||
let hash = null;
|
||||
|
||||
async function getAddress() { return wallet.address; }
|
||||
async function sign(transaction) { return wallet.sign(transaction); }
|
||||
|
||||
var transaction = {
|
||||
gasLimit: config.gasLimit,
|
||||
gasPrice: gasPrice,
|
||||
to: to,
|
||||
value: amount,
|
||||
chainId: chainId
|
||||
};
|
||||
async function resolveName(addressOrName) { return await provider.resolveName(addressOrName); }
|
||||
async function estimateGas(transaction) { return await provider.estimateGas(transaction); }
|
||||
async function getGasPrice() { return await provider.getGasPrice(); }
|
||||
async function getTransactionCount(blockTag) { return await provider.getTransactionCount(blockTag); }
|
||||
async function sendTransaction(transaction) {
|
||||
hash = await provider.sendTransaction(transaction);
|
||||
return hash;
|
||||
}
|
||||
|
||||
const customSigner = {
|
||||
getAddress: getAddress,
|
||||
provider: {
|
||||
resolveName: resolveName,
|
||||
estimateGas: estimateGas,
|
||||
getGasPrice: getGasPrice,
|
||||
getTransactionCount: getTransactionCount,
|
||||
sendTransaction: sendTransaction
|
||||
},
|
||||
sign: sign
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
wallet.sendTransaction(transaction)
|
||||
.then(function(hash) {
|
||||
logTransaction(hash);
|
||||
resolve(hash);
|
||||
}).catch(function(err) {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
const tokenContract = config.tokenContracts[config.token];
|
||||
const contractAddress = tokenContract.address;
|
||||
const contract = new Contract(contractAddress, tokenContract.abi, customSigner);
|
||||
const bigNumberAmount = ethers.utils.bigNumberify(amount);
|
||||
await contract.transfer(to, bigNumberAmount);
|
||||
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
needsFunding: needsFunding,
|
||||
getAddress: getAddress,
|
||||
|
@ -7,6 +7,42 @@ const BOUNTY_LABELS = {
|
||||
'bounty-xl': 10000
|
||||
}
|
||||
|
||||
const ERC20_ABI = [
|
||||
{
|
||||
"constant": false,
|
||||
"inputs": [
|
||||
{
|
||||
"name": "_to",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"name": "_amount",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "transfer",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "success",
|
||||
"type": "bool"
|
||||
}
|
||||
],
|
||||
"payable": false,
|
||||
"type": "function"
|
||||
}
|
||||
];
|
||||
|
||||
const TOKEN_CONTRACTS = {
|
||||
'STT': {
|
||||
address: '0xc55cF4B03948D7EBc8b9E8BAD92643703811d162',
|
||||
abi: ERC20_ABI
|
||||
},
|
||||
'SNT': {
|
||||
address: '0x744d70fdbe2ba4cf95131626614a1763df805b9e',
|
||||
abi: ERC20_ABI
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
// Debug mode for testing the bot
|
||||
debug: true,
|
||||
@ -38,6 +74,9 @@ module.exports = {
|
||||
// Bounty Labels for the issues and the correspondent hours (e.g. {'bounty-xs': 3})
|
||||
bountyLabels: BOUNTY_LABELS,
|
||||
|
||||
// Contract info for the different supported tokens
|
||||
tokenContracts: TOKEN_CONTRACTS,
|
||||
|
||||
// username for the bot which has to comment for starting the process (e.g. status-bounty-)
|
||||
githubUsername: 'status-open-bounty',
|
||||
|
||||
|
8
index.js
8
index.js
@ -96,17 +96,17 @@ const processRequest = function (req) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Promise.all([amountPromise, gasPricePromise])
|
||||
.then(function (results) {
|
||||
let amount = results[0];
|
||||
let gasPrice = results[1];
|
||||
const amount = results[0];
|
||||
const gasPrice = results[1];
|
||||
|
||||
bot.sendTransaction(to, amount, gasPrice)
|
||||
.then(function () {
|
||||
.then(function (hash) {
|
||||
bot.logTransaction(hash);
|
||||
resolve();
|
||||
})
|
||||
.catch(function (err) {
|
||||
reject(err);
|
||||
});
|
||||
|
||||
})
|
||||
.catch(function (err) {
|
||||
reject(err);
|
||||
|
Loading…
x
Reference in New Issue
Block a user