From d5575f6e29d90b0ad662bc844d69cf06dcf7ac18 Mon Sep 17 00:00:00 2001 From: josemarinas <36479864+josemarinas@users.noreply.github.com> Date: Wed, 10 Jan 2024 16:54:46 +0100 Subject: [PATCH] Add: generic entity ID generators (#41) * add generic functions * fix lint * fix operations map --- subgraph/CHANGELOG.md | 6 ++++ subgraph/package.json | 2 +- subgraph/src/ids/callbacks.ts | 7 +++-- subgraph/src/ids/dao.ts | 3 +- subgraph/src/ids/ids.ts | 34 +++++++++++++++++++++++ subgraph/src/ids/index.ts | 1 + subgraph/src/ids/permissions.ts | 30 +++++++++----------- subgraph/src/ids/plugin.ts | 38 ++++++++++++++------------ subgraph/src/ids/proposal.ts | 21 ++++++++++---- subgraph/src/utils/constants.ts | 2 +- subgraph/tests/ids/permissions.test.ts | 4 +-- 11 files changed, 99 insertions(+), 49 deletions(-) create mode 100644 subgraph/src/ids/ids.ts diff --git a/subgraph/CHANGELOG.md b/subgraph/CHANGELOG.md index e776d8ae..48baf4cf 100644 --- a/subgraph/CHANGELOG.md +++ b/subgraph/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## v0.0.3 + +### Changed + +- Use generic functions for generating entity IDs, an unify the way of generating the composed ids. + ## v0.0.2 ### Fixes diff --git a/subgraph/package.json b/subgraph/package.json index 46b1b7a2..522825cf 100644 --- a/subgraph/package.json +++ b/subgraph/package.json @@ -1,6 +1,6 @@ { "name": "@aragon/osx-commons-subgraph", - "version": "0.0.2", + "version": "0.0.3", "description": "The Aragon OSx subgraph package containing common utilities", "module": "index.ts", "types": "index.ts", diff --git a/subgraph/src/ids/callbacks.ts b/subgraph/src/ids/callbacks.ts index 277eeb64..4b3bb6bf 100644 --- a/subgraph/src/ids/callbacks.ts +++ b/subgraph/src/ids/callbacks.ts @@ -1,3 +1,4 @@ +import {generateEntityIdFromAddress, generateEntityIdFromBytes} from './ids'; import {Address, Bytes} from '@graphprotocol/graph-ts'; /** @@ -11,6 +12,8 @@ export function generateStandardCallbackEntityId( dao: Address, interfaceId: Bytes ): string { - const ids = [dao.toHexString(), interfaceId.toHexString()]; - return ids.join('_'); + return [ + generateEntityIdFromAddress(dao), + generateEntityIdFromBytes(interfaceId), + ].join('_'); } diff --git a/subgraph/src/ids/dao.ts b/subgraph/src/ids/dao.ts index 39294a7f..a08255b2 100644 --- a/subgraph/src/ids/dao.ts +++ b/subgraph/src/ids/dao.ts @@ -1,3 +1,4 @@ +import {generateEntityIdFromAddress} from './ids'; import {Address} from '@graphprotocol/graph-ts'; /** @@ -7,7 +8,7 @@ import {Address} from '@graphprotocol/graph-ts'; * @returns A hexadecimal string representation of the provided DAO address. */ export function generateDaoEntityId(dao: Address): string { - return dao.toHexString(); + return generateEntityIdFromAddress(dao); } // TODO: this is not complete, it will be done in it's own task. diff --git a/subgraph/src/ids/ids.ts b/subgraph/src/ids/ids.ts new file mode 100644 index 00000000..be87491f --- /dev/null +++ b/subgraph/src/ids/ids.ts @@ -0,0 +1,34 @@ +import {Address, BigInt, Bytes} from '@graphprotocol/graph-ts'; + +/** + * Returns a valid entity ID based on the provided address. + * + * @export + * @param {Address} address + * @return {*} {string} + */ +export function generateEntityIdFromAddress(address: Address): string { + return address.toHexString(); +} + +/** + * Returns a valid entity ID based on the provided bytes. + * + * @export + * @param {Bytes} bytes + * @return {*} {string} + */ +export function generateEntityIdFromBytes(bytes: Bytes): string { + return bytes.toHexString(); +} + +/** + * Returns a valid entity ID based on the provided BigInt. + * + * @export + * @param {BigInt} number + * @return {*} {string} + */ +export function generateEntityIdFromBigInt(number: BigInt): string { + return number.toHexString(); +} diff --git a/subgraph/src/ids/index.ts b/subgraph/src/ids/index.ts index be2a9ae2..5bf4d071 100644 --- a/subgraph/src/ids/index.ts +++ b/subgraph/src/ids/index.ts @@ -5,3 +5,4 @@ export * from './permissions'; export * from './plugin'; export * from './proposal'; export * from './transfer'; +export * from './ids'; diff --git a/subgraph/src/ids/permissions.ts b/subgraph/src/ids/permissions.ts index d529af32..9216cf79 100644 --- a/subgraph/src/ids/permissions.ts +++ b/subgraph/src/ids/permissions.ts @@ -1,4 +1,4 @@ -import {PERMISSION_OPERATIONS} from '../utils/constants'; +import {generateEntityIdFromAddress, generateEntityIdFromBytes} from './ids'; import {Address, Bytes} from '@graphprotocol/graph-ts'; /** @@ -16,13 +16,12 @@ export function generatePermissionEntityId( where: Address, who: Address ): string { - const ids = [ - emittingContract.toHexString(), - permissionId.toHexString(), - where.toHexString(), - who.toHexString(), - ]; - return ids.join('_'); + return [ + generateEntityIdFromAddress(emittingContract), + generateEntityIdFromBytes(permissionId), + generateEntityIdFromAddress(where), + generateEntityIdFromAddress(who), + ].join('_'); } /** @@ -42,14 +41,11 @@ export function generatePluginPermissionEntityId( who: Address, permissionId: Bytes ): string { - const operationId = PERMISSION_OPERATIONS.get(operation); - const ids = [ + return [ pluginPreparationEntityId, - operationId, - where.toHexString(), - who.toHexString(), - permissionId.toHexString(), - ]; - - return ids.join('_'); + operation.toString(), + generateEntityIdFromAddress(where), + generateEntityIdFromAddress(who), + generateEntityIdFromBytes(permissionId), + ].join('_'); } diff --git a/subgraph/src/ids/plugin.ts b/subgraph/src/ids/plugin.ts index f61b18f7..7e115f46 100644 --- a/subgraph/src/ids/plugin.ts +++ b/subgraph/src/ids/plugin.ts @@ -1,4 +1,4 @@ -import {PERMISSION_OPERATIONS} from '../utils/constants'; +import {generateEntityIdFromAddress, generateEntityIdFromBytes} from './ids'; import { Address, ByteArray, @@ -13,7 +13,7 @@ import { * @returns The entity ID as a string. */ export function generatePluginEntityId(plugin: Address): string { - return plugin.toHexString(); + return generateEntityIdFromAddress(plugin); } /** @@ -23,7 +23,7 @@ export function generatePluginEntityId(plugin: Address): string { * @returns A hexadecimal string representation of the provided address. */ export function generatePluginRepoEntityId(pluginRepo: Address): string { - return pluginRepo.toHexString(); + return generateEntityIdFromAddress(pluginRepo); } /** @@ -33,7 +33,7 @@ export function generatePluginRepoEntityId(pluginRepo: Address): string { * @returns A hexadecimal string representation of the provided address. */ export function generatePluginSetupEntityId(pluginSetup: Address): string { - return pluginSetup.toHexString(); + return generateEntityIdFromAddress(pluginSetup); } /** @@ -56,13 +56,15 @@ export function generatePluginInstallationEntityId( ); if (installationIdTupleEncoded) { - return Bytes.fromHexString( - crypto - .keccak256( - ByteArray.fromHexString(installationIdTupleEncoded.toHexString()) - ) - .toHexString() - ).toHexString(); + return generateEntityIdFromBytes( + Bytes.fromHexString( + crypto + .keccak256( + ByteArray.fromHexString(installationIdTupleEncoded.toHexString()) + ) + .toHexString() + ) + ); } return null; } @@ -79,8 +81,10 @@ export function generatePluginPreparationEntityId( pluginInstallationEntityId: string, prepareSetupId: Bytes ): string { - const ids = [pluginInstallationEntityId, prepareSetupId.toHexString()]; - return ids.join('_'); + return [ + pluginInstallationEntityId, + generateEntityIdFromBytes(prepareSetupId), + ].join('_'); } /** @@ -94,8 +98,7 @@ export function generatePluginReleaseEntityId( pluginRepo: Address, release: i32 ): string { - const ids = [generatePluginRepoEntityId(pluginRepo), release.toString()]; - return ids.join('_'); + return [generatePluginRepoEntityId(pluginRepo), release.toString()].join('_'); } /** @@ -111,10 +114,9 @@ export function generatePluginVersionEntityId( release: i32, build: i32 ): string { - const ids = [ + return [ generatePluginRepoEntityId(pluginRepo), release.toString(), build.toString(), - ]; - return ids.join('_'); + ].join('_'); } diff --git a/subgraph/src/ids/proposal.ts b/subgraph/src/ids/proposal.ts index 058f9b00..8ac63e35 100644 --- a/subgraph/src/ids/proposal.ts +++ b/subgraph/src/ids/proposal.ts @@ -1,4 +1,9 @@ import {bigIntToBytes32} from '../utils/utils'; +import { + generateEntityIdFromAddress, + generateEntityIdFromBigInt, + generateEntityIdFromBytes, +} from './ids'; import {Address, BigInt, Bytes} from '@graphprotocol/graph-ts'; /** @@ -12,8 +17,7 @@ export function generateActionEntityId( proposalEntityId: string, index: i32 ): string { - const ids = [proposalEntityId, index.toString()]; - return ids.join('_'); + return [proposalEntityId, index.toString()].join('_'); } /** @@ -28,8 +32,11 @@ export function generateTransactionActionsProposalEntityId( txHash: Bytes, logIndex: BigInt ): string { - const ids = [proposalEntityId, txHash.toHexString(), logIndex.toHexString()]; - return ids.join('_'); + return [ + proposalEntityId, + generateEntityIdFromBytes(txHash), + generateEntityIdFromBigInt(logIndex), + ].join('_'); } /** @@ -42,6 +49,8 @@ export function generateProposalEntityId( plugin: Address, proposalId: BigInt ): string { - const ids = [plugin.toHexString(), bigIntToBytes32(proposalId)]; - return ids.join('_'); + return [ + generateEntityIdFromAddress(plugin), + bigIntToBytes32(proposalId), + ].join('_'); } diff --git a/subgraph/src/utils/constants.ts b/subgraph/src/utils/constants.ts index d7d923b1..066c8227 100644 --- a/subgraph/src/utils/constants.ts +++ b/subgraph/src/utils/constants.ts @@ -1,4 +1,4 @@ -export const PERMISSION_OPERATIONS = new Map() +export const PERMISSION_OPERATIONS = new Map() .set(0, 'Grant') .set(1, 'Revoke') .set(2, 'GrantWithCondition'); diff --git a/subgraph/tests/ids/permissions.test.ts b/subgraph/tests/ids/permissions.test.ts index b48a0830..91b0a97f 100644 --- a/subgraph/tests/ids/permissions.test.ts +++ b/subgraph/tests/ids/permissions.test.ts @@ -53,9 +53,7 @@ describe('Plugin Permissions ID generation', () => { PERMISSION_ID ); - const expectedId = `${PLUGIN_PREPARATION_ID}_${PERMISSION_OPERATIONS.get( - OPERATION - )}_${WHERE_ADDRESS.toHexString()}_${WHO_ADDRESS.toHexString()}_${PERMISSION_ID.toHexString()}`; + const expectedId = `${PLUGIN_PREPARATION_ID}_${OPERATION}_${WHERE_ADDRESS.toHexString()}_${WHO_ADDRESS.toHexString()}_${PERMISSION_ID.toHexString()}`; assert.stringEquals(pluginPermissionEntityId, expectedId); });