Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tokens/escrow/steel #233

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions tokens/escrow/steel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
test-ledger
25 changes: 25 additions & 0 deletions tokens/escrow/steel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[workspace]
resolver = "2"
members = ["api", "program"]

[workspace.package]
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
homepage = ""
documentation = ""
repository = ""
readme = "./README.md"
keywords = ["solana"]

[workspace.dependencies]
escrow-api = { path = "./api", version = "0.1.0" }
bytemuck = "1.14"
num_enum = "0.7"
solana-program = "1.18"
steel = { features = ["spl"], version = "2.0" }
thiserror = "1.0"
spl-token = { version = "^4", features = ["no-entrypoint"] }
spl-associated-token-account = { version = "^2.3", features = [
"no-entrypoint",
] }
28 changes: 28 additions & 0 deletions tokens/escrow/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Escrow

**Escrow** is a ...

## API
- [`Consts`](api/src/consts.rs) – Program constants.
- [`Error`](api/src/error.rs) – Custom program errors.
- [`Event`](api/src/event.rs) – Custom program events.
- [`Instruction`](api/src/instruction.rs) – Declared instructions.

## Instructions
- [`Add`](program/src/add.rs) – Add ...
- [`Initialize`](program/src/initialize.rs) – Initialize ...

## State
- [`Counter`](api/src/state/counter.rs) – Counter ...

## Get started

Compile your program:
```sh
steel build
```

Run unit and integration tests:
```sh
steel test
```
20 changes: 20 additions & 0 deletions tokens/escrow/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "escrow-api"
description = "API for interacting with the Escrow program"
version.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
documentation.workspace = true
repository.workspace = true
readme.workspace = true
keywords.workspace = true

[dependencies]
bytemuck.workspace = true
num_enum.workspace = true
solana-program.workspace = true
steel.workspace = true
thiserror.workspace = true
spl-token.workspace = true
spl-associated-token-account.workspace = true
2 changes: 2 additions & 0 deletions tokens/escrow/steel/api/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Seed of the offer account PDA.
pub const OFFER: &[u8] = b"offer";
10 changes: 10 additions & 0 deletions tokens/escrow/steel/api/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use steel::*;

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
#[repr(u32)]
pub enum EscrowError {
#[error("This is a dummy error")]
Dummy = 0,
}

error!(EscrowError);
23 changes: 23 additions & 0 deletions tokens/escrow/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use steel::*;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum EscrowInstruction {
MakeOffer = 0,
TakeOffer = 1,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct MakeOffer {
pub token_a_offered_amount: [u8; 8],
pub id: [u8; 8],
pub token_b_wanted_amount: [u8; 8],
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct TakeOffer {}

instruction!(EscrowInstruction, MakeOffer);
instruction!(EscrowInstruction, TakeOffer);
18 changes: 18 additions & 0 deletions tokens/escrow/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod consts;
pub mod error;
pub mod instruction;
pub mod sdk;
pub mod state;

pub mod prelude {
pub use crate::consts::*;
pub use crate::error::*;
pub use crate::instruction::*;
pub use crate::sdk::*;
pub use crate::state::*;
}

use steel::*;

// TODO Set program id
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");
66 changes: 66 additions & 0 deletions tokens/escrow/steel/api/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use steel::*;

use crate::prelude::*;

pub fn make_offer(
maker: Pubkey,
token_mint_a: Pubkey,
token_mint_b: Pubkey,
maker_token_account_a: Pubkey,
vault: Pubkey,
id: u64,
token_a_offered_amount: u64,
token_b_wanted_amount: u64,
) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(maker, true),
AccountMeta::new_readonly(token_mint_a, false),
AccountMeta::new_readonly(token_mint_b, false),
AccountMeta::new_readonly(maker_token_account_a, false),
AccountMeta::new(offer_pda(maker, id).0, false),
AccountMeta::new(vault, false),
AccountMeta::new_readonly(system_program::ID, false),
AccountMeta::new_readonly(spl_token::ID, false),
AccountMeta::new_readonly(spl_associated_token_account::ID, false),
],
data: MakeOffer {
token_a_offered_amount: token_a_offered_amount.to_le_bytes(),
id: id.to_le_bytes(),
token_b_wanted_amount: token_b_wanted_amount.to_le_bytes(),
}
.to_bytes(),
}
}

pub fn take_offer(
taker: Pubkey,
maker: Pubkey,
token_mint_a: Pubkey,
token_mint_b: Pubkey,
taker_token_account_a: Pubkey,
taker_token_account_b: Pubkey,
maker_token_account_b: Pubkey,
vault: Pubkey,
id: u64,
) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(taker, true),
AccountMeta::new(maker, false),
AccountMeta::new_readonly(token_mint_a, false),
AccountMeta::new_readonly(token_mint_b, false),
AccountMeta::new_readonly(taker_token_account_a, false),
AccountMeta::new_readonly(taker_token_account_b, false),
AccountMeta::new_readonly(maker_token_account_b, false),
AccountMeta::new(offer_pda(maker, id).0, false),
AccountMeta::new(vault, false),
AccountMeta::new_readonly(system_program::ID, false),
AccountMeta::new_readonly(spl_token::ID, false),
AccountMeta::new_readonly(spl_associated_token_account::ID, false),
],
data: TakeOffer {}.to_bytes(),
}
}
21 changes: 21 additions & 0 deletions tokens/escrow/steel/api/src/state/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
mod offer;

pub use offer::*;

use steel::*;

use crate::consts::*;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum EscrowAccount {
Offer = 0,
}

/// Fetch PDA of the counter account.
pub fn offer_pda(maker: Pubkey, id: u64) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[OFFER, maker.as_ref(), id.to_le_bytes().as_ref()],
&crate::id(),
)
}
18 changes: 18 additions & 0 deletions tokens/escrow/steel/api/src/state/offer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use steel::*;

use super::EscrowAccount;

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct Offer {
pub id: u64,
pub maker: Pubkey,
pub token_mint_a: Pubkey,
pub token_mint_b: Pubkey,
pub token_b_wanted_amount: u64,
pub bump: u8,

pub _padding: [u8; 7], // Explicit padding to match 960 bits
}

account!(EscrowAccount, Offer);
28 changes: 28 additions & 0 deletions tokens/escrow/steel/program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "escrow-program"
description = ""
version.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
documentation.workspace = true
repository.workspace = true
readme.workspace = true
keywords.workspace = true

[lib]
crate-type = ["cdylib", "lib"]

[dependencies]
escrow-api.workspace = true
solana-program.workspace = true
steel.workspace = true
spl-token.workspace = true
spl-associated-token-account.workspace = true

[dev-dependencies]
base64 = "0.21"
rand = "0.8.5"
solana-program-test = "1.18"
solana-sdk = "1.18"
tokio = { version = "1.35", features = ["full"] }
25 changes: 25 additions & 0 deletions tokens/escrow/steel/program/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
mod make_offer;
mod take_offer;

use make_offer::*;
use take_offer::*;

use escrow_api::prelude::*;
use steel::*;

pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
data: &[u8],
) -> ProgramResult {
let (ix, data) = parse_instruction(&escrow_api::ID, program_id, data)?;

match ix {
EscrowInstruction::MakeOffer => process_make_offer(accounts, data)?,
EscrowInstruction::TakeOffer => process_take_offer(accounts, data)?,
}

Ok(())
}

entrypoint!(process_instruction);
77 changes: 77 additions & 0 deletions tokens/escrow/steel/program/src/make_offer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use escrow_api::prelude::*;
use steel::{transfer as transfer_spl_tokens, *};

pub fn process_make_offer(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
// Parse args.
let args = MakeOffer::try_from_bytes(data)?;
let token_a_offered_amount = u64::from_le_bytes(args.token_a_offered_amount);
let id = u64::from_le_bytes(args.id);
let token_b_wanted_amount = u64::from_le_bytes(args.token_b_wanted_amount);

// Load accounts.
let [maker_info, token_mint_a_info, token_mint_b_info, maker_token_account_a_info, offer_info, vault_info, system_program, token_program, associated_token_program] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};

maker_info.is_signer()?;

token_mint_a_info
.has_address(&token_mint_a_info.key)?
.is_writable()?
.as_mint()?;

token_mint_b_info
.has_address(&token_mint_b_info.key)?
.is_writable()?
.as_mint()?;

maker_token_account_a_info
.as_associated_token_account(maker_info.key, token_mint_a_info.key)?;

offer_info.is_empty()?.is_writable()?.has_seeds(
&[OFFER, maker_info.key.as_ref(), id.to_le_bytes().as_ref()],
&escrow_api::ID,
)?;

vault_info.is_empty()?.is_writable()?;

// Initialize offer.
create_account::<Offer>(
offer_info,
system_program,
maker_info,
&escrow_api::ID,
&[OFFER, maker_info.key.as_ref(), id.to_le_bytes().as_ref()],
)?;

// create valut
create_associated_token_account(
maker_info,
offer_info,
vault_info,
token_mint_a_info,
system_program,
token_program,
associated_token_program,
)?;

transfer_spl_tokens(
maker_info,
maker_token_account_a_info,
vault_info,
token_program,
token_a_offered_amount,
)?;

let offer = offer_info.as_account_mut::<Offer>(&escrow_api::ID)?;
offer.id = id;
offer.maker = *maker_info.key;
offer.token_mint_a = *token_mint_a_info.key;
offer.token_mint_b = *token_mint_b_info.key;
offer.token_b_wanted_amount = token_b_wanted_amount;
offer.bump = offer_pda(*maker_info.key, id).1;

Ok(())
}
Loading