Skip to content

Commit 5bdafb4

Browse files
authored
add tokens/token-swap/steel (#267)
1 parent 2e3800c commit 5bdafb4

25 files changed

+3667
-0
lines changed

tokens/token-swap/steel/Cargo.toml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
token-swap-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"
23+
spl-math = { version = "0.3.0", features = ["no-entrypoint"] }

tokens/token-swap/steel/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Token swap example amm in Steel
2+
3+
**TokenSwap** - Your Gateway to Effortless Trading! Welcome to the world of Automated Market Makers (AMM), where seamless trading is made possible with the power of automation. The primary goal of AMMs is to act as automatic buyers and sellers, readily available whenever users wish to trade their assets.
4+
5+
## API
6+
- [`Consts`](api/src/consts.rs) – Program constants.
7+
- [`Error`](api/src/error.rs) – Custom program errors.
8+
- [`Instruction`](api/src/instruction.rs) – Declared instructions.
9+
10+
## Instructions
11+
- [`CreateAmm`](program/src/create_amm.rs) – Create amm ...
12+
- [`CreatePool`](program/src/create_pool.rs) – Create liquidity pool
13+
- [`DepositLiquidity`](program/src/deposit_liquidity.rs) – Desposit liquidity to pool
14+
- [`WithdrawLiquidity`](program/src/withdraw_liquidity.rs) – Withdraw liquidity from pool
15+
- [`Swap`](program/src/swap.rs) – Swap exact token amount
16+
17+
## State
18+
- [`Amm`](api/src/state/amm.rs) – Amm state
19+
- [`Pool`](api/src/state/pool.rs) – Pool state
20+
21+
## How to run
22+
23+
Compile your program:
24+
25+
```sh
26+
pnpm build
27+
```
28+
29+
Run unit and integration tests:
30+
31+
```sh
32+
pnpm test
33+
```
34+
35+
Run build and test
36+
37+
```sh
38+
pnpm build-and-test
39+
```
40+
41+
Deploy your program:
42+
43+
```sh
44+
pnpm deploy
45+
```
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "token-swap-api"
3+
description = "API for interacting with the TokenSwap 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
20+
spl-math.workspace = true
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use solana_program::pubkey;
2+
use steel::Pubkey;
3+
4+
pub const MINIMUM_LIQUIDITY: u64 = 100;
5+
6+
pub const AUTHORITY_SEED: &[u8] = b"authority";
7+
8+
pub const LIQUIDITY_SEED: &[u8] = b"liquidity";
9+
10+
pub const ASSOCIATED_TOKEN_PROGRAM_ID: Pubkey =
11+
pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use steel::*;
2+
3+
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
4+
#[repr(u32)]
5+
pub enum TokenSwapError {
6+
#[error("Invalid fee, must be between 0 and 10000")]
7+
InvalidFee = 0,
8+
#[error("Account is not existed")]
9+
AccountIsNotExisted = 1,
10+
#[error("Invalid account")]
11+
InvalidAccount = 2,
12+
#[error("Deposit too small")]
13+
DepositTooSmall = 3,
14+
#[error("Withdrawal too small")]
15+
OutputTooSmall,
16+
#[error("Invariant violated")]
17+
InvariantViolated,
18+
}
19+
20+
error!(TokenSwapError);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use steel::*;
2+
3+
#[repr(u8)]
4+
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
5+
pub enum TokenSwapInstruction {
6+
CreateAmm = 0,
7+
CreatePool = 1,
8+
DepositLiquidity = 2,
9+
WithdrawLiquidity = 3,
10+
Swap = 4,
11+
}
12+
13+
#[repr(C)]
14+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
15+
pub struct CreateAmm {
16+
pub id: Pubkey,
17+
pub fee: [u8; 2],
18+
}
19+
20+
#[repr(C)]
21+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
22+
pub struct CreatePool {}
23+
24+
#[repr(C)]
25+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
26+
pub struct DepositLiquidity {
27+
pub amount_a: [u8; 8],
28+
pub amount_b: [u8; 8],
29+
}
30+
31+
#[repr(C)]
32+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
33+
pub struct WithdrawLiquidity {
34+
pub amount: [u8; 8],
35+
}
36+
37+
#[repr(C)]
38+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
39+
pub struct Swap {
40+
pub swap_a: u8,
41+
pub input_amount: [u8; 8],
42+
pub min_output_amount: [u8; 8],
43+
}
44+
45+
instruction!(TokenSwapInstruction, CreateAmm);
46+
instruction!(TokenSwapInstruction, CreatePool);
47+
instruction!(TokenSwapInstruction, DepositLiquidity);
48+
instruction!(TokenSwapInstruction, WithdrawLiquidity);
49+
instruction!(TokenSwapInstruction, Swap);
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod consts;
2+
pub mod error;
3+
pub mod instruction;
4+
pub mod sdk;
5+
pub mod state;
6+
7+
pub mod prelude {
8+
pub use crate::consts::*;
9+
pub use crate::error::*;
10+
pub use crate::instruction::*;
11+
pub use crate::sdk::*;
12+
pub use crate::state::*;
13+
}
14+
15+
use steel::*;
16+
17+
// TODO Set program id
18+
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");

0 commit comments

Comments
 (0)