@@ -2,7 +2,7 @@ import assert from 'assert';
22import  {  BigNumber  }  from  '@ethersproject/bignumber' ; 
33import  {  Abi  }  from  './types' ; 
44import  {  getSelectorFromName  }  from  './utils' ; 
5- import  {  addTransaction  }  from  './starknet' ; 
5+ import  {  addTransaction ,   callContract  }  from  './starknet' ; 
66
77type  Args  =  {  [ inputName : string ] : string  |  string [ ]  } ; 
88type  Calldata  =  string [ ] ; 
@@ -56,19 +56,19 @@ export class Contract {
5656    } ) ; 
5757  } 
5858
59-   public  invoke ( method : string ,  args : Args  =  { } )  { 
60-     // ensure contract is connected 
61-     assert ( this . connectedTo  !==  null ,  'contract isnt connected to an address' ) ; 
62- 
59+   private  validateMethodAndArgs ( type : 'INVOKE'  |  'CALL' ,  method : string ,  args : Args  =  { } )  { 
6360    // ensure provided method exists 
6461    const  invokeableFunctionNames  =  this . abi 
6562      . filter ( ( abi )  =>  { 
6663        const  isView  =  abi . stateMutability  ===  'view' ; 
6764        const  isFunction  =  abi . type  ===  'function' ; 
68-         return  isFunction  &&  ! isView ; 
65+         return  isFunction  &&  type   ===   'INVOKE'  ?  ! isView  :  isView ; 
6966      } ) 
7067      . map ( ( abi )  =>  abi . name ) ; 
71-     assert ( invokeableFunctionNames . includes ( method ) ,  'invokeable method not found in abi' ) ; 
68+     assert ( 
69+       invokeableFunctionNames . includes ( method ) , 
70+       `${ type  ===  'INVOKE'  ? 'invokeable'  : 'viewable' }  
71+     ) ; 
7272
7373    // ensure args match abi type 
7474    const  methodAbi  =  this . abi . find ( ( abi )  =>  abi . name  ===  method ) ! ; 
@@ -94,6 +94,24 @@ export class Contract {
9494        } ) ; 
9595      } 
9696    } ) ; 
97+   } 
98+ 
99+   private  parseResponse ( method : string ,  response : ( string  |  string [ ] ) [ ] ) : Args  { 
100+     const  methodAbi  =  this . abi . find ( ( abi )  =>  abi . name  ===  method ) ! ; 
101+     return  methodAbi . outputs . reduce ( ( acc ,  output ,  i )  =>  { 
102+       return  { 
103+         ...acc , 
104+         [ output . name ] : response [ i ] , 
105+       } ; 
106+     } ,  { }  as  Args ) ; 
107+   } 
108+ 
109+   public  invoke ( method : string ,  args : Args  =  { } )  { 
110+     // ensure contract is connected 
111+     assert ( this . connectedTo  !==  null ,  'contract isnt connected to an address' ) ; 
112+ 
113+     // validate method and args 
114+     this . validateMethodAndArgs ( 'INVOKE' ,  method ,  args ) ; 
97115
98116    // compile calldata 
99117    const  entrypointSelector  =  getSelectorFromName ( method ) ; 
@@ -106,4 +124,22 @@ export class Contract {
106124      entry_point_selector : entrypointSelector , 
107125    } ) ; 
108126  } 
127+ 
128+   public  async  call ( method : string ,  args : Args  =  { } )  { 
129+     // ensure contract is connected 
130+     assert ( this . connectedTo  !==  null ,  'contract isnt connected to an address' ) ; 
131+ 
132+     // validate method and args 
133+     this . validateMethodAndArgs ( 'CALL' ,  method ,  args ) ; 
134+ 
135+     // compile calldata 
136+     const  entrypointSelector  =  getSelectorFromName ( method ) ; 
137+     const  calldata  =  Contract . compileCalldata ( args ) ; 
138+ 
139+     return  callContract ( { 
140+       contract_address : this . connectedTo , 
141+       calldata, 
142+       entry_point_selector : entrypointSelector , 
143+     } ) . then ( ( x )  =>  this . parseResponse ( method ,  x . result ) ) ; 
144+   } 
109145} 
0 commit comments