Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep7Singh committed Feb 10, 2023
1 parent a846105 commit 846ca88
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 51 deletions.
Binary file modified builds/moonbase-polkassembly-v2.tar.gz
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ type ConvictionVote @entity {
delegatedTo: String
isDelegated: Boolean
type: VoteType!
txnHash: String
}

type VotingDelegation @entity {
Expand Down
50 changes: 42 additions & 8 deletions src/mappings/ethereum/extrinsics/transact.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,59 @@
import { BatchContext, SubstrateBlock } from '@subsquid/substrate-processor'
import { BatchContext, SubstrateBlock, toHex } from '@subsquid/substrate-processor'
import { Store } from '@subsquid/typeorm-store'
import { CallItem } from '@subsquid/substrate-processor/lib/interfaces/dataSelection'
import { getTransaction } from '@subsquid/frontier'
import { EthereumCurrentTransactionStatusesStorage } from '../../../types/storage'
import { functions } from '../../../abi/convictionVoteAbi'
import { handleConvictionVotesFromPrecompile } from '../../referendumV2/extrinsics/convictionVoting'
import { handlePrecompileDelegate } from '../../referendumV2/extrinsics/delegate'
import { handlePrecompileUndelegate } from '../../referendumV2/extrinsics/undelegate'
import { handlePrecompiledRemoveVote } from '../../referendumV2/extrinsics/removeVote'
import { handlePrecompileRemoveOtherVote } from '../../referendumV2/extrinsics/removeOtherVote'
import { getOriginAccountId } from '../../../common/tools'

// export async function handlePrecompileTransactionStorage(ctx: BatchContext<Store, unknown>, header: SubstrateBlock, txnHash: string) {
// const storageData = new EthereumCurrentTransactionStatusesStorage(ctx, header)
// console.log(`Storage data: ${JSON.stringify(storageData)}`)
// if (!storageData) return

// if (storageData.isV40){
// const transactions = await storageData.asV40.get()
// if (!transactions) return
// console.log(`Transactions 1st version: ${JSON.stringify(transactions)}`)
// for (const transaction of transactions){
// const { transactionHash, logs, logsBloom } = transaction
// if(toHex(transactionHash) === txnHash){
// return
// }

// }
// }
// else if (storageData.isV900){
// const transactions = await storageData.asV900.get()
// console.log(`Transactions 2nd version: ${JSON.stringify(transactions)}`)
// if (!transactions) return
// for (const transaction of transactions){
// const { transactionHash, logs, logsBloom } = transaction
// if(toHex(transactionHash) === txnHash){
// return
// }

// }

// }
// }


export async function handlePrecompileTransaction(ctx: BatchContext<Store, unknown>,
item: CallItem<'Ethereum.transact', { call: { args: true; origin: true } }>,
header: SubstrateBlock) {
const tx = getTransaction(ctx, item.call)
const originAccountId = tx.from
const txnHash = tx.hash
let flag = false;
try {
const decoded = functions.voteYes.decode(tx.input)
if(decoded){
await handleConvictionVotesFromPrecompile(ctx, item.call, header, decoded, true, originAccountId)
await handleConvictionVotesFromPrecompile(ctx, item.call, header, decoded, true, originAccountId, txnHash)
flag = true
}
}
Expand All @@ -31,7 +65,7 @@ export async function handlePrecompileTransaction(ctx: BatchContext<Store, unkno
const decoded = functions.voteNo.decode(tx.input)
if(decoded){
flag = true
await handleConvictionVotesFromPrecompile(ctx, item.call, header, decoded, true, originAccountId)
await handleConvictionVotesFromPrecompile(ctx, item.call, header, decoded, true, originAccountId, txnHash)
flag = true
}
}
Expand All @@ -43,7 +77,7 @@ export async function handlePrecompileTransaction(ctx: BatchContext<Store, unkno
try{
const decoded = functions.delegate.decode(tx.input)
if(decoded){
await handlePrecompileDelegate(ctx, item.call, header, decoded, originAccountId)
await handlePrecompileDelegate(ctx, item.call, header, decoded, originAccountId, txnHash)
flag = true

}
Expand All @@ -57,7 +91,7 @@ export async function handlePrecompileTransaction(ctx: BatchContext<Store, unkno
const decoded = functions.undelegate.decode(tx.input)
if(decoded){
flag = true
await handlePrecompileUndelegate(ctx, item.call, header, decoded, originAccountId)
await handlePrecompileUndelegate(ctx, item.call, header, decoded, originAccountId, txnHash)
}
}
catch (e){
Expand All @@ -68,7 +102,7 @@ export async function handlePrecompileTransaction(ctx: BatchContext<Store, unkno
try{
const decoded = functions.removeVote.decode(tx.input)
if(decoded){
await handlePrecompiledRemoveVote(ctx, item.call, header, decoded, originAccountId)
await handlePrecompiledRemoveVote(ctx, item.call, header, decoded, originAccountId, txnHash)
flag = true
}
}
Expand All @@ -80,7 +114,7 @@ export async function handlePrecompileTransaction(ctx: BatchContext<Store, unkno
try{
const decoded = functions.removeOtherVote.decode(tx.input)
if(decoded){
await handlePrecompileRemoveOtherVote(ctx, item.call, header, decoded, originAccountId)
await handlePrecompileRemoveOtherVote(ctx, item.call, header, decoded, originAccountId, txnHash)
flag = true
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/mappings/referendumV2/extrinsics/convictionVoting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export async function handleConvictionVote(ctx: BatchContext<Store, unknown>,

}

export async function handleConvictionVotesFromPrecompile(ctx: BatchContext<Store, unknown>, itemCall: any, header: SubstrateBlock, data: any, aye: boolean = true, originAccountId: string) {
export async function handleConvictionVotesFromPrecompile(ctx: BatchContext<Store, unknown>, itemCall: any, header: SubstrateBlock, data: any, aye: boolean = true, originAccountId: string, txnHash: string) {
const [ index, amount, conviction ] = data

const proposal = await ctx.store.get(Proposal, { where: { index, type: ProposalType.ReferendumV2 } })
Expand Down Expand Up @@ -156,10 +156,11 @@ export async function handleConvictionVotesFromPrecompile(ctx: BatchContext<Stor
value: amount
}),
isDelegated: false,
txnHash,
createdAt: new Date(header.timestamp),
type: VoteType.ReferendumV2,
})
)
await addDelegatedVotesReferendumV2(ctx, from, header.height, header.timestamp, proposal, nestedDelegations, proposal.trackNumber)
await addDelegatedVotesReferendumV2(ctx, from, header.height, header.timestamp, proposal, nestedDelegations, proposal.trackNumber, txnHash)

}
5 changes: 3 additions & 2 deletions src/mappings/referendumV2/extrinsics/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export async function handleDelegate(ctx: BatchContext<Store, unknown>,
await addOngoingReferendaDelegatedVotes(ctx, from, header, track)
}

export async function handlePrecompileDelegate(ctx: BatchContext<Store, unknown>, itemCall: any, header: SubstrateBlock, data: any, originAccountId: any): Promise<void> {
export async function handlePrecompileDelegate(ctx: BatchContext<Store, unknown>, itemCall: any, header: SubstrateBlock, data: any, originAccountId: any, txnHash: string): Promise<void> {

const [track, toWallet, lockPeriod, balance] = data

Expand Down Expand Up @@ -170,9 +170,10 @@ export async function handlePrecompileDelegate(ctx: BatchContext<Store, unknown>
createdAt: new Date(header.timestamp),
delegatedTo: toWallet,
isDelegated: true,
txnHash,
type: VoteType.ReferendumV2,
})
)
}
await addOngoingReferendaDelegatedVotes(ctx, from, header, track)
await addOngoingReferendaDelegatedVotes(ctx, from, header, track, txnHash)
}
20 changes: 12 additions & 8 deletions src/mappings/referendumV2/extrinsics/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function convictionToLockPeriod(convictionKind: string): number {
return convictionKind === 'None' ? 0 : Number(convictionKind[convictionKind.search(/\d/)])
}

export async function addDelegatedVotesReferendumV2(ctx: BatchContext<Store, unknown>, toWallet: string, block: number, blockTime: number, referendum: any, nestedDelegations: VotingDelegation[], track: number): Promise<void> {
export async function addDelegatedVotesReferendumV2(ctx: BatchContext<Store, unknown>, toWallet: string, block: number, blockTime: number, referendum: any, nestedDelegations: VotingDelegation[], track: number, txnHash?: string): Promise<void> {
//get top toWallet vote
const votes = await ctx.store.find(ConvictionVote, { where: { voter: toWallet, proposalIndex: referendum.index, removedAtBlock: IsNull(), type: VoteType.ReferendumV2 } })
if (votes.length > 1) {
Expand Down Expand Up @@ -47,6 +47,7 @@ export async function addDelegatedVotesReferendumV2(ctx: BatchContext<Store, unk
delegatedTo: delegation.to,
type: VoteType.ReferendumV2,
isDelegated: true,
txnHash
})
)
}
Expand All @@ -68,7 +69,7 @@ export async function getAllNestedDelegations(ctx: BatchContext<Store, unknown>,
}
}

export async function removeDelegatedVotesOngoingReferenda(ctx: BatchContext<Store, unknown>, wallet: string | undefined, block: number, blockTime: number, track: number): Promise<void> {
export async function removeDelegatedVotesOngoingReferenda(ctx: BatchContext<Store, unknown>, wallet: string | undefined, block: number, blockTime: number, track: number, txnHash?: string): Promise<void> {
//get any ongoing referenda in this track
const ongoingReferenda = await ctx.store.find(Proposal, { where: { endedAt: IsNull(), trackNumber: track, type: ProposalType.ReferendumV2 } })
let nestedDelegations = await getAllNestedDelegations(ctx, wallet, track)
Expand All @@ -77,11 +78,11 @@ export async function removeDelegatedVotesOngoingReferenda(ctx: BatchContext<Sto
if(ongoingReferendum.index === undefined || ongoingReferendum.index === null) {
continue
}
await removeDelegatedVotesReferendum(ctx, block, blockTime, ongoingReferendum.index, nestedDelegations)
await removeDelegatedVotesReferendum(ctx, block, blockTime, ongoingReferendum.index, nestedDelegations, txnHash)
}
}

export async function removeDelegatedVotesReferendum(ctx: BatchContext<Store, unknown>, block: number, blockTime: number, index: number, nestedDelegations: VotingDelegation[]): Promise<void> {
export async function removeDelegatedVotesReferendum(ctx: BatchContext<Store, unknown>, block: number, blockTime: number, index: number, nestedDelegations: VotingDelegation[], txnHash?: string): Promise<void> {
for (let i = 0; i < nestedDelegations.length; i++) {
const delegation = nestedDelegations[i]
const votes = await ctx.store.find(ConvictionVote, { where: { voter: delegation.from, delegatedTo: delegation.to, proposalIndex: index, removedAtBlock: IsNull(), isDelegated: true } })
Expand All @@ -95,11 +96,12 @@ export async function removeDelegatedVotesReferendum(ctx: BatchContext<Store, un
const vote = votes[0]
vote.removedAtBlock = block
vote.removedAt = new Date(blockTime)
vote.txnHash = txnHash
await ctx.store.save(vote)
}
}

export async function removeVote(ctx: BatchContext<Store, unknown>, wallet: string | undefined, proposalIndex: number, block: number, blockTime: number, shouldHaveVote: boolean, isDelegated: boolean = false, delegatedTo?: string): Promise<void> {
export async function removeVote(ctx: BatchContext<Store, unknown>, wallet: string | undefined, proposalIndex: number, block: number, blockTime: number, shouldHaveVote: boolean, isDelegated: boolean = false, delegatedTo?: string, txnHash?: string): Promise<void> {
const votes = await ctx.store.find(ConvictionVote, { where: { voter: wallet, proposalIndex, removedAtBlock: IsNull(), isDelegated, delegatedTo, type: VoteType.ReferendumV2 } })
if (votes.length > 1) {
ctx.log.warn(TooManyOpenVotes(block, proposalIndex, wallet))
Expand All @@ -115,19 +117,20 @@ export async function removeVote(ctx: BatchContext<Store, unknown>, wallet: stri
const vote = votes[0]
vote.removedAtBlock = block
vote.removedAt = new Date(blockTime)
vote.txnHash = txnHash
await ctx.store.save(vote)
}

export async function addOngoingReferendaDelegatedVotes(ctx: BatchContext<Store, unknown>, toWallet: string | undefined, header: SubstrateBlock, track: number): Promise<void> {
export async function addOngoingReferendaDelegatedVotes(ctx: BatchContext<Store, unknown>, toWallet: string | undefined, header: SubstrateBlock, track: number, txnHash?: string): Promise<void> {
const ongoingReferenda = await ctx.store.find(Proposal, { where: { endedAt: IsNull(), trackNumber: track, type: ProposalType.ReferendumV2 } })
const nestedDelegations = await getAllNestedDelegations(ctx, toWallet, track)
for (let i = 0; i < ongoingReferenda.length; i++) {
const ongoingReferendum = ongoingReferenda[i]
await addDelegatedVotesReferendum(ctx, toWallet, header.height, header.timestamp, ongoingReferendum, nestedDelegations, track)
await addDelegatedVotesReferendum(ctx, toWallet, header.height, header.timestamp, ongoingReferendum, nestedDelegations, track, txnHash)
}
}

export async function addDelegatedVotesReferendum(ctx: BatchContext<Store, unknown>, toWallet: string | undefined, block: number, blockTime: number, referendum: any, nestedDelegations: VotingDelegation[], track: number): Promise<void> {
export async function addDelegatedVotesReferendum(ctx: BatchContext<Store, unknown>, toWallet: string | undefined, block: number, blockTime: number, referendum: any, nestedDelegations: VotingDelegation[], track: number, txnHash?: string): Promise<void> {
//get top toWallet vote
const votes = await ctx.store.find(ConvictionVote, { where: { voter: toWallet, proposalIndex: referendum.index, removedAtBlock: IsNull() } })
if (votes.length > 1) {
Expand Down Expand Up @@ -161,6 +164,7 @@ export async function addDelegatedVotesReferendum(ctx: BatchContext<Store, unkno
delegatedTo: delegation.to,
type: VoteType.ReferendumV2,
isDelegated: true,
txnHash
})
)
}
Expand Down
7 changes: 4 additions & 3 deletions src/mappings/referendumV2/extrinsics/removeOtherVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export async function handleRemoveOtherVote(ctx: BatchContext<Store, unknown>,
await removeDelegatedVotesReferendum(ctx, header.height, header.timestamp, index, nestedDelegations)
}

export async function handlePrecompileRemoveOtherVote(ctx: BatchContext<Store, unknown>, itemCall: any, header: SubstrateBlock, data: any, originAccountId: any): Promise<void> {
export async function handlePrecompileRemoveOtherVote(ctx: BatchContext<Store, unknown>, itemCall: any, header: SubstrateBlock, data: any, originAccountId: any, txnHash?: string): Promise<void> {
const [ wallet, track, index ] = data

const referendum = await ctx.store.get(Proposal, { where: { index, type: ProposalType.ReferendumV2} })
if (!referendum || referendum.index == undefined || referendum.index == null || referendum.trackNumber == undefined || referendum.trackNumber == null) {
if (!referendum || referendum.index == undefined || referendum.index == null || referendum.trackNumber == undefined || referendum.trackNumber == null) {
ctx.log.warn(MissingProposalRecordWarn(ProposalType.ReferendumV2, index))
return
}
Expand All @@ -70,7 +70,8 @@ export async function handlePrecompileRemoveOtherVote(ctx: BatchContext<Store, u
const vote = votes[0]
vote.removedAtBlock = header.height
vote.removedAt = new Date(header.timestamp)
vote.txnHash = txnHash
await ctx.store.save(vote)
let nestedDelegations = await getAllNestedDelegations(ctx, wallet, referendum.trackNumber)
await removeDelegatedVotesReferendum(ctx, header.height, header.timestamp, index, nestedDelegations)
await removeDelegatedVotesReferendum(ctx, header.height, header.timestamp, index, nestedDelegations, txnHash)
}
6 changes: 3 additions & 3 deletions src/mappings/referendumV2/extrinsics/removeVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function handleRemoveVote(ctx: BatchContext<Store, unknown>,
await removeDelegatedVotesReferendum(ctx, header.height, header.timestamp, index, nestedDelegations)
}

export async function handlePrecompiledRemoveVote(ctx: BatchContext<Store, unknown>, itemCall: any, header: SubstrateBlock, data: any, originAccountId: any) : Promise<void> {
export async function handlePrecompiledRemoveVote(ctx: BatchContext<Store, unknown>, itemCall: any, header: SubstrateBlock, data: any, originAccountId: any, txnHash?: string) : Promise<void> {
const [ index ] = data
const referendum = await ctx.store.get(Proposal, { where: { index, type: ProposalType.ReferendumV2 } })
if (!referendum || referendum.index == undefined || referendum.index == null || referendum.trackNumber == undefined || referendum.trackNumber == null) {
Expand All @@ -40,7 +40,7 @@ export async function handlePrecompiledRemoveVote(ctx: BatchContext<Store, unkno
return
}
const wallet = originAccountId
await removeVote(ctx, wallet, index, header.height, header.timestamp, true)
await removeVote(ctx, wallet, index, header.height, header.timestamp, true, false, undefined, txnHash)
let nestedDelegations = await getAllNestedDelegations(ctx, wallet, referendum.trackNumber)
await removeDelegatedVotesReferendum(ctx, header.height, header.timestamp, index, nestedDelegations)
await removeDelegatedVotesReferendum(ctx, header.height, header.timestamp, index, nestedDelegations, txnHash)
}

0 comments on commit 846ca88

Please sign in to comment.