Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set state via AnnotatedValue #1592

Merged
merged 19 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
95fa5c7
impl create account raw and set egld balance
BiancaIalangi Apr 24, 2024
19dac18
set nft properties
BiancaIalangi Apr 25, 2024
fbd49fc
impl set developer rewards
BiancaIalangi Apr 25, 2024
e423519
Merge branch 'feat/unified' into set-state-repl
BiancaIalangi Apr 26, 2024
796af09
set local roles
BiancaIalangi Apr 26, 2024
5a2cae8
set state via AnnotatedValue - account address
andrei-marinica Apr 26, 2024
1276f17
set state via AnnotatedValue - balance, nonce
andrei-marinica Apr 26, 2024
c8dfdbe
set state via AnnotatedValue - fungible ESDT
andrei-marinica Apr 27, 2024
0470780
set state via AnnotatedValue - esdt_nft_balance
andrei-marinica Apr 28, 2024
2799dcd
set state via AnnotatedValue - esdt_nft_balance_and_attributes
andrei-marinica Apr 28, 2024
e066ac9
set state via AnnotatedValue - code, owner
andrei-marinica Apr 28, 2024
352ec3d
set state via AnnotatedValue - esdt_nft_all_properties, esdt_nft_last…
BiancaIalangi Apr 29, 2024
f18b36e
set block via AnnotatedValue - block_epoch, block_nonce, block_round,…
BiancaIalangi Apr 29, 2024
c8bda97
check state via AnnotatedValue - code
BiancaIalangi Apr 29, 2024
9ff6a1a
set state via AnnotatedValue - replace in multisig, rewards, price_ag…
BiancaIalangi Apr 29, 2024
b458044
Merge branch 'set-state-repl' into set-state-annot
BiancaIalangi Apr 29, 2024
d8e36e9
set state via AnnotatedValue - whitebox framework
BiancaIalangi Apr 29, 2024
f21b3fc
set state via AnnotatedValue - separate types
BiancaIalangi Apr 29, 2024
f152723
set state via AnnotatedValue - small fix
BiancaIalangi Apr 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ mod price_aggregator_proxy;
const DECIMALS: u8 = 0;
const EGLD_TICKER: &[u8] = b"EGLD";
const NR_ORACLES: usize = 4;
const OWNER_ADDRESS_EXPR: &str = "address:owner";
const PRICE_AGGREGATOR_ADDRESS_EXPR: &str = "sc:price-aggregator";
const PRICE_AGGREGATOR_PATH_EXPR: &str = "mxsc:output/multiversx-price-aggregator-sc.mxsc.json";
const SLASH_AMOUNT: u64 = 10;
const SLASH_QUORUM: usize = 3;
const STAKE_AMOUNT: u64 = 20;
Expand All @@ -21,13 +18,15 @@ const USD_TICKER: &[u8] = b"USDC";

const PRICE_AGGREGATOR_ADDRESS: TestSCAddress = TestSCAddress::new("price-aggregator");
const OWNER_ADDRESS: TestAddress = TestAddress::new("owner");
const PRICE_AGGREGATOR_PATH: MxscPath =
MxscPath::new("output/multiversx-price-aggregator-sc.mxsc.json");

