From db33d8f2e3b6d0ad847d2497974f50672bc76c67 Mon Sep 17 00:00:00 2001 From: dcpp Date: Tue, 1 Oct 2024 13:38:19 +0300 Subject: [PATCH] Update CLI functions --- Cargo.lock | 10 +- spark-cli/Cargo.toml | 4 +- spark-cli/README.md | 95 ++++++++++++------- spark-cli/src/commands/core/cli.rs | 15 ++- spark-cli/src/commands/core/deposit_for.rs | 92 ++++++++++++++++++ spark-cli/src/commands/core/mod.rs | 2 + spark-cli/src/commands/core/withdraw.rs | 2 +- .../src/commands/core/withdraw_to_market.rs | 77 +++++++++++++++ spark-cli/src/main.rs | 2 + spark-market/src/main.sw | 10 +- 10 files changed, 257 insertions(+), 52 deletions(-) create mode 100644 spark-cli/src/commands/core/deposit_for.rs create mode 100644 spark-cli/src/commands/core/withdraw_to_market.rs diff --git a/Cargo.lock b/Cargo.lock index f7acfb4..60fc4c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5477,13 +5477,13 @@ dependencies = [ [[package]] name = "spark-cli" -version = "0.5.0" +version = "0.5.1" dependencies = [ "anyhow", "clap", "dotenv", "fuels", - "spark-market-sdk 0.5.0", + "spark-market-sdk 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "spark-registry-sdk 0.5.0", "tokio", ] @@ -5501,9 +5501,7 @@ dependencies = [ [[package]] name = "spark-market-sdk" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1f79dad7e257c0e0da1f259536c3758a5e6dc2f977c0db76996ea7268d84ad8" +version = "0.5.1" dependencies = [ "anyhow", "fuels", @@ -5514,6 +5512,8 @@ dependencies = [ [[package]] name = "spark-market-sdk" version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5c9ec25ff397daebbea00cc43c7b49d0e6eb7770efcff4b50dfc14b37c66140" dependencies = [ "anyhow", "fuels", diff --git a/spark-cli/Cargo.toml b/spark-cli/Cargo.toml index d99b210..4f80d1f 100644 --- a/spark-cli/Cargo.toml +++ b/spark-cli/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "spark-cli" description = "Spark Rust CLI for Market & Orderbook contract interactions" -version = "0.5.0" +version = "0.5.1" authors = { workspace = true } edition = { workspace = true } license = { workspace = true } @@ -15,7 +15,7 @@ anyhow = { workspace = true } clap = { version = "4.5.18", features = ["derive"] } dotenv = "0.15.0" fuels = { workspace = true } -spark-market-sdk = { version = "0.5.0" } +spark-market-sdk = { version = "0.5.1" } spark-registry-sdk = { version = "0.5.0" } tokio = { workspace = true } diff --git a/spark-cli/README.md b/spark-cli/README.md index 79eba9b..c57237b 100644 --- a/spark-cli/README.md +++ b/spark-cli/README.md @@ -35,7 +35,7 @@ spark-cli core deploy \ ``` Sample output: -Market deployed to: 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe +Market deployed to: 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 Deployment cost: 0 Owner address: fuel173lqaa6y4jxfjd2suq730uwys3zfg4f6zt9vzx4cc45v3xvlmwlszdvdpz 0xf47e0ef744ac8c993550e03d17f1c4844494553a12cac11ab8c568c8999fdbbf @@ -53,7 +53,7 @@ spark-cli core deploy \ ``` Sample output: -SparkMarket deployed to: 0x2dd759eb0671e293b5e386f9b4f1cc8a1cb2c4ed1cf839e5de4f06f822d3790f +SparkMarket deployed to: 0x10860c51de3a37c47ebff2e08a810d9e2963ef161ee3a88274520938c85914e8 Deployment cost: 0 Owner address: fuel173lqaa6y4jxfjd2suq730uwys3zfg4f6zt9vzx4cc45v3xvlmwlszdvdpz 0xf47e0ef744ac8c993550e03d17f1c4844494553a12cac11ab8c568c8999fdbbf @@ -65,7 +65,7 @@ spark-cli core deposit \ --asset-type base \ --amount 10 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ``` @@ -73,7 +73,19 @@ spark-cli core deposit \ --asset-type quote \ --amount 7000 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 +``` + +## Deposit For + +``` +spark-cli core deposit-for \ + --asset-type base \ + --amount 10 \ + --account-type address \ + --account-id 0xf47e0ef744ac8c993550e03d17f1c4844494553a12cac11ab8c568c8999fdbbf \ + --rpc "testnet.fuel.network" \ + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Withdraw @@ -83,7 +95,7 @@ spark-cli core withdraw \ --asset-type base \ --amount 10 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ``` @@ -91,7 +103,18 @@ spark-cli core withdraw \ --asset-type quote \ --amount 7000 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 +``` + +## Withdraw To Market + +``` +spark-cli core withdraw-to-market \ + --asset-type quote \ + --amount 7000 \ + --market-id 0x10860c51de3a37c47ebff2e08a810d9e2963ef161ee3a88274520938c85914e8 \ + --rpc "testnet.fuel.network" \ + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Open Order @@ -102,7 +125,7 @@ spark-cli core open \ --order-type buy \ --price 70000000000000 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ``` @@ -111,7 +134,7 @@ spark-cli core open \ --order-type sell \ --price 70000000000000 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Cancel Order @@ -120,17 +143,17 @@ spark-cli core open \ spark-cli core cancel \ --order-id e950192bd177292dd7b98c69e6f85a46f5d59d93a0ba2f84af1f9d06d1fdf821 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Match Order Pair ``` spark-cli core match-pair \ - --orders b4f6f1967c2b6458effb04be5df781d439a3d9b816dd600e003c2285b715f31c \ - --orders 0a83de22df4fa0c843aba291633c2f729f739c6f8e1aefb76a6385cf05be57dc \ + --orders bc079871bc5cbac3f4c4561c0f01da1175128aa44884418586c2bfb17382d070 \ + --orders 50454d56b6049ee9ad3781e55f9631f5c90f908afb719be581dfeefa5dc48932 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Match Order Many @@ -140,7 +163,7 @@ spark-cli core match-many \ --orders 0a96241df0a2606ead475af4cf66f89097bcbec27fdb59ff5cdb30a7525393e2 \ --orders 2a6273b795e682f9fc4723097e682e0097c29c16f0419d7dc6132f77151e27ca \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Fulfill Order Many @@ -155,7 +178,7 @@ spark-cli core fulfill-many \ --orders 0d0d7540d7350222b39a453452067cb3e1d1a29773a1678293771c9a0a12fe6f \ --orders 12e7c70e34d437960fe455ce41ee9f839a93f5b317d19a9708c3ef51dffb89d0 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Set Protocol Fee @@ -171,7 +194,7 @@ spark-cli core set-protocol-fee \ --fee "2,7,500000000000" \ --fee "1,5,1000000000000" \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Set Matcher Fee @@ -182,7 +205,7 @@ Sets a matcher fee for the market spark-cli core set-matcher-fee \ --amount 1000 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Set Epoch @@ -194,7 +217,7 @@ spark-cli core set-epoch \ --epoch 4611686020152825242 \ --epoch-duration 2332800 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Set Store Order Change Info @@ -205,7 +228,7 @@ Sets a store order change info for the market spark-cli core set-store-order-change-info \ --store \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` @@ -221,7 +244,7 @@ spark-cli info account \ --account-type address \ --account-id 0xf47e0ef744ac8c993550e03d17f1c4844494553a12cac11ab8c568c8999fdbbf \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Config @@ -229,7 +252,7 @@ spark-cli info account \ ``` spark-cli info config \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Epoch @@ -237,7 +260,7 @@ spark-cli info config \ ``` spark-cli info epoch \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Protocol Fee @@ -247,7 +270,7 @@ Protocol fee ``` spark-cli info protocol-fee \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Protocol Fee User @@ -259,7 +282,7 @@ spark-cli info protocol-fee-user \ --account-type address \ --account-id 0xf47e0ef744ac8c993550e03d17f1c4844494553a12cac11ab8c568c8999fdbbf \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Protocol Fee User Amount @@ -272,7 +295,7 @@ spark-cli info protocol-fee-user-amount \ --account-type address \ --account-id 0xf47e0ef744ac8c993550e03d17f1c4844494553a12cac11ab8c568c8999fdbbf \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Matcher Fee @@ -282,7 +305,7 @@ Matcher Fee for the market ``` spark-cli info matcher-fee \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Order ID @@ -295,7 +318,7 @@ spark-cli info order-id \ --price 70000000000000 \ --block-height 10000 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Order @@ -304,7 +327,7 @@ spark-cli info order-id \ spark-cli info order \ --order-id 769663aef01812de5e5b4a4cd96f31a1641d4924cd26bdf7665fc00708487007 \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Store Order Change Info @@ -314,7 +337,7 @@ Store Order Change Info for the market ``` spark-cli info store-order-change-info \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## User Orders @@ -324,7 +347,7 @@ spark-cli info user-orders \ --account-type address \ --account-id 0xf47e0ef744ac8c993550e03d17f1c4844494553a12cac11ab8c568c8999fdbbf \ --rpc "testnet.fuel.network" \ - --contract-id 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe + --contract-id 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 ``` ## Deploy Market Registry @@ -335,7 +358,7 @@ spark-cli registry deploy \ ``` Output: -SparkRegistry deployed to: 0x0f5232e0d927f7a477baa7e6f0380c50f8b3de5c0d6ae42bb29ba8a767e5ff1a +SparkRegistry deployed to: 0xfc3439e66c15c0b8e8e02e0fe4951eecd6001639a6aa8a650885af62ea92540f Deployment cost: 0 Owner address: fuel173lqaa6y4jxfjd2suq730uwys3zfg4f6zt9vzx4cc45v3xvlmwlszdvdpz 0xf47e0ef744ac8c993550e03d17f1c4844494553a12cac11ab8c568c8999fdbbf @@ -344,18 +367,18 @@ Owner address: fuel173lqaa6y4jxfjd2suq730uwys3zfg4f6zt9vzx4cc45v3xvlmwlszdvdpz ``` spark-cli registry register \ - --market 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe \ + --market 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 \ --rpc "testnet.fuel.network" \ - --contract-id 0x0f5232e0d927f7a477baa7e6f0380c50f8b3de5c0d6ae42bb29ba8a767e5ff1a + --contract-id 0xfc3439e66c15c0b8e8e02e0fe4951eecd6001639a6aa8a650885af62ea92540f ``` ## Unregister a market ``` spark-cli registry unregister \ - --market 0xb1ed073b4dccbff48e2ac1b52a49454e6a2921aa54effee89eb958dc5ddbffbe \ + --market 0xbc69e42348c9a992100970e8469247b58a517c2f7dd373741c6707cda35ccfc6 \ --rpc "testnet.fuel.network" \ - --contract-id 0x0f5232e0d927f7a477baa7e6f0380c50f8b3de5c0d6ae42bb29ba8a767e5ff1a + --contract-id 0xfc3439e66c15c0b8e8e02e0fe4951eecd6001639a6aa8a650885af62ea92540f ``` ## Get registered markets by assets @@ -365,7 +388,7 @@ spark-cli registry markets \ --base 0x38e4ca985b22625fff93205e997bfc5cc8453a953da638ad297ca60a9f2600bc \ --quote 0x336b7c06352a4b736ff6f688ba6885788b3df16e136e95310ade51aa32dc6f05 \ --rpc "testnet.fuel.network" \ - --contract-id 0x0f5232e0d927f7a477baa7e6f0380c50f8b3de5c0d6ae42bb29ba8a767e5ff1a + --contract-id 0xfc3439e66c15c0b8e8e02e0fe4951eecd6001639a6aa8a650885af62ea92540f ``` ## Config @@ -373,5 +396,5 @@ spark-cli registry markets \ ``` spark-cli registry config \ --rpc "testnet.fuel.network" \ - --contract-id 0x0f5232e0d927f7a477baa7e6f0380c50f8b3de5c0d6ae42bb29ba8a767e5ff1a + --contract-id 0xfc3439e66c15c0b8e8e02e0fe4951eecd6001639a6aa8a650885af62ea92540f ``` diff --git a/spark-cli/src/commands/core/cli.rs b/spark-cli/src/commands/core/cli.rs index abf3340..b2b283f 100644 --- a/spark-cli/src/commands/core/cli.rs +++ b/spark-cli/src/commands/core/cli.rs @@ -1,9 +1,10 @@ use crate::commands::core::{ cancel_order::CancelCommand, deploy::DeployCommand, deposit::DepositCommand, - fulfill_many::FulfillManyCommand, match_many::MatchManyCommand, match_pair::MatchPairCommand, - open_order::OpenCommand, set_epoch::SetEpochCommand, set_matcher_fee::SetMatcherFeeCommand, - set_protocol_fee::SetProtocolFeeCommand, + deposit_for::DepositForCommand, fulfill_many::FulfillManyCommand, match_many::MatchManyCommand, + match_pair::MatchPairCommand, open_order::OpenCommand, set_epoch::SetEpochCommand, + set_matcher_fee::SetMatcherFeeCommand, set_protocol_fee::SetProtocolFeeCommand, set_store_order_change_info::SetStoreOrderChangeInfoCommand, withdraw::WithdrawCommand, + withdraw_to_market::WithdrawToMarketCommand, }; use clap::Subcommand; @@ -17,6 +18,10 @@ pub(crate) enum CoreCommands { #[clap(short_flag = 'D')] Deploy(DeployCommand), + /// Deposit into the market contract for user + #[clap(short_flag = 'E')] + DepositFor(DepositForCommand), + /// Deposit into the market contract #[clap(short_flag = 'P')] Deposit(DepositCommand), @@ -56,4 +61,8 @@ pub(crate) enum CoreCommands { /// Withdraw from the market contract #[clap(short_flag = 'W')] Withdraw(WithdrawCommand), + + /// Withdraw from the market contract + #[clap(short_flag = 'X')] + WithdrawToMarket(WithdrawToMarketCommand), } diff --git a/spark-cli/src/commands/core/deposit_for.rs b/spark-cli/src/commands/core/deposit_for.rs new file mode 100644 index 0000000..0bd18ff --- /dev/null +++ b/spark-cli/src/commands/core/deposit_for.rs @@ -0,0 +1,92 @@ +use crate::utils::{setup, validate_contract_id, AccountType, AssetType}; +use clap::Args; +use fuels::accounts::ViewOnlyAccount; +use fuels::types::{Address, ContractId, Identity}; +use spark_market_sdk::{AssetType as ContractAssetType, SparkMarketContract}; +use std::str::FromStr; + +#[derive(Args, Clone)] +#[command(about = "Deposits an asset from the wallet to the market for the account")] +pub(crate) struct DepositForCommand { + /// The amount to deposit + #[clap(long)] + pub(crate) amount: u64, + + /// The asset type of the market + #[clap(long)] + pub(crate) asset_type: AssetType, + + /// The b256 id of the account + #[clap(long)] + pub(crate) account_id: String, + + /// The type of account + #[clap(long)] + pub(crate) account_type: AccountType, + + /// The contract id of the market + #[clap(long)] + pub(crate) contract_id: String, + + /// The URL to query + /// Ex. testnet.fuel.network + #[clap(long)] + pub(crate) rpc: String, +} + +impl DepositForCommand { + pub(crate) async fn run(&self) -> anyhow::Result<()> { + let wallet = setup(&self.rpc).await?; + let contract_id = validate_contract_id(&self.contract_id)?; + + let asset_type = match self.asset_type { + AssetType::Base => ContractAssetType::Base, + AssetType::Quote => ContractAssetType::Quote, + }; + + let account = match self.account_type { + AccountType::Address => { + let address = Address::from_str(&self.account_id).expect("Invalid address"); + Identity::Address(address) + } + AccountType::Contract => { + let address = ContractId::from_str(&self.account_id).expect("Invalid contract id"); + Identity::ContractId(address) + } + }; + + // Initial balance prior to contract call - used to calculate contract interaction cost + let balance = wallet + .get_asset_balance(&wallet.provider().unwrap().base_asset_id()) + .await?; + + // Connect to the deployed contract via the rpc + let contract = SparkMarketContract::new(contract_id, wallet.clone()).await; + + let config = contract.config().await?.value; + let asset = if asset_type == ContractAssetType::Base { + config.0 + } else { + config.2 + }; + let asset_balance = wallet.get_asset_balance(&asset).await?; + + let _ = contract.deposit_for(self.amount, asset, account).await?; + + // Balance post-call + let new_balance = wallet + .get_asset_balance(&wallet.provider().unwrap().base_asset_id()) + .await?; + let new_asset_balance = wallet.get_asset_balance(&asset).await?; + + println!("\nContract call cost: {}", balance - new_balance); + println!( + "Deposited {} amount of asset {} for {:?}", + self.amount, + asset_balance - new_asset_balance, + account, + ); + + Ok(()) + } +} diff --git a/spark-cli/src/commands/core/mod.rs b/spark-cli/src/commands/core/mod.rs index f20149b..7818250 100644 --- a/spark-cli/src/commands/core/mod.rs +++ b/spark-cli/src/commands/core/mod.rs @@ -2,6 +2,7 @@ pub(crate) mod cancel_order; pub(crate) mod cli; pub(crate) mod deploy; pub(crate) mod deposit; +pub(crate) mod deposit_for; pub(crate) mod fulfill_many; pub(crate) mod match_many; pub(crate) mod match_pair; @@ -11,3 +12,4 @@ pub(crate) mod set_matcher_fee; pub(crate) mod set_protocol_fee; pub(crate) mod set_store_order_change_info; pub(crate) mod withdraw; +pub(crate) mod withdraw_to_market; diff --git a/spark-cli/src/commands/core/withdraw.rs b/spark-cli/src/commands/core/withdraw.rs index af8f5cf..e0dffb9 100644 --- a/spark-cli/src/commands/core/withdraw.rs +++ b/spark-cli/src/commands/core/withdraw.rs @@ -4,7 +4,7 @@ use fuels::accounts::ViewOnlyAccount; use spark_market_sdk::{AssetType as ContractAssetType, SparkMarketContract}; #[derive(Args, Clone)] -#[command(about = "Deposits an asset from the wallet to the market")] +#[command(about = "Withdraws an asset from the market to the caller")] pub(crate) struct WithdrawCommand { /// The amount to withdraw #[clap(long)] diff --git a/spark-cli/src/commands/core/withdraw_to_market.rs b/spark-cli/src/commands/core/withdraw_to_market.rs new file mode 100644 index 0000000..edcb524 --- /dev/null +++ b/spark-cli/src/commands/core/withdraw_to_market.rs @@ -0,0 +1,77 @@ +use crate::utils::{setup, validate_contract_id, AssetType}; +use clap::Args; +use fuels::accounts::ViewOnlyAccount; +use spark_market_sdk::{AssetType as ContractAssetType, SparkMarketContract}; + +#[derive(Args, Clone)] +#[command(about = "Withdraws an asset from the market to another market caller account")] +pub(crate) struct WithdrawToMarketCommand { + /// The amount to withdraw + #[clap(long)] + pub(crate) amount: u64, + + /// The asset type of the market + #[clap(long)] + pub(crate) asset_type: AssetType, + + /// The contract id of the deposit market + #[clap(long)] + pub(crate) market_id: String, + + /// The contract id of the withdraw market + #[clap(long)] + pub(crate) contract_id: String, + + /// The URL to query + /// Ex. testnet.fuel.network + #[clap(long)] + pub(crate) rpc: String, +} + +impl WithdrawToMarketCommand { + pub(crate) async fn run(&self) -> anyhow::Result<()> { + let wallet = setup(&self.rpc).await?; + let contract_id = validate_contract_id(&self.contract_id)?; + let market_id = validate_contract_id(&self.market_id)?; + + let asset_type = match self.asset_type { + AssetType::Base => ContractAssetType::Base, + AssetType::Quote => ContractAssetType::Quote, + }; + + // Initial balance prior to contract call - used to calculate contract interaction cost + let balance = wallet + .get_asset_balance(&wallet.provider().unwrap().base_asset_id()) + .await?; + + // Connect to the deployed contract via the rpc + let contract = SparkMarketContract::new(contract_id, wallet.clone()).await; + let config = contract.config().await?.value; + let asset = if asset_type == ContractAssetType::Base { + config.0 + } else { + config.2 + }; + let asset_balance = wallet.get_asset_balance(&asset).await?; + + let _ = contract + .withdraw_to_market(self.amount, asset_type.clone(), &market_id.into()) + .await?; + + // Balance post-call + let new_balance = wallet + .get_asset_balance(&wallet.provider().unwrap().base_asset_id()) + .await?; + let new_asset_balance = wallet.get_asset_balance(&asset).await?; + + println!("Contract call cost: {}", balance - new_balance); + println!( + "Withdrawn {} amount of {:?} asset to {:?}", + new_asset_balance - asset_balance, + asset_type.clone(), + market_id, + ); + + Ok(()) + } +} diff --git a/spark-cli/src/main.rs b/spark-cli/src/main.rs index c492e96..bdf149c 100644 --- a/spark-cli/src/main.rs +++ b/spark-cli/src/main.rs @@ -31,6 +31,7 @@ async fn main() -> anyhow::Result<()> { CoreCommands::Cancel(args) => args.run().await, CoreCommands::Deploy(args) => args.run().await, CoreCommands::Deposit(args) => args.run().await, + CoreCommands::DepositFor(args) => args.run().await, CoreCommands::FulfillMany(args) => args.run().await, CoreCommands::Open(args) => args.run().await, CoreCommands::MatchMany(args) => args.run().await, @@ -40,6 +41,7 @@ async fn main() -> anyhow::Result<()> { CoreCommands::SetMatcherFee(args) => args.run().await, CoreCommands::SetStoreOrderChangeInfo(args) => args.run().await, CoreCommands::Withdraw(args) => args.run().await, + CoreCommands::WithdrawToMarket(args) => args.run().await, }, Command::Info(args) => match args.commands { InfoCommands::Account(args) => args.run().await, diff --git a/spark-market/src/main.sw b/spark-market/src/main.sw index 1c6b577..a2396ee 100644 --- a/spark-market/src/main.sw +++ b/spark-market/src/main.sw @@ -751,7 +751,7 @@ fn open_order_internal( let asset = get_asset_id(asset_type); - log_order_change_info( + store_order_change_info( order_id, OrderChangeInfo::new( OrderChangeType::OrderOpened, @@ -802,7 +802,7 @@ fn cancel_order_internal(order_id: b256) { remove_order(user, order_id); storage.account.insert(user, account); - log_order_change_info( + store_order_change_info( order_id, OrderChangeInfo::new( OrderChangeType::OrderCancelled, @@ -1133,7 +1133,7 @@ fn emit_match_events( b_account: Account, ) { // Emit events for the first order - log_order_change_info( + store_order_change_info( id0, OrderChangeInfo::new( OrderChangeType::OrderMatched, @@ -1148,7 +1148,7 @@ fn emit_match_events( ); // Emit events for the second order - log_order_change_info( + store_order_change_info( id1, OrderChangeInfo::new( OrderChangeType::OrderMatched, @@ -1181,7 +1181,7 @@ fn emit_match_events( } #[storage(read, write)] -fn log_order_change_info(order_id: b256, change_info: OrderChangeInfo) { +fn store_order_change_info(order_id: b256, change_info: OrderChangeInfo) { if storage.store_order_change_info.read() { storage.order_change_info.get(order_id).push(change_info); }