-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |