@@ -3,9 +3,10 @@ import {
3
3
Transaction ,
4
4
TransactionError ,
5
5
} 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 " ;
8
8
import { createCustomError } from "../middleware/error" ;
9
+ import { getChainIdFromChain } from "./chain" ;
9
10
10
11
type SimulateTxRawParams = {
11
12
chainId : string ;
@@ -16,21 +17,24 @@ type SimulateTxRawParams = {
16
17
} ;
17
18
18
19
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 ) ,
25
35
} ) ;
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 ;
34
38
} ;
35
39
36
40
export type SimulateTxParams = {
0 commit comments