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

Optimize vtoken minting #1469

Merged
merged 3 commits into from
Oct 16, 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
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ where
C::Api: SalpRuntimeApi<Block, ParaId, AccountId>,
C::Api: StablePoolRuntimeApi<Block>,
C::Api: LendMarketApi<Block, AccountId, Balance>,
C::Api: VtokenMintingRuntimeApi<Block, CurrencyId>,
C::Api: VtokenMintingRuntimeApi<Block, CurrencyId, Balance>,
C::Api: ZenlinkProtocolRuntimeApi<Block, AccountId, AssetId>,
C::Api:
zenlink_stable_amm_runtime_api::StableAmmApi<Block, CurrencyId, Balance, AccountId, PoolId>,
Expand Down Expand Up @@ -154,7 +154,7 @@ where
C::Api: SalpRuntimeApi<Block, ParaId, AccountId>,
C::Api: BbBNCRuntimeApi<Block, AccountId>,
C::Api: LendMarketApi<Block, AccountId, Balance>,
C::Api: VtokenMintingRuntimeApi<Block, CurrencyId>,
C::Api: VtokenMintingRuntimeApi<Block, CurrencyId, Balance>,
C::Api: ZenlinkProtocolRuntimeApi<Block, AccountId, AssetId>,
C::Api: StablePoolRuntimeApi<Block>,
C::Api: BlockBuilder<Block>,
Expand Down
4 changes: 0 additions & 4 deletions pallets/bb-bnc/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,8 @@ impl bifrost_vtoken_minting::Config for Runtime {
type ExitAccount = BifrostExitAccount;
type FeeAccount = BifrostFeeAccount;
type RedeemFeeAccount = BifrostFeeAccount;
type BifrostSlp = Slp;
type BifrostSlpx = SlpxInterface;
type RelayChainToken = RelayCurrencyId;
type CurrencyIdConversion = AssetIdMaps<Runtime>;
type CurrencyIdRegister = AssetIdMaps<Runtime>;
type WeightInfo = ();
type OnRedeemSuccess = ();
type XcmTransfer = XTokens;
Expand All @@ -220,7 +217,6 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
}

