Skip to content

Commit efa3519

Browse files
Add lock instruction; Allow manual execution for locked instructions; Add simplified transfer
1 parent f0da77a commit efa3519

File tree

10 files changed

+432
-221
lines changed

10 files changed

+432
-221
lines changed

pallets/asset/src/lib.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3293,8 +3293,8 @@ impl<T: AssetConfig> Pallet<T> {
32933293
)?;
32943294

32953295
// Updates the balance in the asset pallet
3296-
let sender_new_balance = sender_current_balance - transfer_value;
3297-
let receiver_new_balance = receiver_current_balance + transfer_value;
3296+
let sender_new_balance = sender_current_balance.saturating_sub(transfer_value);
3297+
let receiver_new_balance = receiver_current_balance.saturating_add(transfer_value);
32983298
BalanceOf::<T>::insert(asset_id, sender_portfolio.did, sender_new_balance);
32993299
BalanceOf::<T>::insert(asset_id, receiver_portfolio.did, receiver_new_balance);
33003300

@@ -3461,6 +3461,39 @@ impl<T: AssetConfig> Pallet<T> {
34613461
}
34623462
})
34633463
}
3464+
3465+
/// Transfers `transfer_value` of `asset_id` from `sender_pid` to `receiver_pid`.
3466+
/// Note: This functions skips all compliance and statistics checks, only checking for balance.
3467+
pub fn simplified_fungible_transfer(
3468+
asset_id: AssetId,
3469+
sender_pid: PortfolioId,
3470+
receiver_pid: PortfolioId,
3471+
transfer_value: Balance,
3472+
inst_id: InstructionId,
3473+
inst_memo: Option<Memo>,
3474+
caller_did: IdentityId,
3475+
weight_meter: &mut WeightMeter,
3476+
) -> DispatchResult {
3477+
ensure!(
3478+
BalanceOf::<T>::get(&asset_id, &sender_pid.did) >= transfer_value,
3479+
Error::<T>::InsufficientBalance
3480+
);
3481+
Portfolio::<T>::ensure_portfolio_validity(&receiver_pid)?;
3482+
Portfolio::<T>::ensure_sufficient_balance(&sender_pid, &asset_id, transfer_value)?;
3483+
3484+
Self::unverified_transfer_asset(
3485+
sender_pid,
3486+
receiver_pid,
3487+
asset_id,
3488+
transfer_value,
3489+
Some(inst_id),
3490+
inst_memo,
3491+
caller_did,
3492+
weight_meter,
3493+
)?;
3494+
3495+
Ok(())
3496+
}
34643497
}
34653498

34663499
//==========================================================================

pallets/nft/src/lib.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,10 +712,10 @@ impl<T: Config> Pallet<T> {
712712
// Update the balance of the sender and the receiver
713713
let transferred_amount = nfts.len() as u64;
714714
NumberOfNFTs::<T>::mutate(nfts.asset_id(), sender_portfolio.did, |balance| {
715-
*balance -= transferred_amount
715+
*balance = balance.saturating_sub(transferred_amount)
716716
});
717717
NumberOfNFTs::<T>::mutate(nfts.asset_id(), receiver_portfolio.did, |balance| {
718-
*balance += transferred_amount
718+
*balance = balance.saturating_add(transferred_amount)
719719
});
720720
// Update the portfolio of the sender and the receiver
721721
for nft_id in nfts.ids() {
@@ -870,6 +870,43 @@ impl<T: Config> Pallet<T> {
870870
}
871871
})
872872
}
873+
874+
/// Transfers all `nfts` from `sender_pid` to `receiver_pid`.
875+
/// Note: This functions skips all compliance checks and only checks for onwership.
876+
pub fn simplified_nft_transfer(
877+
sender_pid: PortfolioId,
878+
receiver_pid: PortfolioId,
879+
nfts: NFTs,
880+
inst_id: InstructionId,
881+
inst_memo: Option<Memo>,
882+
caller_did: IdentityId,
883+
) -> DispatchResult {
884+
Portfolio::<T>::ensure_portfolio_validity(&receiver_pid)?;
885+
Self::ensure_sender_owns_nfts(&sender_pid, &nfts)?;
886+
Self::unverified_nfts_transfer(&sender_pid, &receiver_pid, &nfts);
887+
Self::deposit_event(Event::NFTPortfolioUpdated(
888+
caller_did,
889+
nfts,
890+
Some(sender_pid),
891+
Some(receiver_pid),
892+
PortfolioUpdateReason::Transferred {
893+
instruction_id: Some(inst_id),
894+
instruction_memo: inst_memo,
895+
},
896+
));
897+
Ok(())
898+
}
899+
900+
/// Returns `Ok` if `sender_pid` holds all nfts.
901+
fn ensure_sender_owns_nfts(sender_pid: &PortfolioId, nfts: &NFTs) -> DispatchResult {
902+
for nft_id in nfts.ids() {
903+
ensure!(
904+
PortfolioNFT::<T>::contains_key(sender_pid, (nfts.asset_id(), nft_id)),
905+
Error::<T>::InvalidNFTTransferNFTNotOwned
906+
);
907+
}
908+
Ok(())
909+
}
873910
}
874911

875912
impl<T: Config> NFTTrait<T::RuntimeOrigin> for Pallet<T> {

pallets/runtime/common/src/runtime.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ macro_rules! misc_pallet_impls {
556556
type MaxNumberOfPortfolios = MaxNumberOfPortfolios;
557557
type MaxNumberOfVenueSigners = MaxNumberOfVenueSigners;
558558
type MaxInstructionMediators = MaxInstructionMediators;
559+
type MaximumLockPeriod = MaximumLockPeriod;
559560
}
560561

561562
impl pallet_sto::Config for Runtime {

pallets/runtime/develop/src/runtime.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ parameter_types! {
9494
pub const MaxNumberOfPortfolios: u32 = (10 + 100) * 2;
9595
pub const MaxNumberOfVenueSigners: u32 = 50;
9696
pub const MaxInstructionMediators: u32 = 4;
97+
pub const MaximumLockPeriod: Moment = 1_440_000; // 24 hours
9798

9899
// Multisig
99100
pub const MaxMultiSigSigners: u32 = 50;

pallets/runtime/mainnet/src/runtime.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ parameter_types! {
9292
pub const MaxNumberOfPortfolios: u32 = (10 + 100) * 2;
9393
pub const MaxNumberOfVenueSigners: u32 = 50;
9494
pub const MaxInstructionMediators: u32 = 4;
95+
pub const MaximumLockPeriod: Moment = 1_440_000; // 24 hours
9596

9697
// Multisig
9798
pub const MaxMultiSigSigners: u32 = 50;

pallets/runtime/testnet/src/runtime.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ parameter_types! {
9595
pub const MaxNumberOfPortfolios: u32 = (10 + 100) * 2;
9696
pub const MaxNumberOfVenueSigners: u32 = 50;
9797
pub const MaxInstructionMediators: u32 = 4;
98+
pub const MaximumLockPeriod: Moment = 1_440_000; // 24 hours
9899

99100
// Multisig
100101
pub const MaxMultiSigSigners: u32 = 50;

pallets/runtime/tests/src/storage.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ parameter_types! {
214214
pub const MaxNumberOfFungibleAssets: u32 = 100;
215215
pub const MaxNumberOfNFTsPerLeg: u32 = 10;
216216
pub const MaxNumberOfNFTs: u32 = 100;
217+
pub const MaximumLockPeriod: Moment = 2;
217218

218219
// Multisig
219220
pub const MaxMultiSigSigners: u32 = 50;

0 commit comments

Comments
 (0)