generated from codama-idl/renderers-demo
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.ts
More file actions
125 lines (115 loc) · 3.74 KB
/
index.ts
File metadata and controls
125 lines (115 loc) · 3.74 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* This code was AUTOGENERATED using the Codama library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun Codama to update it.
*
* @see https://github.com/codama-idl/codama
*/
import {
AccountRole,
isProgramDerivedAddress,
isTransactionSigner as kitIsTransactionSigner,
type AccountMeta,
type AccountSignerMeta,
type Address,
type ProgramDerivedAddress,
type TransactionSigner,
upgradeRoleToSigner,
} from '@solana/kit';
/**
* Asserts that the given value is not null or undefined.
* @internal
*/
export function expectSome<T>(value: T | null | undefined): T {
if (value === null || value === undefined) {
throw new Error('Expected a value but received null or undefined.');
}
return value;
}
/**
* Asserts that the given value is a PublicKey.
* @internal
*/
export function expectAddress<T extends string = string>(
value: Address<T> | ProgramDerivedAddress<T> | TransactionSigner<T> | null | undefined,
): Address<T> {
if (!value) {
throw new Error('Expected a Address.');
}
if (typeof value === 'object' && 'address' in value) {
return value.address;
}
if (Array.isArray(value)) {
return value[0] as Address<T>;
}
return value as Address<T>;
}
/**
* Asserts that the given value is a PDA.
* @internal
*/
export function expectProgramDerivedAddress<T extends string = string>(
value: Address<T> | ProgramDerivedAddress<T> | TransactionSigner<T> | null | undefined,
): ProgramDerivedAddress<T> {
if (!value || !Array.isArray(value) || !isProgramDerivedAddress(value)) {
throw new Error('Expected a ProgramDerivedAddress.');
}
return value;
}
/**
* Asserts that the given value is a TransactionSigner.
* @internal
*/
export function expectTransactionSigner<T extends string = string>(
value: Address<T> | ProgramDerivedAddress<T> | TransactionSigner<T> | null | undefined,
): TransactionSigner<T> {
if (!value || !isTransactionSigner(value)) {
throw new Error('Expected a TransactionSigner.');
}
return value;
}
/**
* Defines an instruction account to resolve.
* @internal
*/
export type ResolvedAccount<
T extends string = string,
U extends Address<T> | ProgramDerivedAddress<T> | TransactionSigner<T> | null =
| Address<T>
| ProgramDerivedAddress<T>
| TransactionSigner<T>
| null,
> = {
isWritable: boolean;
value: U;
};
/**
* Defines an instruction that stores additional bytes on-chain.
* @internal
*/
export type InstructionWithByteDelta = {
byteDelta: number;
};
/**
* Get account metas and signers from resolved accounts.
* @internal
*/
export function getAccountMetaFactory(programAddress: Address, optionalAccountStrategy: 'omitted' | 'programId') {
return (account: ResolvedAccount): AccountMeta | AccountSignerMeta | undefined => {
if (!account.value) {
if (optionalAccountStrategy === 'omitted') return;
return Object.freeze({ address: programAddress, role: AccountRole.READONLY });
}
const writableRole = account.isWritable ? AccountRole.WRITABLE : AccountRole.READONLY;
return Object.freeze({
address: expectAddress(account.value),
role: isTransactionSigner(account.value) ? upgradeRoleToSigner(writableRole) : writableRole,
...(isTransactionSigner(account.value) ? { signer: account.value } : {}),
});
};
}
export function isTransactionSigner<TAddress extends string = string>(
value: Address<TAddress> | ProgramDerivedAddress<TAddress> | TransactionSigner<TAddress>,
): value is TransactionSigner<TAddress> {
return !!value && typeof value === 'object' && 'address' in value && kitIsTransactionSigner(value);
}