o2-tools 0.4.7-rc2

Reusable tooling for trade account and order book contract interactions on Fuel
Documentation
//! Synthetic historical tier storage for compatibility tests only.
//!
//! Legacy publication is disabled. Seed the records an older deployment would
//! have left behind, then exercise their lifecycle on the current implementation.
//! This is not a deployment helper or a test of upgrading old bytecode.

use super::*;
use crate::prop::TierParams;
use fuels::{
    core::traits::Tokenizable,
    programs::calls::CallHandler,
};

mod mock {
    use fuels::prelude::abigen;

    abigen!(Contract(
        name = "PropMarginPoolMockContract",
        abi = "artifacts/test_artifacts/prop-margin-pool-mock-abi.json"
    ));
}

pub(super) async fn seed_legacy_tier<W: Account + Clone>(
    deployment: &PropDeployment<W>,
    tier_id: u64,
    params: TierParams,
    books: Vec<ContractId>,
) -> Result<u32> {
    let version = deployment
        .pool
        .methods()
        .current_tier_version(tier_id)
        .simulate(Execution::state_read_only())
        .await?
        .value
        .unwrap_or(0)
        .checked_add(1)
        .context("legacy fixture version overflow")?;
    let original_target = deployment
        .pool_proxy
        .methods()
        .proxy_target()
        .simulate(Execution::state_read_only())
        .await?
        .value
        .context("legacy fixture requires an initialized pool proxy")?;
    // Only the loader's salt and chunk size are used; the mock setters do not
    // depend on collateral or any other production configurables.
    let mock_blob_id = upload_implementation(
        &deployment.deployer_wallet,
        include_bytes!("../artifacts/test_artifacts/prop-margin-pool-mock.bin"),
        include_bytes!(
            "../artifacts/test_artifacts/prop-margin-pool-mock-storage_slots.json"
        ),
        Configurables::default(),
        &PropDeployConfig::new(AssetId::zeroed()),
    )
    .await?;
    let mock_pool = mock::PropMarginPoolMockContract::new(
        deployment.pool_id,
        deployment.deployer_wallet.clone(),
    );
    let mock_params = mock::TierParams::from_token(params.into_token())?;
    let mut dependencies = books.clone();
    dependencies.push(deployment.price_feed_id);

    // Seed and restore atomically. Book admission then runs on production code,
    // populating its metadata, asset-decimal pins and allow lists normally.
    CallHandler::new_multi_call(deployment.deployer_wallet.clone())
        .add_call(
            deployment
                .pool_proxy
                .methods()
                .set_proxy_target(mock_blob_id.into()),
        )
        .add_call(mock_pool.methods().set_tier(tier_id, version, mock_params))
        .add_call(mock_pool.methods().set_current_version(tier_id, version))
        .add_call(
            deployment
                .pool_proxy
                .methods()
                .set_proxy_target(original_target),
        )
        .add_call(
            deployment
                .pool
                .methods()
                .add_books(tier_id, books)
                .with_contract_ids(&dependencies),
        )
        .call::<((), (), (), (), ())>()
        .await?;
    Ok(version)
}