Same function in RUST sdk works way faster than in TS sdk

Hello guys
I have created different tests for my system of contracts done with Rust and TS SDK. I have found problem the openOrder function call is working for 2 mins 24 sec while in Rust same func call works for 15 seconds.

Those are scressnshot of test results, for whole scenario with mint toke, deposit and open order

Rust

TS

code of openOrder in TS

 it("open order", async () => {
        const connection = new EvmPriceServiceConnection("https://xc-mainnet.pyth.network");
        const priceIds = [TOKENS_BY_SYMBOL.BTC.priceFeed]
        const updateData = await connection.getPriceFeedsUpdateData(priceIds);
        const parsedUpdateData = updateData.map((v) => Array.from(arrayify(v)));

        const account = new SparkAccount();
        await account.initUser(privateKey);
        const indexerAddress = account.addressB256().slice(2);

        const ordersBefore = await getUserPerpOrders(indexerAddress);

        const price = "41637961421"; //market price of btc
        const size = {value: "11572614", negative: false};
        await account.contracts?.clearingHouseContract.functions
            .open_order({value: TOKENS_BY_SYMBOL.BTC.assetId}, size, price, parsedUpdateData)
            .addContracts([
                account.contracts?.proxyContract,
                account.contracts?.accountBalanceContract,
                account.contracts?.insuranceFundContract,
                account.contracts?.perpMarketContract,
                account.contracts?.vaultContract,
                account.contracts?.pythContract,
            ])
            .callParams({
                forward: {
                    amount: 1,
                    assetId: TOKENS_BY_SYMBOL.ETH.assetId
                }
            }) //this is to pay pyth for price update
            .txParams({gasPrice: 1})
            .call();

        // const ordersAfter = await getUserPerpOrders(indexerAddress);
        // expect(ordersAfter.length - ordersBefore.length).toBe(1);
    }, 1000000000);

Rust code

 use std::{str::FromStr, thread::sleep, time::Duration};

use crate::utils::{
    contracts_utils::{
        pyth_utils::BTC_USD_PRICE_FEED_ID,
        spark_utils::Spark,
        token_utils::{Asset, TokenContract},
    },
    print_title,
};
use fuels::types::{Address, ContractId};
use fuels::{accounts::wallet::WalletUnlocked, prelude::Provider};
use fuels::{accounts::ViewOnlyAccount, types::Bits256};
const RPC: &str = "beta-4.fuel.network";

const PYTH_CONTRACT_ID: &str = "0x2b480dd77abdc48224a22b809608b445604f0de1a999160fb7f126cca2ffc108";
const TOKEN_CONTRACT_ID: &str =
    "0xc3d0426df8a40e7b4803f305537a83e7037be91596c393e7a6b693133f9d7301";

const PROXY_ID: &str = "0x45f4523ea213a07fb6e6224fd9df4e7419b83539ac852f2f696c649ffaa4d352";
const ACCOUNT_BALANCE_ID: &str =
    "0x0b1f3bfc922c6dc3afded60534fd6ee4ce18a2997ca40f3655251bd632f5d8e3";
const CLEARING_HOUSE_ID: &str =
    "0xbf2300a00b13ed67f7f89595c50064c643a9aab59c1eb15daa0c0481427a4d5d";
const INSURANCE_FUND_ID: &str =
    "0x20d1ce5d201508bbf9ce89790662f0e93eed8cea496690a422fc94c6cec028d4";
const PERP_MARKET_ID: &str = "0x7893e011627191efa82851c8a39ff0765d1a1d97526b9d5c2e5e679d13689bbf";
const VAULT_ID: &str = "0xa31cbf2d1e1ca5c097bf27b1f5d9af20b084e42fd53ff2e28ccc2cd52ddd5cc6";

