Skip to content

Commit

Permalink
feat: add manual ci for querying grant role events (#336)
Browse files Browse the repository at this point in the history
- closes #337
  • Loading branch information
viraj124 authored Oct 23, 2024
1 parent e29e1e6 commit 27eb7ba
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/sharp-eggs-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fuel-bridge/solidity-contracts': minor
---

add manual ci for querying grant role events
36 changes: 36 additions & 0 deletions .github/workflows/manual-query-grant-role-event.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Manually Query Grant Role Events

on:
workflow_dispatch:
inputs:
contractAddress:
description: 'Enter the contract address for which you wanna query the grant role events for'
required: true
type: string
rpc:
description: 'Enter network rpc'
required: true
default: 'https://rpc.ankr.com/eth'
type: string

jobs:
verify-upgrade:
runs-on: ubuntu-latest
env:
RPC_URL: ${{ github.event.inputs.rpc }}
steps:
- uses: actions/checkout@v3
- uses: FuelLabs/github-actions/setups/node@master
with:
node-version: 20.16.0
pnpm-version: 9.0.6
- name: Query Events
run: |
npx hardhat compile && npx hardhat grant-role-event-filter --contract ${{ github.event.inputs.contractAddress }}
working-directory: ./packages/solidity-contracts
- name: Upload event payload as an artifact
uses: actions/upload-artifact@v4
with:
name: event-query-payload
path: grantedRoles.json
retention-days: 90
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ packages/fungible-token/exports/types
dist

# verification ci artifacts
packages/solidity-contracts/verification.json
packages/solidity-contracts/verification.json

# grant role query ci artifacts
packages/solidity-contracts/grantedRoles.json
143 changes: 143 additions & 0 deletions packages/solidity-contracts/scripts/hardhat/eventFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { task } from 'hardhat/config';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { writeFileSync } from 'fs';

task(
'grant-role-event-filter',
'Filters grant role event for a specific contract to keep track of assigned roles'
)
.addParam('contract', 'address of the contract')
.setAction(
async (taskArgs: any, hre: HardhatRuntimeEnvironment): Promise<void> => {
const provider = new hre.ethers.JsonRpcProvider(process.env.RPC_URL);

// fetching the abi from the artifacts would require the contract name as an input so avoiding that
const grantRoleEvenABI = [
{
inputs: [
{
internalType: 'bytes32',
name: 'role',
type: 'bytes32',
},
{
internalType: 'address',
name: 'account',
type: 'address',
},
],
name: 'hasRole',
outputs: [
{
internalType: 'bool',
name: '',
type: 'bool',
},
],
stateMutability: 'view',
type: 'function',
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: 'bytes32',
name: 'role',
type: 'bytes32',
},
{
indexed: true,
internalType: 'address',
name: 'account',
type: 'address',
},
{
indexed: true,
internalType: 'address',
name: 'sender',
type: 'address',
},
],
name: 'RoleGranted',
type: 'event',
},
];

// existing roles
const DEFAULT_ADMIN_ROLE = hre.ethers.ZeroHash;
const PAUSER_ROLE = hre.ethers.keccak256(
hre.ethers.toUtf8Bytes('PAUSER_ROLE')
);
const COMMITTER_ROLE = hre.ethers.keccak256(
hre.ethers.toUtf8Bytes('COMMITTER_ROLE')
);
const SET_RATE_LIMITER_ROLE = hre.ethers.keccak256(
hre.ethers.toUtf8Bytes('SET_RATE_LIMITER_ROLE')
);

const FROM_BLOCK = 20620432;

const roles = [
{ name: 'DEFAULT_ADMIN_ROLE', value: DEFAULT_ADMIN_ROLE },
{ name: 'PAUSER_ROLE', value: PAUSER_ROLE },
{ name: 'COMMITTER_ROLE', value: COMMITTER_ROLE },
{ name: 'SET_RATE_LIMITER_ROLE', value: SET_RATE_LIMITER_ROLE },
];

const contract = new hre.ethers.Contract(
taskArgs.contract,
grantRoleEvenABI,
provider
);

const eventPayload: any = [];

try {
const events = await contract.queryFilter(
contract.filters.RoleGranted(),
FROM_BLOCK
);

// check for duplicate events (where the role id and the account are the same)
const filteredEvents = events.reduce((previous: any, current: any) => {
// typing bug with array of (EventLog | Log) type
const isDuplicate = previous.find(
(event: any) =>
// typing bug with element of (EventLog | Log) type
event.args[0] === current.args[0] &&
event.args[1] === current.args[1]
);

if (!isDuplicate) {
return previous.concat([current]);
} else {
return previous;
}
}, []);

for (const event of filteredEvents) {
const eventArgs: any = {};

// only checking for active roles
const hasRole = await contract.hasRole(event.args[0], event.args[1]);
if (hasRole) {
// computing the `role` in a readable format
eventArgs.role =
roles.find((role) => role.value === event.args[0])?.name ||
'UNKNOWN_ROLE';
eventArgs.account = event.args[1];

eventPayload.push(eventArgs);
}
}

writeFileSync(
'grantedRoles.json',
JSON.stringify(eventPayload, undefined, 2)
);
} catch (error) {
throw new Error(`Unable to filter and query events: ${error}`);
}
}
);
1 change: 1 addition & 0 deletions packages/solidity-contracts/scripts/hardhat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './withdrawalResume';
export * from './withdrawalBlacklist';
export * from './withdrawalWhitelist';
export * from './verifyMainnetDeployment';
export * from './eventFilter';

0 comments on commit 27eb7ba

Please sign in to comment.