Skip to content

Commit

Permalink
blackbox tests migration for price aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicalinluca committed Apr 10, 2024
1 parent eafbc37 commit 6302f14
Show file tree
Hide file tree
Showing 4 changed files with 498 additions and 102 deletions.
2 changes: 2 additions & 0 deletions contracts/core/price-aggregator/sc-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[proxy]]
path = "tests/price_aggregator_proxy.rs"
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use multiversx_price_aggregator_sc::{
price_aggregator_data::{OracleStatus, TimestampedPrice, TokenPair},
ContractObj, PriceAggregator, ProxyTrait as _, MAX_ROUND_DURATION_SECONDS,
ContractObj, PriceAggregator, MAX_ROUND_DURATION_SECONDS,
};
use multiversx_sc_modules::{pause::ProxyTrait, staking::ProxyTrait as _};
use multiversx_sc_modules::staking::ProxyTrait as _;

use multiversx_sc_scenario::imports::*;

mod price_aggregator_proxy;

const DECIMALS: u8 = 0;
const EGLD_TICKER: &[u8] = b"EGLD";
const NR_ORACLES: usize = 4;
Expand All @@ -18,6 +20,9 @@ const STAKE_AMOUNT: u64 = 20;
const SUBMISSION_COUNT: usize = 3;
const USD_TICKER: &[u8] = b"USDC";

const PRICE_AGGREGATOR: ScExpr = ScExpr("price-aggregator");
const OWNER: AddressExpr = AddressExpr("owner");

type PriceAggregatorContract = ContractInfo<multiversx_price_aggregator_sc::Proxy<StaticApi>>;

fn world() -> ScenarioWorld {
Expand All @@ -44,7 +49,7 @@ impl PriceAggregatorTestState {
let mut world = world();

let mut set_state_step = SetStateStep::new()
.put_account(OWNER_ADDRESS_EXPR, Account::new().nonce(1))
.put_account(OWNER, Account::new().nonce(1))
.new_address(OWNER_ADDRESS_EXPR, 1, PRICE_AGGREGATOR_ADDRESS_EXPR)
.block_timestamp(100);

Expand Down Expand Up @@ -86,59 +91,69 @@ impl PriceAggregatorTestState {
.collect::<Vec<_>>(),
);

self.world.sc_deploy(
ScDeployStep::new()
.from(OWNER_ADDRESS_EXPR)
.code(price_aggregator_code)
.call(self.price_aggregator_contract.init(
EgldOrEsdtTokenIdentifier::egld(),
STAKE_AMOUNT,
SLASH_AMOUNT,
SLASH_QUORUM,
SUBMISSION_COUNT,
oracles,
)),
);
self.world
.tx()
.from(OWNER)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.init(
EgldOrEsdtTokenIdentifier::egld(),
STAKE_AMOUNT,
SLASH_AMOUNT,
SLASH_QUORUM,
SUBMISSION_COUNT,
oracles,
)
.code(price_aggregator_code)
.run();

for address in self.oracles.iter() {
self.world.sc_call(
ScCallStep::new()
.from(address)
.egld_value(STAKE_AMOUNT)
.call(self.price_aggregator_contract.stake()),
);
self.world
.tx()
.from(&address.to_address())
.to(PRICE_AGGREGATOR)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.stake()
.egld(STAKE_AMOUNT)
.run();
}

self
}

fn set_pair_decimals(&mut self) {
self.world.sc_call(
ScCallStep::new().from(OWNER_ADDRESS_EXPR).call(
self.price_aggregator_contract
.set_pair_decimals(EGLD_TICKER, USD_TICKER, DECIMALS),
),
);
self.world
.tx()
.from(OWNER)
.to(PRICE_AGGREGATOR)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.set_pair_decimals(EGLD_TICKER, USD_TICKER, DECIMALS)
.run();
}

fn unpause_endpoint(&mut self) {
self.world.sc_call(
ScCallStep::new()
.from(OWNER_ADDRESS_EXPR)
.call(self.price_aggregator_contract.unpause_endpoint()),
);
self.world
.tx()
.from(OWNER)
.to(PRICE_AGGREGATOR)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.unpause_endpoint()
.run();
}

fn submit(&mut self, from: &AddressValue, submission_timestamp: u64, price: u64) {
self.world.sc_call(ScCallStep::new().from(from).call(
self.price_aggregator_contract.submit(
self.world
.tx()
.from(&from.to_address())
.to(PRICE_AGGREGATOR)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.submit(
EGLD_TICKER,
USD_TICKER,
submission_timestamp,
price,
DECIMALS,
),
));
)
.run();
}

fn submit_and_expect_err(
Expand All @@ -148,27 +163,31 @@ impl PriceAggregatorTestState {
price: u64,
err_message: &str,
) {
self.world.sc_call(
ScCallStep::new()
.from(from)
.call(self.price_aggregator_contract.submit(
EGLD_TICKER,
USD_TICKER,
submission_timestamp,
price,
DECIMALS,
))
.expect(TxExpect::user_error("str:".to_string() + err_message)),
);
self.world
.tx()
.from(&from.to_address())
.to(PRICE_AGGREGATOR)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.submit(
EGLD_TICKER,
USD_TICKER,
submission_timestamp,
price,
DECIMALS,
)
.with_result(ExpectStatus(4))
.with_result(ExpectMessage(err_message))
.run();
}

fn vote_slash_member(&mut self, from: &AddressValue, member_to_slash: Address) {
self.world.sc_call(
ScCallStep::new().from(from).call(
self.price_aggregator_contract
.vote_slash_member(member_to_slash),
),
);
self.world
.tx()
.from(&from.to_address())
.to(PRICE_AGGREGATOR)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.vote_slash_member(member_to_slash)
.run();
}
}

Expand Down
Loading

0 comments on commit 6302f14

Please sign in to comment.