@@ -99,11 +99,27 @@ public static async Task<string> FetchAbi(ThirdwebClient client, string address,
99
99
public static async Task < T > Read < T > ( ThirdwebContract contract , string method , params object [ ] parameters )
100
100
{
101
101
var rpc = ThirdwebRPC . GetRpcInstance ( contract . Client , contract . Chain ) ;
102
-
103
102
var contractRaw = new Contract ( null , contract . Abi , contract . Address ) ;
103
+
104
104
var function = GetFunctionMatchSignature ( contractRaw , method , parameters ) ;
105
- var data = function . GetData ( parameters ) ;
105
+ if ( function == null )
106
+ {
107
+ if ( method . Contains ( "(" ) )
108
+ {
109
+ try
110
+ {
111
+ var canonicalSignature = ExtractCanonicalSignature ( method ) ;
112
+ var selector = Nethereum . Util . Sha3Keccack . Current . CalculateHash ( canonicalSignature ) [ ..8 ] ;
113
+ function = contractRaw . GetFunctionBySignature ( selector ) ;
114
+ }
115
+ catch
116
+ {
117
+ function = contractRaw . GetFunction ( method ) ;
118
+ }
119
+ }
120
+ }
106
121
122
+ var data = function . GetData ( parameters ) ;
107
123
var resultData = await rpc . SendRequestAsync < string > ( "eth_call" , new { to = contract . Address , data = data } , "latest" ) . ConfigureAwait ( false ) ;
108
124
109
125
return function . DecodeTypeOutput < T > ( resultData ) ;
@@ -122,6 +138,23 @@ public static async Task<ThirdwebTransaction> Prepare(IThirdwebWallet wallet, Th
122
138
{
123
139
var contractRaw = new Contract ( null , contract . Abi , contract . Address ) ;
124
140
var function = GetFunctionMatchSignature ( contractRaw , method , parameters ) ;
141
+ if ( function == null )
142
+ {
143
+ if ( method . Contains ( "(" ) )
144
+ {
145
+ try
146
+ {
147
+ var canonicalSignature = ExtractCanonicalSignature ( method ) ;
148
+ var selector = Nethereum . Util . Sha3Keccack . Current . CalculateHash ( canonicalSignature ) [ ..8 ] ;
149
+ function = contractRaw . GetFunctionBySignature ( selector ) ;
150
+ }
151
+ catch
152
+ {
153
+ function = contractRaw . GetFunction ( method ) ;
154
+ }
155
+ }
156
+ }
157
+
125
158
var data = function . GetData ( parameters ) ;
126
159
var transaction = new ThirdwebTransactionInput
127
160
{
@@ -171,5 +204,35 @@ private static Function GetFunctionMatchSignature(Contract contract, string func
171
204
}
172
205
return null ;
173
206
}
207
+
208
+ /// <summary>
209
+ /// Extracts the canonical signature from the specified method.
210
+ /// </summary>
211
+ /// <param name="method">The method to extract the signature from.</param>
212
+ /// <returns>The canonical signature.</returns>
213
+ /// <exception cref="ArgumentException"></exception>
214
+ private static string ExtractCanonicalSignature ( string method )
215
+ {
216
+ method = method . Split ( "returns" ) [ 0 ] ;
217
+ var startOfParameters = method . IndexOf ( '(' ) ;
218
+ if ( startOfParameters == - 1 )
219
+ {
220
+ throw new ArgumentException ( "Invalid function signature: Missing opening parenthesis." ) ;
221
+ }
222
+
223
+ var endOfParameters = method . LastIndexOf ( ')' ) ;
224
+ if ( endOfParameters == - 1 )
225
+ {
226
+ throw new ArgumentException ( "Invalid function signature: Missing closing parenthesis." ) ;
227
+ }
228
+
229
+ var functionName = method . Substring ( 0 , startOfParameters ) . Trim ( ) . Split ( ' ' ) . Last ( ) ; // Get the last part after any spaces (in case of "function name(...)")
230
+ var parameters = method . Substring ( startOfParameters + 1 , endOfParameters - startOfParameters - 1 ) ;
231
+
232
+ var paramTypes = parameters . Split ( ',' ) . Select ( param => param . Trim ( ) . Split ( ' ' ) [ 0 ] ) . ToArray ( ) ;
233
+
234
+ var canonicalSignature = $ "{ functionName } ({ string . Join ( "," , paramTypes ) } )";
235
+ return canonicalSignature ;
236
+ }
174
237
}
175
238
}
0 commit comments