Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion programs/escrow/src/contexts/make.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anchor_lang::prelude::*;
use anchor_lang::{prelude::*, solana_program::sysvar::clock};

use anchor_spl::{
associated_token::AssociatedToken,
Expand Down Expand Up @@ -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>,
Expand All @@ -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,
});
Expand Down
6 changes: 4 additions & 2 deletions programs/escrow/src/contexts/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 11 additions & 3 deletions programs/escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Make>, 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<Take>) -> 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<Refund>) -> 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
}
11 changes: 11 additions & 0 deletions programs/escrow/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}