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
4 changes: 2 additions & 2 deletions Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ resolution = true
skip-lint = false

[programs.localnet]
burry_escrow = "9Wm9QwzaAdKHz6Pt2jQ6bLG4ssjXnwvcXqiaWvcCSLkQ"
burry_escrow = "3V8LV4FgZCJY6qpRp8XpLGbhdJW9pWLHyXpTs6PRfqzE"

[registry]
url = "https://api.apr.dev"

[provider]
cluster = "devnet"
cluster = "Devnet"
wallet = "~/.config/solana/id.json"

[scripts]
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"prettier": "^2.6.2",
"ts-mocha": "^10.0.0",
"typescript": "^4.3.5"
}
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
17 changes: 9 additions & 8 deletions programs/burry-escrow/src/instructions/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ use crate::state::*;
use anchor_lang::prelude::*;
use anchor_lang::solana_program::{program::invoke, system_instruction::transfer};

pub fn deposit_handler(ctx: Context<Deposit>, escrow_amount: u64, unlock_price: f64) -> Result<()> {
pub fn deposit_handler(ctx: Context<Deposit>, escrow_amount: u64, unlock_price: u64) -> Result<()> {
msg!("Depositing funds in escrow...");

let escrow_state = &mut ctx.accounts.escrow_account;
escrow_state.unlock_price = unlock_price;
escrow_state.escrow_amount = escrow_amount;
let escrow = &mut ctx.accounts.escrow_account;
escrow.unlock_price = unlock_price;
escrow.escrow_amount = escrow_amount;

let transfer_ix = transfer(&ctx.accounts.user.key(), &escrow_state.key(), escrow_amount);
let transfer_instruction = transfer(&ctx.accounts.user.key(), &escrow.key(), escrow_amount);

invoke(
&transfer_ix,
&transfer_instruction,
&[
ctx.accounts.user.to_account_info(),
ctx.accounts.escrow_account.to_account_info(),
Expand All @@ -28,6 +28,7 @@ pub fn deposit_handler(ctx: Context<Deposit>, escrow_amount: u64, unlock_price:
Ok(())
}

pub const ANCHOR_DISCRIMINATOR: usize = 8;
#[derive(Accounts)]
pub struct Deposit<'info> {
// user account
Expand All @@ -39,9 +40,9 @@ pub struct Deposit<'info> {
seeds = [ESCROW_SEED, user.key().as_ref()],
bump,
payer = user,
space = std::mem::size_of::<EscrowState>() + 8
space = Escrow::INIT_SPACE + ANCHOR_DISCRIMINATOR
)]
pub escrow_account: Account<'info, EscrowState>,
pub escrow_account: Account<'info, Escrow>,

pub system_program: Program<'info, System>,
}
17 changes: 9 additions & 8 deletions programs/burry-escrow/src/instructions/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@ use switchboard_solana::AggregatorAccountData;

pub fn withdraw_handler(ctx: Context<Withdraw>) -> Result<()> {
let feed = &ctx.accounts.feed_aggregator.load()?;
let escrow_state = &ctx.accounts.escrow_account;
let escrow = &ctx.accounts.escrow_account;

// get result
let val: f64 = feed.get_result()?.try_into()?;
let val: u64 = feed.get_result()?.try_into()?;

// check whether the feed has been updated in the last 300 seconds
feed.check_staleness(Clock::get().unwrap().unix_timestamp, 300)
.map_err(|_| error!(EscrowErrorCode::StaleFeed))?;

msg!("Current feed result is {}!", val);
msg!("Unlock price is {}", escrow_state.unlock_price);
msg!("Unlock price is {}", escrow.unlock_price);

if val < escrow_state.unlock_price as f64 {
//ensure the feed value is below the unlock price
if val < escrow.unlock_price {
return Err(EscrowErrorCode::SolPriceAboveUnlockPrice.into());
}

// 'Transfer: `from` must not carry data'
**escrow_state.to_account_info().try_borrow_mut_lamports()? = escrow_state
**escrow.to_account_info().try_borrow_mut_lamports()? = escrow
.to_account_info()
.lamports()
.checked_sub(escrow_state.escrow_amount)
.checked_sub(escrow.escrow_amount)
.ok_or(ProgramError::InvalidArgument)?;

**ctx
Expand All @@ -39,7 +40,7 @@ pub fn withdraw_handler(ctx: Context<Withdraw>) -> Result<()> {
.user
.to_account_info()
.lamports()
.checked_add(escrow_state.escrow_amount)
.checked_add(escrow.escrow_amount)
.ok_or(ProgramError::InvalidArgument)?;

Ok(())
Expand All @@ -57,7 +58,7 @@ pub struct Withdraw<'info> {
bump,
close = user
)]
pub escrow_account: Account<'info, EscrowState>,
pub escrow_account: Account<'info, Escrow>,
// Switchboard SOL feed aggregator
#[account(
address = Pubkey::from_str(SOL_USDC_FEED).unwrap()
Expand Down
8 changes: 4 additions & 4 deletions programs/burry-escrow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use anchor_lang::prelude::*;
use instructions::deposit::*;
use instructions::withdraw::*;
use state::*;


pub mod errors;
pub mod instructions;
pub mod state;

declare_id!("9Wm9QwzaAdKHz6Pt2jQ6bLG4ssjXnwvcXqiaWvcCSLkQ");
declare_id!("3V8LV4FgZCJY6qpRp8XpLGbhdJW9pWLHyXpTs6PRfqzE");

#[program]
pub mod burry_escrow {
use super::*;

pub fn deposit(ctx: Context<Deposit>, escrow_amt: u64, unlock_price: f64) -> Result<()> {
deposit_handler(ctx, escrow_amt, unlock_price)
pub fn deposit(ctx: Context<Deposit>, escrow_amount: u64, unlock_price: u64) -> Result<()> {
deposit_handler(ctx, escrow_amount, unlock_price)
}

pub fn withdraw(ctx: Context<Withdraw>) -> Result<()> {
Expand Down
7 changes: 5 additions & 2 deletions programs/burry-escrow/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ pub const ESCROW_SEED: &[u8] = b"MICHAEL BURRY";
pub const SOL_USDC_FEED: &str = "GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR";

#[account]
pub struct EscrowState {
pub unlock_price: f64,
#[derive(InitSpace)]
pub struct Escrow {
pub unlock_price:u64,
pub escrow_amount: u64,
}


Loading