-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added batch, open and close order commands
- Loading branch information
Showing
7 changed files
with
194 additions
and
25 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 |
---|---|---|
@@ -1,11 +1,72 @@ | ||
use crate::utils::{setup, validate_contract_id}; | ||
use clap::Args; | ||
use fuels::{ | ||
accounts::ViewOnlyAccount, | ||
types::{AssetId, Bits256}, | ||
}; | ||
use spark_market_sdk::MarketContract; | ||
|
||
#[derive(Args, Clone)] | ||
#[command(about = "TODO")] | ||
pub(crate) struct BatchCommand {} | ||
#[command(about = "Matches orders")] | ||
pub(crate) struct BatchCommand { | ||
/// The b256 id of the order | ||
#[clap(long)] | ||
pub(crate) order_id: String, | ||
|
||
/// The b256 ids for orders to match against `order_id` | ||
#[clap(long)] | ||
pub(crate) order_ids: Vec<String>, | ||
|
||
/// The contract id of the market | ||
#[clap(long)] | ||
pub(crate) contract_id: String, | ||
|
||
/// The URL to query | ||
/// Ex. beta-5.fuel.network | ||
#[clap(long)] | ||
pub(crate) rpc: String, | ||
} | ||
|
||
impl BatchCommand { | ||
pub(crate) fn run(&self) -> anyhow::Result<()> { | ||
pub(crate) async fn run(&self) -> anyhow::Result<()> { | ||
let wallet = setup(&self.rpc).await?; | ||
let contract_id = validate_contract_id(&self.contract_id)?; | ||
|
||
let order_id = Bits256::from_hex_str(&self.order_id)?; | ||
|
||
if self.order_id.len() as u64 != 64 { | ||
anyhow::bail!("Invalid order id length"); | ||
} | ||
|
||
if self.order_ids.is_empty() { | ||
anyhow::bail!("At least one order ID must be added to the list of ids"); | ||
} | ||
|
||
let mut order_ids: Vec<Bits256> = Vec::with_capacity(self.order_ids.len()); | ||
for id in self.order_ids.iter() { | ||
if id.len() as u64 != 64 { | ||
anyhow::bail!("Invalid order id length: {}", id); | ||
} | ||
|
||
order_ids.push(Bits256::from_hex_str(id)?); | ||
} | ||
|
||
// Initial balance prior to contract call - used to calculate contract interaction cost | ||
let balance = wallet.get_asset_balance(&AssetId::BASE).await?; | ||
|
||
// Connect to the deployed contract via the rpc | ||
let contract = MarketContract::new(contract_id, wallet.clone()).await; | ||
|
||
let _ = contract.batch_fulfill(order_id, order_ids).await?; | ||
|
||
// Balance post-call | ||
let new_balance = wallet.get_asset_balance(&AssetId::BASE).await?; | ||
|
||
// TODO: replace println with tracing | ||
println!("\nContract call cost: {}", balance - new_balance); | ||
// TODO: adjust contract to inform which orders have not been fulfilled and report here? | ||
// this could be via a return value of incomplete orders | ||
|
||
Ok(()) | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,11 +1,52 @@ | ||
use crate::utils::{setup, validate_contract_id}; | ||
use clap::Args; | ||
use fuels::{ | ||
accounts::ViewOnlyAccount, | ||
types::{AssetId, Bits256}, | ||
}; | ||
use spark_market_sdk::MarketContract; | ||
|
||
#[derive(Args, Clone)] | ||
#[command(about = "TODO")] | ||
pub(crate) struct CloseCommand {} | ||
#[command(about = "Closes an open order")] | ||
pub(crate) struct CloseCommand { | ||
/// The b256 id of the order | ||
#[clap(long)] | ||
pub(crate) order_id: String, | ||
|
||
/// The contract id of the market | ||
#[clap(long)] | ||
pub(crate) contract_id: String, | ||
|
||
/// The URL to query | ||
/// Ex. beta-5.fuel.network | ||
#[clap(long)] | ||
pub(crate) rpc: String, | ||
} | ||
|
||
impl CloseCommand { | ||
pub(crate) fn run(&self) -> anyhow::Result<()> { | ||
pub(crate) async fn run(&self) -> anyhow::Result<()> { | ||
let wallet = setup(&self.rpc).await?; | ||
let contract_id = validate_contract_id(&self.contract_id)?; | ||
let order_id = Bits256::from_hex_str(&self.order_id)?; | ||
|
||
if self.order_id.len() as u64 != 64 { | ||
anyhow::bail!("Invalid order id length"); | ||
} | ||
|
||
// Initial balance prior to contract call - used to calculate contract interaction cost | ||
let balance = wallet.get_asset_balance(&AssetId::BASE).await?; | ||
|
||
// Connect to the deployed contract via the rpc | ||
let contract = MarketContract::new(contract_id, wallet.clone()).await; | ||
|
||
let _ = contract.cancel_order(order_id).await?; | ||
|
||
// Balance post-call | ||
let new_balance = wallet.get_asset_balance(&AssetId::BASE).await?; | ||
|
||
// TODO: replace println with tracing | ||
println!("\nContract call cost: {}", balance - new_balance); | ||
|
||
Ok(()) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,77 @@ | ||
use crate::utils::{setup, validate_contract_id, OrderType}; | ||
use clap::Args; | ||
use fuels::{ | ||
accounts::ViewOnlyAccount, | ||
types::{AssetId, ContractId}, | ||
}; | ||
use spark_market_sdk::{MarketContract, OrderType as ContractOrderType}; | ||
use std::str::FromStr; | ||
|
||
#[derive(Args, Clone)] | ||
#[command(about = "TODO")] | ||
pub(crate) struct OpenCommand {} | ||
#[command(about = "Opens a new order")] | ||
pub(crate) struct OpenCommand { | ||
/// The amount of asset | ||
#[clap(long)] | ||
pub(crate) amount: u64, | ||
|
||
/// The id of the asset | ||
#[clap(long)] | ||
pub(crate) asset: String, | ||
|
||
/// The type of order | ||
#[clap(long)] | ||
pub(crate) order_type: OrderType, | ||
|
||
/// The price of the order | ||
#[clap(long)] | ||
pub(crate) price: u64, | ||
|
||
/// The contract id of the market | ||
#[clap(long)] | ||
pub(crate) contract_id: String, | ||
|
||
/// The URL to query | ||
/// Ex. beta-5.fuel.network | ||
#[clap(long)] | ||
pub(crate) rpc: String, | ||
} | ||
|
||
impl OpenCommand { | ||
pub(crate) fn run(&self) -> anyhow::Result<()> { | ||
pub(crate) async fn run(&self) -> anyhow::Result<()> { | ||
let wallet = setup(&self.rpc).await?; | ||
let contract_id = validate_contract_id(&self.contract_id)?; | ||
|
||
if self.asset.len() as u64 != 66 { | ||
anyhow::bail!("Invalid asset length"); | ||
} | ||
|
||
let asset = AssetId::from_str(&self.asset).expect("Invalid asset"); | ||
|
||
// TODO: cli parsing | ||
let order_type = match self.order_type { | ||
OrderType::Buy => ContractOrderType::Buy, | ||
OrderType::Sell => ContractOrderType::Sell, | ||
}; | ||
|
||
// Initial balance prior to contract call - used to calculate contract interaction cost | ||
let balance = wallet.get_asset_balance(&AssetId::BASE).await?; | ||
|
||
// Connect to the deployed contract via the rpc | ||
let contract = MarketContract::new(contract_id, wallet.clone()).await; | ||
|
||
let order_id = contract | ||
.open_order(self.amount, asset, order_type, self.price) | ||
.await? | ||
.value; | ||
|
||
// Balance post-call | ||
let new_balance = wallet.get_asset_balance(&AssetId::BASE).await?; | ||
|
||
// TODO: replace println with tracing | ||
println!("\nContract call cost: {}", balance - new_balance); | ||
// TODO: hack to display, turn into hex manually? | ||
println!("Order ID: {}", ContractId::from(order_id.0)); | ||
|
||
Ok(()) | ||
} | ||
} |
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
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
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