Skip to content

Commit

Permalink
add tokens/escrow/steel #266
Browse files Browse the repository at this point in the history
Co-authored-by: Ayush <[email protected]>
  • Loading branch information
HongThaiPham and heyAyushh authored Jan 2, 2025
1 parent c0cc430 commit cec3ca1
Show file tree
Hide file tree
Showing 18 changed files with 2,297 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tokens/escrow/steel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[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 = { version = "2.0", features = ["spl"] }
thiserror = "1.0"
spl-token = "^4"
40 changes: 40 additions & 0 deletions tokens/escrow/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Escrow

**Escrow** is a ...

## API
- [`Consts`](api/src/consts.rs) – Program constants.
- [`Instruction`](api/src/instruction.rs) – Declared instructions.

## Instructions
- [`MakeOffer`](program/src/make_offer.rs) – Make an offer ...
- [`TakerOffer`](program/src/take_offer.rs) – Take an offer ...

## State
- [`Offer`](api/src/state/offer.rs) – Offer state ...

## How to?

Compile your program:

```sh
pnpm build
```

Run tests:

```sh
pnpm test
```

Run build and test

```sh
pnpm build-and-test
```

Deploy your program:

```sh
pnpm deploy
```
19 changes: 19 additions & 0 deletions tokens/escrow/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[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
8 changes: 8 additions & 0 deletions tokens/escrow/steel/api/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use solana_program::pubkey;
use steel::Pubkey;

/// Seed of the offer account PDA.
pub const OFFER_SEED: &[u8] = b"offer";

pub const ASSOCIATED_TOKEN_PROGRAM_ID: Pubkey =
pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
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,
TakerOffer = 1,
}

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

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

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

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

use steel::*;

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

use crate::prelude::*;

pub fn make_offer(
maker: Pubkey,
mint_a: Pubkey,
mint_b: Pubkey,
id: u64,
token_a_offered_amount: u64,
token_b_wanted_amount: u64,
) -> Instruction {
let (maker_token_account_a, _) = Pubkey::find_program_address(
&[maker.as_ref(), spl_token::ID.as_ref(), mint_a.as_ref()],
&spl_token::ID,
);

let offer = offer_pda(maker, id).0;
let (vault, _) = Pubkey::find_program_address(
&[offer.as_ref(), spl_token::ID.as_ref(), mint_a.as_ref()],
&spl_token::ID,
);
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(maker, true),
AccountMeta::new_readonly(mint_a, false),
AccountMeta::new_readonly(mint_b, false),
AccountMeta::new(maker_token_account_a, false),
AccountMeta::new(offer, false),
AccountMeta::new(vault, false),
AccountMeta::new_readonly(spl_token::ID, false),
AccountMeta::new_readonly(system_program::ID, false),
AccountMeta::new_readonly(ASSOCIATED_TOKEN_PROGRAM_ID, false),
],
data: MakeOffer {
id: id.to_le_bytes(),
token_a_offered_amount: token_a_offered_amount.to_le_bytes(),
token_b_wanted_amount: token_b_wanted_amount.to_le_bytes(),
}
.to_bytes(),
}
}

pub fn take_offer(
taker: Pubkey,
maker: Pubkey,
mint_a: Pubkey,
mint_b: Pubkey,
offer: Pubkey,
) -> Instruction {
let (taker_token_account_a, _) = Pubkey::find_program_address(
&[taker.as_ref(), spl_token::ID.as_ref(), mint_a.as_ref()],
&spl_token::ID,
);
let (taker_token_account_b, _) = Pubkey::find_program_address(
&[taker.as_ref(), spl_token::ID.as_ref(), mint_b.as_ref()],
&spl_token::ID,
);
let (maker_token_account_b, _) = Pubkey::find_program_address(
&[maker.as_ref(), spl_token::ID.as_ref(), mint_b.as_ref()],
&spl_token::ID,
);
let (vault, _) = Pubkey::find_program_address(
&[offer.as_ref(), spl_token::ID.as_ref(), mint_a.as_ref()],
&spl_token::ID,
);
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(taker, true),
AccountMeta::new(maker, false),
AccountMeta::new_readonly(mint_a, false),
AccountMeta::new_readonly(mint_b, false),
AccountMeta::new(taker_token_account_a, false),
AccountMeta::new(taker_token_account_b, false),
AccountMeta::new(maker_token_account_b, false),
AccountMeta::new(offer, false),
AccountMeta::new(vault, false),
AccountMeta::new_readonly(spl_token::ID, false),
AccountMeta::new_readonly(system_program::ID, false),
AccountMeta::new_readonly(ASSOCIATED_TOKEN_PROGRAM_ID, false),
],
data: TakerOffer {}.to_bytes(),
}
}
11 changes: 11 additions & 0 deletions tokens/escrow/steel/api/src/state/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod offer;

pub use offer::*;

use steel::*;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum EscrowAccount {
Offer = 0,
}
26 changes: 26 additions & 0 deletions tokens/escrow/steel/api/src/state/offer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use steel::*;

use crate::consts::OFFER_SEED;

use super::EscrowAccount;

/// Fetch PDA of the counter account.
pub fn offer_pda(maker: Pubkey, id: u64) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[OFFER_SEED, maker.as_ref(), id.to_le_bytes().as_ref()],
&crate::id(),
)
}

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

account!(EscrowAccount, Offer);
31 changes: 31 additions & 0 deletions tokens/escrow/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "steel",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/*.test.ts",
"build-and-test": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./tests/fixtures && pnpm test",
"build": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so",
"deploy": "solana program deploy ./program/target/so/account_data_program.so"
},
"keywords": [],
"author": "Leo Pham <[email protected]>",
"license": "ISC",
"dependencies": {
"@solana/spl-token": "^0.4.9",
"@solana/web3.js": "^1.95.4",
"bs58": "^6.0.0"
},
"devDependencies": {
"@types/chai": "^4.3.7",
"@types/mocha": "^10.0.9",
"@types/node": "^22.7.9",
"borsh": "^2.0.0",
"chai": "^4.3.7",
"mocha": "^10.7.3",
"solana-bankrun": "^0.4.0",
"ts-mocha": "^10.0.0",
"typescript": "^5.6.3"
}
}
Loading

0 comments on commit cec3ca1

Please sign in to comment.