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

get fee from price aggregator #24

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions fee-market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ multiversx_sc::imports!();
pub mod enable_fee;
pub mod fee_common;
pub mod fee_type;
pub mod pairs;
pub mod price_aggregator;
pub mod subtract_fee;

#[multiversx_sc::contract]
Expand All @@ -14,17 +14,17 @@ pub trait FeeMarket:
+ fee_common::CommonFeeModule
+ fee_type::FeeTypeModule
+ subtract_fee::SubtractFeeModule
+ pairs::PairsModule
+ price_aggregator::PriceAggregatorModule
+ utils::UtilsModule
+ bls_signature::BlsSignatureModule
{
#[init]
fn init(&self, esdt_safe_address: ManagedAddress, pair_for_query: ManagedAddress) {
fn init(&self, esdt_safe_address: ManagedAddress, price_aggregator_address: ManagedAddress) {
self.require_sc_address(&esdt_safe_address);
self.require_sc_address(&pair_for_query);
self.require_sc_address(&price_aggregator_address);

self.esdt_safe_address().set(esdt_safe_address);
self.pair_for_query().set(pair_for_query);
self.price_aggregator_address().set(price_aggregator_address);
self.fee_enabled().set(true);
}

Expand Down
96 changes: 0 additions & 96 deletions fee-market/src/pairs.rs

This file was deleted.

94 changes: 94 additions & 0 deletions fee-market/src/price_aggregator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
multiversx_sc::imports!();

pub type AggregatorOutputType<M> =
OptionalValue<MultiValue6<u32, ManagedBuffer<M>, ManagedBuffer<M>, u64, BigUint<M>, u8>>;

pub const DASH_TICKER_LEN: usize = 7;

pub struct AggregatorResult<M: ManagedTypeApi> {
pub round_id: u32,
pub from: ManagedBuffer<M>,
pub to: ManagedBuffer<M>,
pub timestamp: u64,
pub price: BigUint<M>,
pub decimals: u8,
}

impl<M: ManagedTypeApi> From<AggregatorOutputType<M>> for AggregatorResult<M> {
fn from(value: AggregatorOutputType<M>) -> Self {
let opt_value = value.into_option();
if opt_value.is_none() {
M::error_api_impl().signal_error(b"Invalid aggregator value");
}

let result = unsafe { opt_value.unwrap_unchecked() };
let (round_id, from, to, timestamp, price, decimals) = result.into_tuple();

Self {
round_id,
from,
to,
timestamp,
price,
decimals,
}
}
}

mod price_aggregator_proxy {
use super::AggregatorOutputType;

multiversx_sc::imports!();

#[multiversx_sc::proxy]
pub trait PriceAggregatorProxy {
#[view(latestPriceFeedOptional)]
fn latest_price_feed_optional(
&self,
from: ManagedBuffer,
to: ManagedBuffer,
) -> AggregatorOutputType<Self::Api>;
}
}

#[multiversx_sc::module]
pub trait PriceAggregatorModule {
fn get_safe_price(
&self,
input_token_id: &TokenIdentifier,
output_token_id: &TokenIdentifier,
) -> BigUint {
let price_aggregator_address = self.price_aggregator_address().get();
let input_ticker = self.get_token_ticker(input_token_id);
let output_ticker = self.get_token_ticker(output_token_id);

let agg_output: AggregatorOutputType<Self::Api> = self
.price_aggregator_proxy(price_aggregator_address)
.latest_price_feed_optional(input_ticker, output_ticker)
.execute_on_dest_context();
let result = AggregatorResult::from(agg_output);

result.price
}

fn get_token_ticker(&self, token_id: &TokenIdentifier) -> ManagedBuffer {
require!(
token_id.is_valid_esdt_identifier(),
"Invalid ESDT identifier"
);

let buffer = token_id.as_managed_buffer();
let ticker = buffer.copy_slice(0, buffer.len() - DASH_TICKER_LEN);

unsafe { ticker.unwrap_unchecked() }
}

#[storage_mapper("priceAggregatorAddress")]
fn price_aggregator_address(&self) -> SingleValueMapper<ManagedAddress>;

#[proxy]
fn price_aggregator_proxy(
&self,
to: ManagedAddress,
) -> price_aggregator_proxy::Proxy<Self::Api>;
}
5 changes: 3 additions & 2 deletions fee-market/src/subtract_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub trait SubtractFeeModule:
crate::enable_fee::EnableFeeModule
+ crate::fee_type::FeeTypeModule
+ crate::fee_common::CommonFeeModule
+ crate::pairs::PairsModule
+ crate::price_aggregator::PriceAggregatorModule
+ utils::UtilsModule
+ bls_signature::BlsSignatureModule
{
Expand Down Expand Up @@ -213,7 +213,8 @@ pub trait SubtractFeeModule:
mut args: SubtractPaymentArguments<Self::Api>,
) -> FinalPayment<Self::Api> {
let input_payment = args.payment.clone();
let payment_amount_in_fee_token = self.get_safe_price(args.payment, &args.fee_token);
let payment_amount_in_fee_token =
self.get_safe_price(&args.payment.token_identifier, &args.fee_token);
args.payment = EsdtTokenPayment::new(
args.fee_token.clone(),
0,
Expand Down
6 changes: 2 additions & 4 deletions fee-market/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 16
// Endpoints: 14
// Async Callback (empty): 1
// Total number of exported functions: 18
// Total number of exported functions: 16

#![no_std]
#![allow(internal_features)]
Expand All @@ -31,8 +31,6 @@ multiversx_sc_wasm_adapter::endpoints! {
distributeFees => distribute_fees
subtractFee => subtract_fee
getUsersWhitelist => users_whitelist
addPairs => add_pairs
removePairs => remove_pairs
setMinValidSigners => set_min_valid_signers
addSigners => add_signers
removeSigners => remove_signers
Expand Down