Skip to content

Commit cec3ca1

Browse files
add tokens/escrow/steel #266
Co-authored-by: Ayush <[email protected]>
1 parent c0cc430 commit cec3ca1

18 files changed

+2297
-0
lines changed

tokens/escrow/steel/Cargo.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[workspace]
2+
resolver = "2"
3+
members = ["api", "program"]
4+
5+
[workspace.package]
6+
version = "0.1.0"
7+
edition = "2021"
8+
license = "Apache-2.0"
9+
homepage = ""
10+
documentation = ""
11+
repository = ""
12+
readme = "./README.md"
13+
keywords = ["solana"]
14+
15+
[workspace.dependencies]
16+
escrow-api = { path = "./api", version = "0.1.0" }
17+
bytemuck = "1.14"
18+
num_enum = "0.7"
19+
solana-program = "1.18"
20+
steel = { version = "2.0", features = ["spl"] }
21+
thiserror = "1.0"
22+
spl-token = "^4"

tokens/escrow/steel/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Escrow
2+
3+
**Escrow** is a ...
4+
5+
## API
6+
- [`Consts`](api/src/consts.rs) – Program constants.
7+
- [`Instruction`](api/src/instruction.rs) – Declared instructions.
8+
9+
## Instructions
10+
- [`MakeOffer`](program/src/make_offer.rs) – Make an offer ...
11+
- [`TakerOffer`](program/src/take_offer.rs) – Take an offer ...
12+
13+
## State
14+
- [`Offer`](api/src/state/offer.rs) – Offer state ...
15+
16+
## How to?
17+
18+
Compile your program:
19+
20+
```sh
21+
pnpm build
22+
```
23+
24+
Run tests:
25+
26+
```sh
27+
pnpm test
28+
```
29+
30+
Run build and test
31+
32+
```sh
33+
pnpm build-and-test
34+
```
35+
36+
Deploy your program:
37+
38+
```sh
39+
pnpm deploy
40+
```

tokens/escrow/steel/api/Cargo.toml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "escrow-api"
3+
description = "API for interacting with the Escrow program"
4+
version.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
homepage.workspace = true
8+
documentation.workspace = true
9+
repository.workspace = true
10+
readme.workspace = true
11+
keywords.workspace = true
12+
13+
[dependencies]
14+
bytemuck.workspace = true
15+
num_enum.workspace = true
16+
solana-program.workspace = true
17+
steel.workspace = true
18+
thiserror.workspace = true
19+
spl-token.workspace = true

tokens/escrow/steel/api/src/consts.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use solana_program::pubkey;
2+
use steel::Pubkey;
3+
4+
/// Seed of the offer account PDA.
5+
pub const OFFER_SEED: &[u8] = b"offer";
6+
7+
pub const ASSOCIATED_TOKEN_PROGRAM_ID: Pubkey =
8+
pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use steel::*;
2+
3+
#[repr(u8)]
4+
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
5+
pub enum EscrowInstruction {
6+
MakeOffer = 0,
7+
TakerOffer = 1,
8+
}
9+
10+
#[repr(C)]
11+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
12+
pub struct MakeOffer {
13+
pub id: [u8; 8],
14+
pub token_a_offered_amount: [u8; 8],
15+
pub token_b_wanted_amount: [u8; 8],
16+
}
17+
18+
#[repr(C)]
19+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
20+
pub struct TakerOffer {}
21+
22+
instruction!(EscrowInstruction, MakeOffer);
23+
instruction!(EscrowInstruction, TakerOffer);

tokens/escrow/steel/api/src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
pub mod consts;
2+
pub mod instruction;
3+
pub mod sdk;
4+
pub mod state;
5+
6+
pub mod prelude {
7+
pub use crate::consts::*;
8+
pub use crate::instruction::*;
9+
pub use crate::sdk::*;
10+
pub use crate::state::*;
11+
}
12+
13+
use steel::*;
14+
15+
// TODO Set program id
16+
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");

