11import { PolicyUpdate } from "../generated/PolicyRegistry/PolicyRegistry" ;
22import { Court } from "../generated/schema" ;
3- import { ipfs , log , json , Bytes , TypedMap , JSONValue } from "@graphprotocol/graph-ts" ;
3+ import { ipfs , json , Bytes , TypedMap , JSONValue } from "@graphprotocol/graph-ts" ;
44
55export function handlePolicyUpdate ( event : PolicyUpdate ) : void {
66 const courtID = event . params . _courtID . toString ( ) ;
@@ -10,13 +10,7 @@ export function handlePolicyUpdate(event: PolicyUpdate): void {
1010 court . name = event . params . _courtName ;
1111 court . save ( ) ;
1212
13- let jsonStr = ipfs . cat ( event . params . _policy ) ;
14- if ( ! jsonStr ) return ;
15-
16- const jsonObjValueAndSuccess = json . try_fromBytes ( jsonStr as Bytes ) ;
17- if ( ! jsonObjValueAndSuccess . isOk ) return ;
18-
19- const jsonObj = jsonObjValueAndSuccess . value . toObject ( ) ;
13+ const jsonObj = getJsonObj ( event . params . _policy ) ;
2014 if ( ! jsonObj ) return ;
2115
2216 court . name = tryGetValue ( jsonObj , "name" ) ;
@@ -30,3 +24,16 @@ function tryGetValue(jsonObj: TypedMap<string, JSONValue>, key: string): string
3024 const value = jsonObj . get ( key ) ;
3125 return value ? value . toString ( ) : null ;
3226}
27+
28+ function getJsonObj ( CID : string ) : TypedMap < string , JSONValue > | null {
29+ const jsonStr = ipfs . cat ( CID ) ;
30+ if ( ! jsonStr ) return null ;
31+
32+ const jsonObjValueAndSuccess = json . try_fromBytes ( jsonStr as Bytes ) ;
33+ if ( ! jsonObjValueAndSuccess . isOk ) return null ;
34+
35+ const jsonObj = jsonObjValueAndSuccess . value . toObject ( ) ;
36+ if ( ! jsonObj ) return null ;
37+
38+ return jsonObj ;
39+ }
0 commit comments