Skip to content

Commit

Permalink
feat: update tests to use and verify multi call handler works
Browse files Browse the repository at this point in the history
  • Loading branch information
martines3000 committed Aug 14, 2024
1 parent 0d895a4 commit 874c1c2
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 44 deletions.
2 changes: 1 addition & 1 deletion abis/market_abi/src/abi.sw
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ abi Market {

// # 6. Protocol collateral management
#[payable, storage(read)]
fn buy_collateral(asset_id: b256, min_amount: u256, recipient: Address, price_data_update: PriceDataUpdate); // Payment is required: base asset (USDC)
fn buy_collateral(asset_id: b256, min_amount: u256, recipient: Address); // Payment is required: base asset (USDC)

#[storage(read)]
fn collateral_value_to_sell(asset_id: b256, collateral_amount: u256) -> u256;
Expand Down
8 changes: 2 additions & 6 deletions contracts/market/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -530,13 +530,13 @@ impl Market for Contract {
// ## 6.1 Buying collateral
// ### Description:
// - Buy collateral from the protocol
// - Prices are not updated here as it is expected that caller does with using a multicall handler
// ### Parameters:
// - `asset_id`: The asset ID of the collateral asset to be bought
// - `min_amount`: The minimum amount of collateral to be bought
// - `recipient`: The address of the recipient of the collateral
// - `price_data_update`: The price data update struct to be used for updating the price feeds
#[payable, storage(read)]
fn buy_collateral(asset_id: b256, min_amount: u256, recipient: Address, price_data_update: PriceDataUpdate) {
fn buy_collateral(asset_id: b256, min_amount: u256, recipient: Address) {
// Only allow buying collateral if paused flag is not set
require(!storage.pause_config.buy_paused.read(), Error::Paused);
let payment_amount: u256 = msg_amount().into();
Expand All @@ -560,10 +560,6 @@ impl Market for Contract {
// Note: Re-entrancy can skip the reserves check above on a second buyCollateral call.
let reserves = get_collateral_reserves_internal(asset_id);

// Update price data
// FIXME[Martin]: After Fuel responds either remove this or adapt
// update_price_feeds_if_necessary_internal(price_data_update);

// Calculate the quote for a collateral asset in exchange for an amount of the base asset
let collateral_amount = quote_collateral_internal(asset_id, payment_amount);

Expand Down
2 changes: 0 additions & 2 deletions contracts/market/tests/local_tests/functions/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ async fn pause_test() {
uni.bits256,
1,
bob_address,
&price_data_update,
)
.await
.unwrap();
Expand Down Expand Up @@ -479,7 +478,6 @@ async fn pause_test() {
uni.bits256,
1,
bob_address,
&price_data_update,
)
.await
.is_err();
Expand Down
62 changes: 49 additions & 13 deletions contracts/market/tests/local_tests/main_test_uni.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use chrono::Utc;
use fuels::prelude::ViewOnlyAccount;
use fuels::programs::calls::{CallHandler, CallParameters};
use fuels::programs::responses::CallResponse;
use fuels::types::transaction::TxPolicies;
use fuels::types::transaction_builders::VariableOutputPolicy;
use fuels::types::{Address, Bits256, ContractId};
use market::PriceDataUpdate;
use market_sdk::{get_market_config, parse_units, MarketContract};
Expand Down Expand Up @@ -360,6 +364,7 @@ async fn main_test() {
),
)]);

let price_data_update_old = price_data_update.clone();
oracle.update_prices(&prices).await.unwrap();

// New `price_data_update` that will be used in the next steps
Expand Down Expand Up @@ -456,23 +461,54 @@ async fn main_test() {
let balance = bob.get_asset_balance(&usdc.asset_id).await.unwrap();
assert!(balance == (amount as u64));

// Bob calls buy_collateral
// Reset prices back to old values
// This is used to test that multi_call_handler works correctly
market
.with_account(bob)
.await
.unwrap()
.buy_collateral(
&[&oracle.instance],
usdc.asset_id,
amount as u64,
uni.bits256,
1,
bob_address,
&price_data_update,
)
.update_price_feeds_if_necessary(&[&oracle.instance], &price_data_update_old)
.await
.unwrap();

// Prepare calls for multi_call_handler
let tx_policies = TxPolicies::default().with_script_gas_limit(1_000_000);

let call_params_update_price =
CallParameters::default().with_amount(price_data_update.update_fee);

// Update price feeds if necessary
let update_balance_call = market
.instance
.methods()
.update_price_feeds_if_necessary(price_data_update.clone())
.with_contracts(&[&oracle.instance])
.with_tx_policies(tx_policies)
.call_params(call_params_update_price)
.unwrap();

let call_params_base_asset = CallParameters::default()
.with_amount(amount as u64)
.with_asset_id(usdc.asset_id); // Buy collateral with base asset

// Buy collateral with base asset
let buy_collateral_call = market
.instance
.methods()
.buy_collateral(uni.bits256, 1u64.into(), bob_address)
.with_contracts(&[&oracle.instance])
.with_tx_policies(tx_policies)
.call_params(call_params_base_asset)
.unwrap();

let mutli_call_handler = CallHandler::new_multi_call(bob.clone())
.add_call(update_balance_call)
.add_call(buy_collateral_call)
.with_variable_output_policy(VariableOutputPolicy::Exactly(2));

// Sumbit tx
let submitted_tx = mutli_call_handler.submit().await.unwrap();

// Wait for response
let _: CallResponse<((), ())> = submitted_tx.response().await.unwrap();

// Check
let balance = bob.get_asset_balance(&uni.asset_id).await.unwrap();
assert!(balance == parse_units(40, uni.decimals) * AMOUNT_COEFFICIENT);
Expand Down
63 changes: 50 additions & 13 deletions contracts/market/tests/local_tests/main_test_uni_no_debug_mode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::utils::{init_wallets, print_case_title};
use chrono::Utc;
use fuels::prelude::ViewOnlyAccount;
use fuels::programs::calls::{CallHandler, CallParameters};
use fuels::programs::responses::CallResponse;
use fuels::types::transaction::TxPolicies;
use fuels::types::transaction_builders::VariableOutputPolicy;
use fuels::types::{Address, Bits256, ContractId};
use market::PriceDataUpdate;
use market_sdk::{get_market_config, parse_units, MarketContract};
Expand Down Expand Up @@ -350,6 +354,8 @@ async fn main_test_no_debug() {
res.confidence,
),
)]);

