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: scripts for fetching plan execution statuses #5

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# project
.env*

# node
node_modules/
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
65 changes: 65 additions & 0 deletions scripts/README.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As commented in the README, usr usually refers to the DssSpellAction for conforming spells, not to the spell itself.
I believe the script should somehow return the associated spell address. I'm not entirely sure how to get that information, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] I'm not entirely sure how to get that information, though.

Oh, I just noticed that msg.sender is logged as topic 1:
https://vscode.blockscan.com/ethereum/0xbe286431454714f511008713973d3b053a2d38f3#L43

For conforming spells, this can be used to obtain the spell address.
Since it's not guaranteed, maybe the column should be named SENDER and the documentation should explain that this COULD be the spell address 🤷🏻‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 5843d92

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Protego Scripts

## Usage

### 1. Install dependencies

```
npm i
```

### 2. Run scripts

List the 20 most recent plans in `MCD_PAUSE`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that instead of fixing an arbitrary number of events to return, we could maybe let the user specify how many blocks they want to go in the past.

However, that introduces one edge case: events for the same [usr, tag, fax, eta] might be missing because of the from block. It's usually fine, as we can consider any subsequent exec or drop to have been preceded by a plan.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in f6e02c3

```
npm run list
```

Full list of plans in `MCD_PAUSE`
```
npm run list-all
```

Pending plans in `MCD_PAUSE`
```
npm run list-pending
```

## Output

```
╔════════════════════════════════════════════════════════════════════╤════════════════════════════════════════════╤════════════════════════════════════════════════════════════════════╤═══════════════════════════════════╤════════════╤════════════╗
║ HASH │ USR │ TAG │ FAX │ ETA │ STATUS ║
╟────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼───────────────────────────────────┼────────────┼────────────╢
║ 0xafce2325ab2957ec8a85cbc9bc0f8e079f8ea73df2e07eeafaf7f3c79c04576c │ 0xb7fdC60f7497759e1c144a8cf2969B3B352c37F7 │ 0x7f06e43eb0c4a2e7aa90272bc257b5fc6247838ba2d8944095751fa10d9d7616 │ 0x61461954 │ 1711697219 │ EXECUTED ║
╟────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼───────────────────────────────────┼────────────┼────────────╢
║ 0x3b3a337ff1df6e96cbd3633e40c2bb076fd04b1bbcf0a81a4c1dfebe06c63ef2 │ 0x9A09E438266f55dC4b3135CbfE1F491f96a13313 │ 0x94068fe76609e1472bc39801268135e6b841195826c64ae470c80585d2750fa1 │ 0x61461954 │ 1712569127 │ EXECUTED ║
╟────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼───────────────────────────────────┼────────────┼────────────╢
║ 0x2b48073bf5814be9be04bb86077f5b10cf5c8119c17d5d52fda1121aca7e5e1c │ 0xE4768597ca81df6f2FD27945016b03E96a3a7E1D │ 0x06dbd46347fe80f471dc8e6dc30ead3411d21c4896671e3c8acf040557fee896 │ 0x61461954 │ 1713993803 │ EXECUTED ║
╟────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼───────────────────────────────────┼────────────┼────────────╢
║ 0x271d6ce5a94bc0df848bd6f98b435fc6a80a1cd0270119f2b8fc0d2f8adaffe7 │ 0x45BbEAA4111b907c2bD1244701Ea34Df1d13B9a7 │ 0xdbf8118b76005c63d7aede2746c48bb738ba07995e019c33e52bd851a51d56bc │ 0x61461954 │ 1715198375 │ EXECUTED ║
╟────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼───────────────────────────────────┼────────────┼────────────╢
║ 0xfd391f7c694cf20870527de9427e7ac326ad133aced2a7c9e072a27d9057a621 │ 0x6A65D46645bC7C1aE4C95f5f00968e48aDd009C3 │ 0x8d56f5149ab50c0f290a4942cc692ce72111d4e98473f8f7ead1d0c4e37a1724 │ 0x61461954 │ 1716062519 │ EXECUTED ║
╟────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼───────────────────────────────────┼────────────┼────────────╢
║ 0x2eb42ca4e054352eaa8ef09705925b3c12b4c0d1a59a050a20baa2b9d44396a8 │ 0x612938f231DFcd7F92181F11C9E0B575E7ed2Ec1 │ 0x25f5bbaef576d0cf62af933ec55a6bd468b65fa9902387040283da6bf88ea4a9 │ 0x61461954 │ 1717623383 │ EXECUTED ║
╟────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼───────────────────────────────────┼────────────┼────────────╢
║ 0xd37c72ea00f67c76b6cda8dc0dda0b1b6f825145079e9acd23bdbcfe00259a3e │ 0x6481e7443D321fFF02A9A7ae883DCd13FAb64Ef7 │ 0xf218b00aeb30403e97dbedd8542caf1a879c0e15796a20b5836cfadfe9a69b43 │ 0x61461954 │ 1718481719 │ EXECUTED ║
╟────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼───────────────────────────────────┼────────────┼────────────╢
║ 0x9ca00512f5e87da33fa1fa1e4834581c8684ebd9091c7883355ea0ca959a78f2 │ 0x261da1Cdbd788642034288B6574d749198FDf75b │ 0x5e8fe783452994a42bbbe3b0edde5b9901ff6bd7561b680b42af92293206bb32 │ 0x61461954 │ 1719777527 │ EXECUTED ║
╟────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼───────────────────────────────────┼────────────┼────────────╢
║ 0xb8f4b59cbfe45ba425eb9c10c8a50bd1eb919325eefdf095c44369098fd21e59 │ 0x0871e28D09c29C419669D82901d53d60A649B36D │ 0xc8dec11fc102e8810674c4db724fb129a790f93767cf54b691cbe3d2d33dd3d8 │ 0x61461954 │ 1720383443 │ EXECUTED ║
╟────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────┼───────────────────────────────────┼────────────┼────────────╢
║ 0x11e3af57c6821e4b4b45559c92f5c9da681f2b7d2d232f91d7b64aa14d317f6a │ 0xA13D7e21643bD46E2cC09E87cFB91c5B951Bd955 │ 0x041f68b86ef5961f3d578a7192a09f1980b42bcefa8fecb810816369420c1e0c │ 0x61461954 │ 1721070179 │ EXECUTED ║
╚════════════════════════════════════════════════════════════════════╧════════════════════════════════════════════╧════════════════════════════════════════════════════════════════════╧═══════════════════════════════════╧════════════╧════════════╝
```

