|
| 1 | +use steel::*; |
| 2 | + |
| 3 | +use crate::prelude::*; |
| 4 | + |
| 5 | +pub fn make_offer( |
| 6 | + maker: Pubkey, |
| 7 | + mint_a: Pubkey, |
| 8 | + mint_b: Pubkey, |
| 9 | + id: u64, |
| 10 | + token_a_offered_amount: u64, |
| 11 | + token_b_wanted_amount: u64, |
| 12 | +) -> Instruction { |
| 13 | + let (maker_token_account_a, _) = Pubkey::find_program_address( |
| 14 | + &[maker.as_ref(), spl_token::ID.as_ref(), mint_a.as_ref()], |
| 15 | + &spl_token::ID, |
| 16 | + ); |
| 17 | + |
| 18 | + let offer = offer_pda(maker, id).0; |
| 19 | + let (vault, _) = Pubkey::find_program_address( |
| 20 | + &[offer.as_ref(), spl_token::ID.as_ref(), mint_a.as_ref()], |
| 21 | + &spl_token::ID, |
| 22 | + ); |
| 23 | + Instruction { |
| 24 | + program_id: crate::ID, |
| 25 | + accounts: vec![ |
| 26 | + AccountMeta::new(maker, true), |
| 27 | + AccountMeta::new_readonly(mint_a, false), |
| 28 | + AccountMeta::new_readonly(mint_b, false), |
| 29 | + AccountMeta::new(maker_token_account_a, false), |
| 30 | + AccountMeta::new(offer, false), |
| 31 | + AccountMeta::new(vault, false), |
| 32 | + AccountMeta::new_readonly(spl_token::ID, false), |
| 33 | + AccountMeta::new_readonly(system_program::ID, false), |
| 34 | + AccountMeta::new_readonly(ASSOCIATED_TOKEN_PROGRAM_ID, false), |
| 35 | + ], |
| 36 | + data: MakeOffer { |
| 37 | + id: id.to_le_bytes(), |
| 38 | + token_a_offered_amount: token_a_offered_amount.to_le_bytes(), |
| 39 | + token_b_wanted_amount: token_b_wanted_amount.to_le_bytes(), |
| 40 | + } |
| 41 | + .to_bytes(), |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +pub fn take_offer( |
| 46 | + taker: Pubkey, |
| 47 | + maker: Pubkey, |
| 48 | + mint_a: Pubkey, |
| 49 | + mint_b: Pubkey, |
| 50 | + offer: Pubkey, |
| 51 | +) -> Instruction { |
| 52 | + let (taker_token_account_a, _) = Pubkey::find_program_address( |
| 53 | + &[taker.as_ref(), spl_token::ID.as_ref(), mint_a.as_ref()], |
| 54 | + &spl_token::ID, |
| 55 | + ); |
| 56 | + let (taker_token_account_b, _) = Pubkey::find_program_address( |
| 57 | + &[taker.as_ref(), spl_token::ID.as_ref(), mint_b.as_ref()], |
| 58 | + &spl_token::ID, |
| 59 | + ); |
| 60 | + let (maker_token_account_b, _) = Pubkey::find_program_address( |
| 61 | + &[maker.as_ref(), spl_token::ID.as_ref(), mint_b.as_ref()], |
| 62 | + &spl_token::ID, |
| 63 | + ); |
| 64 | + let (vault, _) = Pubkey::find_program_address( |
| 65 | + &[offer.as_ref(), spl_token::ID.as_ref(), mint_a.as_ref()], |
| 66 | + &spl_token::ID, |
| 67 | + ); |
| 68 | + Instruction { |
| 69 | + program_id: crate::ID, |
| 70 | + accounts: vec![ |
| 71 | + AccountMeta::new(taker, true), |
| 72 | + AccountMeta::new(maker, false), |
| 73 | + AccountMeta::new_readonly(mint_a, false), |
| 74 | + AccountMeta::new_readonly(mint_b, false), |
| 75 | + AccountMeta::new(taker_token_account_a, false), |
| 76 | + AccountMeta::new(taker_token_account_b, false), |
| 77 | + AccountMeta::new(maker_token_account_b, false), |
| 78 | + AccountMeta::new(offer, false), |
| 79 | + AccountMeta::new(vault, false), |
| 80 | + AccountMeta::new_readonly(spl_token::ID, false), |
| 81 | + AccountMeta::new_readonly(system_program::ID, false), |
| 82 | + AccountMeta::new_readonly(ASSOCIATED_TOKEN_PROGRAM_ID, false), |
| 83 | + ], |
| 84 | + data: TakerOffer {}.to_bytes(), |
| 85 | + } |
| 86 | +} |
0 commit comments