@@ -24,8 +24,6 @@ import {
2424import { IdlError , ProgramError } from "./error" ;
2525import Coder , {
2626 ACCOUNT_DISCRIMINATOR_SIZE ,
27- SIGHASH_STATE_NAMESPACE ,
28- SIGHASH_GLOBAL_NAMESPACE ,
2927 accountDiscriminator ,
3028 stateDiscriminator ,
3129 accountSize ,
@@ -68,8 +66,7 @@ export type RpcFn = (...args: any[]) => Promise<TransactionSignature>;
6866/**
6967 * Ix is a function to create a `TransactionInstruction` generated from an IDL.
7068 */
71- export type IxFn = IxProps & ( ( ...args : any [ ] ) => TransactionInstruction ) ;
72-
69+ export type IxFn = IxProps & ( ( ...args : any [ ] ) => any ) ;
7370type IxProps = {
7471 accounts : ( ctx : RpcAccounts ) => any ;
7572} ;
@@ -220,23 +217,36 @@ export class RpcFactory {
220217
221218 // Namespace with all rpc functions.
222219 const rpc : Rpcs = { } ;
220+ const ix : Ixs = { } ;
221+
223222 idl . state . methods . forEach ( ( m : IdlStateMethod ) => {
224- rpc [ m . name ] = async ( ...args : any [ ] ) : Promise < TransactionSignature > => {
223+ const accounts = async ( accounts : RpcAccounts ) : Promise < any > => {
224+ const keys = await stateInstructionKeys (
225+ programId ,
226+ provider ,
227+ m ,
228+ accounts
229+ ) ;
230+ return keys . concat ( RpcFactory . accountsArray ( accounts , m . accounts ) ) ;
231+ } ;
232+ const ixFn = async ( ...args : any [ ] ) : Promise < TransactionInstruction > => {
225233 const [ ixArgs , ctx ] = splitArgsAndCtx ( m , [ ...args ] ) ;
226- const keys = await stateInstructionKeys ( programId , provider , m , ctx ) ;
234+ return new TransactionInstruction ( {
235+ keys : await accounts ( ctx . accounts ) ,
236+ programId,
237+ data : coder . instruction . encodeState (
238+ m . name ,
239+ toInstruction ( m , ...ixArgs )
240+ ) ,
241+ } ) ;
242+ } ;
243+ ixFn [ "accounts" ] = accounts ;
244+ ix [ m . name ] = ixFn ;
245+
246+ rpc [ m . name ] = async ( ...args : any [ ] ) : Promise < TransactionSignature > => {
247+ const [ _ , ctx ] = splitArgsAndCtx ( m , [ ...args ] ) ;
227248 const tx = new Transaction ( ) ;
228- tx . add (
229- new TransactionInstruction ( {
230- keys : keys . concat (
231- RpcFactory . accountsArray ( ctx . accounts , m . accounts )
232- ) ,
233- programId,
234- data : coder . instruction . encodeState (
235- m . name ,
236- toInstruction ( m , ...ixArgs )
237- ) ,
238- } )
239- ) ;
249+ tx . add ( await ix [ m . name ] ( ...args ) ) ;
240250 try {
241251 const txSig = await provider . send ( tx , ctx . signers , ctx . options ) ;
242252 return txSig ;
@@ -249,8 +259,9 @@ export class RpcFactory {
249259 }
250260 } ;
251261 } ) ;
252- state [ "rpc" ] = rpc ;
253262
263+ state [ "rpc" ] = rpc ;
264+ state [ "instruction" ] = ix ;
254265 // Calculates the address of the program's global state object account.
255266 state [ "address" ] = async ( ) : Promise < PublicKey > =>
256267 programStateAddress ( programId ) ;
@@ -655,7 +666,7 @@ async function stateInstructionKeys(
655666 programId : PublicKey ,
656667 provider : Provider ,
657668 m : IdlStateMethod ,
658- ctx : RpcContext
669+ accounts : RpcAccounts
659670) {
660671 if ( m . name === "new" ) {
661672 // Ctor `new` method.
@@ -689,7 +700,7 @@ async function stateInstructionKeys(
689700 } ,
690701 ] ;
691702 } else {
692- validateAccounts ( m . accounts , ctx . accounts ) ;
703+ validateAccounts ( m . accounts , accounts ) ;
693704 return [
694705 {
695706 pubkey : await programStateAddress ( programId ) ,
0 commit comments