The script outputs a table with the plans' details:
- HASH: Hash of the plan
- USR: Address of the Spell
oddaf marked this conversation as resolved.
Show resolved Hide resolved
- TAG: `extcodehash` from the address of the Spell
oddaf marked this conversation as resolved.
Show resolved Hide resolved
- FAX: `callcode` to be used when calling the Spell
- ETA: Timestamp of earliest execution time
- STATUS: Status of the plan:
- PENDING: The plan has been plotted (scheduled) on Pause, and is pending execution
- EXECUTED: The plan has already been executed
- DROPPED: The plan was scheduled and subsequently dropped
146 changes: 146 additions & 0 deletions scripts/list-pause-plans.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { ethers } from 'ethers';
import { table } from 'table';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import pauseABI from './pause_abi.json' with { type: 'json' };

const argv = yargs(hideBin(process.argv))
.option('all', {
alias: 'a',
type: 'boolean',
description: 'Show all plans',
default: false
})
.option('pending', {
alias: 'p',
type: 'boolean',
description: 'Show only pending plans',
default: false
})
.check(argv => {
if (argv.all && argv.pending) {
throw new Error("You cannot use --all and --pending together.");
}
return true;
})
.help()
.alias('help', 'h')
.argv;

const PLOT_TOPIC = "0x46d2fbbb00000000000000000000000000000000000000000000000000000000";
const EXEC_TOPIC = "0x168ccd6700000000000000000000000000000000000000000000000000000000";
const DROP_TOPIC = "0x162c7de300000000000000000000000000000000000000000000000000000000";
const MCD_PAUSE = "0xbE286431454714F511008713973d3B053A2d38f3";

const tableConfig = {
columns: {
0: { width: 66, wrapWord: true },
1: { width: 42, wrapWord: true },
2: { width: 66, wrapWord: true },
3: { width: 33, wrapWord: true },
4: { width: 10, wrapWord: true },
5: { width: 10, wrapWord: true },
6: { width: 10, wrapWord: true }
amusingaxl marked this conversation as resolved.
Show resolved Hide resolved
}
};