let price_data_update_old = price_data_update.clone();
oracle.update_prices(&prices).await.unwrap();

// New `price_data_update` that will be used in the next steps
Expand Down Expand Up @@ -443,23 +449,54 @@ async fn main_test_no_debug() {
let balance = bob.get_asset_balance(&usdc.asset_id).await.unwrap();
assert!(balance == amount as u64);

// Bob calls buy_collateral
// Reset prices back to old values
// This is used to test that multi_call_handler works correctly
market
.with_account(bob)
.await
.unwrap()
.buy_collateral(
&[&oracle.instance],
usdc.asset_id,
amount as u64,
uni.bits256,
1,
bob_address,
&price_data_update,
)
.update_price_feeds_if_necessary(&[&oracle.instance], &price_data_update_old)
.await
.unwrap();

// Prepare calls for multi_call_handler
let tx_policies = TxPolicies::default().with_script_gas_limit(1_000_000);

let call_params_update_price =
CallParameters::default().with_amount(price_data_update.update_fee);

// Update price feeds if necessary
let update_balance_call = market
.instance
.methods()
.update_price_feeds_if_necessary(price_data_update.clone())
.with_contracts(&[&oracle.instance])
.with_tx_policies(tx_policies)
.call_params(call_params_update_price)
.unwrap();

let call_params_base_asset = CallParameters::default()
.with_amount(amount as u64)
.with_asset_id(usdc.asset_id); // Buy collateral with base asset

// Buy collateral with base asset
let buy_collateral_call = market
.instance
.methods()
.buy_collateral(uni.bits256, 1u64.into(), bob_address)
.with_contracts(&[&oracle.instance])
.with_tx_policies(tx_policies)
.call_params(call_params_base_asset)
.unwrap();

let mutli_call_handler = CallHandler::new_multi_call(bob.clone())
.add_call(update_balance_call)
.add_call(buy_collateral_call)
.with_variable_output_policy(VariableOutputPolicy::Exactly(2));

// Sumbit tx
let submitted_tx = mutli_call_handler.submit().await.unwrap();

// Wait for response
let _: CallResponse<((), ())> = submitted_tx.response().await.unwrap();

// Check asset balance
let balance = bob.get_asset_balance(&uni.asset_id).await.unwrap();
assert!(balance == 40_000_000_000 * AMOUNT_COEFFICIENT);
Expand Down
13 changes: 4 additions & 9 deletions libs/market_sdk/src/market_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,25 +453,17 @@ impl MarketContract {
asset_id: Bits256,
min_amount: u64,
recipient: Address,
price_data_update: &PriceDataUpdate,
) -> anyhow::Result<CallResponse<()>> {
let tx_policies = TxPolicies::default().with_script_gas_limit(DEFAULT_GAS_LIMIT);

let call_params_base_asset = CallParameters::default()
.with_amount(amount)
.with_asset_id(base_asset_id); // Buy collateral with base asset

let call_params = CallParameters::default().with_amount(price_data_update.update_fee); // Fee for price update

Ok(self
.instance
.methods()
.buy_collateral(
asset_id,
min_amount.into(),
recipient,
price_data_update.clone(),
)
.buy_collateral(asset_id, min_amount.into(), recipient)
.with_tx_policies(tx_policies)
.with_contracts(contract_ids)
.call_params(call_params_base_asset)?
Expand Down Expand Up @@ -740,11 +732,14 @@ impl MarketContract {
) -> anyhow::Result<()> {
let tx_policies = TxPolicies::default().with_script_gas_limit(DEFAULT_GAS_LIMIT);

let call_params = CallParameters::default().with_amount(price_data_update.update_fee);

self.instance
.methods()
.update_price_feeds_if_necessary(price_data_update.clone())
.with_contracts(contract_ids)
.with_tx_policies(tx_policies)
.call_params(call_params)?
.call()
.await?;

Expand Down

0 comments on commit 874c1c2

Please sign in to comment.