Skip to content

Commit

Permalink
cleanup and extra impls for AddressValue
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicalinluca committed Apr 22, 2024
1 parent 8415371 commit dbf0e2b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,24 @@ impl PriceAggregatorTestState {
fn new() -> Self {
let mut world = world();

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

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

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());

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

world.account(address_expr).nonce(1).balance(STAKE_AMOUNT);
oracles.push(address_value);
}
world.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,
multiversx_price_aggregator_sc::contract_obj,
Expand Down Expand Up @@ -104,7 +102,7 @@ impl PriceAggregatorTestState {
for address in self.oracles.iter() {
self.world
.tx()
.from(&address.to_address())
.from(address)
.to(PRICE_AGGREGATOR)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.stake()
Expand Down Expand Up @@ -138,7 +136,7 @@ impl PriceAggregatorTestState {
fn submit(&mut self, from: &AddressValue, submission_timestamp: u64, price: u64) {
self.world
.tx()
.from(&from.to_address())
.from(from)
.to(PRICE_AGGREGATOR)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.submit(
Expand All @@ -160,7 +158,7 @@ impl PriceAggregatorTestState {
) {
self.world
.tx()
.from(&from.to_address())
.from(from)
.to(PRICE_AGGREGATOR)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.submit(
Expand All @@ -178,7 +176,7 @@ impl PriceAggregatorTestState {
fn vote_slash_member(&mut self, from: &AddressValue, member_to_slash: Address) {
self.world
.tx()
.from(&from.to_address())
.from(from)
.to(PRICE_AGGREGATOR)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.vote_slash_member(member_to_slash)
Expand Down Expand Up @@ -283,7 +281,8 @@ fn test_price_aggregator_submit_round_ok() {
let current_timestamp = 110;
state
.world
.set_state_step(SetStateStep::new().block_timestamp(current_timestamp));
.current_block()
.block_timestamp(current_timestamp);

// submit second
state.submit(&state.oracles[1].clone(), 101, 11_000);
Expand Down Expand Up @@ -340,7 +339,8 @@ fn test_price_aggregator_discarded_round() {
let current_timestamp = 100 + MAX_ROUND_DURATION_SECONDS + 1;
state
.world
.set_state_step(SetStateStep::new().block_timestamp(current_timestamp));
.current_block()
.block_timestamp(current_timestamp);

// submit second - this will discard the previous submission
state.submit(&state.oracles[1].clone(), current_timestamp - 1, 11_000);
Expand Down Expand Up @@ -378,7 +378,7 @@ fn test_price_aggregator_slashing() {
state
.world
.tx()
.from(&state.oracles[0].to_address())
.from(&state.oracles[0])
.to(PRICE_AGGREGATOR)
.typed(price_aggregator_proxy::PriceAggregatorProxy)
.slash_member(state.oracles[1].to_address())
Expand Down
84 changes: 84 additions & 0 deletions framework/scenario/src/scenario/model/value/address_value.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use multiversx_sc::{
api::ManagedTypeApi,
types::{AnnotatedValue, ManagedAddress, ManagedBuffer, TxEnv, TxFrom, TxFromSpecified},
};
use std::fmt;

use crate::multiversx_sc::types::{Address, AddressExpr, ScExpr};
Expand Down Expand Up @@ -151,3 +155,83 @@ impl From<ScExpr<'_>> for AddressValue {
}
}
}

impl<M> From<&AddressValue> for ManagedAddress<M>
where
M: ManagedTypeApi,
{
#[inline]
fn from(address_value: &AddressValue) -> Self {
ManagedAddress::from_address(&address_value.value)
}
}

impl<Env> TxFrom<Env> for AddressValue
where
Env: TxEnv,
{
fn resolve_address(&self, _env: &Env) -> ManagedAddress<Env::Api> {
self.into()
}
}

impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for AddressValue
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> multiversx_sc::types::ManagedBuffer<Env::Api> {
ManagedBuffer::from(self.original.to_string())
}

fn to_value(&self, _env: &Env) -> ManagedAddress<Env::Api> {
ManagedAddress::from_address(&self.value)
}

fn into_value(self, _env: &Env) -> ManagedAddress<Env::Api> {
ManagedAddress::from_address(&self.value)
}

fn with_value_ref<F, R>(&self, _env: &Env, f: F) -> R
where
F: FnOnce(&ManagedAddress<Env::Api>) -> R,
{
f(&ManagedAddress::from_address(&self.value))
}
}

impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for &AddressValue
where
Env: TxEnv,
{
fn annotation(&self, _env: &Env) -> multiversx_sc::types::ManagedBuffer<Env::Api> {
ManagedBuffer::from(self.original.to_string())
}

fn to_value(&self, _env: &Env) -> ManagedAddress<Env::Api> {
ManagedAddress::from_address(&self.value)
}

fn into_value(self, _env: &Env) -> ManagedAddress<Env::Api> {
ManagedAddress::from_address(&self.value)
}

fn with_value_ref<F, R>(&self, _env: &Env, f: F) -> R
where
F: FnOnce(&ManagedAddress<Env::Api>) -> R,
{
f(&ManagedAddress::from_address(&self.value))
}
}

impl<Env> TxFromSpecified<Env> for AddressValue where Env: TxEnv {}

impl<Env> TxFrom<Env> for &AddressValue
where
Env: TxEnv,
{
fn resolve_address(&self, _env: &Env) -> ManagedAddress<Env::Api> {
ManagedAddress::from_address(&self.value)
}
}

impl<Env> TxFromSpecified<Env> for &AddressValue where Env: TxEnv {}

0 comments on commit dbf0e2b

Please sign in to comment.