Skip to content

Commit

Permalink
Include annotation support
Browse files Browse the repository at this point in the history
  • Loading branch information
cristovaoth committed Feb 5, 2025
1 parent 4e82107 commit 0ddbe50
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 259 deletions.
167 changes: 0 additions & 167 deletions packages/sdk/src/annotations/applyAnnotations.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/sdk/src/annotations/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/sdk/src/annotations/poster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const encodePost = (content: string) => {
}

export const encodeAnnotationsPost = (
rolesMod: `0x${string}`,
rolesMod: string,
roleKey: string,
updatePost: UpdateAnnotationsPost
) => {
Expand Down
11 changes: 10 additions & 1 deletion packages/sdk/src/calls/encodeCalls.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Roles__factory } from "../../../evm/typechain-types"
import { encodeAnnotationsPost } from "../annotations/poster"
import { flattenCondition } from "../conditions"

import { Call } from "./types"

const rolesInterface = Roles__factory.createInterface()

export const encodeCalls = (calls: Call[]): `0x${string}`[] => {
export const encodeCalls = (
calls: Call[],
rolesMod: string
): `0x${string}`[] => {
return calls.map((call) => {
switch (call.call) {
case "allowTarget": {
Expand Down Expand Up @@ -76,6 +80,11 @@ export const encodeCalls = (calls: Call[]): `0x${string}`[] => {
call.timestamp,
])
}

case "postAnnotations": {
const { roleKey, body } = call
return encodeAnnotationsPost(rolesMod, roleKey, body)
}
}
}) as `0x${string}`[]
}
30 changes: 28 additions & 2 deletions packages/sdk/src/calls/logCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,23 @@ export const logCall = (
break
}

// default:
// log(`Unhandled call ${call.call}`)
case "postAnnotations": {
const { addAnnotations, removeAnnotations } = call.body
const addCount =
addAnnotations?.reduce((acc, add) => acc + add.uris.length, 0) || 0
const removeCount = removeAnnotations?.length || 0
const message = [
addCount > 0 ? "add " + pluralize(addCount, "annotation") : undefined,
removeCount > 0
? "remove " + pluralize(removeCount, "annotation")
: undefined,
]
.filter(Boolean)
.join(", ")

log(`💬 ${message[0].toUpperCase()}${message.slice(1)}`)
break
}
}
}

Expand All @@ -76,3 +91,14 @@ const ExecutionOptionLabel = {
[ExecutionOptions.Send]: "call, send",
[ExecutionOptions.Both]: "call, delegatecall, send",
}

const pluralize = (
count: number,
singular: string,
plural = `${singular}s`
) => {
if (count === 1) {
return `1 ${singular}`
}
return `${count} ${plural}`
}
13 changes: 13 additions & 0 deletions packages/sdk/src/calls/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ interface SetAllowanceCall {
timestamp: bigint
}

interface PostAnnotationsCall {
call: "postAnnotations"
roleKey: string
body: {
addAnnotations?: {
uris: string[]
schema: string
}[]
removeAnnotations?: string[]
}
}

export type Call =
| AllowTargetCall
| ScopeTargetCall
Expand All @@ -68,3 +80,4 @@ export type Call =
| RevokeFunctionCall
| AssignRolesCall
| SetAllowanceCall
| PostAnnotationsCall
4 changes: 2 additions & 2 deletions packages/sdk/src/diff/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export function diffAll({
prev,
next,
}: {
prev?: { roles: Role[]; allowances: Allowance[] }
next?: { roles: Role[]; allowances: Allowance[] }
prev: { roles: Role[]; allowances: Allowance[] } | undefined | null
next: { roles: Role[]; allowances: Allowance[] } | undefined | null
}): Diff {
return merge(
diffRoles({
Expand Down
60 changes: 60 additions & 0 deletions packages/sdk/src/diff/annotations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Annotation } from "zodiac-roles-deployments"

import { groupBy } from "../utils/groupBy"

import { Diff } from "./helpers"

export function diffAnnotations({
roleKey,
prev = [],
next = [],
}: {
roleKey: string
prev?: Annotation[]
next?: Annotation[]
}): Diff {
const removeAnnotations = prev
.map((annotation) => annotation.uri)
.filter((uri) => !next.some((nextAnnotation) => nextAnnotation.uri === uri))

const addAnnotations = groupAnnotations(
next.filter(
(annotation) =>
!prev.some(
(currentAnnotation) =>
currentAnnotation.uri === annotation.uri &&
currentAnnotation.schema === annotation.schema
)
)
)

return {
minus:
removeAnnotations.length > 0
? [
{
call: "postAnnotations",
roleKey,
body: { removeAnnotations },
},
]
: [],
plus: addAnnotations
? [
{
call: "postAnnotations",
roleKey,
body: { addAnnotations },
},
]
: [],
}
}

const groupAnnotations = (annotations: readonly Annotation[]) =>
Object.entries(groupBy(annotations, (annotation) => annotation.schema)).map(
([schema, annotations]) => ({
schema,
uris: annotations.map((annotation) => annotation.uri),
})
)
Loading

0 comments on commit 0ddbe50

Please sign in to comment.