From 9617519850e0fb84650e3db07b0030229042fd3b Mon Sep 17 00:00:00 2001 From: joaco05 <55862776+joaco05@users.noreply.github.com> Date: Tue, 16 Sep 2025 16:03:03 -0300 Subject: [PATCH] challenges completos --- programs/escrow/src/contexts/make.rs | 5 ++++- programs/escrow/src/contexts/take.rs | 6 ++++-- programs/escrow/src/lib.rs | 14 +++++++++++--- programs/escrow/src/state.rs | 11 +++++++++++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/programs/escrow/src/contexts/make.rs b/programs/escrow/src/contexts/make.rs index a623826..e6d09ce 100644 --- a/programs/escrow/src/contexts/make.rs +++ b/programs/escrow/src/contexts/make.rs @@ -1,4 +1,4 @@ -use anchor_lang::prelude::*; +use anchor_lang::{prelude::*, solana_program::sysvar::clock}; use anchor_spl::{ associated_token::AssociatedToken, @@ -43,6 +43,8 @@ pub struct Make<'info> { associated_token::token_program = token_program, )] pub vault: InterfaceAccount<'info, TokenAccount>, + #[account(address = clock::ID)] + pub clock: Sysvar<'info, Clock>, pub associated_token_program: Program<'info, AssociatedToken>, pub token_program: Interface<'info, TokenInterface>, pub system_program: Program<'info, System>, @@ -55,6 +57,7 @@ impl<'info> Make<'info> { maker: self.maker.key(), mint_a: self.mint_a.key(), mint_b: self.mint_b.key(), + clock: Clock::get()?.unix_timestamp, receive, bump: bumps.escrow, }); diff --git a/programs/escrow/src/contexts/take.rs b/programs/escrow/src/contexts/take.rs index 101575b..e6518c1 100644 --- a/programs/escrow/src/contexts/take.rs +++ b/programs/escrow/src/contexts/take.rs @@ -8,8 +8,10 @@ use crate::Escrow; #[derive(Accounts)] pub struct Take<'info> { - #[account(mut)] - pub taker: Signer<'info>, + #[account(mut/*, address = Pubkey::from_str("FhtdXoLhYtG7v5rX6d8c1b3H9g5Jz7y8x9y6z2w3v4u").unwrap() para poner una pubkey especifica */)] + pub taker: Signer<'info>, + // para poner una pubkey especifica + #[account(mut)] pub maker: SystemAccount<'info>, #[account( diff --git a/programs/escrow/src/lib.rs b/programs/escrow/src/lib.rs index 6e6e62d..0df471d 100644 --- a/programs/escrow/src/lib.rs +++ b/programs/escrow/src/lib.rs @@ -15,25 +15,33 @@ pub mod escrow { // un usuario crea un escrow depositando una cierta cantidad de una criptomoneda // y define que moneda y cantidad quiere recivir a cambio pub fn make(ctx: Context, seed: u64, deposit: u64, receive:u64) -> Result<()> { - let _ =ctx.accounts.deposit(deposit); + require!(deposit > 0 && receive > 0, EscrowError::InvalidAmount); + // Para solo aceptar misma cantidad de deposito que de recepcion (no necesario) + // require_eq!(deposit, receive, EscrowError::AmountMismatch); + ctx.accounts.deposit(deposit)?; ctx.accounts.save_escrow(seed, receive, &ctx.bumps) } // otro usuario acepta el escrow y deposita la cantidad y moneda requerida // el usuario que crea el escrow recibe la cantidad y moneda que definio pub fn take(ctx: Context) -> Result<()> { + let clock = Clock::get()?; + require!(clock.unix_timestamp > ctx.accounts.escrow.clock, EscrowError::InvalidTime); ctx.accounts.deposit()?; ctx.accounts.withdraw_and_close() } // el usuario creado cancela el escrow pub fn refund(ctx: Context) -> Result<()> { + let clock = Clock::get()?; + require!(clock.unix_timestamp > ctx.accounts.escrow.clock, EscrowError::InvalidTime); ctx.accounts.refund_and_close_vault() } - // 1 investigar: + // 1 investigar DONE: // require! // require_eq! - // 2 agregar la condicion de que la fecha actual sea mayor a una previamente establecida + // 2 agregar la condicion de que la fecha actual sea mayor a una previamente establecida DONE + // 3 agregar la condicion de que el taker sea una pubkey especifica } diff --git a/programs/escrow/src/state.rs b/programs/escrow/src/state.rs index 8fe7084..c201bd2 100644 --- a/programs/escrow/src/state.rs +++ b/programs/escrow/src/state.rs @@ -9,4 +9,15 @@ pub struct Escrow { pub mint_b: Pubkey, pub receive: u64, pub bump: u8, + pub clock: i64, +} + +#[error_code] +pub enum EscrowError { + #[msg("The amount must be greater than zero")] + InvalidAmount, + #[msg("The amount of deposit must be equal to the amount to receive")] + AmountMismatch, + #[msg("The current time must be greater than the escrow creation time")] + InvalidTime, } \ No newline at end of file