Skip to content

Commit

Permalink
distribute fees to brokers (#69)
Browse files Browse the repository at this point in the history
* distribute fees to brokers

* remove taker rewards

* change trading rewards to liquidity rewards

* TODO: compatible with the previous orders

* rewrite Rewards trait

* consume liquidity and record it

* unit tests of marketing rewards

* prepare release v160

* Update CHANGELOG.md

* update weight

* update CHANGELOG.md

* scale check test

---------

Co-authored-by: Cyberaurora <[email protected]>
  • Loading branch information
kb1ns and kb1ns authored May 16, 2023
1 parent ff7914f commit 8ddd988
Show file tree
Hide file tree
Showing 13 changed files with 811 additions and 150 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
# v0.9.30-mainnet.161

- migrate trading rewards to marketing rewards
- distribute transaction fees to broker
- change the caller of `register_broker` to broker beneficiary

# v0.9.30-mainnet.159

- enable polygon chain bridge
- fix node rpc compile error
- switch the compiler toolchain to stable

# v0.9.30-node.158

- release-sidecar

# v0.9.30-mainnet.158

- fix bug about issue token
- fix bug of issuing token

# v0.9.30-node.157

Expand Down
28 changes: 23 additions & 5 deletions pallets/chainbridge-handler/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ parameter_types! {

pub struct PhantomData;

impl fuso_support::traits::Rewarding<AccountId, Balance, BlockNumber> for PhantomData {
impl fuso_support::traits::Rewarding<AccountId, Balance, (u32, u32), BlockNumber> for PhantomData {
type Balance = Balance;

fn era_duration() -> BlockNumber {
Expand All @@ -174,13 +174,30 @@ impl fuso_support::traits::Rewarding<AccountId, Balance, BlockNumber> for Phanto
0
}

fn save_trading(
_trader: &AccountId,
_amount: Balance,
_at: BlockNumber,
fn put_liquidity(_maker: &AccountId, _symbol: (u32, u32), _vol: Balance, _at: BlockNumber) {}

/// when liquidity is took out, the liquidity provider will get the reward.
/// the rewards are calculated in the formula below:
/// contribution ƒi = vol * min(current - from, era_duration) / 720
/// rewards of contribution ∂ = ƒi / ∑ƒi * era_rewards
/// NOTE: `vol` should be volume rather than amount
fn consume_liquidity(
_maker: &AccountId,
_symbol: (u32, u32),
_vol: Balance,
_current: BlockNumber,
) -> frame_support::pallet_prelude::DispatchResult {
Ok(())
}

/// remove liquidity
fn remove_liquidity(
_maker: &AccountId,
_symbol: (u32, u32),
_vol: Balance,
) -> Result<BlockNumber, frame_support::pallet_prelude::DispatchError> {
Ok(1)
}
}

parameter_types! {
Expand All @@ -194,6 +211,7 @@ parameter_types! {

impl pallet_fuso_verifier::Config for Test {
type Asset = Assets;
type BrokerBeneficiary = ();
type Callback = RuntimeCall;
type DominatorCheckGracePeriod = DominatorCheckGracePeriod;
type DominatorOnlineThreshold = DominatorOnlineThreshold;
Expand Down
25 changes: 23 additions & 2 deletions pallets/fuso-support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pub trait NamedReservableToken<AccountId>: Token<AccountId> {
) -> sp_std::result::Result<Self::Balance, DispatchError>;
}

pub trait Rewarding<AccountId, Volume: Copy, BlockNumber> {
pub trait Rewarding<AccountId, Volume, Symbol, BlockNumber> {
/// $TAO
type Balance: Member
+ Parameter
Expand All @@ -200,7 +200,28 @@ pub trait Rewarding<AccountId, Volume: Copy, BlockNumber> {

fn acked_reward(who: &AccountId) -> Self::Balance;

fn save_trading(trader: &AccountId, amount: Volume, at: BlockNumber) -> DispatchResult;
/// put liquidity `vol` into `symbol`(override the previous value) `at` block number.
/// NOTE: if the `maker` has already added liquidity at the same `symbol`, then the block number will be updated to `at`.
fn put_liquidity(maker: &AccountId, symbol: Symbol, vol: Volume, at: BlockNumber);

/// when liquidity is took out, the liquidity provider will get the reward.
/// the rewards are calculated in the formula below:
/// contribution ƒi = vol * min(current - from, era_duration) / 720
/// rewards of contribution ∂ = ƒi / ∑ƒi * era_rewards
/// NOTE: `vol` should be volume rather than amount
fn consume_liquidity(
maker: &AccountId,
symbol: Symbol,
vol: Volume,
current: BlockNumber,
) -> DispatchResult;

/// remove liquidity
fn remove_liquidity(
maker: &AccountId,
symbol: Symbol,
vol: Volume,
) -> Result<BlockNumber, DispatchError>;
}

pub trait Agent<AccountId> {
Expand Down
2 changes: 0 additions & 2 deletions pallets/indicator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub mod pallet {
}

#[pallet::event]
#[pallet::generate_deposit(pub (super) fn deposit_event)]
pub enum Event<T: Config> {
PriceUpdated(TokenId<T>, Balance<T>),
}
Expand Down Expand Up @@ -103,7 +102,6 @@ pub mod pallet {
+ (Perquintill::from_rational(volume % amount, amount).deconstruct()
as u128)
.into();
Self::deposit_event(Event::PriceUpdated(token_id, p.price));
}
});
}
Expand Down
45 changes: 36 additions & 9 deletions pallets/market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ pub mod pallet {
#[pallet::weight(8_790_000_000)]
pub fn register_broker(
origin: OriginFor<T>,
beneficiary: T::AccountId,
broker: T::AccountId,
rpc_endpoint: Vec<u8>,
name: Vec<u8>,
) -> DispatchResultWithPostInfo {
let broker = ensure_signed(origin)?;
let beneficiary = ensure_signed(origin)?;
ensure!(name.len() <= 20, Error::<T>::BrokerNameTooLong);
let requires = T::BrokerStakingThreshold::get();
T::Assets::transfer_token(
&broker,
&beneficiary,
T::Assets::native_token_id(),
requires,
&Self::system_account(),
Expand All @@ -163,18 +163,45 @@ pub mod pallet {
Ok(().into())
}

#[pallet::weight(8_790_000_000)]
pub fn deregister_broker(
origin: OriginFor<T>,
broker: T::AccountId,
) -> DispatchResultWithPostInfo {
let beneficiary = ensure_signed(origin)?;
Brokers::<T>::try_mutate_exists(&broker, |b| -> DispatchResult {
ensure!(b.is_some(), Error::<T>::BrokerNotFound);
let broker = b.take().unwrap();
ensure!(
broker.beneficiary == beneficiary,
Error::<T>::BrokerNotFound
);
T::Assets::transfer_token(
&Self::system_account(),
T::Assets::native_token_id(),
broker.staked,
&broker.beneficiary,
)?;
Ok(())
})?;
Self::deposit_event(Event::BrokerDeregistered(broker));
Ok(().into())
}

#[pallet::weight(8_790_000_000)]
pub fn broker_set_rpc_endpoint(
origin: OriginFor<T>,
broker: T::AccountId,
rpc_endpoint: Vec<u8>,
) -> DispatchResultWithPostInfo {
let broker = ensure_signed(origin)?;
let admin = ensure_signed(origin)?;
Brokers::<T>::try_mutate_exists(&broker, |b| -> DispatchResult {
if let Some(broker) = b {
broker.rpc_endpoint = rpc_endpoint;
Ok(())
} else {
Err(Error::<T>::BrokerNotFound.into())
match b {
Some(broker) if broker.beneficiary == admin => {
broker.rpc_endpoint = rpc_endpoint;
Ok(())
}
_ => Err(Error::<T>::BrokerNotFound.into()),
}
})?;
Ok(().into())
Expand Down
5 changes: 3 additions & 2 deletions pallets/market/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ pub fn register_broker_should_work() {
b"test-broker".to_vec(),
));
assert_eq!(Balances::free_balance(&ferdie), 90000 * DOLLARS);
assert_eq!(Market::beneficiary(ferdie.clone()), charlie.clone().into());
assert_eq!(Market::beneficiary(charlie.clone()), ferdie.clone().into());
assert_ok!(Market::broker_set_rpc_endpoint(
RuntimeOrigin::signed(ferdie.clone()),
charlie.clone().into(),
b"192.168.1.1".to_vec(),
));
assert_eq!(
crate::Brokers::<Test>::get(ferdie).unwrap().rpc_endpoint,
crate::Brokers::<Test>::get(charlie).unwrap().rpc_endpoint,
b"192.168.1.1"
);
});
Expand Down
Loading

0 comments on commit 8ddd988

Please sign in to comment.