-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate names for common Flow CLI transactions (#182)
* fix interaction source sanitization before compare * hardcode templates for common flow-cli transactions * fix interaction name when forked from history * display tx name in transaction tables
- Loading branch information
1 parent
193e973
commit a261c0b
Showing
7 changed files
with
133 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
frontend/src/pages/interactions/hooks/use-interaction-name.ts
This file was deleted.
Oops, something went wrong.
106 changes: 106 additions & 0 deletions
106
frontend/src/pages/interactions/hooks/use-transaction-name.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { useInteractionRegistry } from "../contexts/interaction-registry.context"; | ||
import { useMemo } from "react"; | ||
import { Transaction } from "@flowser/shared"; | ||
|
||
type UseInteractionNameProps = { | ||
transaction: Transaction | undefined; | ||
}; | ||
|
||
enum TransactionKind { | ||
DEPLOY_CONTRACT, | ||
INITIALIZE_ACCOUNT, | ||
} | ||
|
||
const hardcodedTemplates: [string, TransactionKind][] = [ | ||
[ | ||
`transaction(name: String, code: String ) { | ||
prepare(signer: AuthAccount) { | ||
signer.contracts.add(name: name, code: code.decodeHex() ) | ||
} | ||
}`, | ||
TransactionKind.DEPLOY_CONTRACT, | ||
], | ||
[ | ||
`import Crypto | ||
transaction(publicKeys: [Crypto.KeyListEntry], contracts: {String: String}) { | ||
prepare(signer: AuthAccount) { | ||
let account = AuthAccount(payer: signer) | ||
// add all the keys to the account | ||
for key in publicKeys { | ||
account.keys.add(publicKey: key.publicKey, hashAlgorithm: key.hashAlgorithm, weight: key.weight) | ||
} | ||
// add contracts if provided | ||
for contract in contracts.keys { | ||
account.contracts.add(name: contract, code: contracts[contract]!.decodeHex()) | ||
} | ||
} | ||
}`, | ||
TransactionKind.INITIALIZE_ACCOUNT, | ||
], | ||
]; | ||
|
||
const transactionKindBySource = new Map<string, TransactionKind>( | ||
hardcodedTemplates.map((entry) => [sanitizeCadenceSource(entry[0]), entry[1]]) | ||
); | ||
|
||
export function useTransactionName( | ||
props: UseInteractionNameProps | ||
): string | undefined { | ||
const { transaction } = props; | ||
const { templates } = useInteractionRegistry(); | ||
|
||
return useMemo(() => { | ||
if (!transaction?.script) { | ||
return undefined; | ||
} | ||
|
||
const sanitizedTargetCode = sanitizeCadenceSource(transaction.script); | ||
const matchingTemplateName = templates.find( | ||
(template) => | ||
template.code && | ||
sanitizeCadenceSource(template.code) === sanitizedTargetCode | ||
)?.name; | ||
|
||
return matchingTemplateName ?? getDynamicName(transaction); | ||
}, [transaction, templates]); | ||
} | ||
|
||
function getDynamicName(transaction: Transaction) { | ||
const kind = transactionKindBySource.get( | ||
sanitizeCadenceSource(transaction.script) | ||
); | ||
|
||
switch (kind) { | ||
case TransactionKind.DEPLOY_CONTRACT: | ||
return `Deploy ${ | ||
getArgumentValueById(transaction, "name") ?? "contract" | ||
}`; | ||
case TransactionKind.INITIALIZE_ACCOUNT: | ||
return "Init signer account"; | ||
default: | ||
return undefined; | ||
} | ||
} | ||
|
||
function getArgumentValueById(transaction: Transaction, id: string) { | ||
return JSON.parse( | ||
transaction.arguments.find((argument) => argument.identifier === id) | ||
?.valueAsJson ?? "" | ||
); | ||
} | ||
|
||
function sanitizeCadenceSource(code: string) { | ||
// Ignore imports for comparison, | ||
// since those can differ due to address replacement. | ||
// See: https://developers.flow.com/tooling/fcl-js/api#address-replacement | ||
const strippedImports = code | ||
.split("\n") | ||
.filter((line) => !line.startsWith("import")) | ||
.join("\n"); | ||
|
||
// Replace all whitespace and newlines | ||
return strippedImports.replaceAll(/[\n\t ]/g, ""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,17 @@ | ||
import React, { FunctionComponent, useEffect } from "react"; | ||
import classes from "./Main.module.scss"; | ||
import { useNavigation } from "../../../hooks/use-navigation"; | ||
import { useGetPollingTransactions } from "../../../hooks/use-api"; | ||
import { createColumnHelper } from "@tanstack/table-core"; | ||
import { Transaction } from "@flowser/shared"; | ||
import Label from "../../../components/label/Label"; | ||
import Value from "../../../components/value/Value"; | ||
import { NavLink } from "react-router-dom"; | ||
import MiddleEllipsis from "../../../components/ellipsis/MiddleEllipsis"; | ||
import Table from "../../../components/table/Table"; | ||
import { ExecutionStatus } from "components/status/ExecutionStatus"; | ||
import { GrcpStatus } from "../../../components/status/GrcpStatus"; | ||
import ReactTimeago from "react-timeago"; | ||
import { DecoratedPollingEntity } from "contexts/timeout-polling.context"; | ||
import { AccountLink } from "../../../components/account/link/AccountLink"; | ||
|
||
// TRANSACTIONS TABLE | ||
const columnHelper = createColumnHelper<DecoratedPollingEntity<Transaction>>(); | ||
|
||
export const transactionTableColumns = [ | ||
columnHelper.accessor("id", { | ||
header: () => <Label variant="medium">ID</Label>, | ||
cell: (info) => ( | ||
<Value> | ||
<NavLink to={`/transactions/details/${info.getValue()}`}> | ||
<MiddleEllipsis className={classes.hash}> | ||
{info.getValue()} | ||
</MiddleEllipsis> | ||
</NavLink> | ||
</Value> | ||
), | ||
}), | ||
columnHelper.accessor("blockId", { | ||
header: () => <Label variant="medium">BLOCK ID</Label>, | ||
cell: (info) => ( | ||
<Value> | ||
<NavLink to={`/blocks/details/${info.getValue()}`}> | ||
<MiddleEllipsis className={classes.hash}> | ||
{info.getValue()} | ||
</MiddleEllipsis> | ||
</NavLink> | ||
</Value> | ||
), | ||
}), | ||
columnHelper.accessor("payer", { | ||
header: () => <Label variant="medium">PAYER</Label>, | ||
cell: (info) => ( | ||
<Value> | ||
<AccountLink address={info.getValue()} /> | ||
</Value> | ||
), | ||
}), | ||
columnHelper.accessor("status.executionStatus", { | ||
header: () => <Label variant="medium">EXECUTION</Label>, | ||
cell: (info) => ( | ||
<div> | ||
<ExecutionStatus status={info.row.original.status} /> | ||
</div> | ||
), | ||
}), | ||
columnHelper.accessor("status.grcpStatus", { | ||
header: () => <Label variant="medium">API</Label>, | ||
cell: (info) => ( | ||
<div> | ||
<GrcpStatus status={info.row.original.status} /> | ||
</div> | ||
), | ||
}), | ||
columnHelper.accessor("createdAt", { | ||
header: () => <Label variant="medium">CREATED</Label>, | ||
cell: (info) => ( | ||
<Value> | ||
<ReactTimeago date={info.getValue()} /> | ||
</Value> | ||
), | ||
}), | ||
]; | ||
import { TransactionsTable } from "./TransactionsTable"; | ||
|
||
const Main: FunctionComponent = () => { | ||
const { showNavigationDrawer } = useNavigation(); | ||
const { data, firstFetch, error } = useGetPollingTransactions(); | ||
const { data } = useGetPollingTransactions(); | ||
|
||
useEffect(() => { | ||
showNavigationDrawer(false); | ||
}, []); | ||
|
||
return ( | ||
<Table<DecoratedPollingEntity<Transaction>> | ||
isInitialLoading={firstFetch} | ||
error={error} | ||
data={data} | ||
columns={transactionTableColumns} | ||
/> | ||
); | ||
return <TransactionsTable transactions={data} />; | ||
}; | ||
|
||
export default Main; |
File renamed without changes.
Oops, something went wrong.