tokens/escrow/steel/api/src/sdk.rs

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use steel::*;
2+
3+
use crate::prelude::*;
4+
5+
pub fn make_offer(
6+
maker: Pubkey,
7+
mint_a: Pubkey,
8+
mint_b: Pubkey,
9+
id: u64,
10+
token_a_offered_amount: u64,
11+
token_b_wanted_amount: u64,
12+
) -> Instruction {
13+
let (maker_token_account_a, _) = Pubkey::find_program_address(
14+
&[maker.as_ref(), spl_token::ID.as_ref(), mint_a.as_ref()],
15+
&spl_token::ID,
16+
);
17+
18+
let offer = offer_pda(maker, id).0;
19+
let (vault, _) = Pubkey::find_program_address(
20+
&[offer.as_ref(), spl_token::ID.as_ref(), mint_a.as_ref()],
21+
&spl_token::ID,
22+
);
23+
Instruction {
24+
program_id: crate::ID,
25+
accounts: vec![
26+
AccountMeta::new(maker, true),
27+
AccountMeta::new_readonly(mint_a, false),
28+
AccountMeta::new_readonly(mint_b, false),
29+
AccountMeta::new(maker_token_account_a, false),
30+
AccountMeta::new(offer, false),
31+
AccountMeta::new(vault, false),
32+
AccountMeta::new_readonly(spl_token::ID, false),
33+
AccountMeta::new_readonly(system_program::ID, false),
34+
AccountMeta::new_readonly(ASSOCIATED_TOKEN_PROGRAM_ID, false),
35+
],
36+
data: MakeOffer {
37+
id: id.to_le_bytes(),
38+
token_a_offered_amount: token_a_offered_amount.to_le_bytes(),
39+
token_b_wanted_amount: token_b_wanted_amount.to_le_bytes(),
40+
}
41+
.to_bytes(),
42+
}
43+
}
44+
45+
pub fn take_offer(
46+
taker: Pubkey,
47+
maker: Pubkey,
48+
mint_a: Pubkey,
49+
mint_b: Pubkey,
50+
offer: Pubkey,
51+
) -> Instruction {
52+
let (taker_token_account_a, _) = Pubkey::find_program_address(
53+
&[taker.as_ref(), spl_token::ID.as_ref(), mint_a.as_ref()],
54+
&spl_token::ID,
55+
);
56+
let (taker_token_account_b, _) = Pubkey::find_program_address(
57+
&[taker.as_ref(), spl_token::ID.as_ref(), mint_b.as_ref()],
58+
&spl_token::ID,
59+
);
60+
let (maker_token_account_b, _) = Pubkey::find_program_address(
61+
&[maker.as_ref(), spl_token::ID.as_ref(), mint_b.as_ref()],
62+
&spl_token::ID,
63+
);
64+
let (vault, _) = Pubkey::find_program_address(
65+
&[offer.as_ref(), spl_token::ID.as_ref(), mint_a.as_ref()],
66+
&spl_token::ID,
67+
);
68+
Instruction {
69+
program_id: crate::ID,
70+
accounts: vec![
71+
AccountMeta::new(taker, true),
72+
AccountMeta::new(maker, false),
73+
AccountMeta::new_readonly(mint_a, false),
74+
AccountMeta::new_readonly(mint_b, false),
75+
AccountMeta::new(taker_token_account_a, false),
76+
AccountMeta::new(taker_token_account_b, false),
77+
AccountMeta::new(maker_token_account_b, false),
78+
AccountMeta::new(offer, false),
79+
AccountMeta::new(vault, false),
80+
AccountMeta::new_readonly(spl_token::ID, false),
81+
AccountMeta::new_readonly(system_program::ID, false),
82+
AccountMeta::new_readonly(ASSOCIATED_TOKEN_PROGRAM_ID, false),
83+
],
84+
data: TakerOffer {}.to_bytes(),
85+
}
86+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
mod offer;
2+
3+
pub use offer::*;
4+
5+
use steel::*;
6+
7+
#[repr(u8)]
8+
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
9+
pub enum EscrowAccount {
10+
Offer = 0,
11+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use steel::*;
2+
3+
use crate::consts::OFFER_SEED;
4+
5+
use super::EscrowAccount;
6+
7+
/// Fetch PDA of the counter account.
8+
pub fn offer_pda(maker: Pubkey, id: u64) -> (Pubkey, u8) {
9+
Pubkey::find_program_address(
10+
&[OFFER_SEED, maker.as_ref(), id.to_le_bytes().as_ref()],
11+
&crate::id(),
12+
)
13+
}
14+
15+
#[repr(C)]
16+
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
17+
pub struct Offer {
18+
pub id: [u8; 8],
19+
pub maker: Pubkey,
20+
pub token_mint_a: Pubkey,
21+
pub token_mint_b: Pubkey,
22+
pub token_b_wanted_amount: [u8; 8],
23+
pub bump: u8,
24+
}
25+
26+
account!(EscrowAccount, Offer);

tokens/escrow/steel/package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "steel",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/*.test.ts",
8+
"build-and-test": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./tests/fixtures && pnpm test",
9+
"build": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so",
10+
"deploy": "solana program deploy ./program/target/so/account_data_program.so"
11+
},
12+
"keywords": [],
13+
"author": "Leo Pham <[email protected]>",
14+
"license": "ISC",
15+
"dependencies": {
16+
"@solana/spl-token": "^0.4.9",
17+
"@solana/web3.js": "^1.95.4",
18+
"bs58": "^6.0.0"
19+
},
20+
"devDependencies": {
21+
"@types/chai": "^4.3.7",
22+
"@types/mocha": "^10.0.9",
23+
"@types/node": "^22.7.9",
24+
"borsh": "^2.0.0",
25+
"chai": "^4.3.7",
26+
"mocha": "^10.7.3",
27+
"solana-bankrun": "^0.4.0",
28+
"ts-mocha": "^10.0.0",
29+
"typescript": "^5.6.3"
30+
}
31+
}

0 commit comments

Comments
 (0)