Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from Giveth/master
Browse files Browse the repository at this point in the history
syncing with main giveth repo
  • Loading branch information
bowensanders authored May 3, 2019
2 parents fe5cb35 + cce29c4 commit 05fe026
Show file tree
Hide file tree
Showing 9 changed files with 20,380 additions and 18,845 deletions.
4 changes: 2 additions & 2 deletions config/beta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"pollTime": 300000,
"homeStartBlock" : 5875760,
"foreignStartBlock": 2548160,
"homeNodeURL": "https://mew.giveth.io",
"foreignNodeURL": "https://rinkeby.giveth.io",
"homeNodeURL": "wss://mainnet.eth.aragon.network/ws",
"foreignNodeURL": "wss://rinkeby.giveth.io/ws",
"homeContractAddress" : "0x30f938fED5dE6e06a9A7Cd2Ac3517131C317B1E7",
"foreignContractAddress" : "0xfF9CD5140e79377feB23f6DFaF1f8b558C0FE621"
}
29 changes: 18 additions & 11 deletions feathers-src/lib/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const HomeBridgeABI = HomeBridgeContract.compilerOutput.abi;
const ForeignBridgeContract = require('giveth-bridge/build/ForeignGivethBridge.json');
const ForeignBridgeABI = ForeignBridgeContract.compilerOutput.abi;

module.exports = async () => {
const populate = async () => {
const homeNodeURL = app.get('homeNodeURL');
const foreignNodeURL = app.get('foreignNodeURL');

Expand Down Expand Up @@ -63,12 +63,12 @@ module.exports = async () => {
}
const homeRange = {
fromBlock: range.home,
toBlock: currentHomeBlock,
toBlock: Math.min(currentHomeBlock, range.home + 500000),
};

const foreignRange = {
fromBlock: range.foreign,
toBlock: currentForeignBlock,
toBlock: Math.min(currentForeignBlock, range.foreign + 500000),
};

console.log('Getting past events...');
Expand Down Expand Up @@ -102,7 +102,8 @@ module.exports = async () => {
}
console.log('Success!');

const securityGuardLastCheckin = await homeContract.methods.securityGuardLastCheckin().call() * 1000;
const securityGuardLastCheckin =
(await homeContract.methods.securityGuardLastCheckin().call()) * 1000;
app.set('securityGuardLastCheckin', securityGuardLastCheckin);

console.log('Blockchain interaction finished, creating records...');
Expand All @@ -126,7 +127,7 @@ module.exports = async () => {
matched: false,
matches: [],
hasDuplicates: false,
_id: donation.transactionHash,
_id: donation.id,
});
});

Expand All @@ -137,7 +138,7 @@ module.exports = async () => {
matched: false,
matches: [],
hasDuplicates: false,
_id: donation.transactionHash,
_id: donation.id,
});
});

Expand All @@ -147,7 +148,7 @@ module.exports = async () => {
matched: false,
matches: [],
hasDuplicates: false,
_id: deposit.transactionHash,
_id: deposit.id,
});
});

Expand All @@ -157,7 +158,7 @@ module.exports = async () => {
matched: false,
matches: [],
hasDuplicates: false,
_id: withdrawal.transactionHash,
_id: withdrawal.id,
});
});

