11using System ;
22using System . Collections . Generic ;
3+ using System . Windows . Documents ;
34using System . IO ;
45using System . Security . Cryptography ;
56using System . Linq ;
1819using Wpc = Wpf . Ui . Controls ;
1920using Nethereum . Util ;
2021
21- using static Stratis . DevEx . Result ;
22+ using Stratis . DevEx ;
2223using Stratis . DevEx . Ethereum ;
2324using Stratis . VS . StratisEVM . UI . ViewModel ;
24- using Stratis . DevEx ;
25- using System . Windows . Documents ;
25+ using static Stratis . DevEx . Result ;
26+ using Nethereum . Hex . HexTypes ;
2627
2728namespace Stratis . VS . StratisEVM . UI
2829{
@@ -923,7 +924,7 @@ private async void RunContractCmd_Executed(object sender, ExecutedRoutedEventArg
923924 } ;
924925 var formPanel = ( StackPanel ) ( _sp ) . Children [ 2 ] ;
925926 var statusPanel = ( ( StackPanel ) ( _sp ) . Children [ 3 ] ) ;
926- await CreateRunContractFormAsync ( formPanel , statusPanel , item . Data , transactCheckBox , fromAddressTextBox ) ;
927+ await CreateRunContractFormAsync ( formPanel , statusPanel , item . Data , transactCheckBox , fromAddressTextBox , ( ) => ( estimateGasRadioButton . IsChecked ?? false ) ? null : new HexBigInteger ( ( long ) customGasNumberBox . Value ) ) ;
927928 dw . ButtonClicked += ( cd , args ) => { } ;
928929 dw . Closing += ( d , args ) => { } ;
929930 await dw . ShowAsync ( ) ;
@@ -987,7 +988,7 @@ private void ShowValidationSuccess(StackPanel successPanel, Wpc.TextBlock succes
987988
988989 private void HideValidationSuccess ( StackPanel successPanel ) => successPanel . Visibility = Visibility . Hidden ;
989990
990- private async Task CreateRunContractFormAsync ( StackPanel form , StackPanel statusPanel , Dictionary < string , object > contractData , CheckBox transactCheckBox , Wpc . TextBox fromAddress )
991+ private async Task CreateRunContractFormAsync ( StackPanel form , StackPanel statusPanel , Dictionary < string , object > contractData , CheckBox transactCheckBox , Wpc . TextBox fromAddress , Func < HexBigInteger > gas )
991992 {
992993 form . Children . Clear ( ) ;
993994 var errors = ( Wpc . TextBlock ) ( ( Grid ) statusPanel . Children [ 0 ] ) . Children [ 0 ] ;
@@ -1027,9 +1028,7 @@ private async Task CreateRunContractFormAsync(StackPanel form, StackPanel status
10271028 ShowValidationErrors ( errors , $ "Could not retrieve balance for contract. { balr . FailureMessage } ") ;
10281029 return ;
10291030 }
1030-
1031-
1032-
1031+
10331032 foreach ( var function in _abi . Functions )
10341033 {
10351034 var vsp = new StackPanel ( )
@@ -1078,8 +1077,14 @@ private async Task CreateRunContractFormAsync(StackPanel form, StackPanel status
10781077
10791078 button . Click += ( s , e ) =>
10801079 {
1081- var paramVals = GetContractFunctionParams ( vsp , function . InputParameters . ToDictionary ( ip => ip . Name , ip => ip . Type ) ) ;
1082- if ( paramVals . Length != function . InputParameters . Count ( ) )
1080+ var paramVals = GetContractFunctionParams ( vsp , function . InputParameters . ToDictionary ( ip => ip . Name , ip => ip . Type ) , out string paramError ) ;
1081+ if ( ! string . IsNullOrEmpty ( paramError ) )
1082+ {
1083+ ShowValidationErrors ( errors , $ "Error parsing function parameters: \n { paramError } ") ;
1084+ VSUtil . LogToStratisEVMWindow ( $ "\n ========== Call contract { address } at { rpcurl } failed.==========\n Error parsing function parameters: { paramError } ") ;
1085+ return ;
1086+ }
1087+ else if ( paramVals . Length != function . InputParameters . Count ( ) )
10831088 {
10841089 ShowValidationErrors ( errors , $ "The { function . Name } function requires { function . InputParameters . Count ( ) } parameters.") ;
10851090 VSUtil . LogToStratisEVMWindow ( $ "\n ========== Call contract { address } at { rpcurl } failed.==========\n The { function . Name } function requires { function . InputParameters . Count ( ) } parameters.") ;
@@ -1098,7 +1103,7 @@ private async Task CreateRunContractFormAsync(StackPanel form, StackPanel status
10981103 ShowValidationErrors ( errors , "Enter a valid from address to send the transaction from." ) ;
10991104 return ;
11001105 }
1101- r = ThreadHelper . JoinableTaskFactory . Run ( ( ) => ExecuteAsync ( Network . SendContractTransactionAsync ( rpcurl , address , abi , function . Name , fromAddress . Text , functionInput : paramVals ) ) ) ;
1106+ r = ThreadHelper . JoinableTaskFactory . Run ( ( ) => ExecuteAsync ( Network . SendContractTransactionAsync ( rpcurl , address , abi , function . Name , fromAddress . Text , gas : gas ( ) , functionInput : paramVals ) ) ) ;
11021107 }
11031108 else
11041109 {
@@ -1134,7 +1139,7 @@ private async Task CreateRunContractFormAsync(StackPanel form, StackPanel status
11341139 ShowValidationErrors ( errors , "Enter a valid from address to send the transaction from." ) ;
11351140 return ;
11361141 }
1137- r = ThreadHelper . JoinableTaskFactory . Run ( ( ) => ExecuteAsync ( Network . SendContractTransactionAsync ( rpcurl , address , abi , function . Name , fromAddress . Text ) ) ) ;
1142+ r = ThreadHelper . JoinableTaskFactory . Run ( ( ) => ExecuteAsync ( Network . SendContractTransactionAsync ( rpcurl , address , abi , function . Name , fromAddress . Text , gas : gas ( ) ) ) ) ;
11381143 }
11391144 else
11401145 {
@@ -1162,8 +1167,9 @@ private async Task CreateRunContractFormAsync(StackPanel form, StackPanel status
11621167 }
11631168 }
11641169
1165- private object [ ] GetContractFunctionParams ( StackPanel form , Dictionary < string , string > paramTypes )
1170+ private object [ ] GetContractFunctionParams ( StackPanel form , Dictionary < string , string > paramTypes , out string error )
11661171 {
1172+ error = null ;
11671173 Dictionary < string , ( string , string ) > paramValues = new Dictionary < string , ( string , string ) > ( ) ;
11681174 foreach ( var child in form . Children )
11691175 {
@@ -1176,7 +1182,15 @@ private object[] GetContractFunctionParams(StackPanel form, Dictionary<string, s
11761182 }
11771183 }
11781184 }
1179- return Contract . ParseFunctionParameterValues ( paramValues ) ;
1185+ try
1186+ {
1187+ return Contract . ParseFunctionParameterValues ( paramValues ) ;
1188+ }
1189+ catch ( Exception ex )
1190+ {
1191+ error = ex . Message ;
1192+ return Array . Empty < object > ( ) ;
1193+ }
11801194 }
11811195 #endregion
11821196
0 commit comments