Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9bd875acba | |||
| cae2bd84b2 | |||
| 74a99d7c08 | |||
| 98a3ee087b | |||
| 217219fc68 |
167
index.js
167
index.js
@@ -16,41 +16,117 @@ const WalletSubprovider = require('ethereumjs-wallet/provider-engine');
|
|||||||
const Web3Subprovider = require('web3-provider-engine/subproviders/web3.js');
|
const Web3Subprovider = require('web3-provider-engine/subproviders/web3.js');
|
||||||
const Web3 = require('web3');
|
const Web3 = require('web3');
|
||||||
|
|
||||||
let engine = new ProviderEngine();
|
|
||||||
|
|
||||||
let walletPath = process.env.KREDITS_WALLET_PATH || './wallet.json';
|
|
||||||
let walletJson = fs.readFileSync(walletPath);
|
|
||||||
let wallet = Wallet.fromV3(JSON.parse(walletJson), process.env.KREDITS_WALLET_PASSWORD);
|
|
||||||
let providerUrl = process.env.KREDITS_PROVIDER_URL || 'http://localhost:8545';
|
|
||||||
let hubotWalletAddress = '0x' + wallet.getAddress().toString('hex');
|
|
||||||
|
|
||||||
let config = {};
|
|
||||||
if (process.env.KREDITS_CONTRACT_ADDRESS) {
|
|
||||||
config = { Kredits: { address: process.env.KREDITS_CONTRACT_ADDRESS }};
|
|
||||||
}
|
|
||||||
|
|
||||||
engine.addProvider(new WalletSubprovider(wallet, {}));
|
|
||||||
engine.addProvider(new Web3Subprovider(new Web3.providers.HttpProvider(providerUrl)));
|
|
||||||
// TODO only start engine if providerURL is accessible
|
|
||||||
engine.start();
|
|
||||||
|
|
||||||
let web3 = new Web3(engine);
|
|
||||||
let contracts = kreditsContracts(web3, config);
|
|
||||||
let Kredits = contracts['Kredits'];
|
|
||||||
|
|
||||||
console.log('[HUBOT-KREDITS] Wallet address: ' + hubotWalletAddress);
|
|
||||||
web3.eth.getBalance(hubotWalletAddress, function (err, balance) {
|
|
||||||
if (err) { console.log('[HUBOT-KREDITS] Error checking balance'); return; }
|
|
||||||
if (balance <= 0) {
|
|
||||||
console.log('[HUBOT-KREDITS] Hubot is broke. Please send some ETH to ' + hubotWalletAddress);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
let engine = new ProviderEngine();
|
||||||
|
|
||||||
|
let walletPath = process.env.KREDITS_WALLET_PATH || './wallet.json';
|
||||||
|
let walletJson = fs.readFileSync(walletPath);
|
||||||
|
let wallet = Wallet.fromV3(JSON.parse(walletJson), process.env.KREDITS_WALLET_PASSWORD);
|
||||||
|
let providerUrl = process.env.KREDITS_PROVIDER_URL || 'http://localhost:8545';
|
||||||
|
let hubotWalletAddress = '0x' + wallet.getAddress().toString('hex');
|
||||||
|
|
||||||
|
let config = {};
|
||||||
|
if (process.env.KREDITS_CONTRACT_ADDRESS) {
|
||||||
|
config = { Kredits: { address: process.env.KREDITS_CONTRACT_ADDRESS }};
|
||||||
|
}
|
||||||
|
|
||||||
|
engine.addProvider(new WalletSubprovider(wallet, {}));
|
||||||
|
engine.addProvider(new Web3Subprovider(new Web3.providers.HttpProvider(providerUrl)));
|
||||||
|
// TODO only start engine if providerURL is accessible
|
||||||
|
engine.start();
|
||||||
|
|
||||||
|
let web3 = new Web3(engine);
|
||||||
|
web3.eth.defaultAccount = hubotWalletAddress;
|
||||||
|
|
||||||
|
let contracts = kreditsContracts(web3, config);
|
||||||
|
let kredits = contracts['Kredits'];
|
||||||
|
|
||||||
module.exports = function(robot) {
|
module.exports = function(robot) {
|
||||||
|
|
||||||
|
robot.logger.info('[hubot-kredits] Wallet address: ' + hubotWalletAddress);
|
||||||
|
|
||||||
|
getBalance().then(balance => {
|
||||||
|
if (balance <= 0) {
|
||||||
|
messageRoom('Yo gang, I\m broke! Please drop me some ETH to ${hubotWalletAddress}. kthxbai.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function getBalance() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
web3.eth.getBalance(hubotWalletAddress, function (err, balance) {
|
||||||
|
if (err) {
|
||||||
|
robot.logger.error('[hubot-kredits] Error checking balance');
|
||||||
|
reject(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
resolve(balance);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getValueFromContract(contractMethod, ...args) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
kredits[contractMethod](...args, (err, data) => {
|
||||||
|
if (err) { reject(err); }
|
||||||
|
resolve(data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getContributorData(i) {
|
||||||
|
let promise = new Promise((resolve, reject) => {
|
||||||
|
getValueFromContract('contributorAddresses', i).then(address => {
|
||||||
|
robot.logger.debug('address', address);
|
||||||
|
getValueFromContract('contributors', address).then(person => {
|
||||||
|
robot.logger.debug('person', person);
|
||||||
|
let contributor = {
|
||||||
|
address: address,
|
||||||
|
github_username: person[1],
|
||||||
|
github_uid: person[0],
|
||||||
|
ipfsHash: person[2]
|
||||||
|
};
|
||||||
|
robot.logger.debug('[kredits] contributor', contributor);
|
||||||
|
resolve(contributor);
|
||||||
|
});
|
||||||
|
}).catch(err => reject(err));
|
||||||
|
});
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getContributors() {
|
||||||
|
return getValueFromContract('contributorsCount').then(contributorsCount => {
|
||||||
|
let contributors = [];
|
||||||
|
|
||||||
|
for(var i = 0; i < contributorsCount.toNumber(); i++) {
|
||||||
|
contributors.push(getContributorData(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.all(contributors);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getContributorByGithubUser(username) {
|
||||||
|
let promise = new Promise((resolve, reject) => {
|
||||||
|
getContributors().then(contributors => {
|
||||||
|
let contrib = contributors.find(c => {
|
||||||
|
return c.github_username === username;
|
||||||
|
});
|
||||||
|
if (contrib) {
|
||||||
|
resolve(contrib);
|
||||||
|
} else {
|
||||||
|
reject();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
function messageRoom(message) {
|
||||||
|
robot.messageRoom(process.env.KREDITS_ROOM, message);
|
||||||
|
}
|
||||||
|
|
||||||
function amountFromIssueLabels(issue) {
|
function amountFromIssueLabels(issue) {
|
||||||
let kreditsLabel = issue.labels.map(l => l.name)
|
let kreditsLabel = issue.labels.map(l => l.name)
|
||||||
.filter(n => n.match(/^kredits/))[0];
|
.filter(n => n.match(/^kredits/))[0];
|
||||||
@@ -75,12 +151,19 @@ web3.eth.getBalance(hubotWalletAddress, function (err, balance) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createProposal(recipient, amount, url/*, metaData*/) {
|
function createProposal(recipient, amount, url/*, metaData*/) {
|
||||||
return new Promise((resolve/*, reject*/) => {
|
return new Promise((resolve, reject) => {
|
||||||
// TODO write metaData to IPFS
|
// TODO write metaData to IPFS
|
||||||
console.log(`Creating proposal to issue ${amount}₭S to ${recipient} for ${url}...`);
|
robot.logger.debug(`Creating proposal to issue ${amount}₭S to ${recipient} for ${url}...`);
|
||||||
|
|
||||||
|
getContributorByGithubUser(recipient).then(c => {
|
||||||
|
kredits.addProposal(c.address, amount, url, '', (e/* , d */) => {
|
||||||
|
if (e) { reject(); return; }
|
||||||
|
messageRoom(`New proposal created: ${amount} for ${recipient}`);
|
||||||
|
});
|
||||||
|
}, () => {
|
||||||
|
messageRoom(`Couldn\'t find contributor data for ${recipient}. Please add them first!`);
|
||||||
|
});
|
||||||
|
|
||||||
Kredits.addProposal(recipient, amount, url, '');
|
|
||||||
robot.messageRoom(process.env.KREDITS_ROOM, `new proposal: ${amount} for ${recipient}`);
|
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -146,10 +229,24 @@ web3.eth.getBalance(hubotWalletAddress, function (err, balance) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
robot.respond(/(got ETH)|(got gas)\?/i, res => {
|
||||||
|
getBalance().then(balance => {
|
||||||
|
if (balance <= 0) {
|
||||||
|
res.send(`HALP, I\'m totally broke! Not a single wei in my pocket.`);
|
||||||
|
}
|
||||||
|
else if (balance >= 1e+17) {
|
||||||
|
res.send(`my wallet contains ${balance} wei`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.send(`I\'m almost broke! Only have ${balance} wei left in my pocket. :(`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
robot.router.post('/incoming/kredits/github/'+process.env.KREDITS_WEBHOOK_TOKEN, (req, res) => {
|
robot.router.post('/incoming/kredits/github/'+process.env.KREDITS_WEBHOOK_TOKEN, (req, res) => {
|
||||||
let evt = req.header('X-GitHub-Event');
|
let evt = req.header('X-GitHub-Event');
|
||||||
let data = req.body;
|
let data = req.body;
|
||||||
console.log(`Received GitHub hook. Event: ${evt}, action: ${data.action}`);
|
robot.logger.debug(`Received GitHub hook. Event: ${evt}, action: ${data.action}`);
|
||||||
|
|
||||||
if (evt === 'pull_request' && data.action === 'closed') {
|
if (evt === 'pull_request' && data.action === 'closed') {
|
||||||
handleGitHubPullRequestClosed(data).then(() => res.send(200));
|
handleGitHubPullRequestClosed(data).then(() => res.send(200));
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hubot-kredits",
|
"name": "hubot-kredits",
|
||||||
"version": "1.2.0",
|
"version": "1.4.0",
|
||||||
"description": "Kosmos Kredits functionality for chat bots",
|
"description": "Kosmos Kredits functionality for chat bots",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ethereumjs-wallet": "mvayngrib/ethereumjs-wallet",
|
"ethereumjs-wallet": "mvayngrib/ethereumjs-wallet",
|
||||||
"kredits-contracts": "67p/kredits-contracts",
|
"kredits-contracts": "2.0.0",
|
||||||
"node-fetch": "^1.6.3",
|
"node-fetch": "^1.6.3",
|
||||||
"prompt": "^1.0.0",
|
"prompt": "^1.0.0",
|
||||||
"web3": "^0.18.4",
|
"web3": "^0.18.4",
|
||||||
|
|||||||
Reference in New Issue
Block a user