Skip to content

Commit

Permalink
fix: use v5 SDK for simulating raw tx (thirdweb-dev#606)
Browse files Browse the repository at this point in the history
* fix: use v5 SDK for simulating raw tx
this fixes broken behaviour for simulating methods that return data, which was being misidentified as error data in v4

* chore: fix comments
  • Loading branch information
d4mr authored Aug 14, 2024
1 parent a4b8e38 commit 59ad3ba
Show file tree
Hide file tree
Showing 3 changed files with 314 additions and 238 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"pino-pretty": "^10.0.0",
"prisma": "^5.14.0",
"superjson": "^2.2.1",
"thirdweb": "^5.39.0",
"thirdweb": "^5.45.1",
"uuid": "^9.0.1",
"viem": "^1.14.0",
"zod": "^3.23.8"
Expand Down
36 changes: 20 additions & 16 deletions src/server/utils/simulateTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
Transaction,
TransactionError,
} from "@thirdweb-dev/sdk";
import { ethers } from "ethers";
import { getSdk } from "../../utils/cache/getSdk";
import { Hex, defineChain, getAddress, simulateTransaction } from "thirdweb";
import { thirdwebClient } from "../../utils/sdk";
import { createCustomError } from "../middleware/error";
import { getChainIdFromChain } from "./chain";

type SimulateTxRawParams = {
chainId: string;
Expand All @@ -16,21 +17,24 @@ type SimulateTxRawParams = {
};

const simulateTxRaw = async (args: SimulateTxRawParams) => {
const sdk = await getSdk({ chainId: parseInt(args.chainId) });
const simulateResult = await sdk.getProvider().call({
to: `${args.toAddress}`,
from: `${args.fromAddress}`,
data: `${args.data}`,
value: `${args.value}`,
const { chainId, toAddress, fromAddress, data, value } = args;
const chainIdParsed = await getChainIdFromChain(chainId);

if (!toAddress) throw new Error("toAddress is required");
if (!fromAddress) throw new Error("fromAddress is required");

// simulateTransaction throws if transaction reverts
const simulateResult = await simulateTransaction({
transaction: {
chain: defineChain(chainIdParsed),
to: getAddress(toAddress),
data: data as Hex,
client: thirdwebClient,
},
from: getAddress(fromAddress),
});
if (simulateResult.length > 2) {
// '0x' is the success result value
const decoded = ethers.utils.defaultAbiCoder.decode(
["string"],
ethers.utils.hexDataSlice(simulateResult, 4),
);
throw new Error(decoded[0]);
}

return simulateResult;
};

export type SimulateTxParams = {
Expand Down
Loading

0 comments on commit 59ad3ba

Please sign in to comment.