Skip to content

Commit

Permalink
update for relayer fee (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymousGiga authored Dec 29, 2022
1 parent a10c47f commit d5582e7
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 103 deletions.
12 changes: 3 additions & 9 deletions bridge/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ benchmarks! {
origin.into(),
"test-account.testnet".to_string().as_bytes().to_vec(),
min,
min,
);
assert!(ret.is_ok());
}
Expand All @@ -120,7 +119,7 @@ benchmarks! {
sender: caller,
receiver: "test-account.testnet".to_string().as_bytes().to_vec(),
amount: min.into(),
fee: min.checked_into().unwrap(),
fee: 0u128.checked_into().unwrap(),
sequence: 1u64,
}
.into());
Expand Down Expand Up @@ -152,7 +151,6 @@ benchmarks! {
asset_id,
"test-account.testnet".to_string().as_bytes().to_vec(),
10_000u32.into(),
fee,
);
}
verify {
Expand All @@ -162,7 +160,7 @@ benchmarks! {
receiver: "test-account.testnet".to_string().as_bytes().to_vec(),
amount: 10_000u32.into(),
sequence: 1u64,
fee,
fee: 0u128.checked_into().unwrap(),
}
.into());
}
Expand All @@ -182,8 +180,6 @@ benchmarks! {
0u128.into(),
0u128.into(),
"test-account.testnet".to_string().as_bytes().to_vec(),
fee,
100u32,
);

