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

Add CLI command to list transactions for a given deployment #821

Merged
merged 3 commits into from
Oct 24, 2024
Merged
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
17 changes: 0 additions & 17 deletions packages/core/src/internal/execution/deployment-state-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,3 @@ export function shouldBeJournaled(message: JournalMessage): boolean {

return true;
}

/**
* Reads the journal and returns an array of future IDs in the order they were executed.
*/
export async function getExecutionOrder(
deploymentLoader: DeploymentLoader
): Promise<string[]> {
const futureIds: string[] = [];

for await (const message of deploymentLoader.readFromJournal()) {
if ("futureId" in message && !futureIds.includes(message.futureId)) {
futureIds.push(message.futureId);
}
}

return futureIds;
}
166 changes: 87 additions & 79 deletions packages/core/src/list-transactions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { ArtifactResolver } from "./types/artifact";

import findLastIndex from "lodash/findLastIndex";

import { IgnitionError } from "./errors";
import { FileDeploymentLoader } from "./internal/deployment-loader/file-deployment-loader";
import { ERRORS } from "./internal/errors-list";
import {
getExecutionOrder,
loadDeploymentState,
} from "./internal/execution/deployment-state-helpers";
import { loadDeploymentState } from "./internal/execution/deployment-state-helpers";
import { ExecutionResultType } from "./internal/execution/types/execution-result";
import {
ExecutionSateType,
Expand All @@ -19,6 +18,7 @@ import {
type Transaction,
TransactionReceiptStatus,
} from "./internal/execution/types/jsonrpc";
import { JournalMessageType } from "./internal/execution/types/messages";
import { assertIgnitionInvariant } from "./internal/utils/assertions";
import {
type ListTransactionsResult,
Expand Down Expand Up @@ -48,86 +48,94 @@ export async function listTransactions(
});
}

const executionOrder = await getExecutionOrder(deploymentLoader);
const transactions: ListTransactionsResult = [];

for (const futureId of executionOrder) {
const exState = deploymentState.executionStates[futureId];

if (!doesSendTransactions(exState)) {
for await (const message of deploymentLoader.readFromJournal()) {
if (message.type !== JournalMessageType.TRANSACTION_SEND) {
continue;
}

for (const networkInteraction of exState.networkInteractions) {
assertIgnitionInvariant(
networkInteraction.type === "ONCHAIN_INTERACTION",
"Expected network interaction to be an onchain interaction"
);

for (const [
index,
transaction,
] of networkInteraction.transactions.entries()) {
switch (exState.type) {
case ExecutionSateType.DEPLOYMENT_EXECUTION_STATE: {
transactions.push({
type: exState.type,
from: exState.from,
txHash: transaction.hash,
status: getTransactionStatus(
transaction,
index === networkInteraction.transactions.length - 1
),
name: exState.contractName,
address:
transaction.receipt?.status === TransactionReceiptStatus.SUCCESS
? exState.result?.type === ExecutionResultType.SUCCESS
? exState.result.address
: undefined
: undefined,
params: exState.constructorArgs,
value: networkInteraction.value,
});

break;
}
case ExecutionSateType.CALL_EXECUTION_STATE: {
const artifact = await deploymentLoader.loadArtifact(
exState.artifactId
);

transactions.push({
type: exState.type,
from: exState.from,
txHash: transaction.hash,
status: getTransactionStatus(
transaction,
index === networkInteraction.transactions.length - 1
),
name: `${artifact.contractName}#${exState.functionName}`,
to: networkInteraction.to,
params: exState.args,
value: networkInteraction.value,
});

break;
}
case ExecutionSateType.SEND_DATA_EXECUTION_STATE: {
transactions.push({
type: exState.type,
from: exState.from,
txHash: transaction.hash,
status: getTransactionStatus(
transaction,
index === networkInteraction.transactions.length - 1
),
to: networkInteraction.to,
value: networkInteraction.value,
});

break;
}
}
const exState = deploymentState.executionStates[message.futureId];

assertIgnitionInvariant(
doesSendTransactions(exState),
"Expected execution state to be a type that sends transactions"
);

const networkInteraction =
exState.networkInteractions[message.networkInteractionId - 1];
Copy link
Member

Choose a reason for hiding this comment

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

This -1 caught me by surprise tbh. Do we always have the network interaction ids be 1, 2, 3, ...

TBH I don't remember, and I couldn't find it easily in the code.

Copy link
Member

Choose a reason for hiding this comment

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

I search the code, and we normally do

const onchainInteraction = executionState.networkInteractions.find(
    (interaction) => interaction.id === networkInteractionId
  );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The -1 is because i'm indexing it directly, not using a find. As far as I know, we've always started the networkInteraction ID's at 1


assertIgnitionInvariant(
networkInteraction.type === "ONCHAIN_INTERACTION",
"Expected network interaction to be an onchain interaction"
);

// this seems redundant, but we use it later to determine pending vs dropped status
const lastTxIndex = findLastIndex(
networkInteraction.transactions,
(tx) => tx.hash === message.transaction.hash
);

const transaction = networkInteraction.transactions[lastTxIndex];

switch (exState.type) {
case ExecutionSateType.DEPLOYMENT_EXECUTION_STATE: {
transactions.push({
type: exState.type,
from: exState.from,
txHash: transaction.hash,
status: getTransactionStatus(
transaction,
lastTxIndex === networkInteraction.transactions.length - 1
),
name: exState.contractName,
address:
transaction.receipt?.status === TransactionReceiptStatus.SUCCESS
? exState.result?.type === ExecutionResultType.SUCCESS
? exState.result.address
: undefined
: undefined,
params: exState.constructorArgs,
value: networkInteraction.value,
});

break;
}
case ExecutionSateType.CALL_EXECUTION_STATE: {
const artifact = await deploymentLoader.loadArtifact(
exState.artifactId
);

transactions.push({
type: exState.type,
from: exState.from,
txHash: transaction.hash,
status: getTransactionStatus(
transaction,
lastTxIndex === networkInteraction.transactions.length - 1
),
name: `${artifact.contractName}#${exState.functionName}`,
to: networkInteraction.to,
params: exState.args,
value: networkInteraction.value,
});

break;
}
case ExecutionSateType.SEND_DATA_EXECUTION_STATE: {
transactions.push({
type: exState.type,
from: exState.from,
txHash: transaction.hash,
status: getTransactionStatus(
transaction,
lastTxIndex === networkInteraction.transactions.length - 1
),
to: networkInteraction.to,
value: networkInteraction.value,
});

break;
}
}
}
Expand Down
Loading