Created
August 6, 2020 23:37
-
-
Save crazyrabbitLTC/0bbe58786c53786da02c5f227119d896 to your computer and use it in GitHub Desktop.
Subgraph Mapping function
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
export function handleDelegateChanged(event: DelegateChanged): void { | |
let delegator = event.params.delegator | |
let fromDelegate = event.params.fromDelegate | |
let toDelegate = event.params.toDelegate | |
let txHash = event.transaction.hash.toHex() | |
let timestamp = event.block.timestamp | |
let blockNumber = event.block.number | |
// Load Token Contract to get official votes (rather than computing from events) | |
let token = COMP.bind(event.address) | |
let delegationAmount = token.getCurrentVotes(delegator) | |
//Create VoteDelegationID | |
let voteDelegationId = delegator.toHex().concat(toDelegate.toHex()) | |
//Create and save Delegation Event | |
let voteDelegationEvent = new VoteDelegationEvent(txHash) | |
voteDelegationEvent.timestamp = timestamp | |
voteDelegationEvent.block = blockNumber | |
voteDelegationEvent.amount = delegationAmount | |
voteDelegationEvent.voteDelegation = voteDelegationId | |
voteDelegationEvent.fromDelegate = fromDelegate | |
voteDelegationEvent.toDelegate = toDelegate | |
//Get Voters voteDelegation history and save update | |
let voteDelegation = getVoteDelegation(voteDelegationId) | |
voteDelegation.delegator = delegator.toHex() | |
voteDelegation.toDelegate = toDelegate.toHex() | |
voteDelegation.timestamp = timestamp | |
voteDelegation.block = blockNumber | |
voteDelegation.amount = delegationAmount | |
//Add this delegation event to this relationships history | |
let historyOfVotersDelegations = voteDelegation.voteDelegationHistory | |
historyOfVotersDelegations.push(voteDelegationEvent.id) | |
voteDelegation.voteDelegationHistory = historyOfVotersDelegations | |
//Get Voter that has changed their delegation | |
let voterDelegator = getVoter(delegator.toHex()) | |
//Get new delegatee | |
let voterDelegatee = getVoter(toDelegate.toHex()) | |
//get old delegatee | |
let voterFormerDelegatee = getVoter(fromDelegate.toHex()) | |
//Is the voter delegating to themselves? | |
let selfDelegating = false | |
if (delegator == toDelegate) { | |
selfDelegating = true | |
} | |
//Update if voter is delegating or not | |
voterDelegator.isVoterDelegating = true | |
//Update if the voter is delegating to someone else | |
voterDelegator.isDelegatingToAnother = selfDelegating | |
//update delegatee | |
voterDelegator.delegatingTo = voterDelegatee.id | |
//Update history of Delegating to this person | |
let voterDelegationHistory = voterDelegator.voteDelegatorHistory | |
voterDelegationHistory.push(voteDelegation.id) | |
voterDelegator.voteDelegatorHistory = voterDelegationHistory | |
//Update the delegatee | |
voterDelegatee.isDelegatedTo = true | |
//Add voterDelegator to the list of voters delegating to the delegatee | |
let voterDelegateeFrom = voterDelegatee.delegatedFrom | |
voterDelegateeFrom.push(voterDelegator.id) | |
voterDelegatee.delegatedFrom = voterDelegateeFrom | |
//Add ths VoteDelegation object to the history of being delegateed too | |
let voteDelegateeHistory = voterDelegatee.voteDelegeeHistory | |
voteDelegateeHistory.push(voterDelegator.id) | |
voterDelegatee.voteDelegeeHistory = voteDelegateeHistory | |
//TODO: Look for more perforant way to remove former delegatee as this currently break in sync | |
// //update former delegatee to remove delegations | |
let formerDelegateelistofDelegators = voterFormerDelegatee.delegatedFrom | |
if (formerDelegateelistofDelegators.length == 0) { | |
log.warning("Possible Mismatch in how delegators are counted and applied", [delegator.toString(), fromDelegate.toString(), toDelegate.toString()]) | |
} | |
//Remove this voter from the list of delegators, only if they are delegating to someone else | |
if (fromDelegate != toDelegate) { | |
let delegator2 = event.params.delegator | |
let newListofDelegators = formerDelegateelistofDelegators.filter(function (voterId: string, index: i32, array: string[]) { | |
return voterId != delegator2.toHex() | |
}) | |
voterFormerDelegatee.delegatedFrom = newListofDelegators | |
//If no one is delegating to them, mark them as not being delegated to | |
if (newListofDelegators.length < 1) { | |
voterFormerDelegatee.isDelegatedTo = false | |
} | |
} | |
//Get History and add Delegation Event and overwrite current delegtation | |
let history = getHistory() | |
let historyOfDelEvents = history.delegationHistory | |
historyOfDelEvents.push(voteDelegationEvent.id) | |
history.delegationHistory = historyOfDelEvents | |
//Update the current delegations | |
//First find the previos delegation if it existed, and delete it. | |
let currentHistoryDelegations = history.delegations | |
let previousDelegationId = delegator.toHex().concat(fromDelegate.toHex()) | |
let previousVoterDelegation = getVoteDelegation(previousDelegationId) | |
if (currentHistoryDelegations.length = 0) { | |
log.warning("Possible mismatch of how we add delegations on history", [delegator.toString(), fromDelegate.toString(), toDelegate.toString()]) | |
} | |
//TODO: Look for more performant method of removing non-current delegations | |
// //Remove this delgation only if the new delegate is different from the old one. | |
if (fromDelegate != toDelegate) { | |
let filteredCurrentDelegations = currentHistoryDelegations.filter(function (delegationId: string, index: i32, array: string[]) { | |
//For some reason previousVoterDelegation isn't visible inside this block without the following | |
let previousDelegationId = event.params.delegator.toHex().concat(event.params.fromDelegate.toHex()) | |
let previousVoterDelegation = getVoteDelegation(previousDelegationId) | |
return delegationId != previousVoterDelegation.id | |
}) | |
//update the new list of current delegations on the history | |
history.delegations = filteredCurrentDelegations | |
} | |
//Save the history | |
history.save() | |
//Save Vote Delegation | |
voteDelegation.save() | |
//Save Former Delegatee | |
voterFormerDelegatee.save() | |
//Save Delegatee | |
voterDelegatee.save() | |
//save Delegator | |
voterDelegator.save() | |
//Save vote delegation event | |
voteDelegationEvent.save() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment