Skip to content

Commit

Permalink
update to latest lpp-dacs plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ewingrj committed Dec 20, 2017
1 parent 4e5c786 commit 2f5c359
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 58 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
"feathers-swagger": "^0.5.0",
"fs-blob-store": "^5.2.1",
"helmet": "^3.8.1",
"liquidpledging": "git://github.com/giveth/liquidpledging.git#28313ee",
"giveth-liquidpledging": "0.0.10",
"lpp-campaign": "0.0.14",
"lpp-capped-milestone": "0.0.3",
"lpp-dac": "0.0.5",
"lpp-dacs": "0.0.2",
"minimetoken": "^0.2.0",
"mkdirp": "^0.5.1",
"multer": "^1.3.0",
Expand Down
2 changes: 1 addition & 1 deletion scripts/confirm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Web3 = require('web3');
const { LPVault } = require('liquidpledging');
const { LPVault } = require('giveth-liquidpledging');

const web3 = new Web3("ws://localhost:8546");

Expand Down
10 changes: 5 additions & 5 deletions scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Web3 = require('web3');
const { LiquidPledging, LPVault } = require('liquidpledging');
const { LPPDacFactory, LPPDacRuntimeByteCode } = require('lpp-dac');
const { LiquidPledging, LPVault } = require('giveth-liquidpledging');
const { LPPDacs, LPPDacsRuntimeByteCode } = require('lpp-dacs');
const { LPPCampaignFactory, LPPCampaignRuntimeByteCode } = require('lpp-campaign');
const { LPPCappedMilestones, LPPCappedMilestonesRuntimeByteCode} = require('lpp-capped-milestone');

