-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
534 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const Delegator = require('../delegator/delegator.model') | ||
const Share = require('../share/share.model') | ||
const mongoose = require('../../config/mongoose') | ||
const { MathBN } = require('./utils') | ||
|
||
// Fetch the round-id delegator total stake from the last share and make a sub with the current total stake | ||
const getDelegatorCurrentRewardTokens = async ( | ||
currentRoundId, | ||
delegatorAddress, | ||
currentDelegatorTotalStake | ||
) => { | ||
if (!currentRoundId) { | ||
console.error( | ||
'[Delegator utils] - no round id was provided on getDelegateCurrentRewardTokens()' | ||
) | ||
throw new Error( | ||
'[Delegator utils] - no round id was provided on getDelegateCurrentRewardTokens()' | ||
) | ||
} | ||
if (!delegatorAddress) { | ||
console.error( | ||
'[Delegator utils] - no delegatorAddress was provided on getDelegateCurrentRewardTokens()' | ||
) | ||
throw new Error( | ||
'[Delegator utils] - no delegatorAddress was provided on getDelegateCurrentRewardTokens()' | ||
) | ||
} | ||
if (!currentDelegatorTotalStake) { | ||
console.error( | ||
'[Delegator utils] - no currentDelegatorTotalStake was provided on getDelegateCurrentRewardTokens()' | ||
) | ||
throw new Error( | ||
'[Delegator utils] - no currentDelegatorTotalStake was provided on getDelegateCurrentRewardTokens()' | ||
) | ||
} | ||
const lastRoundId = currentRoundId - 1 | ||
const lastDelegatorShareId = `${delegatorAddress}-${lastRoundId}` | ||
const lastDelegatorShare = await Share.findById(lastDelegatorShareId) | ||
// The first time we register the delegator on the db, he won't have any shares, we save 0 | ||
if (!lastDelegatorShare) { | ||
console.error('[Delegator utils] - last share not found') | ||
return 0 | ||
} | ||
const newShare = MathBN.sub(currentDelegatorTotalStake, lastDelegatorShare.totalStakeOnRound) | ||
console.log(`[Delegator utils] - returning new share: ${newShare}`) | ||
return newShare | ||
} | ||
|
||
// Receives all the delegates that are stored locally and the delegates from the graph | ||
// If there are delegates who are not stored locally, save them on the db | ||
const checkAndUpdateMissingLocalDelegators = async fetchedDelegators => { | ||
console.error(`[Delegator utils] - checkAndUpdateMissingLocalDelegators Start`) | ||
if (!fetchedDelegators || fetchedDelegators.length === 0) { | ||
console.error( | ||
'[Delegator utils] - there were no remote delegators received on checkAndUpdateMissingLocalDelegators()' | ||
) | ||
return | ||
} | ||
const updateDelegatorPromises = [] | ||
for (let remoteDelegatorIterator of fetchedDelegators) { | ||
const remoteId = remoteDelegatorIterator.address | ||
const delegateAddress = remoteDelegatorIterator.delegateAddress | ||
? remoteDelegatorIterator.delegateAddress | ||
: remoteDelegatorIterator.delegate | ||
if (!remoteId) { | ||
console.error(`[Delegator utils] - delegator ${remoteDelegatorIterator} has not id, skipped`) | ||
continue | ||
} | ||
if (!delegateAddress) { | ||
console.error(`[Delegator utils] - delegator ${remoteId} has not delegate address, skipped`) | ||
continue | ||
} | ||
const localFound = await Delegator.findById(remoteId) | ||
if (!localFound) { | ||
console.log(`[Delegator utils] - remote delegator ${remoteId} not found locally, adding it`) | ||
const { startRound, totalStake } = remoteDelegatorIterator | ||
const newDelegator = new Delegator({ | ||
_id: remoteId, | ||
delegate: delegateAddress, | ||
startRound, | ||
totalStake | ||
}) | ||
updateDelegatorPromises.push(newDelegator.save()) | ||
} else { | ||
// If found, just update it | ||
const updatedDelegator = new Delegator({ | ||
_id: remoteId, | ||
delegate: delegateAddress, | ||
startRound: remoteDelegatorIterator.startRound, | ||
totalStake: remoteDelegatorIterator.totalStake, | ||
shares: localFound.shares | ||
}) | ||
updateDelegatorPromises.push(updatedDelegator.save()) | ||
} | ||
} | ||
await Promise.all(updateDelegatorPromises) | ||
console.error(`[Delegator utils] - checkAndUpdateMissingLocalDelegators Finished`) | ||
} | ||
|
||
const delegatorUtils = { | ||
checkAndUpdateMissingLocalDelegators, | ||
getDelegatorCurrentRewardTokens | ||
} | ||
|
||
module.exports = delegatorUtils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
const mongoose = require('../../config/mongoose') | ||
const delegatorUtils = require('./delegatorUtils') | ||
const Share = require('../share/share.model') | ||
const Round = require('../round/round.model') | ||
const Delegator = require('../delegator/delegator.model') | ||
const Subscriber = require('../subscriber/subscriber.model') | ||
|
||
const updateDelegatorSharesOfRound = async (round, delegator) => { | ||
console.log('[Update Delegators Shares] - Starts updating delegator shares') | ||
if (!round) { | ||
console.error('[Update Delegators Shares] - No round shares were provided') | ||
throw new Error('[Update Delegators Shares] - No round shares were provided') | ||
} | ||
if (!delegator) { | ||
console.error('[Update Delegators Shares] - No delegator was provided') | ||
throw new Error('[Update Delegators Shares] - No delegator was provided') | ||
} | ||
const delegatorAddress = delegator.address | ||
if (!delegatorAddress) { | ||
console.error(`[Update Delegators Shares] - delegator ${delegator} has not address`) | ||
throw new Error(`[Update Delegators Shares] - delegator ${delegator} has not address`) | ||
} | ||
// Checks that the delegator exists before continue | ||
delegator = await Delegator.findById(delegatorAddress) | ||
if (!delegator) { | ||
console.error( | ||
`[Update Delegators Shares] - Delegator ${delegatorAddress} not found, did you called checkAndUpdateMissingLocalDelegators() before?` | ||
) | ||
throw new Error( | ||
'[Update Delegators Shares] - Delegator ${delegatorAddress} not found, did you called checkAndUpdateMissingLocalDelegators() before?' | ||
) | ||
} | ||
// Checks that the round exists before continue | ||
const { roundId } = round | ||
round = await Round.findById(roundId) | ||
if (!round) { | ||
console.error('[Update Delegators Shares] - The round provided does not exists') | ||
throw new Error('[Update Delegators Shares] - The round provided does not exists') | ||
} | ||
|
||
// Creates the share object | ||
const { totalStake } = delegator | ||
const shareId = `${delegatorAddress}-${roundId}` | ||
const rewardTokens = await delegatorUtils.getDelegatorCurrentRewardTokens( | ||
roundId, | ||
delegatorAddress, | ||
totalStake | ||
) | ||
|
||
let newSavedShared = new Share({ | ||
_id: shareId, | ||
rewardTokens, | ||
totalStakeOnRound: totalStake, | ||
delegator: delegatorAddress, | ||
delegate: delegatorAddress, | ||
round: roundId | ||
}) | ||
|
||
try { | ||
// Saves the share | ||
console.log(`[Update Delegators Shares] - Saving new share for delegator ${delegatorAddress}`) | ||
newSavedShared = await newSavedShared.save() | ||
// Updates the pool with the share | ||
round.shares.push(newSavedShared) | ||
console.log('[Update Delegators Shares] - Updating round with share') | ||
round = await round.save() | ||
// Finally updates the delegator with the new share | ||
delegator.shares.push(newSavedShared) | ||
delegator = await delegator.save() | ||
} catch (err) { | ||
console.error( | ||
`[Update Delegators Shares] - Error Updating share on delegator ${delegatorAddress}` | ||
) | ||
console.error(err) | ||
throw err | ||
} | ||
} | ||
|
||
// Executed on round changed, only executes for the delegators which are subscribed | ||
const updateDelegatorsShares = async newRound => { | ||
console.log('[Update Delegator shares] - Start') | ||
if (!newRound) { | ||
throw new Error('[Update Delegator shares] - No round was provided') | ||
} | ||
|
||
// Fetch all the delegators that are subscribed | ||
const delegatorsAndSubscribersList = await Subscriber.getDelegatorSubscribers() | ||
if (!delegatorsAndSubscribersList || delegatorsAndSubscribersList.length === 0) { | ||
console.log('[Update Delegator shares] - No delegators subscribers found') | ||
return | ||
} | ||
const delegators = [] | ||
delegatorsAndSubscribersList.forEach(element => { | ||
if (element.delegator) { | ||
delegators.push(element.delegator) | ||
} | ||
}) | ||
if (!delegators || delegators.length === 0) { | ||
console.log('[Update Delegator shares] - No delegators subscribers found') | ||
return | ||
} | ||
|
||
try { | ||
// Then checks if all the fetched delegators exists locally, otherwise, add the ones that are missing | ||
await delegatorUtils.checkAndUpdateMissingLocalDelegators(delegators) | ||
|
||
// Then updates the delegators shares on the current round | ||
for (let delegatorIterator of delegators) { | ||
await service.updateDelegatorSharesOfRound(newRound, delegatorIterator) | ||
} | ||
|
||
console.log('[Update Delegators Share] - Finish') | ||
} catch (err) { | ||
console.error(`[Update Delegators Share] - Error when updating delegators shares: ${err}`) | ||
throw new Error(`[Update Delegators Share] - Error when updating delegators shares: ${err}`) | ||
} | ||
} | ||
|
||
const service = { | ||
updateDelegatorsShares, | ||
updateDelegatorSharesOfRound | ||
} | ||
|
||
module.exports = service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.