assert!(ret.is_ok());
Expand All @@ -195,7 +191,7 @@ benchmarks! {
item: 0u128.into(),
sender: caller,
receiver: "test-account.testnet".to_string().as_bytes().to_vec(),
fee,
fee: 0u128.checked_into().unwrap(),
sequence: 1u64,
}
.into());
Expand Down Expand Up @@ -314,8 +310,6 @@ benchmarks! {
0u128.into(),
0u128.into(),
"test-account.testnet".to_string().as_bytes().to_vec(),
fee,
100u32,
);
let fee = <T as BridgeConfig>::Currency::minimum_balance();
}: {
Expand Down
56 changes: 23 additions & 33 deletions bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,11 @@ pub mod pallet {
origin: OriginFor<T>,
receiver_id: Vec<u8>,
amount: BalanceOf<T>,
fee: BalanceOf<T>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(T::AppchainInterface::is_activated(), Error::<T>::NotActivated);

Self::do_lock_fungible_transfer_fee(sender.clone(), fee)?;
Self::do_lock(sender, receiver_id, amount, fee)?;
Self::do_lock(sender, receiver_id, amount)?;

Ok(())
}
Expand All @@ -199,13 +197,11 @@ pub mod pallet {
asset_id: T::AssetId,
receiver_id: Vec<u8>,
amount: T::AssetBalance,
fee: BalanceOf<T>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(T::AppchainInterface::is_activated(), Error::<T>::NotActivated);

Self::do_lock_fungible_transfer_fee(sender.clone(), fee)?;
Self::do_burn_nep141(asset_id, sender, receiver_id, amount, fee)?;
Self::do_burn_nep141(asset_id, sender, receiver_id, amount)?;

Ok(())
}
Expand All @@ -217,14 +213,11 @@ pub mod pallet {
collection_id: T::CollectionId,
item_id: T::ItemId,
receiver_id: Vec<u8>,
fee: BalanceOf<T>,
metadata_length: u32,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(T::AppchainInterface::is_activated(), Error::<T>::NotActivated);

Self::do_lock_nonfungible_transfer_fee(sender.clone(), fee, metadata_length)?;
Self::do_lock_nonfungible(collection_id, item_id, sender, receiver_id, fee)?;
Self::do_lock_nonfungible(collection_id, item_id, sender, receiver_id)?;

Ok(())
}
Expand Down Expand Up @@ -482,8 +475,8 @@ pub mod pallet {
NativeTokenPriceSetedIsZero,
/// Update token price must use oracle account.
UpdatePriceWithNoPermissionAccount,
/// Invalid fee.
InvalidFee,
/// Not enough balance for deduction fee.
NotEnoughBalanceForDeductionFee,
/// Invalid coef.
InvalidCoef,
/// Invalid fee make overflow
Expand Down Expand Up @@ -628,46 +621,43 @@ impl<T: Config> Pallet<T> {
Ok(())
}

fn do_lock_fungible_transfer_fee(sender: T::AccountId, fee: BalanceOf<T>) -> DispatchResult {
let min_fee: Option<BalanceOf<T>> = <CrosschainTransferFee<T>>::try_get(
pub(crate) fn do_lock_fungible_transfer_fee(
sender: &T::AccountId,
) -> Result<BalanceOf<T>, DispatchError> {
let fee: Option<BalanceOf<T>> = <CrosschainTransferFee<T>>::try_get(
CrossChainTransferType::Fungible,
)
.unwrap_or_else(|_| {
// Need Check.
log!(warn, "Storage CrosschainTransferFee is empty, default ft fee is zero.");
0u128.checked_into()
});
let fee = fee.unwrap();

if Some(fee) < min_fee {
return Err(Error::<T>::InvalidFee.into())
}

T::Currency::transfer(&sender, &Self::account_id(), fee, AllowDeath)?;
T::Currency::transfer(sender, &Self::account_id(), fee, AllowDeath)
.map_err(|_| Error::<T>::NotEnoughBalanceForDeductionFee)?;

Ok(())
Ok(fee)
}

fn do_lock_nonfungible_transfer_fee(
sender: T::AccountId,
fee: BalanceOf<T>,
pub(crate) fn do_lock_nonfungible_transfer_fee(
sender: &T::AccountId,
_metadata_length: u32,
) -> DispatchResult {
let min_fee: Option<BalanceOf<T>> =
) -> Result<BalanceOf<T>, DispatchError> {
log!(debug, "Metadata length is {:?} .", _metadata_length);

let fee: Option<BalanceOf<T>> =
<CrosschainTransferFee<T>>::try_get(CrossChainTransferType::Nonfungible)
.unwrap_or_else(|_| {
// Need Check.
log!(warn, "Storage CrosschainTransferFee is empty, default nft fee is zero.");
0u128.checked_into()
});
let fee = fee.unwrap();

// The min_fee will be related to metadata_length in future.

if Some(fee) < min_fee {
return Err(Error::<T>::InvalidFee.into())
}
T::Currency::transfer(sender, &Self::account_id(), fee, AllowDeath)
.map_err(|_| Error::<T>::NotEnoughBalanceForDeductionFee)?;

T::Currency::transfer(&sender, &Self::account_id(), fee, AllowDeath)?;

Ok(())
Ok(fee)
}
}
3 changes: 2 additions & 1 deletion bridge/src/nep141.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ impl<T: Config> Pallet<T> {
sender: T::AccountId,
receiver_id: Vec<u8>,
amount: T::AssetBalance,
fee: BalanceOf<T>,
) -> DispatchResult {
let receiver_id =
String::from_utf8(receiver_id).map_err(|_| Error::<T>::InvalidReceiverId)?;
Expand All @@ -18,6 +17,8 @@ impl<T: Config> Pallet<T> {

let token_id = String::from_utf8(token_id).map_err(|_| Error::<T>::InvalidTokenId)?;

//deduction fee
let fee: BalanceOf<T> = Self::do_lock_fungible_transfer_fee(&sender)?;
let fee_wrapped: u128 = fee.checked_into().ok_or(Error::<T>::AmountOverflow)?;

<T::Fungibles as fungibles::Mutate<T::AccountId>>::burn_from(asset_id, &sender, amount)?;
Expand Down
8 changes: 7 additions & 1 deletion bridge/src/nonfungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ impl<T: Config> Pallet<T> {
item: T::ItemId,
sender: T::AccountId,
receiver_id: Vec<u8>,
fee: BalanceOf<T>,
) -> DispatchResult {
let receiver_id =
String::from_utf8(receiver_id).map_err(|_| Error::<T>::InvalidReceiverId)?;

let metadata = T::Convertor::convert_into_nep171_metadata(collection, item)
.ok_or::<Error<T>>(Error::<T>::ConvertorNotImplement.into())?;

// Deduction fee:
// This is a temporary scheme for calculating fees.
// It may be changed to be related to the length of messages later.
let data_in_vec =
metadata.clone().try_to_vec().map_err(|_| Error::<T>::BorshSerializeFailed)?;
let fee: BalanceOf<T> =
Self::do_lock_nonfungible_transfer_fee(&sender, data_in_vec.len() as u32)?;
let fee_wrapped: u128 = fee.checked_into().ok_or(Error::<T>::AmountOverflow)?;

<T::Nonfungibles as nonfungibles::Transfer<T::AccountId>>::transfer(
Expand Down
16 changes: 1 addition & 15 deletions bridge/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,25 @@ fn test_lock() {
100000
));
let minimum_amount = Balances::minimum_balance();
let fee = Balances::minimum_balance();
assert_noop!(
OctopusBridge::lock(
origin.clone(),
"test-account.testnet".to_string().as_bytes().to_vec(),
minimum_amount,
fee,
),
Error::<Test>::NotActivated
);

assert_ok!(OctopusAppchain::force_set_is_activated(RuntimeOrigin::root(), true));
assert_noop!(
OctopusBridge::lock(origin.clone(), vec![0, 159], minimum_amount, fee),
OctopusBridge::lock(origin.clone(), vec![0, 159], minimum_amount),
Error::<Test>::InvalidReceiverId
);

assert_ok!(OctopusBridge::lock(
origin,
"test-account.testnet".to_string().as_bytes().to_vec(),
minimum_amount,
fee,
));
});
}
Expand All @@ -133,7 +130,6 @@ fn test_burn_nep141() {
let origin = RuntimeOrigin::signed(alice.clone());
let source = sp_runtime::MultiAddress::Id(alice);
new_tester().execute_with(|| {
let fee = Balances::minimum_balance();
assert_ok!(Balances::set_balance(
RuntimeOrigin::root(),
source.clone(),
Expand All @@ -148,7 +144,6 @@ fn test_burn_nep141() {
0,
"test-account.testnet".to_string().as_bytes().to_vec(),
10000000000,
fee,
),
Error::<Test>::NotActivated
);
Expand All @@ -163,7 +158,6 @@ fn test_burn_nep141() {
0,
"test-account.testnet".to_string().as_bytes().to_vec(),
100000000,
fee,
));
});
}
Expand All @@ -174,7 +168,6 @@ pub fn test_lock_nonfungible() {
let origin = RuntimeOrigin::signed(alice.clone());
let source = sp_runtime::MultiAddress::Id(alice);
new_tester().execute_with(|| {
let fee = Balances::minimum_balance();
assert_ok!(Balances::set_balance(
RuntimeOrigin::root(),
source.clone(),
Expand All @@ -189,8 +182,6 @@ pub fn test_lock_nonfungible() {
0,
42,
"test-account.testnet".to_string().as_bytes().to_vec(),
fee,
1,
),
Error::<Test>::NotActivated
);
Expand All @@ -200,8 +191,6 @@ pub fn test_lock_nonfungible() {
0,
42,
"test-account.testnet".to_string().as_bytes().to_vec(),
fee,
1,
));
});
}
Expand Down Expand Up @@ -266,7 +255,6 @@ pub fn test_force_unlock_nft() {
assert_ok!(Uniques::force_create(RuntimeOrigin::root(), 0, source.clone(), true));
assert_ok!(Uniques::mint(origin.clone(), 0, 42, source.clone(),));
assert_ok!(OctopusAppchain::force_set_is_activated(RuntimeOrigin::root(), true));
let fee = Balances::minimum_balance();
assert_ok!(Balances::set_balance(
RuntimeOrigin::root(),
source.clone(),
Expand All @@ -278,8 +266,6 @@ pub fn test_force_unlock_nft() {
0,
42,
"test-account.testnet".to_string().as_bytes().to_vec(),
fee,
1,
));

assert_ok!(OctopusBridge::force_unlock_nonfungible(
Expand Down
4 changes: 3 additions & 1 deletion bridge/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ impl<T: Config> Pallet<T> {
sender: T::AccountId,
receiver_id: Vec<u8>,
amount: BalanceOf<T>,
fee: BalanceOf<T>,
) -> DispatchResult {
let receiver_id =
String::from_utf8(receiver_id).map_err(|_| Error::<T>::InvalidReceiverId)?;

let amount_wrapped: u128 = amount.checked_into().ok_or(Error::<T>::AmountOverflow)?;

//deduction fee
let fee: BalanceOf<T> = Self::do_lock_fungible_transfer_fee(&sender)?;
let fee_wrapped: u128 = fee.checked_into().ok_or(Error::<T>::AmountOverflow)?;

T::Currency::transfer(&sender, &Self::account_id(), amount, AllowDeath)?;
Expand Down
Loading

0 comments on commit d5582e7

Please sign in to comment.