@@ -1126,13 +1126,13 @@ export class VaultClient {
11261126 vault : PublicKey ,
11271127 uiTxParams ?: TxParams
11281128 ) : Promise < TransactionSignature > {
1129- const ix = await this . getManagerWithdrawIx ( vault ) ;
1130- return this . createAndSendTxn ( [ ix ] , uiTxParams ) ;
1129+ const ixs = await this . getManagerWithdrawIx ( vault ) ;
1130+ return this . createAndSendTxn ( ixs , uiTxParams ) ;
11311131 }
11321132
11331133 public async getManagerWithdrawIx (
11341134 vault : PublicKey
1135- ) : Promise < TransactionInstruction > {
1135+ ) : Promise < TransactionInstruction [ ] > {
11361136 const vaultAccount = await this . program . account . vault . fetch ( vault ) ;
11371137
11381138 const user = await this . getSubscribedVaultUser ( vaultAccount . user ) ;
@@ -1162,7 +1162,48 @@ export class VaultClient {
11621162 ) ;
11631163 }
11641164
1165- return this . program . instruction . managerWithdraw ( {
1165+ const isSolMarket = spotMarket . mint . equals ( WRAPPED_SOL_MINT ) ;
1166+ let userAta = getAssociatedTokenAddressSync (
1167+ spotMarket . mint ,
1168+ vaultAccount . manager ,
1169+ true
1170+ ) ;
1171+
1172+ const preIxs : TransactionInstruction [ ] = [ ] ;
1173+ const postIxs : TransactionInstruction [ ] = [ ] ;
1174+
1175+ if ( isSolMarket ) {
1176+ const { ixs, pubkey } =
1177+ await this . driftClient . getWrappedSolAccountCreationIxs ( ZERO , false ) ;
1178+
1179+ userAta = pubkey ;
1180+ preIxs . push ( ...ixs ) ;
1181+ postIxs . push ( createSyncNativeInstruction ( userAta ) ) ;
1182+ postIxs . push (
1183+ createCloseAccountInstruction (
1184+ userAta ,
1185+ vaultAccount . manager ,
1186+ vaultAccount . manager ,
1187+ [ ]
1188+ )
1189+ ) ;
1190+ } else {
1191+ const userAtaExists = await this . driftClient . connection . getAccountInfo (
1192+ userAta
1193+ ) ;
1194+ if ( userAtaExists === null ) {
1195+ preIxs . push (
1196+ createAssociatedTokenAccountInstruction (
1197+ vaultAccount . manager ,
1198+ userAta ,
1199+ vaultAccount . manager ,
1200+ spotMarket . mint
1201+ )
1202+ ) ;
1203+ }
1204+ }
1205+
1206+ const withdrawIx = await this . program . instruction . managerWithdraw ( {
11661207 accounts : {
11671208 vault,
11681209 manager : vaultAccount . manager ,
@@ -1178,16 +1219,14 @@ export class VaultClient {
11781219 ) ,
11791220 driftState : await this . driftClient . getStatePublicKey ( ) ,
11801221 driftSpotMarketVault : spotMarket . vault ,
1181- userTokenAccount : getAssociatedTokenAddressSync (
1182- spotMarket . mint ,
1183- vaultAccount . manager ,
1184- true
1185- ) ,
1222+ userTokenAccount : userAta ,
11861223 driftSigner : this . driftClient . getStateAccount ( ) . signer ,
11871224 tokenProgram : TOKEN_PROGRAM_ID ,
11881225 } ,
11891226 remainingAccounts,
11901227 } ) ;
1228+
1229+ return [ ...preIxs , withdrawIx , ...postIxs ] ;
11911230 }
11921231
11931232 public async managerBorrow (
0 commit comments