ord_parameter_types! {
Expand Down
4 changes: 0 additions & 4 deletions pallets/buy-back/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,8 @@ impl bifrost_vtoken_minting::Config for Runtime {
type ExitAccount = BifrostExitAccount;
type FeeAccount = BifrostFeeAccount;
type RedeemFeeAccount = BifrostFeeAccount;
type BifrostSlp = Slp;
type BifrostSlpx = SlpxInterface;
type RelayChainToken = RelayCurrencyId;
type CurrencyIdConversion = AssetIdMaps<Runtime>;
type CurrencyIdRegister = AssetIdMaps<Runtime>;
type WeightInfo = ();
type OnRedeemSuccess = ();
type XcmTransfer = XTokens;
Expand All @@ -306,7 +303,6 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
}

parameter_types! {
Expand Down
4 changes: 0 additions & 4 deletions pallets/deprecated/system-maker/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,8 @@ impl bifrost_vtoken_minting::Config for Runtime {
type ExitAccount = BifrostExitAccount;
type FeeAccount = BifrostFeeAccount;
type RedeemFeeAccount = BifrostFeeAccount;
type BifrostSlp = Slp;
type BifrostSlpx = SlpxInterface;
type RelayChainToken = RelayCurrencyId;
type CurrencyIdConversion = AssetIdMaps<Runtime>;
type CurrencyIdRegister = AssetIdMaps<Runtime>;
type WeightInfo = ();
type OnRedeemSuccess = ();
type XcmTransfer = XTokens;
Expand All @@ -305,7 +302,6 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
}

parameter_types! {
Expand Down
4 changes: 0 additions & 4 deletions pallets/fee-share/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,8 @@ impl bifrost_vtoken_minting::Config for Runtime {
type ExitAccount = BifrostExitAccount;
type FeeAccount = BifrostFeeAccount;
type RedeemFeeAccount = BifrostFeeAccount;
type BifrostSlp = Slp;
type BifrostSlpx = SlpxInterface;
type RelayChainToken = RelayCurrencyId;
type CurrencyIdConversion = AssetIdMaps<Runtime>;
type CurrencyIdRegister = AssetIdMaps<Runtime>;
type WeightInfo = ();
type OnRedeemSuccess = ();
type XcmTransfer = XTokens;
Expand All @@ -418,7 +415,6 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
}

parameter_types! {
Expand Down
10 changes: 7 additions & 3 deletions pallets/leverage-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,13 @@ impl<T: Config> Pallet<T> {
let account_borrows = lend_market::Pallet::<T>::get_current_borrow_balance(&who, asset_id)?;

// Formula
// current_rate = account_borrows / ( vtoken_to_token(account_deposits) - account_borrows )
let deposits_token_value =
T::VtokenMinting::vtoken_to_token(asset_id, vtoken_id, account_deposits)?;
// current_rate = account_borrows / (
// get_currency_amount_by_v_currency_amount(account_deposits) - account_borrows )
let deposits_token_value = T::VtokenMinting::get_currency_amount_by_v_currency_amount(
asset_id,
vtoken_id,
account_deposits,
)?;
let base_token_value = deposits_token_value
.checked_sub(account_borrows)
.ok_or(ArithmeticError::Overflow)?;
Expand Down
4 changes: 0 additions & 4 deletions pallets/leverage-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,7 @@ impl bifrost_vtoken_minting::Config for Test {
type ExitAccount = BifrostExitAccount;
type FeeAccount = One;
type RedeemFeeAccount = One;
type BifrostSlp = Slp;
type RelayChainToken = RelayCurrencyId;
type CurrencyIdConversion = AssetIdMaps<Test>;
type CurrencyIdRegister = AssetIdMaps<Test>;
type WeightInfo = ();
type OnRedeemSuccess = ();
type XcmTransfer = XTokens;
Expand All @@ -314,7 +311,6 @@ impl bifrost_vtoken_minting::Config for Test {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type AssetIdMaps = AssetIdMaps<Test>;
}

pub struct Slp;
Expand Down
2 changes: 1 addition & 1 deletion pallets/salp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ pub mod pallet {
)?;
},
cid if cid == relay_vtoken_id => {
let token_value = T::VtokenMinting::vtoken_to_token(
let token_value = T::VtokenMinting::get_currency_amount_by_v_currency_amount(
relay_currency_id,
relay_vtoken_id,
value,
Expand Down
4 changes: 0 additions & 4 deletions pallets/salp/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,7 @@ impl bifrost_vtoken_minting::Config for Test {
type ExitAccount = BifrostExitAccount;
type FeeAccount = CouncilAccount;
type RedeemFeeAccount = CouncilAccount;
type BifrostSlp = Slp;
type RelayChainToken = RelayCurrencyId;
type CurrencyIdConversion = AssetIdMaps<Test>;
type CurrencyIdRegister = AssetIdMaps<Test>;
type WeightInfo = ();
type OnRedeemSuccess = ();
type XcmTransfer = XTokens;
Expand All @@ -432,7 +429,6 @@ impl bifrost_vtoken_minting::Config for Test {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type AssetIdMaps = AssetIdMaps<Test>;
}

parameter_types! {
Expand Down
2 changes: 1 addition & 1 deletion pallets/salp/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ fn refund_meanwhile_issue_should_work() {
Salp::buyback_vstoken_by_stable_pool(Some(ALICE).into(), 1, KSM, 100),
orml_tokens::Error::<Test>::BalanceTooLow
);
let token_value = VtokenMinting::token_to_vtoken(KSM, VKSM, 100);
let token_value = VtokenMinting::get_v_currency_amount_by_currency_amount(KSM, VKSM, 100);
assert_eq!(token_value, Ok(100));
assert_eq!(Tokens::free_balance(KSM, &ALICE), 95000);
assert_ok!(Tokens::set_balance(RuntimeOrigin::root(), buyback_account, KSM, 100, 0));
Expand Down
4 changes: 0 additions & 4 deletions pallets/slp-v2/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,6 @@ impl bifrost_vtoken_minting::Config for Test {
type FeeAccount = BifrostFeeAccount;
type RedeemFeeAccount = BifrostFeeAccount;
type RelayChainToken = RelayCurrencyId;
type CurrencyIdConversion = AssetIdMaps<Test>;
type CurrencyIdRegister = AssetIdMaps<Test>;
type BifrostSlp = MockSlp;
type BifrostSlpx = SlpxInterface;
type WeightInfo = ();
type OnRedeemSuccess = ();
Expand All @@ -253,7 +250,6 @@ impl bifrost_vtoken_minting::Config for Test {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type AssetIdMaps = AssetIdMaps<Test>;
}

parameter_types! {
Expand Down
4 changes: 0 additions & 4 deletions pallets/slp/src/mocks/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,6 @@ impl bifrost_vtoken_minting::Config for Runtime {
type FeeAccount = BifrostFeeAccount;
type RedeemFeeAccount = BifrostFeeAccount;
type RelayChainToken = RelayCurrencyId;
type CurrencyIdConversion = AssetIdMaps<Runtime>;
type CurrencyIdRegister = AssetIdMaps<Runtime>;
type BifrostSlp = Slp;
type BifrostSlpx = SlpxInterface;
type WeightInfo = ();
type OnRedeemSuccess = ();
Expand All @@ -210,7 +207,6 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
}

parameter_types! {
Expand Down
4 changes: 0 additions & 4 deletions pallets/slp/src/mocks/mock_kusama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,6 @@ impl bifrost_vtoken_minting::Config for Runtime {
type FeeAccount = BifrostFeeAccount;
type RedeemFeeAccount = BifrostFeeAccount;
type RelayChainToken = RelayCurrencyId;
type CurrencyIdConversion = AssetIdMaps<Runtime>;
type CurrencyIdRegister = AssetIdMaps<Runtime>;
type BifrostSlp = Slp;
type BifrostSlpx = SlpxInterface;
type WeightInfo = ();
type OnRedeemSuccess = ();
Expand All @@ -266,7 +263,6 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
}

parameter_types! {
Expand Down
16 changes: 8 additions & 8 deletions pallets/slpx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,7 @@ pub mod pallet {
// Check the validity of origin
T::ControlOrigin::ensure_origin(origin)?;
// Check in advance to avoid hook errors
T::VtokenMintingInterface::vtoken_id(currency_id)
.ok_or(Error::<T>::ErrorConvertVtoken)?;
currency_id.to_vtoken().map_err(|_| Error::<T>::ErrorConvertVtoken)?;
let mut currency_list = CurrencyIdList::<T>::get();
if is_support {
ensure!(!currency_list.contains(&currency_id), Error::<T>::CurrencyAlreadyExists);
Expand Down Expand Up @@ -1100,12 +1099,13 @@ impl<T: Config> Pallet<T> {
let vtoken_id =
order.currency_id.to_vtoken().map_err(|_| Error::<T>::ErrorConvertVtoken)?;

let vtoken_amount = T::VtokenMintingInterface::token_to_vtoken(
order.currency_id,
vtoken_id,
currency_amount,
)
.map_err(|_| Error::<T>::ErrorVtokenMiting)?;
let vtoken_amount =
T::VtokenMintingInterface::get_v_currency_amount_by_currency_amount(
order.currency_id,
vtoken_id,
currency_amount,
)
.map_err(|_| Error::<T>::ErrorVtokenMiting)?;

T::VtokenMintingInterface::mint(
order.derivative_account.clone(),
Expand Down
4 changes: 0 additions & 4 deletions pallets/slpx/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ impl bifrost_vtoken_minting::Config for Test {
type FeeAccount = BifrostFeeAccount;
type RedeemFeeAccount = BifrostFeeAccount;
type RelayChainToken = RelayCurrencyId;
type CurrencyIdConversion = AssetIdMaps<Test>;
type CurrencyIdRegister = AssetIdMaps<Test>;
type BifrostSlp = MockSlp;
type BifrostSlpx = SlpxInterface;
type WeightInfo = ();
type OnRedeemSuccess = ();
Expand All @@ -194,7 +191,6 @@ impl bifrost_vtoken_minting::Config for Test {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type AssetIdMaps = AssetIdMaps<Test>;
}

parameter_types! {
Expand Down
4 changes: 0 additions & 4 deletions pallets/stable-pool/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,7 @@ impl bifrost_vtoken_minting::Config for Test {
type ExitAccount = BifrostExitAccount;
type FeeAccount = One;
type RedeemFeeAccount = One;
type BifrostSlp = Slp;
type RelayChainToken = RelayCurrencyId;
type CurrencyIdConversion = AssetIdMaps<Test>;
type CurrencyIdRegister = AssetIdMaps<Test>;
type WeightInfo = ();
type OnRedeemSuccess = ();
type XcmTransfer = XTokens;
Expand All @@ -292,7 +289,6 @@ impl bifrost_vtoken_minting::Config for Test {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type AssetIdMaps = AssetIdMaps<Test>;
}

pub struct Slp;
Expand Down
24 changes: 17 additions & 7 deletions pallets/system-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,20 +518,26 @@ pub mod pallet {
let token_info = <TokenStatus<T>>::get(&token).ok_or(Error::<T>::TokenInfoNotFound)?;

// token_id convert to vtoken_id
let vtoken_id =
T::VtokenMintingInterface::vtoken_id(token).ok_or(Error::<T>::TokenInfoNotFound)?;
let vtoken_id = token.to_vtoken().map_err(|_| Error::<T>::TokenInfoNotFound)?;

let pallet_account: AccountIdOf<T> = T::PalletId::get().into_account_truncating();

// Calculate the revenue generated by vtoken
let vfree_amount = T::MultiCurrency::free_balance(vtoken_id, &pallet_account);
let free_amount =
T::VtokenMintingInterface::vtoken_to_token(token, vtoken_id, vfree_amount)?;
let free_amount = T::VtokenMintingInterface::get_currency_amount_by_v_currency_amount(
token,
vtoken_id,
vfree_amount,
)?;
let token_amount = free_amount.saturating_sub(token_info.system_shadow_amount);

// Calculate the number of benefits converted to vtoken
let vtoken_amount =
T::VtokenMintingInterface::token_to_vtoken(token, vtoken_id, token_amount)?;
T::VtokenMintingInterface::get_v_currency_amount_by_currency_amount(
token,
vtoken_id,
token_amount,
)?;

// Transfer vtoken(benefits) to TreasuryAccount
T::MultiCurrency::transfer(
Expand Down Expand Up @@ -647,11 +653,15 @@ impl<T: Config> Pallet<T> {
.saturating_sub(stakable_amount);

// token_id convert to vtoken_id
if let Some(vtoken_id) = T::VtokenMintingInterface::vtoken_id(token_id) {
if let Ok(vtoken_id) = token_id.to_vtoken() {
// Calculate how many ksm can be received by vksm through VtokenMintingInterface
// ===> vredeem_amount(vksm amount)
let vredeem_amount =
T::VtokenMintingInterface::token_to_vtoken(token_id, vtoken_id, redeem_amount)?;
T::VtokenMintingInterface::get_v_currency_amount_by_currency_amount(
token_id,
vtoken_id,
redeem_amount,
)?;
if vredeem_amount != BalanceOf::<T>::zero() {
// redeem vksm ===> vTokenMinting redeem_inner on_redeemed , update
// pending_redeem_amount += token_amount
Expand Down
4 changes: 0 additions & 4 deletions pallets/system-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,8 @@ impl bifrost_vtoken_minting::Config for Runtime {
type ExitAccount = BifrostExitAccount;
type FeeAccount = BifrostFeeAccount;
type RedeemFeeAccount = BifrostFeeAccount;
type BifrostSlp = Slp;
type BifrostSlpx = SlpxInterface;
type RelayChainToken = RelayCurrencyId;
type CurrencyIdConversion = AssetIdMaps<Runtime>;
type CurrencyIdRegister = AssetIdMaps<Runtime>;
type WeightInfo = ();
type OnRedeemSuccess = ();
type XcmTransfer = XTokens;
Expand All @@ -202,7 +199,6 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
}

ord_parameter_types! {
Expand Down
2 changes: 0 additions & 2 deletions pallets/vtoken-minting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ xcm-executor = { workspace = true }
xcm-builder = { workspace = true }
pallet-xcm = { workspace = true }
xcm = { workspace = true }
bifrost-slp = { workspace = true }
bifrost-asset-registry = { workspace = true }
bifrost-runtime-common = { workspace = true }
env_logger = { workspace = true }
Expand All @@ -54,7 +53,6 @@ std = [
"bifrost-primitives/std",
"orml-traits/std",
"orml-xtokens/std",
"bifrost-slp/std",
"bifrost-asset-registry/std",
"bifrost-runtime-common/std",
]
Expand Down
Loading