11use anchor_lang:: prelude:: * ;
22use anchor_spl:: {
33 token:: {
4- Mint , TokenAccount , Token , transfer as transfer_spl, Transfer as TransferSPL ,
4+ transfer as transfer_spl, TokenAccount , Mint , Transfer as TransferSPL , Token ,
55 } ,
66 associated_token:: AssociatedToken ,
77} ;
8- declare_id ! ( "8b7Pshe6L28ee9oCTjGHCYTugFCQjKSBGfbaXZt9f3EF " ) ;
8+ declare_id ! ( "5hP1158bStJmsUneTSbR3b7ENHDUjYiC8hwncye7WYVV " ) ;
99#[ program]
1010pub mod escrow_program {
1111 use super :: * ;
12- pub fn make_offer (
13- ctx : Context < MakeOfferContext > ,
14- token_a_offered_amount : u64 ,
15- token_b_wanted_amount : u64 ,
16- id : u64 ,
12+ pub fn make (
13+ ctx : Context < MakeContext > ,
14+ deposit_amount : u64 ,
15+ offer_amount : u64 ,
16+ seed : u64 ,
1717 ) -> Result < ( ) > {
1818 ctx. accounts . escrow . auth_bump = ctx. bumps . auth ;
1919 ctx. accounts . escrow . vault_bump = ctx. bumps . vault ;
2020 ctx. accounts . escrow . escrow_bump = ctx. bumps . escrow ;
2121 ctx. accounts . escrow . maker = ctx. accounts . maker . key ( ) ;
22- ctx. accounts . escrow . token_mint_a = ctx . accounts . token_mint_a . key ( ) ;
23- ctx. accounts . escrow . token_mint_b = ctx . accounts . token_mint_b . key ( ) ;
24- ctx. accounts . escrow . token_b_wanted_amount = token_b_wanted_amount ;
25- ctx. accounts . escrow . id = id ;
22+ ctx. accounts . escrow . amount = offer_amount ;
23+ ctx. accounts . escrow . seed = seed ;
24+ ctx. accounts . escrow . maker_mint = ctx . accounts . maker_mint . key ( ) ;
25+ ctx. accounts . escrow . taker_mint = ctx . accounts . taker_mint . key ( ) ;
2626 let cpi_accounts = TransferSPL {
27- from : ctx. accounts . maker_token_account_a . to_account_info ( ) ,
27+ from : ctx. accounts . maker_ata . to_account_info ( ) ,
2828 to : ctx. accounts . vault . to_account_info ( ) ,
2929 authority : ctx. accounts . maker . to_account_info ( ) ,
3030 } ;
3131 let cpi_ctx = CpiContext :: new (
3232 ctx. accounts . token_program . to_account_info ( ) ,
3333 cpi_accounts,
3434 ) ;
35- transfer_spl ( cpi_ctx, token_a_offered_amount ) ?;
35+ transfer_spl ( cpi_ctx, deposit_amount ) ?;
3636 Ok ( ( ) )
3737 }
38- pub fn take_offer ( ctx : Context < TakeOfferContext > ) -> Result < ( ) > {
38+ pub fn refund ( ctx : Context < RefundContext > ) -> Result < ( ) > {
3939 let cpi_accounts = TransferSPL {
40- from : ctx. accounts . taker_token_account_a . to_account_info ( ) ,
41- to : ctx. accounts . maker_token_account_a . to_account_info ( ) ,
40+ from : ctx. accounts . vault . to_account_info ( ) ,
41+ to : ctx. accounts . maker_ata . to_account_info ( ) ,
42+ authority : ctx. accounts . auth . to_account_info ( ) ,
43+ } ;
44+ let signer_seeds = & [ & b"auth" [ ..] , & [ ctx. accounts . escrow . auth_bump ] ] ;
45+ let binding = [ & signer_seeds[ ..] ] ;
46+ let cpi_ctx = CpiContext :: new_with_signer (
47+ ctx. accounts . token_program . to_account_info ( ) ,
48+ cpi_accounts,
49+ & binding,
50+ ) ;
51+ transfer_spl ( cpi_ctx, ctx. accounts . escrow . amount ) ?;
52+ Ok ( ( ) )
53+ }
54+ pub fn take ( ctx : Context < TakeContext > ) -> Result < ( ) > {
55+ let cpi_accounts = TransferSPL {
56+ from : ctx. accounts . taker_ata . to_account_info ( ) ,
57+ to : ctx. accounts . maker_ata . to_account_info ( ) ,
4258 authority : ctx. accounts . taker . to_account_info ( ) ,
4359 } ;
4460 let cpi_ctx = CpiContext :: new (
4561 ctx. accounts . token_program . to_account_info ( ) ,
4662 cpi_accounts,
4763 ) ;
48- transfer_spl ( cpi_ctx, ctx. accounts . escrow . token_b_wanted_amount ) ?;
64+ transfer_spl ( cpi_ctx, ctx. accounts . escrow . amount ) ?;
4965 let cpi_accounts = TransferSPL {
5066 from : ctx. accounts . vault . to_account_info ( ) ,
51- to : ctx. accounts . taker_token_account_b . to_account_info ( ) ,
67+ to : ctx. accounts . taker_receive_ata . to_account_info ( ) ,
5268 authority : ctx. accounts . auth . to_account_info ( ) ,
5369 } ;
5470 let signer_seeds = & [ & b"auth" [ ..] , & [ ctx. accounts . escrow . auth_bump ] ] ;
@@ -58,118 +74,156 @@ pub mod escrow_program {
5874 cpi_accounts,
5975 & binding,
6076 ) ;
61- transfer_spl ( cpi_ctx, ctx. accounts . escrow . token_b_wanted_amount ) ?;
77+ transfer_spl ( cpi_ctx, ctx. accounts . escrow . amount ) ?;
6278 Ok ( ( ) )
6379 }
6480}
6581#[ derive( Accounts ) ]
66- #[ instruction( id: u64 ) ]
67- pub struct MakeOfferContext < ' info > {
82+ #[ instruction( seed: u64 ) ]
83+ pub struct MakeContext < ' info > {
84+ #[ account( seeds = [ b"auth" ] , bump) ]
85+ /// CHECK: This acc is safe
86+ pub auth : UncheckedAccount < ' info > ,
87+ #[ account( ) ]
88+ pub taker_mint : Account < ' info , Mint > ,
6889 #[ account(
6990 init,
7091 payer = maker,
7192 space = 123 ,
7293 seeds = [ b"escrow" ,
7394 maker. key( ) . as_ref( ) ,
74- id . to_le_bytes( ) . as_ref( ) ] ,
95+ seed . to_le_bytes( ) . as_ref( ) ] ,
7596 bump,
7697 ) ]
77- pub escrow : Account < ' info , Escrow > ,
78- #[ account(
79- mut ,
80- associated_token:: mint = token_mint_a,
81- associated_token:: authority = maker,
82- ) ]
83- pub maker_token_account_a : Account < ' info , TokenAccount > ,
84- #[ account( mut ) ]
85- pub maker : Signer < ' info > ,
98+ pub escrow : Account < ' info , EscrowState > ,
8699 #[ account( ) ]
87- pub token_mint_a : Account < ' info , Mint > ,
88- #[ account( ) ]
89- pub token_mint_b : Account < ' info , Mint > ,
100+ pub maker_mint : Account < ' info , Mint > ,
90101 #[ account(
91102 init,
92103 payer = maker,
93104 seeds = [ b"vault" ,
94105 escrow. key( ) . as_ref( ) ] ,
95- token:: mint = token_mint_a ,
106+ token:: mint = maker_mint ,
96107 token:: authority = auth,
97108 bump,
98109 ) ]
99110 pub vault : Account < ' info , TokenAccount > ,
100- #[ account( seeds = [ b"auth" ] , bump) ]
101- /// CHECK: This acc is safe
102- pub auth : UncheckedAccount < ' info > ,
111+ #[ account(
112+ mut ,
113+ associated_token:: mint = maker_mint,
114+ associated_token:: authority = maker,
115+ ) ]
116+ pub maker_ata : Account < ' info , TokenAccount > ,
117+ #[ account( mut ) ]
118+ pub maker : Signer < ' info > ,
103119 pub associated_token_program : Program < ' info , AssociatedToken > ,
104120 pub token_program : Program < ' info , Token > ,
105121 pub system_program : Program < ' info , System > ,
106122}
107123#[ derive( Accounts ) ]
108- pub struct TakeOfferContext < ' info > {
109- #[ account( mut ) ]
110- pub maker : SystemAccount < ' info > ,
111- #[ account( mut ) ]
112- pub taker : Signer < ' info > ,
124+ pub struct RefundContext < ' info > {
113125 #[ account(
114126 mut ,
115127 seeds = [ b"vault" ,
116128 escrow. key( ) . as_ref( ) ] ,
117- token:: mint = token_mint_a ,
129+ token:: mint = maker_mint ,
118130 token:: authority = auth,
119131 bump,
120132 ) ]
121133 pub vault : Account < ' info , TokenAccount > ,
134+ #[ account(
135+ mut ,
136+ seeds = [ b"escrow" ,
137+ maker. key( ) . as_ref( ) ,
138+ escrow. seed. to_le_bytes( ) . as_ref( ) ] ,
139+ has_one = maker,
140+ bump,
141+ close = maker,
142+ ) ]
143+ pub escrow : Account < ' info , EscrowState > ,
122144 #[ account( ) ]
123- pub token_mint_b : Account < ' info , Mint > ,
145+ pub maker_mint : Account < ' info , Mint > ,
146+ #[ account( mut ) ]
147+ pub maker : Signer < ' info > ,
148+ #[ account(
149+ mut ,
150+ associated_token:: mint = maker_mint,
151+ associated_token:: authority = maker,
152+ ) ]
153+ pub maker_ata : Account < ' info , TokenAccount > ,
154+ #[ account( seeds = [ b"auth" ] , bump) ]
155+ /// CHECK: This acc is safe
156+ pub auth : UncheckedAccount < ' info > ,
157+ pub associated_token_program : Program < ' info , AssociatedToken > ,
158+ pub token_program : Program < ' info , Token > ,
159+ pub system_program : Program < ' info , System > ,
160+ }
161+ #[ derive( Accounts ) ]
162+ pub struct TakeContext < ' info > {
124163 #[ account( seeds = [ b"auth" ] , bump) ]
125164 /// CHECK: This acc is safe
126165 pub auth : UncheckedAccount < ' info > ,
166+ #[ account( mut ) ]
167+ pub taker : Signer < ' info > ,
168+ #[ account( ) ]
169+ pub taker_mint : Account < ' info , Mint > ,
170+ #[ account( mut ) ]
171+ pub maker : SystemAccount < ' info > ,
127172 #[ account(
128173 mut ,
129174 seeds = [ b"escrow" ,
130175 maker. key( ) . as_ref( ) ,
131- escrow. id . to_le_bytes( ) . as_ref( ) ] ,
176+ escrow. seed . to_le_bytes( ) . as_ref( ) ] ,
132177 has_one = maker,
133- has_one = token_mint_a ,
134- has_one = token_mint_b ,
178+ has_one = maker_mint ,
179+ has_one = taker_mint ,
135180 bump,
136181 close = maker,
137182 ) ]
138- pub escrow : Account < ' info , Escrow > ,
183+ pub escrow : Account < ' info , EscrowState > ,
139184 #[ account(
140185 init_if_needed,
141186 payer = taker,
142- associated_token:: mint = token_mint_a ,
187+ associated_token:: mint = maker_mint ,
143188 associated_token:: authority = taker,
144189 ) ]
145- pub taker_token_account_a : Account < ' info , TokenAccount > ,
146- #[ account( ) ]
147- pub token_mint_a : Account < ' info , Mint > ,
190+ pub taker_receive_ata : Account < ' info , TokenAccount > ,
148191 #[ account(
149192 mut ,
150- associated_token:: mint = token_mint_a ,
193+ associated_token:: mint = maker_mint ,
151194 associated_token:: authority = maker,
152195 ) ]
153- pub maker_token_account_a : Account < ' info , TokenAccount > ,
196+ pub maker_ata : Account < ' info , TokenAccount > ,
154197 #[ account(
155198 init_if_needed,
156199 payer = taker,
157- associated_token:: mint = token_mint_a ,
200+ associated_token:: mint = maker_mint ,
158201 associated_token:: authority = taker,
159202 ) ]
160- pub taker_token_account_b : Account < ' info , TokenAccount > ,
203+ pub taker_ata : Account < ' info , TokenAccount > ,
204+ #[ account( ) ]
205+ pub maker_mint : Account < ' info , Mint > ,
206+ #[ account(
207+ mut ,
208+ seeds = [ b"vault" ,
209+ escrow. key( ) . as_ref( ) ] ,
210+ token:: mint = maker_mint,
211+ token:: authority = auth,
212+ bump,
213+ ) ]
214+ pub vault : Account < ' info , TokenAccount > ,
161215 pub associated_token_program : Program < ' info , AssociatedToken > ,
162- pub token_program : Program < ' info , Token > ,
216+ pub token_program : Program < ' info , Token > ,
163217 pub system_program : Program < ' info , System > ,
164218}
165219#[ account]
166- pub struct Escrow {
220+ pub struct EscrowState {
167221 pub maker : Pubkey ,
168- pub token_mint_a : Pubkey ,
169- pub token_mint_b : Pubkey ,
170- pub token_b_wanted_amount : u64 ,
171- pub escrow_bump : u8 ,
172- pub id : u64 ,
222+ pub maker_mint : Pubkey ,
223+ pub taker_mint : Pubkey ,
224+ pub amount : u64 ,
225+ pub seed : u64 ,
173226 pub auth_bump : u8 ,
227+ pub escrow_bump : u8 ,
174228 pub vault_bump : u8 ,
175229}
0 commit comments