Skip to content

Commit 627c275

Browse files
ts: Add instruction method to state namespace
1 parent 9fb42ce commit 627c275

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ incremented for features.
1818
* spl: Add shared memory api.
1919
* lang/attribute/access-control: Allow specifying multiple modifier functions.
2020
* lang/syn: Allow state structs that don't have a ctor or impl block (just trait implementations).
21+
* ts: Add instruction method to state namespace.
2122

2223
## [0.2.0] - 2021-02-08
2324

ts/src/rpc.ts

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import {
2424
import { IdlError, ProgramError } from "./error";
2525
import 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);
7370
type 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

Comments
 (0)