git.delta.rocks / unique-network / refs/commits / 1c8c05f45308

difftreelog

From Karura transfers

str-mv2022-01-26parent: #ed10be8.patch.diff
in: master

2 files changed

modifiedREADME.mddiffbeforeafterboth
--- a/README.md
+++ b/README.md
@@ -340,6 +340,7 @@
 To get started, you need to open the hrmr channel.
 Next, we need to register our asset in Karura.
 
+```
 assetRegistry -> registerForeignAsset(location, metadata)
 location:
 	V0(X2(Parent, Parachain(PARA_ID))) 
@@ -348,20 +349,46 @@
 	symbol       OPL
 	decimals     15
 minimalBalance	 1
+```
 
 Next, we can send tokens of our chain:
+```
 polkadotXcm -> reserveTransferAssets
 dest:
-	V0(X2(Parent, Parachain(KARURA_PARA_ID))) 
+	V0(X2(Parent, Parachain(<KARURA_PARA_ID>))) 
 beneficiary:
-	X1(AccountId(Any, ACCOUNT))
+	X1(AccountId(Any, <ACCOUNT>))
 assets:
-	V1(Concrete(0,Here), Fungible(AMOUNT))
+	V1(Concrete(0,Here), Fungible(<AMOUNT>))
 feeAssetItem: 
 	0	
 weightLimit:
-	Limit
-	
+	<LIMIT>
+```	
 
 The result will be displayed in ChainState   
 tokens -> accounts	