Expand All @@ -13,17 +13,17 @@ async function deploy() {
const liquidPledging = await LiquidPledging.new(web3, vault.$address, escapeHatch, escapeHatch);
await vault.setLiquidPledging(liquidPledging.$address);

const dacFactory = await LPPDacFactory.new(web3, escapeHatch, escapeHatch, {gas: 6500000});
const dacs = await LPPDacs.new(web3, liquidPledging.$address, escapeHatch, escapeHatch, {gas: 6500000});
const campaignFactory = await LPPCampaignFactory.new(web3, escapeHatch, escapeHatch, {gas: 6500000});
const cappedMilestones = await LPPCappedMilestones.new(web3, liquidPledging.$address, escapeHatch, escapeHatch);

await liquidPledging.addValidPlugin(web3.utils.keccak256(LPPDacRuntimeByteCode));
await liquidPledging.addValidPlugin(web3.utils.keccak256(LPPDacsRuntimeByteCode));
await liquidPledging.addValidPlugin(web3.utils.keccak256(LPPCampaignRuntimeByteCode));
await liquidPledging.addValidPlugin(web3.utils.keccak256(LPPCappedMilestonesRuntimeByteCode));

console.log('vault Address: ', vault.$address);
console.log('liquidPledging Address: ', liquidPledging.$address);
console.log('LPPDacFactory Address: ', dacFactory.$address);
console.log('LPPDacs Address: ', dacs.$address);
console.log('LPPCampaignFactory Address: ', campaignFactory.$address);
console.log('LPPCappedMilestones Address: ', cappedMilestones.$address);
process.exit(); // some reason, this script won't exit. I think it has to do with web3 subscribing to tx confirmations?
Expand Down
2 changes: 1 addition & 1 deletion scripts/getState.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Web3 = require('web3');
const { LiquidPledging, LiquidPledgingState, LPVault } = require('liquidpledging');
const { LiquidPledging, LiquidPledgingState, LPVault } = require('giveth-liquidpledging');

const web3 = new Web3('ws://localhost:8546');

Expand Down
11 changes: 4 additions & 7 deletions src/blockchain/Admins.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logger from 'winston';

import { LPPDac } from 'lpp-dac';
import { LPPCappedMilestones } from 'lpp-capped-milestone';
import { LPPCappedMilestonesRuntimeByteCode } from 'lpp-capped-milestone/build/LPPCappedMilestones.sol';
import { LPPCampaign } from 'lpp-campaign';
Expand All @@ -15,10 +14,11 @@ const BreakSignal = () => {
* class to keep feathers cache in sync with liquidpledging admins
*/
class Admins {
constructor(app, liquidPledging, eventQueue) {
constructor(app, liquidPledging, lppdacs, eventQueue) {
this.app = app;
this.web3 = liquidPledging.$web3;
this.liquidPledging = liquidPledging;
this.lppdacs = lppdacs;
this.queue = eventQueue;
}

Expand Down Expand Up @@ -189,11 +189,8 @@ class Admins {
return data[ 0 ];
});

const getTokenInfo = (delegate) => {
const dac = new LPPDac(this.web3, delegate.plugin);

return dac.token().then(addr => getTokenInformation(this.web3, addr));
};
const getTokenInfo = () => this.lppdacs.getDac(delegateId)
.then(({ token }) => getTokenInformation(this.web3, token));

return this.liquidPledging.getPledgeAdmin(delegateId)
.then(delegate => Promise.all([ delegate, findDAC(delegate), getTokenInfo(delegate) ]))
Expand Down
4 changes: 2 additions & 2 deletions src/blockchain/FailedTxMonitor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hexToNumber } from 'web3-utils';
import { LiquidPledgingAbi } from 'liquidpledging/build/LiquidPledging.sol';
import { LPVaultAbi } from 'liquidpledging/build/LPVault.sol';
import { LiquidPledgingAbi } from 'giveth-liquidpledging/build/LiquidPledging.sol';
import { LPVaultAbi } from 'giveth-liquidpledging/build/LPVault.sol';
import { LPPCappedMilestonesAbi } from 'lpp-capped-milestone/build/LPPCappedMilestones.sol'
import EventEmitter from 'events';
import logger from 'winston';
Expand Down
37 changes: 30 additions & 7 deletions src/blockchain/LiquidPledgingMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import CappedMilestones from './CappedMilestones';
import Tokens from './Tokens';
import createModel from '../models/blockchain.model';
import EventQueue from './EventQueue';
import { LiquidPledgingState } from "liquidpledging";
import { LiquidPledgingState } from "giveth-liquidpledging";


// Storing this in the db ensures that we don't miss any events on a restart
Expand All @@ -15,17 +15,18 @@ const defaultConfig = {
};

export default class {
constructor(app, web3, liquidPledging, cappedMilestones, txMonitor, opts) {
constructor(app, web3, liquidPledging, cappedMilestones, lppdacs, txMonitor, opts) {
this.app = app;
this.web3 = web3;
this.txMonitor = txMonitor;
this.cappedMilestonesContract = cappedMilestones;
this.liquidPledging = liquidPledging;
this.lppdacs = lppdacs;

const eventQueue = new EventQueue();

this.payments = new Payments(app, this.liquidPledging.$vault);
this.admins = new Admins(app, this.liquidPledging, eventQueue);
this.admins = new Admins(app, this.liquidPledging, lppdacs, eventQueue);
this.pledges = new Pledges(app, this.liquidPledging, eventQueue);
this.cappedMilestones = new CappedMilestones(app, this.web3);
this.tokens = new Tokens(app, this.web3);
Expand All @@ -48,7 +49,8 @@ export default class {
this.subscribeLP();
this.subscribeCappedMilestones();
this.subscribeVault();
this.subscribeTokens();
this.subscribeCampaignTokens();
this.subscribeDacTokens();
});

this.txMonitor.on(this.txMonitor.LP_EVENT, this.handleEvent.bind(this));
Expand Down Expand Up @@ -107,9 +109,9 @@ export default class {
}

/**
* subscribe to GenerateTokens event for any liquidPledgine plugins
* subscribe to GenerateTokens event for any liquidPledging lpp-campaign plugins
*/
subscribeTokens() {
subscribeCampaignTokens() {
// start a listener for all GenerateToken events associated with this liquidPledging contract
this.web3.eth.subscribe('logs', {
fromBlock: this.web3.utils.toHex(this.config.lastBlock + 1) || this.web3.utils.toHex(1), // convert to hex due to web3 bug https://github.com/ethereum/web3.js/issues/1097
Expand All @@ -119,7 +121,7 @@ export default class {
],
}, () => {
}) // TODO fix web3 bug so we don't have to pass a cb
.on('data', this.tokens.tokensGenerated.bind(this.tokens))
.on('data', this.tokens.campaignTokensGenerated.bind(this.tokens))
.on('changed', (event) => {
// I think this is emitted when a chain reorg happens and the tx has been removed
logger.info('GenerateTokens changed: ', event);
Expand All @@ -128,6 +130,21 @@ export default class {
.on('error', err => logger.error('SUBSCRIPTION ERROR: ', err));
}

/**
* subscribe to GenerateTokens event for lpp-dacs plugin
*/
subscribeDacTokens() {
// starts a listener on the vault contract
const fromBlock = this.config.lastBlock + 1 || 1;
this.lppdacs.$contract.events.allEvents({ fromBlock })
.on('data', this.handleEvent.bind(this))
.on('changed', (event) => {
// I think this is emitted when a chain reorg happens and the tx has been removed
logger.info('lppdacs changed: ', event);
})
.on('error', err => logger.error('SUBSCRIPTION ERROR: ', err));
}

/**
* get config from database
*
Expand Down Expand Up @@ -232,6 +249,12 @@ export default class {
case 'PaymentCollected':
this.cappedMilestones.paymentCollected(event);
break;
case 'GenerateTokens':
this.tokens.dacTokensGenerated(event);
break;
case 'DestroyTokens':
this.tokens.dacTokensDestroyed(event);
break;
default:
logger.error('Unknown event: ', event);
}
Expand Down
76 changes: 50 additions & 26 deletions src/blockchain/Tokens.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Contract from 'web3-eth-contract';
import { toBN } from 'web3-utils';
import { LPPDac } from 'lpp-dac';
import { LPPCampaign } from 'lpp-campaign';

import { getTokenInformation } from './helpers';
import getNetwork from "./getNetwork";

const GenerateTokenEvent = {
anonymous: false,
Expand All @@ -28,38 +29,61 @@ class Tokens {
this.tokens = this.app.service('tokens');
}

tokensGenerated(event) {
campaignTokensGenerated(event) {
const decodedEvent = decodeEventABI(event);
console.log('handling GenerateTokens Event: ', decodedEvent); // eslint-disable-line no-console
console.log('handling campaign GenerateTokens Event: ', decodedEvent); // eslint-disable-line no-console

// This is a hacky solution for now, but should define an interface for these token plugins
// TODO define token plugin interface or fetch token address from dacs/campains service
new LPPDac(this.web3, decodedEvent.address)
new LPPCampaign(this.web3, decodedEvent.address)
.token()
.then(tokenAddress =>
this.tokens.find({
query: { tokenAddress, userAddress: decodedEvent.returnValues.addr },
paginate: false,
})
.then((data) => {
if (data.length === 0) {
return getTokenInformation(this.web3, tokenAddress)
.then(tokenInfo => this.tokens.create({
tokenAddress,
tokenName: tokenInfo.name,
tokenSymbol: tokenInfo.symbol,
balance: decodedEvent.returnValues.amount,
userAddress: decodedEvent.returnValues.addr,
}));
}
const t = data[0];
.then(token => this.updateTokens(token, decodedEvent.returnValues.addr, decodedEvent.returnValues.amount))
.catch(console.error); // eslint-disable-line no-console
}

dacTokensGenerated(event) {
console.log('handling dac GenerateTokens Event: ', event); // eslint-disable-line no-console

getNetwork()
.then(network => new LPPDacs(this.web3, network.dacsAddress)
.getDac(event.idDelegate))
.then(({ token }) => this.updateTokens(token, event.returnValues.addr, event.returnValues.amount))
.catch(console.error); // eslint-disable-line no-console
}

const balance = toBN(t.balance).add(toBN(decodedEvent.returnValues.amount)).toString();
dacTokensDestroyed(event) {
console.log('handling dac GenerateTokens Event: ', event); // eslint-disable-line no-console

return this.tokens.patch(t._id, { balance });
}))
getNetwork()
.then(network => new LPPDacs(this.web3, network.dacsAddress)
.getDac(event.idDelegate))
.then(({ token }) => this.updateTokens(token, event.returnValues.addr, event.returnValues.amount, false))
.catch(console.error); // eslint-disable-line no-console
}

updateTokens(tokenAddress, addr, amount, generated = true) {
return this.tokens.find({
query: { tokenAddress, userAddress: addr },
paginate: false,
})
.then((data) => {
if (data.length === 0) {
return getTokenInformation(this.web3, tokenAddress)
.then(tokenInfo => this.tokens.create({
tokenAddress,
tokenName: tokenInfo.name,
tokenSymbol: tokenInfo.symbol,
balance: amount,
userAddress: addr,
}));
}
const t = data[ 0 ];

const balance = (generated) ?
toBN(t.balance).add(toBN(amount)).toString() :
toBN(t.balance).sub(toBN(amount)).toString();

return this.tokens.patch(t._id, { balance });
});
}
}

export default Tokens;
14 changes: 9 additions & 5 deletions src/blockchain/getNetwork.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
import { LiquidPledging, LPVault } from 'liquidpledging';
import { LPPCappedMilestones } from "lpp-capped-milestone";

const networks = {
main: {
vaultAddress: '0x0',
liquidPledgingAddress: '0x0',
vaultAddress: '0x91a973BEE89225c6c186419B5Bab1944Fc5736C7',
liquidPledgingAddress: '0x3f45D2D5FeB6b4b000d2d3B84442eeDDF54A735a',
cappedMilestoneAddress: '0x0',
dacsAddress: '0x0',
},
morden: {
vaultAddress: '0x0',
liquidPledgingAddress: '0x0',
cappedMilestoneAddress: '0x0',
dacsAddress: '0x0',
},
ropsten: {
vaultAddress: '0x547626030c9e9df93657a38075339f429e7a998b',
liquidPledgingAddress: '0x9a3e76a27e18994ebdb1ab813e87f4315d8faa5e',
cappedMilestoneAddress: '0x0',
dacsAddress: '0x0',
},
rinkeby: {
vaultAddress: '0xBf0bA4c72daab5BFeF6B9C496db91e4614a57131',
liquidPledgingAddress: '0x1B8F84E443668C81FeE5BEc266bc098e3c7fBC00',
cappedMilestoneAddress: '0x137802c8F48294331654108dd64d8acD48b3321d',
dacsAddress: '0x0',
},
kovan: {
vaultAddress: '0x0',
liquidPledgingAddress: '0x0',
cappedMilestoneAddress: '0x0',
dacsAddress: '0x0',
},
giveth: {
vaultAddress: '0x98bE0A726C9937Ba5E0227E84E1ccCaceFee88b4',
liquidPledgingAddress: '0xc2E1c6cf5D18247d63618dABf58E14F058D02c7C',
cappedMilestoneAddress: '0x0',
dacsAddress: '0x0',
},
default: {
vaultAddress: '0xe78A0F7E598Cc8b0Bb87894B0F60dD2a88d6a8Ab',
liquidPledgingAddress: '0x5b1869D9A4C187F2EAa108f3062412ecf0526b24',
cappedMilestoneAddress: '0xD833215cBcc3f914bD1C9ece3EE7BF8B14f841bb',
dacsAddress: '0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B',
},
};

Expand Down
6 changes: 4 additions & 2 deletions src/blockchain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import logger from 'winston';
import LiquidPledgingMonitor from './LiquidPledgingMonitor';
import FailedTxMonitor from './FailedTxMonitor';
import getNetwork from "./getNetwork";
import { LiquidPledging, LPVault } from "liquidpledging";
import { LiquidPledging, LPVault } from "giveth-liquidpledging";
import { LPPCappedMilestones } from "lpp-capped-milestone";
import { LPPDacs } from "lpp-dacs";

const ONE_MINUTE = 60 * 1000;

Expand Down Expand Up @@ -40,8 +41,9 @@ export default function () {
const liquidPledging = new LiquidPledging(web3, network.liquidPledgingAddress);
liquidPledging.$vault = new LPVault(web3, network.vaultAddress);
const cappedMilestones = new LPPCappedMilestones(web3, network.cappedMilestoneAddress);
const lppdacs = new LPPDacs(web3, network.dacsAddress);

lpMonitor = new LiquidPledgingMonitor(app, web3, liquidPledging, cappedMilestones, txMonitor, opts);
lpMonitor = new LiquidPledgingMonitor(app, web3, liquidPledging, lppdacs, cappedMilestones, txMonitor, opts);
lpMonitor.start();
})

Expand Down

0 comments on commit 2f5c359

Please sign in to comment.