11import { Address } from '@solana/addresses' ;
2+ import { pipe } from '@solana/functional' ;
3+ import {
4+ InstructionPlan ,
5+ assertIsSingleTransactionPlan ,
6+ createTransactionPlanner ,
7+ parallelInstructionPlan ,
8+ sequentialInstructionPlan ,
9+ } from '@solana/instruction-plans' ;
10+ import {
11+ GetAccountInfoApi ,
12+ GetMinimumBalanceForRentExemptionApi ,
13+ GetStakeMinimumDelegationApi ,
14+ } from '@solana/rpc-api' ;
15+ import { Rpc } from '@solana/rpc-spec' ;
216import {
317 appendTransactionMessageInstruction ,
418 createTransactionMessage ,
19+ setTransactionMessageFeePayer ,
520 TransactionVersion ,
621 TransactionMessage ,
722} from '@solana/transaction-messages' ;
@@ -40,7 +55,7 @@ import {
4055} from './quarantine.js' ;
4156
4257interface DepositParams {
43- rpc : any ; // XXX Rpc<???>
58+ rpc : Rpc < GetAccountInfoApi & GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi > ;
4459 pool : PoolAddress ;
4560 userWallet : Address ;
4661 userStakeAccount ?: Address ;
@@ -52,7 +67,7 @@ interface DepositParams {
5267}
5368
5469interface WithdrawParams {
55- rpc : any ; // XXX Rpc<???>
70+ rpc : Rpc < GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi > ;
5671 pool : PoolAddress ;
5772 userWallet : Address ;
5873 userStakeAccount : Address ;
@@ -78,83 +93,72 @@ export const SinglePoolProgram = {
7893 createAndDelegateUserStake : createAndDelegateUserStakeTransaction ,
7994} ;
8095
81- export async function initializeTransaction (
82- rpc : any , // XXX not exported: Rpc<??? >,
96+ async function getInitializeInstructionPlan (
97+ rpc : Rpc < GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi > ,
8398 voteAccount : VoteAccountAddress ,
8499 payer : Address ,
85100 skipMetadata = false ,
86- ) : Promise < TransactionMessage > {
87- let transaction = createTransactionMessage ( { version : 0 } ) ;
88-
101+ ) : Promise < InstructionPlan > {
89102 const pool = await findPoolAddress ( SINGLE_POOL_PROGRAM_ID , voteAccount ) ;
90- const [ stake , mint , onramp , poolRent , stakeRent , mintRent , minimumDelegationObj ] =
91- await Promise . all ( [
92- findPoolStakeAddress ( SINGLE_POOL_PROGRAM_ID , pool ) ,
93- findPoolMintAddress ( SINGLE_POOL_PROGRAM_ID , pool ) ,
94- findPoolOnRampAddress ( SINGLE_POOL_PROGRAM_ID , pool ) ,
95- rpc . getMinimumBalanceForRentExemption ( SINGLE_POOL_ACCOUNT_SIZE ) . send ( ) ,
96- rpc . getMinimumBalanceForRentExemption ( STAKE_ACCOUNT_SIZE ) . send ( ) ,
97- rpc . getMinimumBalanceForRentExemption ( MINT_SIZE ) . send ( ) ,
98- rpc . getStakeMinimumDelegation ( ) . send ( ) ,
99- ] ) ;
103+ const [
104+ stake ,
105+ mint ,
106+ onramp ,
107+ poolRent ,
108+ stakeRent ,
109+ mintRent ,
110+ minimumDelegationObj ,
111+ initializePool ,
112+ initializeOnRamp ,
113+ ] = await Promise . all ( [
114+ findPoolStakeAddress ( SINGLE_POOL_PROGRAM_ID , pool ) ,
115+ findPoolMintAddress ( SINGLE_POOL_PROGRAM_ID , pool ) ,
116+ findPoolOnRampAddress ( SINGLE_POOL_PROGRAM_ID , pool ) ,
117+ rpc . getMinimumBalanceForRentExemption ( SINGLE_POOL_ACCOUNT_SIZE ) . send ( ) ,
118+ rpc . getMinimumBalanceForRentExemption ( STAKE_ACCOUNT_SIZE ) . send ( ) ,
119+ rpc . getMinimumBalanceForRentExemption ( MINT_SIZE ) . send ( ) ,
120+ rpc . getStakeMinimumDelegation ( ) . send ( ) ,
121+ initializePoolInstruction ( voteAccount ) ,
122+ initializeOnRampInstruction ( pool ) ,
123+ ] ) ;
100124 const lamportsPerSol = 1_000_000_000n ;
101125 const minimumPoolBalance =
102126 minimumDelegationObj . value > lamportsPerSol ? minimumDelegationObj . value : lamportsPerSol ;
103127
104- transaction = appendTransactionMessageInstruction (
105- SystemInstruction . transfer ( {
106- from : payer ,
107- to : pool ,
108- lamports : poolRent ,
109- } ) ,
110- transaction ,
111- ) ;
112-
113- transaction = appendTransactionMessageInstruction (
114- SystemInstruction . transfer ( {
115- from : payer ,
116- to : stake ,
117- lamports : stakeRent + minimumPoolBalance ,
118- } ) ,
119- transaction ,
120- ) ;
121-
122- transaction = appendTransactionMessageInstruction (
123- SystemInstruction . transfer ( {
124- from : payer ,
125- to : onramp ,
126- lamports : stakeRent ,
127- } ) ,
128- transaction ,
129- ) ;
130-
131- transaction = appendTransactionMessageInstruction (
132- SystemInstruction . transfer ( {
133- from : payer ,
134- to : mint ,
135- lamports : mintRent ,
136- } ) ,
137- transaction ,
138- ) ;
139-
140- transaction = appendTransactionMessageInstruction (
141- await initializePoolInstruction ( voteAccount ) ,
142- transaction ,
143- ) ;
144-
145- transaction = appendTransactionMessageInstruction (
146- await initializeOnRampInstruction ( pool ) ,
147- transaction ,
148- ) ;
149-
150- if ( ! skipMetadata ) {
151- transaction = appendTransactionMessageInstruction (
152- await createTokenMetadataInstruction ( pool , payer ) ,
153- transaction ,
154- ) ;
155- }
128+ return sequentialInstructionPlan ( [
129+ parallelInstructionPlan ( [
130+ SystemInstruction . transfer ( { from : payer , to : pool , lamports : poolRent } ) ,
131+ SystemInstruction . transfer ( {
132+ from : payer ,
133+ to : stake ,
134+ lamports : stakeRent + minimumPoolBalance ,
135+ } ) ,
136+ SystemInstruction . transfer ( { from : payer , to : onramp , lamports : stakeRent } ) ,
137+ SystemInstruction . transfer ( { from : payer , to : mint , lamports : mintRent } ) ,
138+ ] ) ,
139+ initializePool ,
140+ initializeOnRamp ,
141+ ...( skipMetadata ? [ ] : [ await createTokenMetadataInstruction ( pool , payer ) ] ) ,
142+ ] ) ;
143+ }
156144
157- return transaction ;
145+ export async function initializeTransaction (
146+ rpc : Rpc < GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi > ,
147+ voteAccount : VoteAccountAddress ,
148+ payer : Address ,
149+ skipMetadata = false ,
150+ ) : Promise < TransactionMessage > {
151+ const transactionPlanner = createTransactionPlanner ( {
152+ createTransactionMessage : ( ) =>
153+ pipe ( createTransactionMessage ( { version : 0 } ) , ( m ) =>
154+ setTransactionMessageFeePayer ( payer , m ) ,
155+ ) ,
156+ } ) ;
157+
158+ const instructionPlan = await getInitializeInstructionPlan ( rpc , voteAccount , payer , skipMetadata ) ;
159+ const transactionPlan = await transactionPlanner ( instructionPlan ) ;
160+ assertIsSingleTransactionPlan ( transactionPlan ) ;
161+ return transactionPlan . message ;
158162}
159163
160164export async function replenishPoolTransaction (
@@ -321,7 +325,7 @@ export async function updateTokenMetadataTransaction(
321325}
322326
323327export async function initializeOnRampTransaction (
324- rpc : any , // XXX not exported: Rpc<??? >,
328+ rpc : Rpc < GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi > ,
325329 pool : PoolAddress ,
326330 payer : Address ,
327331) : Promise < TransactionMessage > {
@@ -351,7 +355,7 @@ export async function initializeOnRampTransaction(
351355
352356/** @deprecated */
353357export async function createAndDelegateUserStakeTransaction (
354- rpc : any , // XXX not exported: Rpc<??? >,
358+ rpc : Rpc < GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi > ,
355359 voteAccount : VoteAccountAddress ,
356360 userWallet : Address ,
357361 stakeAmount : bigint ,
0 commit comments