1- use cainome:: parser:: tokens:: { CompositeType , Function , Token } ;
1+ use cainome:: parser:: tokens:: { CompositeType , Function , StateMutability , Token } ;
22use convert_case:: { Case , Casing } ;
33use dojo_world:: contracts:: naming;
44
@@ -35,8 +35,9 @@ impl TsFunctionGenerator {
3535 contract_name : & str ,
3636 token : & Function ,
3737 ) -> String {
38- format ! (
39- "\t const {contract_name}_{} = async ({}) => {{
38+ match token. state_mutability {
39+ StateMutability :: External => format ! (
40+ "\t const {contract_name}_{} = async ({}) => {{
4041\t \t try {{
4142\t \t \t return await provider.execute(
4243\t \t \t \t snAccount,
@@ -51,15 +52,36 @@ impl TsFunctionGenerator {
5152\t \t \t console.error(error);
5253\t \t }}
5354\t }};\n ",
54- token. name. to_case( Case :: Camel ) ,
55- self . format_function_inputs( token) ,
56- token. name,
57- self . format_function_calldata( token)
58- )
55+ token. name. to_case( Case :: Camel ) ,
56+ self . format_function_inputs( token) ,
57+ token. name,
58+ self . format_function_calldata( token)
59+ ) ,
60+ StateMutability :: View => format ! (
61+ "\t const {contract_name}_{} = async ({}) => {{
62+ \t \t try {{
63+ \t \t \t return await provider.call(\" {namespace}\" , {{
64+ \t \t \t \t contractName: \" {contract_name}\" ,
65+ \t \t \t \t entrypoint: \" {}\" ,
66+ \t \t \t \t calldata: [{}],
67+ \t \t \t }});
68+ \t \t }} catch (error) {{
69+ \t \t \t console.error(error);
70+ \t \t }}
71+ \t }};\n ",
72+ token. name. to_case( Case :: Camel ) ,
73+ self . format_function_inputs( token) ,
74+ token. name,
75+ self . format_function_calldata( token)
76+ ) ,
77+ }
5978 }
6079
6180 fn format_function_inputs ( & self , token : & Function ) -> String {
62- let inputs = vec ! [ "snAccount: Account" . to_owned( ) ] ;
81+ let inputs = match token. state_mutability {
82+ StateMutability :: External => vec ! [ "snAccount: Account" . to_owned( ) ] ,
83+ StateMutability :: View => Vec :: new ( ) ,
84+ } ;
6385 token
6486 . inputs
6587 . iter ( )
@@ -273,6 +295,13 @@ mod tests {
273295 let expected = "snAccount: Account, value: BigNumberish" ;
274296 assert_eq ! ( expected, generator. format_function_inputs( & function) )
275297 }
298+ #[ test]
299+ fn test_format_function_inputs_view ( ) {
300+ let generator = TsFunctionGenerator { } ;
301+ let function = create_basic_view_function ( ) ;
302+ let expected = "" ;
303+ assert_eq ! ( expected, generator. format_function_inputs( & function) )
304+ }
276305
277306 #[ test]
278307 fn test_format_function_inputs_complex ( ) {
@@ -408,6 +437,17 @@ mod tests {
408437 ) ] ,
409438 )
410439 }
440+
441+ fn create_basic_view_function ( ) -> Function {
442+ Function {
443+ name : "allowance" . to_owned ( ) ,
444+ state_mutability : cainome:: parser:: tokens:: StateMutability :: View ,
445+ inputs : vec ! [ ] ,
446+ outputs : vec ! [ ] ,
447+ named_outputs : vec ! [ ] ,
448+ }
449+ }
450+
411451 fn create_change_theme_function_camelized ( ) -> Function {
412452 create_test_function (
413453 "changeTheme" ,
0 commit comments