forked from solana-foundation/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.ts
98 lines (85 loc) · 2.5 KB
/
context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {
AccountMeta,
Signer,
ConfirmOptions,
TransactionInstruction,
} from "@solana/web3.js";
import { Address } from "./common";
import { IdlAccountItem, IdlAccounts, IdlInstruction } from "../idl";
/**
* Context provides all non-argument inputs for generating Anchor transactions.
*/
export type Context<A extends Accounts = Accounts> = {
/**
* Accounts used in the instruction context.
*/
accounts?: A;
/**
* All accounts to pass into an instruction *after* the main `accounts`.
* This can be used for optional or otherwise unknown accounts.
*/
remainingAccounts?: AccountMeta[];
/**
* Accounts that must sign a given transaction.
*/
signers?: Array<Signer>;
/**
* @deprecated use preInstructions instead.
* Instructions to run *before* a given method. Often this is used, for
* example to create accounts prior to executing a method.
*/
instructions?: TransactionInstruction[];
/**
* Instructions to run *before* a given method. Often this is used, for
* example to create accounts prior to executing a method.
*/
preInstructions?: TransactionInstruction[];
/**
* Instructions to run *after* a given method. Often this is used, for
* example to close accounts after executing a method.
*/
postInstructions?: TransactionInstruction[];
/**
* Commitment parameters to use for a transaction.
*/
options?: ConfirmOptions;
/**
* @hidden
*
* Private namespace for development.
*/
__private?: { logAccounts: boolean };
};
/**
* A set of accounts mapping one-to-one to the program's accounts struct, i.e.,
* the type deriving `#[derive(Accounts)]`.
*
* The name of each field should match the name for that account in the IDL.
*
* If multiple accounts are nested in the rust program, then they should be
* nested here.
*/
export type Accounts<A extends IdlAccountItem = IdlAccountItem> = {
[N in A["name"]]: Account<A & { name: N }>;
};
type Account<A extends IdlAccountItem> = A extends IdlAccounts
? Accounts<A["accounts"][number]>
: Address;
export function splitArgsAndCtx(
idlIx: IdlInstruction,
args: any[]
): [any[], Context] {
let options = {};
const inputLen = idlIx.args ? idlIx.args.length : 0;
if (args.length > inputLen) {
if (args.length !== inputLen + 1) {
throw new Error(
`provided too many arguments ${args} to instruction ${
idlIx?.name
} expecting: ${idlIx.args?.map((a) => a.name) ?? []}`
);
}
options = args.pop();
}
return [args, options];
}