-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathupdateAuthority.ts
More file actions
100 lines (96 loc) · 3.25 KB
/
updateAuthority.ts
File metadata and controls
100 lines (96 loc) · 3.25 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
import {
AuthorityType,
getSetAuthorityInstruction,
getUpdateTokenMetadataUpdateAuthorityInstruction,
TOKEN_2022_PROGRAM_ADDRESS,
} from 'gill/programs/token';
import type {
Address,
Instruction,
FullTransaction,
TransactionVersion,
TransactionMessageWithFeePayer,
TransactionSigner,
Rpc,
SolanaRpcApi,
} from 'gill';
import { createTransaction } from 'gill';
type AuthorityRole = AuthorityType | 'Metadata';
/**
* Returns the appropriate instruction(s) to update the authority for a given mint and role.
*
* If the role is 'Metadata', this will return an instruction to update the metadata update authority.
* Otherwise, it returns a set authority instruction for the specified authority type.
*
* @param input - The parameters for the authority update.
* @param input.mint - The address of the mint whose authority is being updated.
* @param input.role - The authority role to update ('Metadata' or an AuthorityType).
* @param input.currentAuthority - The current authority signer.
* @param input.newAuthority - The new authority address.
* @returns An array containing the instruction to update the authority.
*/
export const getUpdateAuthorityInstructions = (input: {
mint: Address;
role: AuthorityRole;
currentAuthority: TransactionSigner<string>;
newAuthority: Address;
}): Instruction<string>[] => {
if (input.role === 'Metadata') {
return [
getUpdateTokenMetadataUpdateAuthorityInstruction(
{
metadata: input.mint,
updateAuthority: input.currentAuthority,
newUpdateAuthority: input.newAuthority,
},
{
programAddress: TOKEN_2022_PROGRAM_ADDRESS,
}
),
];
}
return [
getSetAuthorityInstruction({
owned: input.mint,
owner: input.currentAuthority.address,
newAuthority: input.newAuthority,
authorityType: input.role,
}),
];
};
/**
* Creates a transaction to update the authority for a given mint and role.
*
* This function fetches the latest blockhash, builds the appropriate instruction(s)
* using `getUpdateAuthorityInstructions`, and returns a full transaction ready to be signed and sent.
*
* @param input - The parameters for the authority update transaction.
* @param input.rpc - The Solana RPC client.
* @param input.payer - The transaction fee payer.
* @param input.mint - The address of the mint whose authority is being updated.
* @param input.role - The authority role to update ('Metadata' or an AuthorityType).
* @param input.currentAuthority - The current authority signer.
* @param input.newAuthority - The new authority address.
* @returns A promise that resolves to the constructed full transaction.
*/
export const getUpdateAuthorityTransaction = async (input: {
rpc: Rpc<SolanaRpcApi>;
payer: TransactionSigner<string>;
mint: Address;
role: AuthorityRole;
currentAuthority: TransactionSigner<string>;
newAuthority: Address;
}): Promise<
FullTransaction<TransactionVersion, TransactionMessageWithFeePayer>
> => {
const instructions = getUpdateAuthorityInstructions(input);
const { value: latestBlockhash } = await input.rpc
.getLatestBlockhash()
.send();
return createTransaction({
feePayer: input.payer,
version: 'legacy',
latestBlockhash,
instructions,
});
};