|
@@ -24,8 +24,6 @@ import {
|
|
import { IdlError, ProgramError } from "./error";
|
|
import { IdlError, ProgramError } from "./error";
|
|
import Coder, {
|
|
import Coder, {
|
|
ACCOUNT_DISCRIMINATOR_SIZE,
|
|
ACCOUNT_DISCRIMINATOR_SIZE,
|
|
- SIGHASH_STATE_NAMESPACE,
|
|
|
|
- SIGHASH_GLOBAL_NAMESPACE,
|
|
|
|
accountDiscriminator,
|
|
accountDiscriminator,
|
|
stateDiscriminator,
|
|
stateDiscriminator,
|
|
accountSize,
|
|
accountSize,
|
|
@@ -68,8 +66,7 @@ export type RpcFn = (...args: any[]) => Promise<TransactionSignature>;
|
|
/**
|
|
/**
|
|
* Ix is a function to create a `TransactionInstruction` generated from an IDL.
|
|
* Ix is a function to create a `TransactionInstruction` generated from an IDL.
|
|
*/
|
|
*/
|
|
-export type IxFn = IxProps & ((...args: any[]) => TransactionInstruction);
|
|
|
|
-
|
|
|
|
|
|
+export type IxFn = IxProps & ((...args: any[]) => any);
|
|
type IxProps = {
|
|
type IxProps = {
|
|
accounts: (ctx: RpcAccounts) => any;
|
|
accounts: (ctx: RpcAccounts) => any;
|
|
};
|
|
};
|
|
@@ -220,23 +217,36 @@ export class RpcFactory {
|
|
|
|
|
|
// Namespace with all rpc functions.
|
|
// Namespace with all rpc functions.
|
|
const rpc: Rpcs = {};
|
|
const rpc: Rpcs = {};
|
|
|
|
+ const ix: Ixs = {};
|
|
|
|
+
|
|
idl.state.methods.forEach((m: IdlStateMethod) => {
|
|
idl.state.methods.forEach((m: IdlStateMethod) => {
|
|
- rpc[m.name] = async (...args: any[]): Promise<TransactionSignature> => {
|
|
|
|
|
|
+ const accounts = async (accounts: RpcAccounts): Promise<any> => {
|
|
|
|
+ const keys = await stateInstructionKeys(
|
|
|
|
+ programId,
|
|
|
|
+ provider,
|
|
|
|
+ m,
|
|
|
|
+ accounts
|
|
|
|
+ );
|
|
|
|
+ return keys.concat(RpcFactory.accountsArray(accounts, m.accounts));
|
|
|
|
+ };
|
|
|
|
+ const ixFn = async (...args: any[]): Promise<TransactionInstruction> => {
|
|
const [ixArgs, ctx] = splitArgsAndCtx(m, [...args]);
|
|
const [ixArgs, ctx] = splitArgsAndCtx(m, [...args]);
|
|
- const keys = await stateInstructionKeys(programId, provider, m, ctx);
|
|
|
|
|
|
+ return new TransactionInstruction({
|
|
|
|
+ keys: await accounts(ctx.accounts),
|
|
|
|
+ programId,
|
|
|
|
+ data: coder.instruction.encodeState(
|
|
|
|
+ m.name,
|
|
|
|
+ toInstruction(m, ...ixArgs)
|
|
|
|
+ ),
|
|
|
|
+ });
|
|
|
|
+ };
|
|
|
|
+ ixFn["accounts"] = accounts;
|
|
|
|
+ ix[m.name] = ixFn;
|
|
|
|
+
|
|
|
|
+ rpc[m.name] = async (...args: any[]): Promise<TransactionSignature> => {
|
|
|
|
+ const [_, ctx] = splitArgsAndCtx(m, [...args]);
|
|
const tx = new Transaction();
|
|
const tx = new Transaction();
|
|
- tx.add(
|
|
|
|
- new TransactionInstruction({
|
|
|
|
- keys: keys.concat(
|
|
|
|
- RpcFactory.accountsArray(ctx.accounts, m.accounts)
|
|
|
|
- ),
|
|
|
|
- programId,
|
|
|
|
- data: coder.instruction.encodeState(
|
|
|
|
- m.name,
|
|
|
|
- toInstruction(m, ...ixArgs)
|
|
|
|
- ),
|
|
|
|
- })
|
|
|
|
- );
|
|
|
|
|
|
+ tx.add(await ix[m.name](...args));
|
|
try {
|
|
try {
|
|
const txSig = await provider.send(tx, ctx.signers, ctx.options);
|
|
const txSig = await provider.send(tx, ctx.signers, ctx.options);
|
|
return txSig;
|
|
return txSig;
|
|
@@ -249,8 +259,9 @@ export class RpcFactory {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
});
|
|
});
|
|
- state["rpc"] = rpc;
|
|
|
|
|
|
|
|
|
|
+ state["rpc"] = rpc;
|
|
|
|
+ state["instruction"] = ix;
|
|
// Calculates the address of the program's global state object account.
|
|
// Calculates the address of the program's global state object account.
|
|
state["address"] = async (): Promise<PublicKey> =>
|
|
state["address"] = async (): Promise<PublicKey> =>
|
|
programStateAddress(programId);
|
|
programStateAddress(programId);
|
|
@@ -655,7 +666,7 @@ async function stateInstructionKeys(
|
|
programId: PublicKey,
|
|
programId: PublicKey,
|
|
provider: Provider,
|
|
provider: Provider,
|
|
m: IdlStateMethod,
|
|
m: IdlStateMethod,
|
|
- ctx: RpcContext
|
|
|
|
|
|
+ accounts: RpcAccounts
|
|
) {
|
|
) {
|
|
if (m.name === "new") {
|
|
if (m.name === "new") {
|
|
// Ctor `new` method.
|
|
// Ctor `new` method.
|
|
@@ -689,7 +700,7 @@ async function stateInstructionKeys(
|
|
},
|
|
},
|
|
];
|
|
];
|
|
} else {
|
|
} else {
|
|
- validateAccounts(m.accounts, ctx.accounts);
|
|
|
|
|
|
+ validateAccounts(m.accounts, accounts);
|
|
return [
|
|
return [
|
|
{
|
|
{
|
|
pubkey: await programStateAddress(programId),
|
|
pubkey: await programStateAddress(programId),
|