Skip to content

Commit 59ad3ba

Browse files
authored
fix: use v5 SDK for simulating raw tx (#606)
* 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
1 parent a4b8e38 commit 59ad3ba

File tree

3 files changed

+314
-238
lines changed

3 files changed

+314
-238
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"pino-pretty": "^10.0.0",
6969
"prisma": "^5.14.0",
7070
"superjson": "^2.2.1",
71-
"thirdweb": "^5.39.0",
71+
"thirdweb": "^5.45.1",
7272
"uuid": "^9.0.1",
7373
"viem": "^1.14.0",
7474
"zod": "^3.23.8"

src/server/utils/simulateTx.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import {
33
Transaction,
44
TransactionError,
55
} from "@thirdweb-dev/sdk";
6-
import { ethers } from "ethers";
7-
import { getSdk } from "../../utils/cache/getSdk";
6+
import { Hex, defineChain, getAddress, simulateTransaction } from "thirdweb";
7+
import { thirdwebClient } from "../../utils/sdk";
88
import { createCustomError } from "../middleware/error";
9+
import { getChainIdFromChain } from "./chain";
910

1011
type SimulateTxRawParams = {
1112
chainId: string;
@@ -16,21 +17,24 @@ type SimulateTxRawParams = {
1617
};
1718

1819
const simulateTxRaw = async (args: SimulateTxRawParams) => {
19-
const sdk = await getSdk({ chainId: parseInt(args.chainId) });
20-
const simulateResult = await sdk.getProvider().call({
21-
to: `${args.toAddress}`,
22-
from: `${args.fromAddress}`,
23-
data: `${args.data}`,
24-
value: `${args.value}`,
20+
const { chainId, toAddress, fromAddress, data, value } = args;
21+
const chainIdParsed = await getChainIdFromChain(chainId);
22+
23+
if (!toAddress) throw new Error("toAddress is required");
24+
if (!fromAddress) throw new Error("fromAddress is required");
25+
26+
// simulateTransaction throws if transaction reverts
27+
const simulateResult = await simulateTransaction({
28+
transaction: {
29+
chain: defineChain(chainIdParsed),
30+
to: getAddress(toAddress),
31+
data: data as Hex,
32+
client: thirdwebClient,
33+
},
34+
from: getAddress(fromAddress),
2535
});
26-
if (simulateResult.length > 2) {
27-
// '0x' is the success result value
28-
const decoded = ethers.utils.defaultAbiCoder.decode(
29-
["string"],
30-
ethers.utils.hexDataSlice(simulateResult, 4),
31-
);
32-
throw new Error(decoded[0]);
33-
}
36+
37+
return simulateResult;
3438
};
3539

3640
export type SimulateTxParams = {

0 commit comments

Comments
 (0)