function getProvider() {
const url = process.env.ETH_RPC_URL || "mainnet";
if (!process.env.ETH_RPC_URL) {
console.warn("ETH_RPC_URL not set, falling back to a public RPC provider. For improved results set ETH_RPC_URL to a trusted node.");
}
return ethers.getDefaultProvider(url);
}

function decodeLogNote(log, contract) {
const eventFragment = contract.interface.getEvent('LogNote');
return contract.interface.decodeEventLog(eventFragment, log.data, log.topics).toObject();
}

function decodeCallParams(sig, fax, contract) {
const functionFragment = contract.interface.getFunction(sig);
return contract.interface.decodeFunctionData(functionFragment, fax).toObject();
}

function hash(params) {
const abiCoder = new ethers.AbiCoder();
const types = ["address", "bytes32", "bytes", "uint256"];
const encoded = abiCoder.encode(types, [params.usr, params.tag, params.fax, params.eta]);
return ethers.keccak256(encoded);
}

async function getFilteredEvents(contract) {
try {
const events = await contract.queryFilter('*');
return events.filter(log => [PLOT_TOPIC, EXEC_TOPIC, DROP_TOPIC].includes(log.topics[0]));
amusingaxl marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
console.error("Error fetching filtered events:", error);
throw error;
}
}

function processEvent(event, contract) {
const decoded = decodeLogNote(event, contract);
const decodedCall = decodeCallParams(event.topics[0].slice(0, 10), decoded.fax, contract);
return {
...event,
decoded,
decodedCall,
planHash: hash(decodedCall)
};
}

function prepareTableData(events, contract) {
const decodedEvents = events.map(event => processEvent(event, contract));

let tableData = [["HASH", "USR", "TAG", "FAX", "ETA", "STATUS"]];
const hashMap = new Map();

decodedEvents.forEach(event => {
const planHash = event.planHash;

if (event.topics[0] === PLOT_TOPIC) {
const row = [planHash, event.decodedCall.usr, event.decodedCall.tag, event.decodedCall.fax.trim(), event.decodedCall.eta, "PENDING"];
tableData.push(row);
hashMap.set(planHash, row);
} else if (event.topics[0] === EXEC_TOPIC) {
const row = hashMap.get(planHash);
if (row) {
row[5] = "EXECUTED";
}
} else if (event.topics[0] === DROP_TOPIC) {
const row = hashMap.get(planHash);
if (row) {
row[5] = "DROPPED";
}
}
});

return tableData;
}
amusingaxl marked this conversation as resolved.
Show resolved Hide resolved
amusingaxl marked this conversation as resolved.
Show resolved Hide resolved

async function main() {
try {
const provider = getProvider();
const pause = new ethers.Contract(MCD_PAUSE, pauseABI, provider);

const events = await getFilteredEvents(pause);
let tableData = prepareTableData(events, pause);

if (!argv.all && !argv.pending) {
tableData = tableData.length > 21 ? [tableData[0], ...tableData.slice(-20)] : tableData;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the suggestion about data sorting by newest values first is applied, this can be simplified as:

Suggested change
tableData = tableData.length > 21 ? [tableData[0], ...tableData.slice(-20)] : tableData;
tableData = tableData.length > 21 ? tableData.slice(0, 21) : tableData;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic was removed in favor of the fromBlock parameter: f6e02c3

} else if (argv.pending) {
tableData = tableData.filter(row => row[6] === "pending" || row[0] === "HASH");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comparison will fail because of mismatching cases:

Suggested change
tableData = tableData.filter(row => row[6] === "pending" || row[0] === "HASH");
tableData = tableData.filter(row => row[6] === "PENDING" || row[0] === "HASH");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in general, I would prefer to keep the presentation layer as simple as possible and do all filtering when data is being fetched.
This allows that part of the script to be reused by a front-end, which would not need to re-implement this sort of logic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e37ee56, also separated all the filtering from presentation layer 56872ae and 4f032ba

}

if (tableData.length === 1) {
console.log("No records to display.");
} else {
console.log(table(tableData, tableConfig));
}
} catch (error) {
console.error("An error occurred:", error);
}
}

main();
Loading
Loading