1- import { BigInt } from "@graphprotocol/graph-ts" ;
1+ import { BigInt , BigDecimal } from "@graphprotocol/graph-ts" ;
22import { User } from "../../generated/schema" ;
33import { ONE , ZERO } from "../utils" ;
44
5+ export function computeCoherenceScore ( totalCoherent : BigInt , totalResolvedDisputes : BigInt ) : BigInt {
6+ const smoothingFactor = BigDecimal . fromString ( "10" ) ;
7+
8+ let denominator = totalResolvedDisputes . toBigDecimal ( ) . plus ( smoothingFactor ) ;
9+ let coherencyRatio = totalCoherent . toBigDecimal ( ) . div ( denominator ) ;
10+
11+ const coherencyScore = coherencyRatio . times ( BigDecimal . fromString ( "100" ) ) ;
12+
13+ const roundedScore = coherencyScore . plus ( BigDecimal . fromString ( "0.5" ) ) ;
14+
15+ return BigInt . fromString ( roundedScore . toString ( ) . split ( "." ) [ 0 ] ) ;
16+ }
17+
518export function ensureUser ( id : string ) : User {
619 const user = User . load ( id ) ;
720
@@ -24,6 +37,7 @@ export function createUserFromAddress(id: string): User {
2437 user . totalAppealingDisputes = ZERO ;
2538 user . totalDisputes = ZERO ;
2639 user . totalCoherent = ZERO ;
40+ user . coherenceScore = ZERO ;
2741 user . save ( ) ;
2842
2943 return user ;
@@ -52,6 +66,7 @@ export function resolveUserDispute(id: string, previousFeeAmount: BigInt, feeAmo
5266 user . totalCoherent = user . totalCoherent . plus ( ONE ) ;
5367 }
5468 }
69+ user . coherenceScore = computeCoherenceScore ( user . totalCoherent , user . totalResolvedDisputes ) ;
5570 user . save ( ) ;
5671 return ;
5772 }
@@ -61,5 +76,6 @@ export function resolveUserDispute(id: string, previousFeeAmount: BigInt, feeAmo
6176 user . totalCoherent = user . totalCoherent . plus ( ONE ) ;
6277 }
6378 user . activeDisputes = user . activeDisputes . minus ( ONE ) ;
79+ user . coherenceScore = computeCoherenceScore ( user . totalCoherent , user . totalResolvedDisputes ) ;
6480 user . save ( ) ;
6581}
0 commit comments