fn world() -> ScenarioWorld {
let mut blockchain = ScenarioWorld::new();

blockchain.set_current_dir_from_workspace("contracts/core/price-aggregator");
blockchain.register_contract(
PRICE_AGGREGATOR_PATH_EXPR,
PRICE_AGGREGATOR_PATH,
multiversx_price_aggregator_sc::ContractBuilder,
);

Expand All @@ -48,22 +47,23 @@ impl PriceAggregatorTestState {
world.current_block().block_timestamp(100);

world.set_state_step(SetStateStep::new()).new_address(
OWNER_ADDRESS_EXPR,
OWNER_ADDRESS,
1,
PRICE_AGGREGATOR_ADDRESS_EXPR,
PRICE_AGGREGATOR_ADDRESS,
);

let mut oracles = Vec::new();
for i in 1..=NR_ORACLES {
let address_expr = format!("address:oracle{}", i);
let address_value = AddressValue::from(address_expr.as_str());
let address_name = format!("oracle{i}");
let address = TestAddress::new(&address_name);
let address_value = AddressValue::from(address);

world.account(address_expr).nonce(1).balance(STAKE_AMOUNT);
world.account(address).nonce(1).balance(STAKE_AMOUNT);
oracles.push(address_value);
}

let price_aggregator_whitebox = WhiteboxContract::new(
PRICE_AGGREGATOR_ADDRESS_EXPR,
PRICE_AGGREGATOR_ADDRESS,
multiversx_price_aggregator_sc::contract_obj,
);

Expand All @@ -75,8 +75,6 @@ impl PriceAggregatorTestState {
}

fn deploy(&mut self) -> &mut Self {
let price_aggregator_code = self.world.code_expression(PRICE_AGGREGATOR_PATH_EXPR);

let oracles = MultiValueVec::from(
self.oracles
.iter()
Expand All @@ -96,7 +94,7 @@ impl PriceAggregatorTestState {
SUBMISSION_COUNT,
oracles,
)
.code(price_aggregator_code)
.code(PRICE_AGGREGATOR_PATH)
.run();

for address in self.oracles.iter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use multiversx_sc_scenario::imports::*;
const DECIMALS: u8 = 0;
const EGLD_TICKER: &[u8] = b"EGLD";
const NR_ORACLES: usize = 50;
const OWNER_ADDRESS_EXPR: &str = "address:owner";
const OWNER: TestAddress = TestAddress::new("owner");
const PRICE_AGGREGATOR_ADDRESS_EXPR: &str = "sc:price-aggregator";
const PRICE_AGGREGATOR_PATH_EXPR: &str = "mxsc:../output/multiversx-price-aggregator-sc.mxsc.json";
const PRICE_AGGREGATOR_ADDRESS: TestSCAddress = TestSCAddress::new("price-aggregator");
const PRICE_AGGREGATOR_PATH: MxscPath =
MxscPath::new("../output/multiversx-price-aggregator-sc.mxsc.json");
const SLASH_AMOUNT: u64 = 10;
const SLASH_QUORUM: usize = 3;
const STAKE_AMOUNT: u64 = 20;
Expand All @@ -20,14 +20,11 @@ const USD_TICKER: &[u8] = b"USDC";

mod price_aggregator_proxy;

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

fn world() -> ScenarioWorld {
let mut blockchain = ScenarioWorld::new();

blockchain.set_current_dir_from_workspace("contracts/core/price-aggregator");
blockchain.register_contract(
PRICE_AGGREGATOR_PATH_EXPR,
PRICE_AGGREGATOR_PATH,
multiversx_price_aggregator_sc::ContractBuilder,
);

Expand All @@ -37,50 +34,45 @@ fn world() -> ScenarioWorld {
struct PriceAggregatorTestState {
world: ScenarioWorld,
oracles: Vec<AddressValue>,
price_aggregator_contract: PriceAggregatorContract,
price_aggregator_whitebox: WhiteboxContract<ContractObj<DebugApi>>,
}

impl PriceAggregatorTestState {
fn new() -> Self {
let mut world = world();
world.start_trace();

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

let mut oracles = Vec::new();
for i in 1..=NR_ORACLES {
let address_expr = format!("address:oracle{}", i);
let address_value = AddressValue::from(address_expr.as_str());
let address_expr = format!("oracle{}", i);
let address: TestAddress = TestAddress::new(address_expr.as_str());
let address_value = AddressValue::from(address.eval_to_expr().as_str());

set_state_step = set_state_step.put_account(
address_expr.as_str(),
Account::new().nonce(1).balance(STAKE_AMOUNT),
);
world.account(address).nonce(1).balance(STAKE_AMOUNT);

oracles.push(address_value);
}
world.start_trace().set_state_step(set_state_step);

let price_aggregator_contract = PriceAggregatorContract::new(PRICE_AGGREGATOR_ADDRESS_EXPR);
let price_aggregator_whitebox = WhiteboxContract::new(
PRICE_AGGREGATOR_ADDRESS_EXPR,
PRICE_AGGREGATOR_ADDRESS,
multiversx_price_aggregator_sc::contract_obj,
);

Self {
world,
oracles,
price_aggregator_contract,
price_aggregator_whitebox,
}
}

fn deploy(&mut self) -> &mut Self {
let price_aggregator_code = self.world.code_expression(PRICE_AGGREGATOR_PATH_EXPR);

let oracles = MultiValueVec::from(
self.oracles
.iter()
Expand All @@ -101,14 +93,14 @@ impl PriceAggregatorTestState {
SUBMISSION_COUNT,
oracles,
)
.code(price_aggregator_code)
.code(PRICE_AGGREGATOR_PATH)
.run();

for address in self.oracles.iter() {
self.world
.tx()
.from(&address.to_address())
.to(&self.price_aggregator_contract.to_address())
.to(PRICE_AGGREGATOR_ADDRESS)
.gas(5_000_000u64)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.stake()
Expand All @@ -123,7 +115,7 @@ impl PriceAggregatorTestState {
self.world
.tx()
.from(OWNER)
.to(&self.price_aggregator_contract.to_address())
.to(PRICE_AGGREGATOR_ADDRESS)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.set_pair_decimals(EGLD_TICKER, USD_TICKER, DECIMALS)
.run();
Expand All @@ -133,7 +125,7 @@ impl PriceAggregatorTestState {
self.world
.tx()
.from(OWNER)
.to(&self.price_aggregator_contract.to_address())
.to(PRICE_AGGREGATOR_ADDRESS)
.gas(5_000_000u64)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.unpause_endpoint()
Expand All @@ -144,7 +136,7 @@ impl PriceAggregatorTestState {
self.world
.tx()
.from(&from.to_address())
.to(&self.price_aggregator_contract.to_address())
.to(PRICE_AGGREGATOR_ADDRESS)
.gas(7_000_000u64)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.submit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use crowdfunding_esdt::crowdfunding_esdt_proxy;
use multiversx_sc_scenario::imports::*;

const CF_DEADLINE: u64 = 7 * 24 * 60 * 60; // 1 week in seconds
const CF_TOKEN_ID: &[u8] = b"CROWD-123456";
const CF_TOKEN_ID_EXPR: &str = "str:CROWD-123456";
const CF_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("CROWD-123456");
const FIRST_USER_ADDRESS: TestAddress = TestAddress::new("first-user");
const OWNER_ADDRESS: TestAddress = TestAddress::new("owner");
const SECOND_USER_ADDRESS: TestAddress = TestAddress::new("second-user");
Expand All @@ -26,18 +25,18 @@ impl CrowdfundingESDTTestState {
fn new() -> Self {
let mut world = world();

world.account(OWNER_ADDRESS).nonce(1);

world
.account(OWNER_ADDRESS)
.nonce(1)
.account(FIRST_USER_ADDRESS)
.nonce(1)
.balance("1000")
.esdt_balance(CF_TOKEN_ID_EXPR, "1000")
.balance(1000)
.esdt_balance(CF_TOKEN_ID, 1000);

world
.account(SECOND_USER_ADDRESS)
.nonce(1)
.esdt_balance(CF_TOKEN_ID_EXPR, "1000");

world.new_address(OWNER_ADDRESS, 1, CROWDFUNDING_ADDRESS);
.esdt_balance(CF_TOKEN_ID, 1000);

Self { world }
}
Expand All @@ -53,6 +52,7 @@ impl CrowdfundingESDTTestState {
EgldOrEsdtTokenIdentifier::esdt(CF_TOKEN_ID),
)
.code(CODE_PATH)
.new_address(CROWDFUNDING_ADDRESS)
.run();
}

Expand All @@ -76,7 +76,7 @@ impl CrowdfundingESDTTestState {
.query()
.to(CROWDFUNDING_ADDRESS)
.typed(crowdfunding_esdt_proxy::CrowdfundingProxy)
.deposit(donor.eval_to_array())
.deposit(donor)
.returns(ExpectValue(amount))
.run();
}
Expand All @@ -101,10 +101,10 @@ impl CrowdfundingESDTTestState {
.run();
}

fn check_esdt_balance(&mut self, address: TestAddress, balance_expr: &str) {
fn check_esdt_balance(&mut self, address: TestAddress, balance: u64) {
self.world
.check_account(address)
.esdt_balance(CF_TOKEN_ID_EXPR, balance_expr);
.esdt_balance(CF_TOKEN_ID, balance);
}

fn set_block_timestamp(&mut self, block_timestamp_expr: u64) {
Expand Down Expand Up @@ -181,9 +181,9 @@ fn test_successful_cf() {
// owner claim
state.claim(OWNER_ADDRESS);

state.check_esdt_balance(OWNER_ADDRESS, "2_000");
state.check_esdt_balance(FIRST_USER_ADDRESS, "0");
state.check_esdt_balance(SECOND_USER_ADDRESS, "0");
state.check_esdt_balance(OWNER_ADDRESS, 2000);
state.check_esdt_balance(FIRST_USER_ADDRESS, 0);
state.check_esdt_balance(SECOND_USER_ADDRESS, 0);
}

#[test]
Expand Down Expand Up @@ -211,7 +211,7 @@ fn test_failed_cf() {
// second user claim
state.claim(SECOND_USER_ADDRESS);

state.check_esdt_balance(OWNER_ADDRESS, "0");
state.check_esdt_balance(FIRST_USER_ADDRESS, "1_000");
state.check_esdt_balance(SECOND_USER_ADDRESS, "1_000");
state.check_esdt_balance(OWNER_ADDRESS, 0);
state.check_esdt_balance(FIRST_USER_ADDRESS, 1000);
state.check_esdt_balance(SECOND_USER_ADDRESS, 1000);
}
Loading
Loading