Skip to content

Commit c81e9fb

Browse files
authored
Reward manager init in typescript (#13238)
### Description Implement the reward manager init instruction in typescript in spl package. ### How Has This Been Tested? Tested with a script locally, see #13229. Successful tx: https://explorer.solana.com/tx/3aezu4NokWVfNqc7YDL5cJty71ogLdkGMiN9h2qfujRcwAwbrZ7VktXeyWNXEMvvgQUbNHg5BtdUdXXTuskMTXEo?cluster=mainnet-beta
1 parent ad02abb commit c81e9fb

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

packages/spl/src/reward-manager/RewardManagerProgram.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ import {
2525
DecodedCreateSenderPublicInstruction,
2626
DecodedDeleteSenderPublicInstruction,
2727
DecodedEvaluateAttestationsInstruction,
28+
DecodedInitRewardManagerInstruction,
2829
DecodedRewardManagerInstruction,
2930
DecodedSubmitAttestationsInstruction,
3031
EvaluateAttestationsInstructionData,
3132
EvaluateRewardAttestationsParams,
33+
InitRewardManagerInstructionData,
34+
InitRewardManagerParams,
3235
RewardManagerStateData,
3336
SubmitAttestationInstructionData,
3437
SubmitRewardAttestationParams,
@@ -51,6 +54,10 @@ export class RewardManagerProgram {
5154
)
5255

5356
public static readonly layouts = {
57+
initRewardManagerInstructionData: struct<InitRewardManagerInstructionData>([
58+
u8('instruction'),
59+
u8('minVotes')
60+
]),
5461
createSenderInstructionData: struct<CreateSenderInstructionData>([
5562
u8('instruction'),
5663
ethAddress('senderEthAddress'),
@@ -101,6 +108,76 @@ export class RewardManagerProgram {
101108
])
102109
}
103110

111+
public static createInitInstruction({
112+
rewardManagerState,
113+
tokenAccount,
114+
mint,
115+
manager,
116+
minVotes,
117+
rewardManagerProgramId = RewardManagerProgram.programId
118+
}: InitRewardManagerParams) {
119+
const data = Buffer.alloc(
120+
RewardManagerProgram.layouts.initRewardManagerInstructionData.span
121+
)
122+
RewardManagerProgram.layouts.initRewardManagerInstructionData.encode(
123+
{
124+
instruction: RewardManagerInstruction.Init,
125+
minVotes
126+
},
127+
data
128+
)
129+
130+
const authority = RewardManagerProgram.deriveAuthority({
131+
programId: rewardManagerProgramId,
132+
rewardManagerState
133+
})
134+
135+
const keys: AccountMeta[] = [
136+
{ pubkey: rewardManagerState, isSigner: false, isWritable: true },
137+
{ pubkey: tokenAccount, isSigner: false, isWritable: true },
138+
{ pubkey: mint, isSigner: false, isWritable: false },
139+
{ pubkey: manager, isSigner: false, isWritable: false },
140+
{ pubkey: authority, isSigner: false, isWritable: false },
141+
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
142+
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }
143+
]
144+
return new TransactionInstruction({
145+
programId: rewardManagerProgramId,
146+
keys,
147+
data
148+
})
149+
}
150+
151+
public static decodeInitInstruction({
152+
programId,
153+
keys: [
154+
rewardManagerState,
155+
tokenAccount,
156+
mint,
157+
manager,
158+
authority,
159+
tokenProgram,
160+
rent
161+
],
162+
data
163+
}: TransactionInstruction): DecodedInitRewardManagerInstruction {
164+
return {
165+
programId,
166+
keys: {
167+
rewardManagerState,
168+
tokenAccount,
169+
mint,
170+
manager,
171+
authority,
172+
tokenProgram,
173+
rent
174+
},
175+
data: RewardManagerProgram.layouts.initRewardManagerInstructionData.decode(
176+
data
177+
)
178+
}
179+
}
180+
104181
public static createSenderInstruction({
105182
senderEthAddress,
106183
operatorEthAddress,
@@ -434,6 +511,7 @@ export class RewardManagerProgram {
434511
): DecodedRewardManagerInstruction {
435512
switch (instruction.data[0]) {
436513
case RewardManagerInstruction.Init:
514+
return RewardManagerProgram.decodeInitInstruction(instruction)
437515
case RewardManagerInstruction.ChangeManagerAccount:
438516
throw new Error('Not Implemented')
439517
case RewardManagerInstruction.CreateSender:
@@ -461,6 +539,12 @@ export class RewardManagerProgram {
461539
}
462540
}
463541

542+
public static isInitInstruction(
543+
decoded: DecodedRewardManagerInstruction
544+
): decoded is DecodedInitRewardManagerInstruction {
545+
return decoded.data.instruction === RewardManagerInstruction.Init
546+
}
547+
464548
public static isCreateSenderInstruction(
465549
decoded: DecodedRewardManagerInstruction
466550
): decoded is DecodedCreateSenderInstruction {

packages/spl/src/reward-manager/types.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,49 @@ import { AccountMeta, PublicKey } from '@solana/web3.js'
22

33
import { RewardManagerInstruction } from './constants'
44

5+
export type InitRewardManagerParams = {
6+
/** The account to initialize as the reward manager state. */
7+
rewardManagerState: PublicKey
8+
/** The token account to hold rewards. */
9+
tokenAccount: PublicKey
10+
/** The mint for the token account. */
11+
mint: PublicKey
12+
/** The admin account that will manage the reward manager. */
13+
manager: PublicKey
14+
/** Minimum number of votes required to disburse rewards. */
15+
minVotes: number
16+
/** The programId of the Reward Manager Program. */
17+
rewardManagerProgramId?: PublicKey
18+
}
19+
20+
export type InitRewardManagerInstructionData = {
21+
/** The instruction identifier. */
22+
instruction: RewardManagerInstruction
23+
/** Minimum number of votes required to disburse rewards. */
24+
minVotes: number
25+
}
26+
27+
export type DecodedInitRewardManagerInstruction = {
28+
programId: PublicKey
29+
keys: {
30+
/** The account to initialize as the reward manager state. */
31+
rewardManagerState: AccountMeta
32+
/** The token account to hold rewards. */
33+
tokenAccount: AccountMeta
34+
/** The mint for the token account. */
35+
mint: AccountMeta
36+
/** The admin account that will manage the reward manager. */
37+
manager: AccountMeta
38+
/** The reward manager authority PDA. */
39+
authority: AccountMeta
40+
/** The SPL Token program. */
41+
tokenProgram: AccountMeta
42+
/** The rent sysvar account. */
43+
rent: AccountMeta
44+
}
45+
data: InitRewardManagerInstructionData
46+
}
47+
548
export type CreateRewardSenderParams = {
649
/** The node's Ethereum wallet address. */
750
senderEthAddress: string
@@ -259,6 +302,7 @@ export type DecodedEvaluateAttestationsInstruction = {
259302
}
260303

261304
export type DecodedRewardManagerInstruction =
305+
| DecodedInitRewardManagerInstruction
262306
| DecodedCreateSenderInstruction
263307
| DecodedCreateSenderPublicInstruction
264308
| DecodedDeleteSenderPublicInstruction

0 commit comments

Comments
 (0)