Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add id generation for iplugin, iproposal and transactionActionsProposal #13

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion subgraph/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ ignorePatterns:
- imported/
- generated/
- extended-schema.ts
- tests/

rules: {'@typescript-eslint/ban-types': ['error', {'types': {'BigInt': false}}]}
5 changes: 5 additions & 0 deletions subgraph/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## v1.0.0

### Changed

- Renamed `pluginRepo` to `plugin`

### Added

- Added `generateActionEntityId` and `generateStandardCAllbackEntityId`.
- Added `generateTransactionActionsProposalEntityId`, `generateProposalEntityId` and `generatePluginEntityId`
- Added `generatePermissionId` and `generatePluginPermissionId`.
- Added `generatePluginRepoEntityId`, `generatePluginSetupEntityId`, `generatePluginInstallationEntityId`, `generatePluginPreparationEntityId`, `generatePluginReleaseEntityId`, and `generatePluginVersionEntityId`.
9 changes: 9 additions & 0 deletions subgraph/src/ids/pluginRepo.ts → subgraph/src/ids/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import {
ethereum,
} from '@graphprotocol/graph-ts';

/**
* Generates an entity ID for a plugin.
* @param plugin - The address of the plugin.
* @returns The entity ID as a string.
*/
export function generatePluginEntityId(plugin: Address): string {
return plugin.toHexString();
}

/**
* Generates the plugin repository's ID using its address in hexadecimal format.
*
Expand Down
33 changes: 33 additions & 0 deletions subgraph/src/ids/proposal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {bigIntToBytes32} from '../utils/utils';
import {Address, BigInt, Bytes} from '@graphprotocol/graph-ts';

/**
* Generates a unique action ID using the given parameters.
*
Expand All @@ -12,3 +15,33 @@ export function generateActionEntityId(
const ids = [proposalEntityId, index.toString()];
return ids.join('_');
}

/**
* Generates a unique identifier for a transaction action proposal entity.
heueristik marked this conversation as resolved.
Show resolved Hide resolved
* @param proposalEntityId - The ID of the proposal entity.
* @param txHash - The hash of the transaction.
* @param logIndex - The index of the log.
* @returns A string representing the unique identifier for the transaction action proposal entity.
*/
export function generateTransactionActionsProposalEntityId(
proposalEntityId: string,
txHash: Bytes,
logIndex: BigInt
): string {
const ids = [proposalEntityId, txHash.toHexString(), logIndex.toHexString()];
return ids.join('_');
}

/**
* Generates an entity ID for a proposal.
* @param plugin - The address of the plugin that created the proposal.
* @param proposalId - The ID of the proposal.
* @returns The generated entity ID.
*/
export function generateProposalEntityId(
plugin: Address,
proposalId: BigInt
): string {
const ids = [plugin.toHexString(), bigIntToBytes32(proposalId)];
return ids.join('_');
}
2 changes: 1 addition & 1 deletion subgraph/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './ids/dao';
export * from './ids/pluginRepo';
export * from './ids/plugin';
export * from './ids/permissions';
14 changes: 14 additions & 0 deletions subgraph/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {BigInt} from '@graphprotocol/graph-ts';

/**
* Converts a BigInt to a bytes32 string with a 64 zero padding.
* @param input The BigInt to convert.
* @returns The bytes32 string representation of the input BigInt.
*/
export function bigIntToBytes32(input: BigInt): string {
const hexString = input
.toHexString() // convert to hex, example: 0x1
.slice(2) // remove 0x
.padStart(64, '0'); // pad left with '0' until reaching target length of 32 bytes
return `0x${hexString}`; // add 0x to the start
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import {
generatePluginPreparationEntityId,
generatePluginReleaseEntityId,
generatePluginVersionEntityId,
generatePluginEntityId,
} from '../../src';
import {ADDRESS_ONE, ADDRESS_TWO, DUMMY_BYTES32_HEX} from '../constants';
import {Address, Bytes, crypto} from '@graphprotocol/graph-ts';
import {assert, describe, test} from 'matchstick-as/assembly/index';

describe('PluginRepo ID generation', () => {
describe('Plugin ID generation', () => {
test('`generatePluginEntityId` should return the hex string representation of the plugin address', () => {
const pluginAddress = Address.fromString(ADDRESS_ONE);
const expectedId = pluginAddress.toHexString();

assert.stringEquals(generatePluginEntityId(pluginAddress), expectedId);
});
test('`generatePluginRepoEntityId` should return the hexadecimal representation of the provided address', () => {
// Constants
const PLUGIN_REPO_ADDRESS = Address.fromString(ADDRESS_ONE);
Expand Down
43 changes: 39 additions & 4 deletions subgraph/tests/ids/proposal.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import {generateActionEntityId} from '../../src/ids/proposal';
import {DUMMY_PROPOSAL_ID} from '../constants';
import {assert, describe, test} from 'matchstick-as/assembly/index';
import {
generateActionEntityId,
generateProposalEntityId,
generateTransactionActionsProposalEntityId,
} from '../../src/ids/proposal';
import {bigIntToBytes32} from '../../src/utils/utils';
import {ADDRESS_ONE, DUMMY_BYTES32_HEX, DUMMY_PROPOSAL_ID} from '../constants';
import {Address, BigInt, Bytes} from '@graphprotocol/graph-ts';
import {assert, describe, test} from 'matchstick-as';

describe('Proposal ID generation', () => {
describe('Transaction Actions Proposal ID generation', () => {
test('`generateActionEntityId` should return the id representation of an action', () => {
const proposalId = DUMMY_PROPOSAL_ID;
const index = 0;
Expand All @@ -11,4 +17,33 @@ describe('Proposal ID generation', () => {

assert.stringEquals(generateActionEntityId(proposalId, index), expectedId);
});
test('`generateTransactionActionsProposalEntityId` should return the id representation of a transaction actions proposal', () => {
const proposalEntityId = generateProposalEntityId(
Address.fromString(ADDRESS_ONE),
BigInt.fromI32(1)
);
const txHash = Bytes.fromHexString(DUMMY_BYTES32_HEX);
const logIndex = BigInt.fromI32(1);

const expectedId = `${proposalEntityId}_${txHash.toHexString()}_${logIndex.toHexString()}`;

assert.stringEquals(
generateTransactionActionsProposalEntityId(
proposalEntityId,
txHash,
logIndex
),
expectedId
);
});
test('`generateProposalEntityId` should return the id representation of a proposal', () => {
const plugin = Address.fromString(ADDRESS_ONE);
const proposalId = BigInt.fromI32(1);

const expectedId = `${plugin.toHexString()}_${bigIntToBytes32(proposalId)}`;
assert.stringEquals(
generateProposalEntityId(plugin, proposalId),
expectedId
);
});
});
13 changes: 13 additions & 0 deletions subgraph/tests/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {bigIntToBytes32} from '../../src/utils/utils';
import {BigInt} from '@graphprotocol/graph-ts';
import {assert, describe, test} from 'matchstick-as/assembly/index';

describe('bigIntToBytes32', () => {
test('should convert a BigInt to a bytes32 string with 64 zero padding', () => {
const input = BigInt.fromI32(123);
const expectedOutput =
'0x000000000000000000000000000000000000000000000000000000000000007b';

assert.stringEquals(bigIntToBytes32(input), expectedOutput);
});
});