Remove dead code (#160)

This commit is contained in:
Armani Ferrante 2021-07-27 19:33:50 -07:00 committed by GitHub
parent 06ae280d9c
commit a3b4cf7119
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 12 additions and 672 deletions

View File

@ -8,8 +8,6 @@ _defaults: &defaults
- docker
before_install:
- scripts/travis/run-docker.sh
install:
- mkdir -p bin && ./bpf-sdk-install.sh bin
before_cache:
- scripts/travis/stop-docker.sh
@ -25,11 +23,7 @@ _localnet: &localnet
jobs:
include:
- <<: *defaults
name: Dex unit tests
script:
- docker exec dev ./do.sh test dex
- <<: *defaults
name: Dex integration tests
name: Dex tests
<<: *localnet
script:
- docker exec dev ./scripts/travis/dex-tests.sh

View File

@ -2,13 +2,11 @@
members = [
"assert-owner",
"common",
"context",
"dex/crank",
"pool",
"pool/schema",
"pool/examples/admin-controlled",
"pool/examples/simple",
"cli"
]
exclude = [
"dex",

View File

@ -55,10 +55,7 @@ sudo apt-get install -y pkg-config build-essential python3-pip jq
### Install Solana
```bash
curl -sSf https://raw.githubusercontent.com/solana-labs/solana/v1.4.14/install/solana-install-init.sh | sh -s - v1.4.14
export PATH="/home/ubuntu/.local/share/solana/install/active_release/bin:$PATH"
```
Directions can be found [here](https://docs.solana.com/cli/install-solana-cli-tools#use-solanas-install-tool).
### Download the source
@ -66,27 +63,18 @@ export PATH="/home/ubuntu/.local/share/solana/install/active_release/bin:$PATH"
git clone https://github.com/project-serum/serum-dex.git
```
### Install the BPF SDK
```bash
./do.sh update
```
### Build, deploy, and test programs
See individual crates for documentation. For example, to build the dex see its [README](https://github.com/project-serum/serum-dex/tree/master/dex).
## Running a local Solana cluster
The easiest way to run a local cluster is to run the docker container provided by Solana.
Instructions can be found [here](https://solana-labs.github.io/solana-web3.js/). For local development, however, it's often convenient to build and run a validator from [source](https://github.com/solana-labs/solana#building).
The easiest way to run a local cluster is to use [solana-test-validator](https://docs.solana.com/developing/test-validator).
## Directories
* `assert-owner`: Solana utility program for checking account ownership.
* `cli`: Serum command line interface.
* `common`: Common rust utilities.
* `context`: Global environment used by Serum crates, read from a configuration file.
* `dex`: Serum DEX program and client utility.
* `docker`: Docker image definitions.
* `pool`: Serum pool protocol.

View File

@ -1,22 +0,0 @@
#!/usr/bin/env bash
set -e
installDir=$1
channel=v1.3.15
if [[ -n $2 ]]; then
channel=$2
fi
echo "Installing $channel BPF SDK into $installDir"
set -x
cd "$installDir/"
curl -L --retry 5 --retry-delay 2 -o bpf-sdk.tar.bz2 \
https://solana-sdk.s3.amazonaws.com/"$channel"/bpf-sdk.tar.bz2
rm -rf bpf-sdk
mkdir -p bpf-sdk
tar jxf bpf-sdk.tar.bz2
rm -f bpf-sdk.tar.bz2
cat bpf-sdk/version.txt

View File

@ -1,28 +0,0 @@
[package]
name = "serum-cli"
version = "0.1.0"
authors = ["armaniferrante <armaniferrante@gmail.com>"]
edition = "2018"
[[bin]]
name = "serum"
path = "src/main.rs"
[features]
dev = ["serum-common", "serum_dex", "solana-sdk", "serde_json", "spl-token", "solana-client"]
default = []
[dependencies]
anyhow = "1.0.32"
clap = { version = "3.0.0-beta.2", features = ["yaml"] }
crank = { path = "../dex/crank" }
serum-context = { path = "../context" }
# Dev feature flag.
serum-common = { path = "../common", optional = true }
serum_dex = { path = "../dex", default-features = false, features = ["client"], optional = true }
solana-sdk = { version = "1.3.14", optional = true }
serde_json = { version = "1.0.59", optional = true }
spl-token = { version = "2.0.6", optional = true }
solana-client = { version = "1.4.4", optional = true }

View File

@ -1,81 +0,0 @@
use anyhow::{anyhow, Result};
use serum_common::client::rpc;
use serum_context::Context;
use solana_client::rpc_config::RpcSendTransactionConfig;
use solana_sdk::commitment_config::CommitmentConfig;
use solana_sdk::instruction::{AccountMeta, Instruction};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Signer;
use solana_sdk::sysvar;
use solana_sdk::transaction::Transaction;
const FAUCET_SIZE: usize = 77;
// `admin` must be the current mint authority.
//
// Faucet program here:
//
// https://github.com/paul-schaaf/spl-token-faucet/blob/main/src/program/src/instruction.rs.
pub fn create(ctx: &Context, mint: Pubkey, amount: u64, admin: Pubkey) -> Result<Pubkey> {
let faucet_pid = ctx.faucet_pid.ok_or(anyhow!("faucet not provided"))?;
let faucet = rpc::create_account_rent_exempt(
&ctx.rpc_client(),
&ctx.wallet()?,
FAUCET_SIZE,
&faucet_pid,
)?
.pubkey();
let ixs = {
let (faucet_pda, _nonce) =
Pubkey::find_program_address(&["faucet".to_string().as_bytes()], &faucet_pid);
let set_auth_ix = spl_token::instruction::set_authority(
&spl_token::ID,
&mint,
Some(&faucet_pda),
spl_token::instruction::AuthorityType::MintTokens,
&admin,
&[],
)?;
let create_faucet_ix = {
let accounts = vec![
AccountMeta::new_readonly(mint, false),
AccountMeta::new(faucet, false),
AccountMeta::new(sysvar::rent::ID, false),
AccountMeta::new_readonly(admin, false),
];
let mut data = vec![0];
data.extend_from_slice(&amount.to_le_bytes());
Instruction {
program_id: faucet_pid,
data,
accounts,
}
};
[set_auth_ix, create_faucet_ix]
};
let _tx = {
let client = ctx.rpc_client();
let payer = ctx.wallet()?;
let (recent_hash, _fee_calc) = client.get_recent_blockhash()?;
let tx =
Transaction::new_signed_with_payer(&ixs, Some(&payer.pubkey()), &[&payer], recent_hash);
let sig = client.send_and_confirm_transaction_with_spinner_and_config(
&tx,
CommitmentConfig::single(),
RpcSendTransactionConfig {
skip_preflight: true,
..RpcSendTransactionConfig::default()
},
)?;
sig
};
Ok(faucet)
}

View File

@ -1,160 +0,0 @@
//! Misc dev utilities.
extern crate crank as serum_crank;
use anyhow::Result;
use clap::Clap;
use serum_common::client::rpc;
use serum_context::Context;
use serum_dex::instruction::NewOrderInstructionV1;
use serum_dex::matching::{OrderType, Side};
use serum_registry::client::Client;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Signer;
use std::num::NonZeroU64;
mod faucet;
#[derive(Debug, Clap)]
pub enum Command {
/// Creates 1) SRM mint, 2) MSRM mint 3) SRM funded token account, and
/// 4) MSRM funded token account, all owned by the configured wallet.
InitMint {
#[clap(short, long)]
faucet: bool,
},
AllocateAccount {
#[clap(short, long)]
program_id: Pubkey,
#[clap(short, long)]
size: usize,
},
GenerateOrders {
#[clap(short, long)]
coin_wallet: Pubkey,
#[clap(short, long)]
pc_wallet: Pubkey,
},
}
pub fn run(ctx: Context, cmd: Command) -> Result<()> {
match cmd {
Command::InitMint { faucet } => init_mint(&ctx, faucet),
Command::AllocateAccount { program_id, size } => allocate_account(&ctx, program_id, size),
Command::GenerateOrders {
coin_wallet,
pc_wallet,
} => generate_orders(&ctx, coin_wallet, pc_wallet),
}
}
fn init_mint(ctx: &Context, faucet: bool) -> Result<()> {
// Doesn't matter.
let program_id = Pubkey::new_from_array([0; 32]).to_string();
let payer_filepath = &ctx.wallet_path.to_string();
let cluster = ctx.cluster.to_string();
let (srm_faucet, msrm_faucet) = match faucet {
false => (None, None),
true => {
let srm_faucet =
faucet::create(ctx, g.srm_mint, 1_000_000_000_000, ctx.wallet().pubkey())?;
let msrm_faucet =
faucet::create(ctx, g.msrm_mint, 1_000_000_000_000, ctx.wallet().pubkey())?;
(Some(srm_faucet), Some(msrm_faucet))
}
};
println!(
"{}",
serde_json::json!({
"wallet": g.wallet.to_string(),
"srmMint": g.srm_mint.to_string(),
"msrmMint": g.msrm_mint.to_string(),
"god": g.god.to_string(),
"godMsrm": g.god_msrm.to_string(),
"godBalanceBefore": g.god_balance_before,
"godMsrmBalanceBefore": g.god_msrm_balance_before,
"godOwner": g.god_owner.to_string(),
"srmFaucet": match srm_faucet {
None => "null".to_string(),
Some(f) => f.to_string(),
},
"msrmFaucet": match msrm_faucet {
None => "null".to_string(),
Some(f) => f.to_string(),
}
})
);
Ok(())
}
fn allocate_account(ctx: &Context, program_id: Pubkey, size: usize) -> Result<()> {
let rpc_client = ctx.rpc_client();
let wallet = ctx.wallet().unwrap();
let pk = rpc::create_account_rent_exempt(&rpc_client, &wallet, size, &program_id)?.pubkey();
println!("{}", serde_json::json!({"account": pk.to_string()}));
Ok(())
}
fn generate_orders(ctx: &Context, coin_wallet: Pubkey, pc_wallet: Pubkey) -> Result<()> {
let client = ctx.rpc_client();
let market_keys = serum_crank::list_market(
&client,
&ctx.dex_pid,
&ctx.wallet()?,
&ctx.srm_mint,
&ctx.msrm_mint,
1_000_000,
10_000,
)?;
loop {
// Place bid.
let mut orders = None;
serum_crank::place_order(
&client,
&ctx.dex_pid,
&ctx.wallet()?,
&pc_wallet,
&market_keys,
&mut orders,
NewOrderInstructionV1 {
side: Side::Bid,
limit_price: NonZeroU64::new(500).unwrap(),
max_qty: NonZeroU64::new(1_000).unwrap(),
order_type: OrderType::Limit,
client_id: 019269,
},
)?;
// Place offer.
let mut orders = None;
serum_crank::place_order(
&client,
&ctx.dex_pid,
&ctx.wallet()?,
&coin_wallet,
&market_keys,
&mut orders,
NewOrderInstructionV1 {
side: Side::Ask,
limit_price: NonZeroU64::new(499).unwrap(),
max_qty: NonZeroU64::new(1_000).unwrap(),
order_type: OrderType::Limit,
client_id: 985982,
},
)?;
// Match orders.
std::thread::sleep(std::time::Duration::new(15, 0));
serum_crank::match_orders(
&client,
&ctx.dex_pid,
&ctx.wallet()?,
&market_keys,
&coin_wallet,
&pc_wallet,
)?;
}
}

View File

@ -1,51 +0,0 @@
use anyhow::Result;
use clap::Clap;
use crank as serum_crank;
use serum_context::{ConfigPath, Context};
#[cfg(feature = "dev")]
mod dev;
#[derive(Debug, Clap)]
#[clap(name = "Serum CLI")]
pub struct Opts {
#[clap(short, long, default_value)]
pub config: ConfigPath,
#[clap(subcommand)]
pub cmd: Command,
}
#[derive(Debug, Clap)]
pub enum Command {
/// Crank client.
Crank(serum_crank::Command),
/// Development utilities.
#[cfg(feature = "dev")]
Dev(dev::Command),
}
fn main() {
let opts = Opts::parse();
let ctx = Context::from_config(opts.config).unwrap_or_else(|e| {
println!("{}", e.to_string());
std::process::exit(1);
});
run(ctx, opts.cmd).unwrap_or_else(|e| {
println!("{}", e.to_string());
std::process::exit(1);
});
}
pub fn run(ctx: Context, cmd: Command) -> Result<()> {
match cmd {
Command::Crank(cmd) => serum_crank::start(
Some(ctx.clone()),
serum_crank::Opts {
cluster: ctx.cluster,
command: cmd,
},
),
#[cfg(feature = "dev")]
Command::Dev(dev_cmd) => dev::run(ctx, dev_cmd),
}
}

View File

@ -1,16 +0,0 @@
[package]
name = "serum-context"
version = "0.1.0"
description = "Serum Context"
repository = "https://github.com/project-serum/serum-dex"
edition = "2018"
[dependencies]
clap = "3.0.0-beta.1"
solana-sdk = "1.3.9"
anyhow = "1.0.32"
dirs = "3.0"
serde = "1.0"
serde_yaml = "0.8"
solana-client = "1.5.0"
serum-common = { path = "../common" }

View File

@ -1,106 +0,0 @@
//! serum-context defines the global state used by Serum crates, read from
//! a configuration file.
use anyhow::Result;
use anyhow::{anyhow, format_err};
use serde::{Deserialize, Serialize};
use serum_common::client::Cluster;
use solana_client::rpc_client::RpcClient;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Keypair;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::fs;
use std::str::FromStr;
#[derive(Clone, Debug)]
pub struct Context {
pub cluster: Cluster,
pub wallet_path: WalletPath,
pub srm_mint: Pubkey,
pub msrm_mint: Pubkey,
pub dex_pid: Pubkey,
pub faucet_pid: Option<Pubkey>,
}
impl Context {
pub fn from_config(path: ConfigPath) -> Result<Self> {
Config::from(&path.to_string())?.try_into()
}
pub fn rpc_client(&self) -> RpcClient {
RpcClient::new(self.cluster.url().to_string())
}
pub fn wallet(&self) -> Result<Keypair> {
solana_sdk::signature::read_keypair_file(&self.wallet_path.to_string())
.map_err(|_| anyhow!("Unable to read provided wallet file"))
}
}
// Config represents the data read from a config file.
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Config {
pub wallet_path: Option<String>,
pub network: Network,
pub mints: Mints,
pub programs: Programs,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Network {
pub cluster: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Mints {
pub srm: String,
pub msrm: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Programs {
pub dex_pid: String,
pub faucet_pid: Option<String>,
}
impl Config {
fn from(path: &str) -> Result<Self> {
let rdr = fs::File::open(path)?;
serde_yaml::from_reader(&rdr)
.map_err(|e| format_err!("Unable to read yaml config: {}", e.to_string()))
}
}
impl TryFrom<Config> for Context {
type Error = anyhow::Error;
fn try_from(cfg: Config) -> std::result::Result<Self, anyhow::Error> {
let cluster = cfg
.network
.cluster
.map_or(Ok(Default::default()), |c| c.parse())?;
let faucet_pid = cfg
.programs
.faucet_pid
.or_else(|| match &cluster {
Cluster::Devnet => Some("4bXpkKSV8swHSnwqtzuboGPaPDeEgAn4Vt8GfarV5rZt".to_string()),
_ => None,
})
.map(|f| f.parse().unwrap());
Ok(Self {
cluster,
wallet_path: cfg
.wallet_path
.map_or(Default::default(), |p| WalletPath(p)),
srm_mint: cfg.mints.srm.parse()?,
msrm_mint: cfg.mints.msrm.parse()?,
dex_pid: cfg.programs.dex_pid.parse()?,
faucet_pid,
})
}
}
// Declare our default file paths, relative to the home directory.
serum_common::home_path!(ConfigPath, ".config/serum/cli/config.yaml");
serum_common::home_path!(WalletPath, ".config/solana/id.json");
serum_common::home_path!(DataDirPath, ".config/serum/cli/data/");

View File

@ -1,5 +1,3 @@
# Note: This crate must be built using do.sh
[package]
name = "serum_dex"
version = "0.3.1"

View File

@ -10,7 +10,6 @@ path = "src/bin/main.rs"
[dependencies]
serum_dex = { path = "../", default-features = false, features = ["client"] }
serum-common = { path = "../../common", features = ["client"] }
serum-context = { path = "../../context" }
spl-token = { version = "3.0.0-pre1", features = ["no-entrypoint"], default-features = false }
clap = "3.0.0-beta.1"
enumflags2 = "0.6.4"

View File

@ -4,5 +4,5 @@ use crank::Opts;
fn main() -> Result<()> {
let opts = Opts::parse();
crank::start(None, opts)
crank::start(opts)
}

View File

@ -40,7 +40,6 @@ use serum_common::client::rpc::{
create_and_init_mint, create_token_account, mint_to_new_account, send_txn, simulate_transaction,
};
use serum_common::client::Cluster;
use serum_context::Context;
use serum_dex::instruction::{
cancel_order_by_client_order_id as cancel_order_by_client_order_id_ix,
close_open_orders as close_open_orders_ix, init_open_orders as init_open_orders_ix,
@ -200,7 +199,7 @@ pub enum Command {
},
}
pub fn start(ctx: Option<Context>, opts: Opts) -> Result<()> {
pub fn start(opts: Opts) -> Result<()> {
let client = opts.client();
match opts.command {

178
do.sh
View File

@ -1,178 +0,0 @@
#!/usr/bin/env bash
cd "$(dirname "$0")"
usage() {
cat <<EOF
Usage: do.sh <action> <project> <action specific arguments>
Supported actions:
build
build-lib
clean
clippy
doc
dump
fmt
test
update
Supported projects:
all
any directory containing a Cargo.toml file
EOF
}
sdkParentDir=bin
sdkDir="$sdkParentDir"/bpf-sdk
profile=bpfel-unknown-unknown/release
perform_action() {
set -e
projectDir="$PWD"/$2
targetDir="$projectDir"/target
if ! [[ -f "$projectDir"/Cargo.lock ]]; then
# Program is part of workspace
targetDir="$PWD"/target
fi
features=
if [[ -f "$projectDir"/Xargo.toml ]]; then
features="--features=program"
fi
case "$1" in
build)
if [[ -f "$projectDir"/Xargo.toml ]]; then
"$sdkDir"/rust/build.sh "$projectDir"
so_path="$targetDir/$profile"
so_name="serum_${2//[-\/]/_}"
cp "$so_path/${so_name}.so" "$so_path/${so_name}_debug.so"
"$sdkDir"/dependencies/llvm-native/bin/llvm-objcopy --strip-all "$so_path/${so_name}.so" "$so_path/$so_name.so"
else
echo "$projectDir does not contain a program, skipping"
fi
;;
build-lib)
(
cd "$projectDir"
echo "build $projectDir"
export RUSTFLAGS="${@:3}"
cargo build
)
;;
clean)
"$sdkDir"/rust/clean.sh "$projectDir"
;;
clippy)
(
cd "$projectDir"
echo "clippy $projectDir"
cargo +nightly clippy $features ${@:3}
)
;;
doc)
(
cd "$projectDir"
echo "generating docs $projectDir"
cargo doc $features ${@:3}
)
;;
dump)
# Dump depends on tools that are not installed by default and must be installed manually
# - readelf
# - rustfilt
(
pwd
"$0" build "$2"
so_path="$targetDir/$profile"
so_name="serum_${2//\-/_}"
so="$so_path/${so_name}_debug.so"
dump="$so_path/${so_name}_dump"
echo $so_path
echo $so_name
echo $so
echo $dump
if [ -f "$so" ]; then
ls \
-la \
"$so" \
>"${dump}_mangled.txt"
readelf \
-aW \
"$so" \
>>"${dump}_mangled.txt"
"$sdkDir/dependencies/llvm-native/bin/llvm-objdump" \
-print-imm-hex \
--source \
--disassemble \
"$so" \
>>"${dump}_mangled.txt"
sed \
s/://g \
<"${dump}_mangled.txt" |
rustfilt \
>"${dump}.txt"
else
echo "Warning: No dump created, cannot find: $so"
fi
)
;;
fmt)
(
cd "$projectDir"
echo "formatting $projectDir"
cargo fmt ${@:3}
)
;;
help)
usage
exit
;;
test)
(
cd "$projectDir"
echo "test $projectDir"
cargo test "$features,test" ${@:3}
)
;;
update)
mkdir -p $sdkParentDir
./bpf-sdk-install.sh $sdkParentDir
./do.sh clean all
;;
*)
echo "Error: Unknown command"
usage
exit
;;
esac
}
set -e
if [[ $1 == "update" ]]; then
perform_action "$1"
exit
else
if [[ "$#" -lt 2 ]]; then
usage
exit
fi
if [[ ! -d "$sdkDir" ]]; then
./do.sh update
fi
fi
if [[ $2 == "all" ]]; then
# Perform operation on all projects
for project in */; do
if [[ -f "$project"Cargo.toml ]]; then
perform_action "$1" "${project%/}" ${@:3}
else
continue
fi
done
else
# Perform operation on requested project
perform_action "$1" "$2" "${@:3}"
fi

View File

@ -29,7 +29,13 @@ main() {
yes | solana airdrop --url $CLUSTER_URL 100
set -e
#
# Run the tests.
# Run the unit tests.
#
pushd dex
cargo test
popd
#
# Run the integration tests.
#
dex_whole_shebang
}