@@ -37,6 +37,12 @@ import {
3737 getTokenProgram ,
3838} from "../utils" ;
3939import { getDammV2Pool } from "../pda" ;
40+ import {
41+ DAMM_V1_PROGRAM_ID ,
42+ DAMM_V1_SWAP_DISC ,
43+ getDammV1Pool ,
44+ getDammV1RemainingAccounts ,
45+ } from "../damm_v1" ;
4046
4147export const ZAP_PROGRAM_ID = new PublicKey ( ZapIDL . address ) ;
4248
@@ -53,6 +59,71 @@ export function createZapProgram(): ZapProgram {
5359 return program ;
5460}
5561
62+ export async function zapOutDammv1 (
63+ svm : LiteSVM ,
64+ user : PublicKey ,
65+ inputTokenMint : PublicKey ,
66+ pool : PublicKey
67+ ) : Promise < Transaction > {
68+ const zapProgram = createZapProgram ( ) ;
69+
70+ const poolState = getDammV1Pool ( svm , pool ) ;
71+ const isTokenA = poolState . tokenAMint . equals ( inputTokenMint ) ;
72+ const outputTokenMint = isTokenA
73+ ? poolState . tokenBMint
74+ : poolState . tokenAMint ;
75+ const protocolTokenFee = isTokenA
76+ ? poolState . protocolTokenAFee
77+ : poolState . protocolTokenBFee ;
78+ const inputTokenProgram = getTokenProgram ( svm , inputTokenMint ) ;
79+ const outputTokenProgram = getTokenProgram ( svm , outputTokenMint ) ;
80+
81+ const userTokenInAccount = getAssociatedTokenAddressSync (
82+ inputTokenMint ,
83+ user ,
84+ true ,
85+ inputTokenProgram
86+ ) ;
87+ const userTokenOutAccount = getAssociatedTokenAddressSync (
88+ outputTokenMint ,
89+ user ,
90+ true ,
91+ outputTokenProgram
92+ ) ;
93+
94+ const preUserTokenBalance = getTokenBalance ( svm , userTokenInAccount ) ;
95+
96+ const remainingAccounts = getDammV1RemainingAccounts (
97+ svm ,
98+ pool ,
99+ user ,
100+ userTokenInAccount ,
101+ userTokenOutAccount ,
102+ protocolTokenFee
103+ ) ;
104+ const minAmountOutBuffer = new BN ( 10 ) . toArrayLike ( Buffer , "le" , 8 ) ;
105+ const amount = new BN ( 0 ) . toArrayLike ( Buffer , "le" , 8 ) ;
106+ const payloadData = Buffer . concat ( [
107+ Buffer . from ( DAMM_V1_SWAP_DISC ) ,
108+ amount ,
109+ minAmountOutBuffer ,
110+ ] ) ;
111+ return await zapProgram . methods
112+ . zapOut ( {
113+ percentage : 100 ,
114+ offsetAmountIn : 8 ,
115+ preUserTokenBalance,
116+ maxSwapAmount : new BN ( "100000000000" ) ,
117+ payloadData,
118+ } )
119+ . accountsPartial ( {
120+ userTokenInAccount,
121+ ammProgram : DAMM_V1_PROGRAM_ID ,
122+ } )
123+ . remainingAccounts ( remainingAccounts )
124+ . transaction ( ) ;
125+ }
126+
56127export async function zapOutDammv2 (
57128 svm : LiteSVM ,
58129 user : PublicKey ,
0 commit comments