@@ -4,6 +4,7 @@ import { SignedTransaction, UnwrapCCC } from "codechain-sdk/lib/core/classes";
44import { Custom } from "codechain-sdk/lib/core/transaction/Custom" ;
55import {
66 actionFromCustom as stakeActionFromCustom ,
7+ getCandidates ,
78 getCCSHolders ,
89 getUndelegatedCCS
910} from "codechain-stakeholder-sdk" ;
@@ -179,6 +180,41 @@ export async function payFee(
179180 return Promise . all ( queries ) ;
180181}
181182
183+ async function getDeposit (
184+ sdk : SDK ,
185+ blockNumber : number ,
186+ pubkey : H512 ,
187+ transaction : Transaction
188+ ) : Promise < U64 > {
189+ // Get the deposit at the previous block
190+ const candidates = await getCandidates ( sdk , blockNumber - 1 ) ;
191+ const target = candidates . find ( c => c . pubkey . isEqualTo ( pubkey ) ) ;
192+ let deposit = new U64 ( 0 ) ;
193+ if ( target != null ) {
194+ deposit = target . deposit ;
195+ }
196+
197+ // Get the accumulated self nomination transactions
198+ const address = PlatformAddress . fromPublic ( pubkey , {
199+ networkId : sdk . networkId
200+ } ) ;
201+ const nominations = await CCCChangeModel . getNominations (
202+ address . toString ( ) ,
203+ blockNumber ,
204+ transaction
205+ ) ;
206+ for ( const n of nominations ) {
207+ const change : string = n . get ( "change" ) ;
208+ if ( ! change . startsWith ( "-" ) ) {
209+ throw new Error (
210+ 'CCCChange with reason "deposit" must have negative change'
211+ ) ;
212+ }
213+ deposit = deposit . plus ( change . slice ( 1 ) ) ;
214+ }
215+ return deposit ;
216+ }
217+
182218export async function trackBalanceChangeByTx (
183219 sdk : SDK ,
184220 blockNumber : number ,
@@ -280,23 +316,58 @@ export async function trackBalanceChangeByTx(
280316 sdk ,
281317 tx . unsigned as Custom
282318 ) ;
283- if ( stakeAction && stakeAction . type === "selfNominate" ) {
284- const signer = tx . getSignerAddress ( {
285- networkId : sdk . networkId
286- } ) ;
287- const deposit = stakeAction . deposit ;
288- queries . push (
289- CCCChangeModel . stakeDeposit (
290- {
291- address : signer . value ,
292- change : deposit ,
293- isNegative : true ,
294- blockNumber,
295- transactionHash
296- } ,
297- { transaction }
298- )
299- ) ;
319+ if ( stakeAction === null ) {
320+ break ;
321+ }
322+ switch ( stakeAction . type ) {
323+ case "selfNominate" : {
324+ const signer = tx . getSignerAddress ( {
325+ networkId : sdk . networkId
326+ } ) ;
327+ const deposit = stakeAction . deposit ;
328+ queries . push (
329+ CCCChangeModel . stakeDeposit (
330+ {
331+ address : signer . value ,
332+ change : deposit ,
333+ isNegative : true ,
334+ blockNumber,
335+ transactionHash
336+ } ,
337+ { transaction }
338+ )
339+ ) ;
340+ break ;
341+ }
342+ case "reportDoubleVote" : {
343+ const signer = tx . getSignerAddress ( {
344+ networkId : sdk . networkId
345+ } ) ;
346+ const criminal = stakeAction . criminal ( ) ;
347+ const deposit = await getDeposit (
348+ sdk ,
349+ blockNumber ,
350+ criminal ,
351+ transaction
352+ ) ;
353+ if ( deposit . isGreaterThan ( 0 ) ) {
354+ queries . push (
355+ CCCChangeModel . reportDoubleVote (
356+ {
357+ address : signer . value ,
358+ change : deposit ,
359+ blockNumber,
360+ transactionHash
361+ } ,
362+ { transaction }
363+ )
364+ ) ;
365+ }
366+ break ;
367+ }
368+ default : {
369+ break ;
370+ }
300371 }
301372 }
302373 default :
0 commit comments