Expand All @@ -175,7 +176,7 @@ module.exports = async () => {
earliestPayTime: Number(p.earliestPayTime) * 1000,
securityGuardDelay: Number(p.securityGuardDelay),
hasDuplicates: false,
_id: payment.transactionHash,
_id: payment.id,
}),
);
});
Expand Down Expand Up @@ -210,8 +211,8 @@ module.exports = async () => {
app.set('depositor', depositor);

const patched = await app.service('range').patch(1, {
home: currentHomeBlock + 1,
foreign: currentForeignBlock + 1,
home: homeRange.toBlock + 1,
foreign: foreignRange.toBlock + 1,
});

// update payment status
Expand All @@ -236,5 +237,11 @@ module.exports = async () => {
.then(promises => Promise.all(promises));

console.log('Success!');

if (currentForeignBlock > foreignRange.toBlock || currentHomeBlock > homeRange.toBlock) {
return await populate();
}
return true;
};

module.exports = populate;
79 changes: 39 additions & 40 deletions feathers-src/services/payments/payments.hooks.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@


module.exports = {
before: {
all: [],
find: [],
get: [],
create: [
async (context) => {

const payment = context.data;
const reference = payment.event.returnValues.reference;
async context => {
const payment = context.data;
const reference = payment.event.returnValues.reference;

// find referenced withdrawal
// TODO: this could probably be done with get(reference), since the hashes are used as the ids, but it might need a try/catch
const withdrawals = await context.app.service('withdrawals').find({
query: {
'event.transactionHash': reference,
}
});
// find referenced withdrawal
// TODO: this could probably be done with get(reference), since the hashes are used as the ids, but it might need a try/catch
const withdrawals = await context.app.service('withdrawals').find({
query: {
'event.transactionHash': reference,
'event.returnValues.recipient': payment.event.returnValues.recipient,
'event.returnValues.token': payment.event.returnValues.token,
'event.returnValues.amount': payment.event.returnValues.amount,
},
});

// if no withdrawals are found, bail
if (withdrawals.total === 0) return context;
// if no withdrawals are found, bail
if (withdrawals.total === 0) return context;

const withdrawal = withdrawals.data[0];
const withdrawal_id = withdrawal._id;
const withdrawal = withdrawals.data[0];
const withdrawal_id = withdrawal._id;

// if the withdrawal has already been matched, it now has duplicate references
const hasDuplicates = withdrawal.matched;
// if the withdrawal has already been matched, it now has duplicate references
const hasDuplicates = withdrawal.matched;

context.data.matched = true;
context.data.hasDuplicates = hasDuplicates;
context.data.matched = true;
context.data.hasDuplicates = hasDuplicates;

// find the previously matched payments and add this one to the matches
// match.patch tells the withdrawal (in it's before patch hook) if the payment needs to be flagged as a duplicate, since the first payment added to the matches doesn't know if it will be a duplicate until another is added
const previousMatches = withdrawal.matches;
const matches = previousMatches.concat({
hash: payment.event.transactionHash,
patch: !hasDuplicates,
});
// find the previously matched payments and add this one to the matches
// match.patch tells the withdrawal (in it's before patch hook) if the payment needs to be flagged as a duplicate, since the first payment added to the matches doesn't know if it will be a duplicate until another is added
const previousMatches = withdrawal.matches;
const matches = previousMatches.concat({
hash: payment.event.transactionHash,
patch: !hasDuplicates,
});

await context.app.service('withdrawals').patch(withdrawal_id, {
matches,
matched: true,
hasDuplicates,
});
await context.app.service('withdrawals').patch(withdrawal_id, {
matches,
matched: true,
hasDuplicates,
});

return context;
}
return context;
},
],
update: [],
patch: [],
remove: []
remove: [],
},

after: {
Expand Down Expand Up @@ -87,11 +87,10 @@ module.exports = {
//
// return context;
// }

],
update: [],
patch: [],
remove: []
remove: [],
},

error: {
Expand All @@ -101,6 +100,6 @@ module.exports = {
create: [],
update: [],
patch: [],
remove: []
}
remove: [],
},
};
5 changes: 4 additions & 1 deletion feathers-src/services/withdrawals/withdrawals.hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module.exports = {
const payments = await context.app.service('payments').find({
query: {
'event.returnValues.reference': hash,
'event.returnValues.recipient': context.data.event.returnValues.recipient,
'event.returnValues.token': context.data.event.returnValues.token,
'event.returnValues.amount': context.data.event.returnValues.amount,
}
});

Expand Down Expand Up @@ -52,7 +55,7 @@ module.exports = {
// (there SHOULD only be one, and it SHOULD be the first, but this is a safeguard)
for (let i = 0; i < matches.length; i++){
if (matches[i].patch) {
paymentToPatch = matches[i].hash;
paymentToPatch = matches[i]._id;
index = i;
break;
}
Expand Down
Loading

0 comments on commit 05fe026

Please sign in to comment.