@@ -23,6 +23,7 @@ import {
2323 type Instruction ,
2424 type InstructionWithAccounts ,
2525 type InstructionWithData ,
26+ type ReadonlyAccount ,
2627 type ReadonlyUint8Array ,
2728 type WritableAccount ,
2829} from '@solana/kit' ;
@@ -38,11 +39,18 @@ export function getSyncNativeDiscriminatorBytes() {
3839export type SyncNativeInstruction <
3940 TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS ,
4041 TAccountAccount extends string | AccountMeta < string > = string ,
42+ TAccountRent extends string | AccountMeta < string > | undefined = undefined ,
4143 TRemainingAccounts extends readonly AccountMeta < string > [ ] = [ ] ,
4244> = Instruction < TProgram > &
4345 InstructionWithData < ReadonlyUint8Array > &
4446 InstructionWithAccounts <
45- [ TAccountAccount extends string ? WritableAccount < TAccountAccount > : TAccountAccount , ...TRemainingAccounts ]
47+ [
48+ TAccountAccount extends string ? WritableAccount < TAccountAccount > : TAccountAccount ,
49+ ...( TAccountRent extends undefined
50+ ? [ ]
51+ : [ TAccountRent extends string ? ReadonlyAccount < TAccountRent > : TAccountRent ] ) ,
52+ ...TRemainingAccounts ,
53+ ]
4654 > ;
4755
4856export type SyncNativeInstructionData = { discriminator : number } ;
@@ -67,31 +75,45 @@ export function getSyncNativeInstructionDataCodec(): FixedSizeCodec<
6775 return combineCodec ( getSyncNativeInstructionDataEncoder ( ) , getSyncNativeInstructionDataDecoder ( ) ) ;
6876}
6977
70- export type SyncNativeInput < TAccountAccount extends string = string > = {
78+ export type SyncNativeInput < TAccountAccount extends string = string , TAccountRent extends string = string > = {
7179 /** The native token account to sync with its underlying lamports. */
7280 account : Address < TAccountAccount > ;
81+ /** Rent sysvar. */
82+ rent ?: Address < TAccountRent > ;
7383} ;
7484
7585export function getSyncNativeInstruction <
7686 TAccountAccount extends string ,
87+ TAccountRent extends string ,
7788 TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS ,
7889> (
79- input : SyncNativeInput < TAccountAccount > ,
90+ input : SyncNativeInput < TAccountAccount , TAccountRent > ,
8091 config ?: { programAddress ?: TProgramAddress } ,
81- ) : SyncNativeInstruction < TProgramAddress , TAccountAccount > {
92+ ) : SyncNativeInstruction < TProgramAddress , TAccountAccount , TAccountRent > {
8293 // Program address.
8394 const programAddress = config ?. programAddress ?? TOKEN_PROGRAM_ADDRESS ;
8495
8596 // Original accounts.
86- const originalAccounts = { account : { value : input . account ?? null , isWritable : true } } ;
97+ const originalAccounts = {
98+ account : { value : input . account ?? null , isWritable : true } ,
99+ rent : { value : input . rent ?? null , isWritable : false } ,
100+ } ;
87101 const accounts = originalAccounts as Record < keyof typeof originalAccounts , ResolvedInstructionAccount > ;
88102
89- const getAccountMeta = getAccountMetaFactory ( programAddress , 'programId' ) ;
103+ // Resolve default values.
104+ if ( ! accounts . rent . value ) {
105+ accounts . rent . value =
106+ 'SysvarRent111111111111111111111111111111111' as Address < 'SysvarRent111111111111111111111111111111111' > ;
107+ }
108+
109+ const getAccountMeta = getAccountMetaFactory ( programAddress , 'omitted' ) ;
90110 return Object . freeze ( {
91- accounts : [ getAccountMeta ( 'account' , accounts . account ) ] ,
111+ accounts : [ getAccountMeta ( 'account' , accounts . account ) , getAccountMeta ( 'rent' , accounts . rent ) ] . filter (
112+ < T > ( x : T | undefined ) : x is T => x !== undefined ,
113+ ) ,
92114 data : getSyncNativeInstructionDataEncoder ( ) . encode ( { } ) ,
93115 programAddress,
94- } as SyncNativeInstruction < TProgramAddress , TAccountAccount > ) ;
116+ } as SyncNativeInstruction < TProgramAddress , TAccountAccount , TAccountRent > ) ;
95117}
96118
97119export type ParsedSyncNativeInstruction <
@@ -102,6 +124,8 @@ export type ParsedSyncNativeInstruction<
102124 accounts : {
103125 /** The native token account to sync with its underlying lamports. */
104126 account : TAccountMetas [ 0 ] ;
127+ /** Rent sysvar. */
128+ rent ?: TAccountMetas [ 1 ] | undefined ;
105129 } ;
106130 data : SyncNativeInstructionData ;
107131} ;
@@ -123,9 +147,15 @@ export function parseSyncNativeInstruction<TProgram extends string, TAccountMeta
123147 accountIndex += 1 ;
124148 return accountMeta ;
125149 } ;
150+ let optionalAccountsRemaining = instruction . accounts . length - 1 ;
151+ const getNextOptionalAccount = ( ) => {
152+ if ( optionalAccountsRemaining === 0 ) return undefined ;
153+ optionalAccountsRemaining -= 1 ;
154+ return getNextAccount ( ) ;
155+ } ;
126156 return {
127157 programAddress : instruction . programAddress ,
128- accounts : { account : getNextAccount ( ) } ,
158+ accounts : { account : getNextAccount ( ) , rent : getNextOptionalAccount ( ) } ,
129159 data : getSyncNativeInstructionDataDecoder ( ) . decode ( instruction . data ) ,
130160 } ;
131161}
0 commit comments