Skip to content

Commit

Permalink
Add: generic entity ID generators (#41)
Browse files Browse the repository at this point in the history
* add generic functions

* fix lint

* fix operations map
  • Loading branch information
josemarinas authored Jan 10, 2024
1 parent ea1b44d commit d5575f6
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 49 deletions.
6 changes: 6 additions & 0 deletions subgraph/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion subgraph/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 5 additions & 2 deletions subgraph/src/ids/callbacks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {generateEntityIdFromAddress, generateEntityIdFromBytes} from './ids';
import {Address, Bytes} from '@graphprotocol/graph-ts';

/**
Expand All @@ -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('_');
}
3 changes: 2 additions & 1 deletion subgraph/src/ids/dao.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {generateEntityIdFromAddress} from './ids';
import {Address} from '@graphprotocol/graph-ts';

/**
Expand All @@ -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.
34 changes: 34 additions & 0 deletions subgraph/src/ids/ids.ts
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 1 addition & 0 deletions subgraph/src/ids/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './permissions';
export * from './plugin';
export * from './proposal';
export * from './transfer';
export * from './ids';
30 changes: 13 additions & 17 deletions subgraph/src/ids/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PERMISSION_OPERATIONS} from '../utils/constants';
import {generateEntityIdFromAddress, generateEntityIdFromBytes} from './ids';
import {Address, Bytes} from '@graphprotocol/graph-ts';

/**
Expand All @@ -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('_');
}

/**
Expand All @@ -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('_');
}
38 changes: 20 additions & 18 deletions subgraph/src/ids/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PERMISSION_OPERATIONS} from '../utils/constants';
import {generateEntityIdFromAddress, generateEntityIdFromBytes} from './ids';
import {
Address,
ByteArray,
Expand All @@ -13,7 +13,7 @@ import {
* @returns The entity ID as a string.
*/
export function generatePluginEntityId(plugin: Address): string {
return plugin.toHexString();
return generateEntityIdFromAddress(plugin);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}
Expand All @@ -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('_');
}

/**
Expand All @@ -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('_');
}

/**
Expand All @@ -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('_');
}
21 changes: 15 additions & 6 deletions subgraph/src/ids/proposal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {bigIntToBytes32} from '../utils/utils';
import {
generateEntityIdFromAddress,
generateEntityIdFromBigInt,
generateEntityIdFromBytes,
} from './ids';
import {Address, BigInt, Bytes} from '@graphprotocol/graph-ts';

/**
Expand All @@ -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('_');
}

/**
Expand All @@ -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('_');
}

/**
Expand All @@ -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('_');
}
2 changes: 1 addition & 1 deletion subgraph/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const PERMISSION_OPERATIONS = new Map<number, string>()
export const PERMISSION_OPERATIONS = new Map<i32, string>()
.set(0, 'Grant')
.set(1, 'Revoke')
.set(2, 'GrantWithCondition');
4 changes: 1 addition & 3 deletions subgraph/tests/ids/permissions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit d5575f6

Please sign in to comment.