+
+
+To send tokens from karura:
+
+```
+xtokens -> transfer
+
+currencyId:
+	ForeingAsset
+		<TOKEN_ID>
+
+amount:
+		<AMOUNT>
+dest:
+	V1
+	(
+		Parents:1, 
+		X2(Parachain(<KARURA_PARA_ID>), AccountId(Any, <ACCOUNT>)
+	)
+destWeight:
+	<WEIGHT>
+		
+
+```
\ No newline at end of file
modifiedruntime/src/lib.rsdiffbeforeafterboth
24 Permill, Perbill, Percent, create_runtime_str, generic, impl_opaque_keys,24 Permill, Perbill, Percent, create_runtime_str, generic, impl_opaque_keys,
25 traits::{25 traits::{
26 AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, Verify, AccountIdConversion,26 AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, Verify,
27 AccountIdConversion, Zero,
27 },28 },
28 transaction_validity::{TransactionSource, TransactionValidity},29 transaction_validity::{TransactionSource, TransactionValidity},
29 ApplyExtrinsicResult, MultiSignature, RuntimeAppPublic,30 ApplyExtrinsicResult, MultiSignature, RuntimeAppPublic,
45 dispatch::DispatchResult,46 dispatch::DispatchResult,
46 PalletId, parameter_types, StorageValue, ConsensusEngineId,47 PalletId, parameter_types, StorageValue, ConsensusEngineId,
47 traits::{48 traits::{
48 Everything, Currency, ExistenceRequirement, Get, IsInVec, KeyOwnerProofSystem,49 tokens::currency::Currency as CurrencyT, OnUnbalanced as OnUnbalancedT, Everything,
49 LockIdentifier, OnUnbalanced, Randomness, FindAuthor,50 Currency, ExistenceRequirement, Get, IsInVec, KeyOwnerProofSystem, LockIdentifier,
51 OnUnbalanced, Randomness, FindAuthor,
50 },52 },
69use pallet_evm::{Account as EVMAccount, FeeCalculator, GasWeightMapping, OnMethodCall};71use pallet_evm::{Account as EVMAccount, FeeCalculator, GasWeightMapping, OnMethodCall};
70use fp_rpc::TransactionStatus;72use fp_rpc::TransactionStatus;
71use sp_runtime::{73use sp_runtime::{
72 traits::{BlockNumberProvider, Dispatchable, PostDispatchInfoOf},74 traits::{BlockNumberProvider, Dispatchable, PostDispatchInfoOf, Saturating},
73 transaction_validity::TransactionValidityError,75 transaction_validity::TransactionValidityError,
76 SaturatedConversion,
74};77};
7578
76// pub use pallet_timestamp::Call as TimestampCall;79// pub use pallet_timestamp::Call as TimestampCall;
85 EnsureXcmOrigin, FixedWeightBounds, LocationInverter, NativeAsset,88 EnsureXcmOrigin, FixedWeightBounds, LocationInverter, NativeAsset, ParentAsSuperuser,
86 ParentAsSuperuser, ParentIsDefault, RelayChainAsNative, SiblingParachainAsNative,89 ParentIsDefault, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
87 SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32,90 SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
88 SovereignSignedViaLocation, TakeWeightCredit, UsingComponents,
89};91};
90use xcm_executor::{Config, XcmExecutor};92use xcm_executor::{Config, XcmExecutor, Assets};
91
92use log::{debug};
93use sp_std::{borrow::Borrow, marker::PhantomData};93use sp_std::{marker::PhantomData};
9494
95use xcm::latest::{95use xcm::latest::{
96// Xcm, 96 // Xcm,
97 AssetId::{Concrete},97 AssetId::{Concrete},
98 Fungibility::Fungible as XcmFungible,98 Fungibility::Fungible as XcmFungible,
99 MultiAsset99 MultiAsset,
100 Error as XcmError,
100};101};
101use xcm_executor::traits::{ShouldExecute, WeightBounds, MatchesFungible, WeightTrader, Convert};102use xcm_executor::traits::{MatchesFungible, WeightTrader};
102//use xcm_executor::traits::MatchesFungible;103//use xcm_executor::traits::MatchesFungible;
103use sp_runtime::traits::CheckedConversion;104use sp_runtime::traits::CheckedConversion;
104105
606 type XcmpMessageHandler = XcmpQueue;607 type XcmpMessageHandler = XcmpQueue;
607}608}
608
609pub struct SiblingParachainConvertsVia2<ParaId, AccountId>(PhantomData<(ParaId, AccountId)>);
610impl<ParaId: From<u32> + Into<u32> + AccountIdConversion<AccountId>, AccountId: Clone + core::fmt::Debug>
611 Convert<MultiLocation, AccountId> for SiblingParachainConvertsVia2<ParaId, AccountId>
612{
613 fn convert_ref(location: impl Borrow<MultiLocation>) -> Result<AccountId, ()> {
614
615
616
617 match location.borrow() {
618 MultiLocation { parents: 1, interior: X1(Parachain(id)) } => {
619 log::debug!(
620 target: "xcm::converter",
621 "SiblingParachainConvertsVia2 accountId: {:?}",
622 ParaId::from(*id).into_account(),
623 );
624 Ok(ParaId::from(*id).into_account())
625 }
626 _ => Err(()),
627 }
628 }
629
630 fn reverse_ref(who: impl Borrow<AccountId>) -> Result<MultiLocation, ()> {
631 if let Some(id) = ParaId::try_from_account(who.borrow()) {
632 Ok(MultiLocation::new(1, X1(Parachain(id.into()))))
633 } else {
634 Err(())
635 }
636 }
637}
638
639609
640impl parachain_info::Config for Runtime {}610impl parachain_info::Config for Runtime {}
655 // The parent (Relay-chain) origin converts to the default `AccountId`.625 // The parent (Relay-chain) origin converts to the default `AccountId`.
656 ParentIsDefault<AccountId>,626 ParentIsDefault<AccountId>,
657 // Sibling parachain origins convert to AccountId via the `ParaId::into`.627 // Sibling parachain origins convert to AccountId via the `ParaId::into`.
658 SiblingParachainConvertsVia2<Sibling, AccountId>,628 SiblingParachainConvertsVia<Sibling, AccountId>,
659 // Straight up local `AccountId32` origins just alias directly to `AccountId`.629 // Straight up local `AccountId32` origins just alias directly to `AccountId`.
660 AccountId32Aliases<RelayNetwork, AccountId>,630 AccountId32Aliases<RelayNetwork, AccountId>,
661);631);
733 // ^^^ Parent & its unit plurality gets free execution701 // ^^^ Parent & its unit plurality gets free execution
734);702);
703
704pub struct UsingOnlySelfCurrencyComponents<
705 WeightToFee: WeightToFeePolynomial<Balance = Currency::Balance>,
706 AssetId: Get<MultiLocation>,
707 AccountId,
708 Currency: CurrencyT<AccountId>,
709 OnUnbalanced: OnUnbalancedT<Currency::NegativeImbalance>,
710>(
711 Weight,
712 Currency::Balance,
713 PhantomData<(WeightToFee, AssetId, AccountId, Currency, OnUnbalanced)>,
714);
715impl<
716 WeightToFee: WeightToFeePolynomial<Balance = Currency::Balance>,
717 AssetId: Get<MultiLocation>,
718 AccountId,
719 Currency: CurrencyT<AccountId>,
720 OnUnbalanced: OnUnbalancedT<Currency::NegativeImbalance>,
721 > WeightTrader
722 for UsingOnlySelfCurrencyComponents<WeightToFee, AssetId, AccountId, Currency, OnUnbalanced>
723{
724 fn new() -> Self {
725 Self(0, Zero::zero(), PhantomData)
726 }
727
728 fn buy_weight(&mut self, weight: Weight, payment: Assets) -> Result<Assets, XcmError> {
729 let amount = WeightToFee::calc(&weight);
730 let u128_amount: u128 = amount.try_into().map_err(|_| XcmError::Overflow)?;
731
732 // location to this parachain through relay chain
733 let option1: xcm::v1::AssetId = Concrete(MultiLocation {
734 parents: 1,
735 interior: X1(Parachain(ParachainInfo::parachain_id().into())),
736 });
737 // direct location
738 let option2: xcm::v1::AssetId = Concrete(MultiLocation {
739 parents: 0,
740 interior: Here,
741 });
742
743 let required = if payment.fungible.contains_key(&option1) {
744 (option1, u128_amount).into()
745 } else if payment.fungible.contains_key(&option2) {
746 (option2, u128_amount).into()
747 } else {
748 (Concrete(MultiLocation::default()), u128_amount).into()
749 };
750
751 let unused = payment
752 .checked_sub(required)
753 .map_err(|_| XcmError::TooExpensive)?;
754 self.0 = self.0.saturating_add(weight);
755 self.1 = self.1.saturating_add(amount);
756 Ok(unused)
757 }
758
759 fn refund_weight(&mut self, weight: Weight) -> Option<MultiAsset> {
760 let weight = weight.min(self.0);
761 let amount = WeightToFee::calc(&weight);
762 self.0 -= weight;
763 self.1 = self.1.saturating_sub(amount);
764 let amount: u128 = amount.saturated_into();
765 if amount > 0 {
766 Some((AssetId::get(), amount).into())
767 } else {
768 None
769 }
770 }
771}
772impl<
773 WeightToFee: WeightToFeePolynomial<Balance = Currency::Balance>,
774 AssetId: Get<MultiLocation>,
775 AccountId,
776 Currency: CurrencyT<AccountId>,
777 OnUnbalanced: OnUnbalancedT<Currency::NegativeImbalance>,
778 > Drop
779 for UsingOnlySelfCurrencyComponents<WeightToFee, AssetId, AccountId, Currency, OnUnbalanced>
780{
781 fn drop(&mut self) {
782 OnUnbalanced::on_unbalanced(Currency::issue(self.1));
783 }
784}
735785
736pub struct XcmConfig;786pub struct XcmConfig;
737impl Config for XcmConfig {787impl Config for XcmConfig {
745 type LocationInverter = LocationInverter<Ancestry>;795 type LocationInverter = LocationInverter<Ancestry>;
746 type Barrier = Barrier;796 type Barrier = Barrier;
747 type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;797 type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
748 type Trader = UsingComponents<IdentityFee<Balance>, RelayLocation, AccountId, Balances, ()>;798 type Trader = UsingOnlySelfCurrencyComponents<
799 IdentityFee<Balance>,
800 RelayLocation,
801 AccountId,
802 Balances,
803 (),
804 >;
749 type ResponseHandler = (); // Don't handle responses for now.805 type ResponseHandler = (); // Don't handle responses for now.
750 type SubscriptionService = PolkadotXcm;806 type SubscriptionService = PolkadotXcm;