import { Address , BigInt , Bytes } from '@graphprotocol/graph-ts' ; import { Organization as OrganizationEntity , Permission as PermissionEntity , Role as RoleEntity , Param as ParamEntity , } from '../../generated/schema' ; import { ACL as AclContract , SetPermission as SetPermissionEvent , SetPermissionParams as SetPermissionParamsEvent , ChangePermissionManager as ChangePermissionManagerEvent , } from '../../generated/templates/Acl/ACL' ; import { ZERO_ADDR } from '../helpers/constants' ; export function handleSetPermission ( event ) { const acl = AclContract . bind ( event . address ) ; const orgAddress = acl . kernel ( ) ; const orgId = orgAddress . toHex ( ) ; const org = OrganizationEntity . load ( orgId ) ; const appAddress = event . params . app ; const roleHash = event . params . role ; const granteeAddress = event . params . entity ; const role = loadOrCreateRole ( appAddress , roleHash ) ; const permission = loadOrCreatePermission ( appAddress , roleHash , granteeAddress ) ; permission . allowed = event . params . allowed ; const orgPermissions = org . permissions ; orgPermissions . push ( permission . id ) ; org . permissions = orgPermissions ; org . save ( ) ; permission . save ( ) ; role . save ( ) ; } export function handleChangePermissionManager ( event ) { const appAddress = event . params . app ; const roleHash = event . params . role ; const role = loadOrCreateRole ( appAddress , roleHash ) ; role . manager = event . params . manager ; role . save ( ) ; } export function handleSetPermissionParams ( event ) { const acl = AclContract . bind ( event . address ) ; const appAddress = event . params . app ; const roleHash = event . params . role ; const granteeAddress = event . params . entity ; const permission = loadOrCreatePermission ( appAddress , roleHash , granteeAddress ) ; const paramsLength = acl . getPermissionParamsLength ( granteeAddress , appAddress , roleHash ) . toI32 ( ) ; for ( let index = 0 ; index < paramsLength ; index ++ ) { const param = loadOrCreateParam ( appAddress , roleHash , granteeAddress , event . params . paramsHash , BigInt . fromI32 ( index ) , acl ) ; const permissionParams = permission . params ; permissionParams . push ( param . id ) ; permission . params = permissionParams ; param . save ( ) ; } permission . save ( ) ; } function buildRoleId ( appAddress , roleHash ) { return appAddress . toHexString ( ) . concat ( '-' ) . concat ( roleHash . toHexString ( ) ) ; } function loadOrCreateRole ( appAddress , roleHash ) { const roleId = buildRoleId ( appAddress , roleHash ) ; let role = RoleEntity . load ( roleId ) ; if ( role === null ) { role = new RoleEntity ( roleId ) ; role . hash = roleHash ; role . app = appAddress . toHexString ( ) ; role . manager = Bytes . fromHexString ( ZERO_ADDR ) as Bytes ; } return role ! ; } function buildPermissionId ( appAddress , roleHash , granteeAddress ) { return appAddress . toHexString ( ) . concat ( '-' ) . concat ( roleHash . toHexString ( ) ) . concat ( '-' ) . concat ( granteeAddress . toHexString ( ) ) ; } function loadOrCreatePermission ( appAddress , roleHash , granteeAddress ) { const permissionId = buildPermissionId ( appAddress , roleHash , granteeAddress ) ; let permission = PermissionEntity . load ( permissionId ) ; if ( permission === null ) { permission = new PermissionEntity ( permissionId ) ; permission . granteeAddress = granteeAddress ; permission . allowed = false ; permission . params = [ ] ; permission . app = appAddress . toHexString ( ) ; permission . role = buildRoleId ( appAddress , roleHash ) ; } return permission ! ; } function buildParamId ( paramHash , index ) { return paramHash . toHexString ( ) . concat ( '-' ) . concat ( index . toString ( ) ) ; } function loadOrCreateParam ( appAddress , roleHash , granteeAddress , paramHash , index , acl ) { const paramId = buildParamId ( paramHash , index . toI32 ( ) ) ; let param = ParamEntity . load ( paramId ) ; if ( param === null ) { param = new ParamEntity ( paramId ) ; const paramData = acl . getPermissionParam ( granteeAddress , appAddress , roleHash , index ) ; param . argumentId = paramData . value0 ; param . operationType = paramData . value1 ; param . argumentValue = paramData . value2 ; } return param ! ; }