#[tokio::test]
async fn match_orders_action() {
    dotenv::dotenv().ok();

    print_title("πŸ’ž Testnet spark match_orders action 🀩 ");

    let provider = Provider::connect(RPC).await.unwrap();

    let admin_pk = std::env::var("ADMIN").unwrap().parse().unwrap();
    let admin = &WalletUnlocked::new_from_private_key(admin_pk, Some(provider.clone()));

    let alice_pk = std::env::var("ALICE").unwrap().parse().unwrap();
    let alice = &WalletUnlocked::new_from_private_key(alice_pk, Some(provider.clone()));
    let alice_address = Address::from(alice.address());

    let bob_pk = std::env::var("BOB").unwrap().parse().unwrap();
    let bob = &WalletUnlocked::new_from_private_key(bob_pk, Some(provider.clone()));
    let bob_address = Address::from(bob.address());

    let token_contarct = TokenContract::new(
        &ContractId::from_str(TOKEN_CONTRACT_ID).unwrap().into(),
        admin.clone(),
    );
    let usdc = Asset::new(admin.clone(), token_contarct.contract_id().into(), "USDC");
    let btc = Asset::new(admin.clone(), token_contarct.contract_id().into(), "BTC");

    println!("usdc = {:?}", usdc.asset_id);
    println!("btc = {:?}", btc.asset_id);

    let btc_price_feed = Bits256::from_hex_str(BTC_USD_PRICE_FEED_ID).unwrap();

    let spark = Spark::new(
        admin,
        ACCOUNT_BALANCE_ID,
        CLEARING_HOUSE_ID,
        INSURANCE_FUND_ID,
        PERP_MARKET_ID,
        VAULT_ID,
        PYTH_CONTRACT_ID,
        PROXY_ID,
    );

    let btc_price = spark.get_pyth_price(btc_price_feed).await.unwrap().value;
    let btc_price = btc_price.price / 100;

    if spark.get_market(btc.asset_id).await.is_err() {
        spark
            ._create_market(
                btc.asset_id,
                btc.decimals as u32,
                btc_price_feed,
                60000,
                50000,
                btc_price,
            )
            .await
            .unwrap();
    }

    let block = provider.latest_block_height().await.unwrap();
    println!("🏁 Start_block: {block}");
    sleep(Duration::from_secs(1));

    let amount = usdc.parse_units(1500_f64) as u64;
    usdc.mint(alice_address, amount).await.unwrap();

    spark
        .with_account(alice)
        .deposit_collateral(amount, usdc.asset_id)
        .await
        .unwrap();

    sleep(Duration::from_secs(1));

    let amount = usdc.parse_units(1500_f64) as u64;
    usdc.mint(bob_address, amount).await.unwrap();
    spark
        .with_account(bob)
        .deposit_collateral(amount, usdc.asset_id)
        .await
        .unwrap();

    sleep(Duration::from_secs(1));
    spark
        .with_account(alice)
        .open_order(
            btc.asset_id,
            btc.parse_units(0.5) as i64, //Long position
            btc_price,
        )
        .await
        .unwrap();

    spark
        .with_account(bob)
        .open_order(
            btc.asset_id,
            -1 * btc.parse_units(0.5) as i64, //Short position
            btc_price,
        )
        .await
        .unwrap();

    sleep(Duration::from_secs(1));
    let sell_order = spark
        .get_trader_orders(bob_address, btc.asset_id)
        .await
        .unwrap()
        .value[0]
        .id;
    let buy_order = spark
        .get_trader_orders(alice_address, btc.asset_id)
        .await
        .unwrap()
        .value[0]
        .id;

    spark.match_orders(sell_order, buy_order).await.unwrap();
}

I assume that it’s related to the parse time of the result because the system of contracts is complex and the result is big too

The TS and Rust code are the same. Could you please fix the Rust code?

sorry, just fixed β€”

Also when I connect Fuel Wallet waiting time increases twice because same time I have to wait just to sign transaction in wallet and then while transaction is sent

Would you also mind sharing the repository and branch? I’d like to try stepping through the test.

1 Like