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/token-swap/steel #267

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
23 changes: 23 additions & 0 deletions tokens/token-swap/steel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[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]
token-swap-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"
spl-math = { version = "0.3.0", features = ["no-entrypoint"] }
45 changes: 45 additions & 0 deletions tokens/token-swap/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Token swap example amm in Steel

**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.

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

## Instructions
- [`CreateAmm`](program/src/create_amm.rs) – Create amm ...
- [`CreatePool`](program/src/create_pool.rs) – Create liquidity pool
- [`DepositLiquidity`](program/src/deposit_liquidity.rs) – Desposit liquidity to pool
- [`WithdrawLiquidity`](program/src/withdraw_liquidity.rs) – Withdraw liquidity from pool
- [`Swap`](program/src/swap.rs) – Swap exact token amount

## State
- [`Amm`](api/src/state/amm.rs) – Amm state
- [`Pool`](api/src/state/pool.rs) – Pool state

## How to run

Compile your program:

```sh
pnpm build
```

Run unit and integration tests:

```sh
pnpm test
```

Run build and test

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

Deploy your program:

```sh
pnpm deploy
```
20 changes: 20 additions & 0 deletions tokens/token-swap/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "token-swap-api"
description = "API for interacting with the TokenSwap 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-math.workspace = true
11 changes: 11 additions & 0 deletions tokens/token-swap/steel/api/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use solana_program::pubkey;
use steel::Pubkey;

pub const MINIMUM_LIQUIDITY: u64 = 100;

pub const AUTHORITY_SEED: &[u8] = b"authority";

pub const LIQUIDITY_SEED: &[u8] = b"liquidity";

pub const ASSOCIATED_TOKEN_PROGRAM_ID: Pubkey =
pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
20 changes: 20 additions & 0 deletions tokens/token-swap/steel/api/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use steel::*;

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
#[repr(u32)]
pub enum TokenSwapError {
#[error("Invalid fee, must be between 0 and 10000")]
InvalidFee = 0,
#[error("Account is not existed")]
AccountIsNotExisted = 1,
#[error("Invalid account")]
InvalidAccount = 2,
#[error("Deposit too small")]
DepositTooSmall = 3,
#[error("Withdrawal too small")]
OutputTooSmall,
#[error("Invariant violated")]
InvariantViolated,
}

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

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum TokenSwapInstruction {
CreateAmm = 0,
CreatePool = 1,
DepositLiquidity = 2,
WithdrawLiquidity = 3,
Swap = 4,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct CreateAmm {
pub id: Pubkey,
pub fee: [u8; 2],
}

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

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct DepositLiquidity {
pub amount_a: [u8; 8],
pub amount_b: [u8; 8],
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct WithdrawLiquidity {
pub amount: [u8; 8],
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Swap {
pub swap_a: u8,
pub input_amount: [u8; 8],
pub min_output_amount: [u8; 8],
}

instruction!(TokenSwapInstruction, CreateAmm);
instruction!(TokenSwapInstruction, CreatePool);
instruction!(TokenSwapInstruction, DepositLiquidity);
instruction!(TokenSwapInstruction, WithdrawLiquidity);
instruction!(TokenSwapInstruction, Swap);
18 changes: 18 additions & 0 deletions tokens/token-swap/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");
Loading
Loading