Skip to content

Commit

Permalink
🦀 market-making-tool is done by ys
Browse files Browse the repository at this point in the history
  • Loading branch information
en committed Sep 22, 2019
1 parent 25f0e9f commit 78a42bd
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pwc-node/market-making-tool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[package]
name = "market-making-tool"
version = "0.1.0"
authors = ["Yuanchao Sun <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "~2.32", features = ["yaml"] }
tokio = { version = "0.1.11" }
url = "1.7"
substrate-subxt = { git="https://github.com/en/substrate-subxt.git", branch="superfluid" }
futures = "0.1.17"
futures03 = { package = "futures-preview", version = "=0.3.0-alpha.17", features = ["compat"] }

[dependencies.pwc-node-runtime]
path = '../runtime'

[dependencies.codec]
package = 'parity-scale-codec'
version = '1.0.0'

[dependencies.srml-system]
default_features = false
git = 'https://github.com/paritytech/substrate.git'
package = 'srml-system'
rev = '7276eeab7da8b78f007a99129aad6e89e9d588c7'

[dependencies.srml-balances]
git = 'https://github.com/paritytech/substrate.git'
package = 'srml-balances'
rev = '7276eeab7da8b78f007a99129aad6e89e9d588c7'

[dependencies.keyring]
git = 'https://github.com/paritytech/substrate.git'
package = 'substrate-keyring'
rev = '7276eeab7da8b78f007a99129aad6e89e9d588c7'

[dependencies.runtime-primitives]
git = 'https://github.com/paritytech/substrate.git'
package = 'sr-primitives'
rev = '7276eeab7da8b78f007a99129aad6e89e9d588c7'

[dependencies.substrate-primitives]
default_features = false
git = 'https://github.com/paritytech/substrate.git'
package = 'substrate-primitives'
rev = '7276eeab7da8b78f007a99129aad6e89e9d588c7'
10 changes: 10 additions & 0 deletions pwc-node/market-making-tool/src/cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: market-making-tool
author: "Yuanchao Sun <[email protected]>"
subcommands:
- run:
args:
- addr:
long: addr
help: The websocket address of superfluid chain.
takes_value: true
required: true
167 changes: 167 additions & 0 deletions pwc-node/market-making-tool/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
use clap::load_yaml;
use codec::Decode;
use futures::stream::Stream;
use futures::Future;
use keyring::AccountKeyring;
use pwc_node_runtime::{self, uniswap::RawEvent as UniswapEvent, uniswap::Trait as UniswapTrait};
use runtime_primitives::generic::Era;
use substrate_primitives::crypto::Pair;
use substrate_subxt::{
srml::{
superfluid::{Superfluid, SuperfluidXt},
system::System,
},
ClientBuilder,
};
use url::Url;

fn main() {
let yaml = load_yaml!("cli.yml");
let matches = clap::App::from_yaml(yaml)
.version(env!("CARGO_PKG_VERSION"))
.get_matches();

execute(matches)
}

fn print_usage(matches: &clap::ArgMatches) {
println!("{}", matches.usage());
}

fn execute(matches: clap::ArgMatches) {
match matches.subcommand() {
("run", Some(matches)) => {
let addr = matches
.value_of("addr")
.expect("The address of superfluid chain is required; thus it can't be None; qed");
let addr = Url::parse(&format!("ws://{}", addr)).expect("Is valid url; qed");

let mut rt = tokio::runtime::Runtime::new().unwrap();
let executor = rt.executor();
let client_future = ClientBuilder::<Runtime>::new()
.set_url(addr.clone())
.build();
let client = rt.block_on(client_future).unwrap();

let stream = rt.block_on(client.subscribe_events()).unwrap();
let block_events = stream
.for_each(move |change_set| {
change_set
.changes
.iter()
.filter_map(|(_key, data)| {
data.as_ref().map(|data| Decode::decode(&mut &data.0[..]))
})
.for_each(
|result: Result<
Vec<
srml_system::EventRecord<
<Runtime as System>::Event,
<Runtime as System>::Hash,
>,
>,
codec::Error,
>| {
let _ = result.map(|events| {
events.into_iter().for_each(|event| match event.event {
pwc_node_runtime::Event::uniswap(
UniswapEvent::ReserveChanged(1, balance),
) => {
println!(
"The balance of asset 1 has changed to {:?}",
balance
);
if balance <= 80 {
let signer = AccountKeyring::Bob.pair();
let add_liquidity = ClientBuilder::<Runtime>::new()
.set_url(addr.clone())
.build()
.and_then(move |client| client.xt(signer, None))
.and_then(move |xt| {
xt.superfluid(|calls| {
calls.add_liquidity(1, 2, 20, 0)
})
.submit()
})
.map(|_| ())
.map_err(|e| println!("{:?}", e));

println!("add 20 liquidity to asset 1");
executor.spawn(add_liquidity);
} else if balance >= 120 {
let swap = ClientBuilder::<Runtime>::new()
.set_url(addr.clone())
.build()
.and_then(move |client| {
client.xt(AccountKeyring::Bob.pair(), None)
})
.and_then(move |xt| {
xt.superfluid(|calls| {
calls.swap_assets_with_exact_output(
AccountKeyring::Bob.pair().public(),
0,
1,
20,
20,
)
})
.submit()
})
.map(|_| ())
.map_err(|e| println!("{:?}", e));

println!("swap 20 asset 1 for profit");
executor.spawn(swap);
}
}
_ => {}
})
});
},
);
Ok(())
})
.map_err(|e| println!("{:?}", e));
rt.spawn(block_events);
rt.shutdown_on_idle().wait().unwrap();
}
_ => print_usage(&matches),
}
}

struct Runtime;

impl System for Runtime {
type Index = <pwc_node_runtime::Runtime as srml_system::Trait>::Index;
type BlockNumber = <pwc_node_runtime::Runtime as srml_system::Trait>::BlockNumber;
type Hash = <pwc_node_runtime::Runtime as srml_system::Trait>::Hash;
type Hashing = <pwc_node_runtime::Runtime as srml_system::Trait>::Hashing;
type AccountId = <pwc_node_runtime::Runtime as srml_system::Trait>::AccountId;
type Lookup = <pwc_node_runtime::Runtime as srml_system::Trait>::Lookup;
type Header = <pwc_node_runtime::Runtime as srml_system::Trait>::Header;
type Event = <pwc_node_runtime::Runtime as srml_system::Trait>::Event;

type SignedExtra = (
srml_system::CheckVersion<pwc_node_runtime::Runtime>,
srml_system::CheckGenesis<pwc_node_runtime::Runtime>,
srml_system::CheckEra<pwc_node_runtime::Runtime>,
srml_system::CheckNonce<pwc_node_runtime::Runtime>,
srml_system::CheckWeight<pwc_node_runtime::Runtime>,
srml_balances::TakeFees<pwc_node_runtime::Runtime>,
);
fn extra(nonce: Self::Index) -> Self::SignedExtra {
(
srml_system::CheckVersion::<pwc_node_runtime::Runtime>::new(),
srml_system::CheckGenesis::<pwc_node_runtime::Runtime>::new(),
srml_system::CheckEra::<pwc_node_runtime::Runtime>::from(Era::Immortal),
srml_system::CheckNonce::<pwc_node_runtime::Runtime>::from(nonce),
srml_system::CheckWeight::<pwc_node_runtime::Runtime>::new(),
srml_balances::TakeFees::<pwc_node_runtime::Runtime>::from(0),
)
}
}

impl Superfluid for Runtime {
type Balance = <pwc_node_runtime::Runtime as UniswapTrait>::Balance;
type AssetId = <pwc_node_runtime::Runtime as UniswapTrait>::AssetId;
}

0 comments on commit 78a42bd

Please sign in to comment.