Skip to content

Commit

Permalink
Merge pull request #14 from Tenderly/update-transaction-handler
Browse files Browse the repository at this point in the history
Update transaction handler
  • Loading branch information
dzimiks authored Sep 23, 2024
2 parents 4e37806 + 6b29d50 commit d6af4b1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x, 18.x]
node-version: [16.x, 18.17]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -24,7 +24,7 @@ jobs:
- run: yarn lint
# - run: yarn test
- name: Cache snap build
if: ${{ matrix.node-version == '18.x' }}
if: ${{ matrix.node-version == '18.17' }}
uses: actions/cache@v3
with:
path: ./packages/snap/dist
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/tenderly/tenderly-snap.git"
},
"source": {
"shasum": "vNrNCKPLLktsGjVi1vgs3YKLy4zdPJZ4yMueXNGvWTA=",
"shasum": "5cRgUkIM5DUnrcysawb1az3in+dJ0tMwWWOG9PF1wSQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
5 changes: 4 additions & 1 deletion packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
*
* @param args - The request handler args as object.
* @param args.transaction - The transaction to handle.
* @param args.transactionOrigin - The transaction origin.
* @param args.transactionOrigin - The transaction origin.
* @param args.chainId - The chain ID of the transaction.
*/
export const onTransaction: OnTransactionHandler = async ({
transaction,
transactionOrigin,
chainId,
}) => {
if (!isObject(transaction) || !hasProperty(transaction, 'to')) {
return {
Expand All @@ -67,6 +69,7 @@ export const onTransaction: OnTransactionHandler = async ({
}

const simulationResponse = await simulate(
chainId,
transaction,
transactionOrigin || '',
);
Expand Down
17 changes: 9 additions & 8 deletions packages/snap/src/tenderly/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Json } from '@metamask/utils';
import { TenderlyApi, TenderlySnapVersion } from '../constants';
import { fetchCredentials, TenderlyCredentials } from './credentials-access';
import { formatResponse, formatSimulationUrl } from './formatter';
import { hex2int, requestSnapPrompt } from './utils';
import { hex2int, parseChainId, requestSnapPrompt } from './utils';

/**
* Updates the credentials associated with Tenderly project.
Expand Down Expand Up @@ -37,13 +37,19 @@ export async function fetchPublicTenderlyNetworks() {
* handles any errors returned by the API, and if there are no errors,
* formats the response received from the Tenderly API for output.
*
* @param chainIdOrigin - The chain ID of the transaction.
* @param transaction - The transaction to simulate.
* @param transactionOrigin - The origin of the transaction.
*/
export async function simulate(
chainIdOrigin: string,
transaction: { [key: string]: Json },
transactionOrigin: string,
): Promise<Panel> {
if (!chainIdOrigin) {
throw new Error('Chain ID is not provided.');
}

const credentials: TenderlyCredentials | null = await fetchCredentials();

if (!credentials) {
Expand All @@ -54,13 +60,8 @@ export async function simulate(
]);
}

// Get chain id
const chainId = await ethereum.request({ method: 'eth_chainId' });
const networkId = hex2int(chainId as string);

if (!chainId) {
throw new Error('Chain ID is not provided.');
}
const chainId: string = parseChainId(chainIdOrigin);
const networkId = hex2int(chainId);

// Fetch Tenderly-supported networks
const tenderlyNetworks = await fetchPublicTenderlyNetworks();
Expand Down
29 changes: 29 additions & 0 deletions packages/snap/src/tenderly/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,32 @@ export const isTenderlyDomain = (origin: string) => {
return false;
}
};

/**
* Parses a CAIP-2 compliant chain ID string and returns the formatted hexadecimal chain ID.
*
* This function takes a CAIP-2 chain ID string in the format "namespace:reference",
* validates the namespace, and for "eip155" namespace, formats the reference as a
* lowercase hexadecimal string prefixed with "0x".
*
* @param chainId - The CAIP-2 chain ID string to parse (e.g., "eip155:1").
* @returns The formatted hexadecimal chain ID (e.g., "0x1") for eip155 namespace.
* @throws {Error} If the input format is invalid, or processing fails.
* @example
* parseChainId("eip155:1") // returns "0x1"
* parseChainId("eip155:a86a") // returns "0xa86a"
* parseChainId("eip155:76adf1") // returns "0x76adf1"
*/
export const parseChainId = (chainId: string): string => {
try {
// Split the chainId string
const [, reference] = chainId.split(':');

// Append '0x' to the reference and return
return `0x${reference}`;
} catch (error) {
throw new Error(
`An unexpected error occurred while parsing CAIP-2 chainId (${chainId}): ${error.message}`,
);
}
};
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8657,9 +8657,9 @@ __metadata:
linkType: hard

"caniuse-lite@npm:^1.0.0, caniuse-lite@npm:^1.0.30001400, caniuse-lite@npm:^1.0.30001407":
version: 1.0.30001419
resolution: "caniuse-lite@npm:1.0.30001419"
checksum: 7a4dc2794a6773574b5aebcd1c9c0d56159654821714152d8a0b04e261e1522bfd3d86589b8406ce81c7bf5b706118b73b2cb85d577ae433e303dd48ac9ff65f
version: 1.0.30001660
resolution: "caniuse-lite@npm:1.0.30001660"
checksum: 8b2c5de2f5facd31980426afbba68238270984acfe8c1ae925b8b6480448eea2fae292f815674617e9170c730c8a238d7cc0db919f184dc0e3cd9bec18f5e5ad
languageName: node
linkType: hard

Expand Down

0 comments on commit d6af4b1

Please sign in to comment.