Skip to content

Commit

Permalink
Merge pull request #36 from multiversx/alpha/fee-market
Browse files Browse the repository at this point in the history
Alpha Sovereign SCs
  • Loading branch information
andreiblt1304 authored Apr 24, 2024
2 parents 193b1b7 + 990347d commit 3094b60
Show file tree
Hide file tree
Showing 47 changed files with 2,375 additions and 1,049 deletions.
56 changes: 56 additions & 0 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ members = [
"esdt-safe",
"esdt-safe/meta",
"fee-market",
"fee-market/meta"
"fee-market/meta",
"pair-mock",
"pair-mock/meta",
"router-mock",
"router-mock/meta",
"multisigverifier",
"multisigverifier/meta",
]
71 changes: 71 additions & 0 deletions common/transaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,68 @@ pub type TxAsMultiValue<M> = MultiValue7<
pub type PaymentsVec<M> = ManagedVec<M, EsdtTokenPayment<M>>;
pub type TxBatchSplitInFields<M> = MultiValue2<BatchId, MultiValueEncoded<M, TxAsMultiValue<M>>>;

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi, ManagedVecItem, Clone)]
pub struct Operation<M: ManagedTypeApi> {
pub to: ManagedAddress<M>,
pub tokens: ManagedVec<M, OperationEsdtPayment<M>>,
pub data: OperationData<M>,
}

impl<M: ManagedTypeApi> Operation<M> {
pub fn get_tokens_as_tuple_arr(
&self,
) -> MultiValueEncoded<M, MultiValue3<TokenIdentifier<M>, u64, EsdtTokenData<M>>> {
let mut tuple_arr = MultiValueEncoded::new();

for token in &self.tokens {
tuple_arr.push(MultiValue3::from((
token.token_identifier,
token.token_nonce,
token.token_data.into(),
)));
}

tuple_arr
}
}

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi, ManagedVecItem, Clone)]
pub struct TransferData<M: ManagedTypeApi> {
pub gas_limit: GasLimit,
pub function: ManagedBuffer<M>,
pub args: ManagedVec<M, ManagedBuffer<M>>,
}

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi, ManagedVecItem, Clone)]
pub struct OperationData<M: ManagedTypeApi> {
pub op_nonce: TxId,
pub op_sender: ManagedAddress<M>,
pub opt_transfer_data: Option<TransferData<M>>,
}

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi, ManagedVecItem, Clone)]
pub struct OperationTuple<M: ManagedTypeApi> {
pub op_hash: ManagedBuffer<M>,
pub operation: Operation<M>,
}

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi, ManagedVecItem, Clone)]
pub struct OperationEsdtPayment<M: ManagedTypeApi> {
pub token_identifier: TokenIdentifier<M>,
pub token_nonce: u64,
pub token_data: StolenFromFrameworkEsdtTokenData<M>,
}

impl<M: ManagedTypeApi> From<OperationEsdtPayment<M>> for EsdtTokenPayment<M> {
fn from(payment: OperationEsdtPayment<M>) -> Self {
EsdtTokenPayment {
token_identifier: payment.token_identifier,
token_nonce: payment.token_nonce,
amount: payment.token_data.amount,
}
}
}

// Temporary until Clone is implemented for EsdtTokenData
#[derive(
TopDecode, TopEncode, NestedDecode, NestedEncode, TypeAbi, Debug, ManagedVecItem, Clone,
Expand Down Expand Up @@ -83,6 +138,22 @@ impl<M: ManagedTypeApi> From<EsdtTokenData<M>> for StolenFromFrameworkEsdtTokenD
}
}

impl<M: ManagedTypeApi> From<StolenFromFrameworkEsdtTokenData<M>> for EsdtTokenData<M> {
fn from(token_data: StolenFromFrameworkEsdtTokenData<M>) -> Self {
EsdtTokenData {
token_type: token_data.token_type,
amount: token_data.amount,
frozen: token_data.frozen,
hash: token_data.hash,
name: token_data.name,
attributes: token_data.attributes,
creator: token_data.creator,
royalties: token_data.royalties,
uris: token_data.uris,
}
}
}

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi, ManagedVecItem, Clone)]
pub struct Transaction<M: ManagedTypeApi> {
pub block_nonce: BlockNonce,
Expand Down
3 changes: 3 additions & 0 deletions esdt-safe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ path = "../common/utils"
[dependencies.fee-market]
path = "../fee-market"

[dependencies.multisigverifier]
path = "../multisigverifier"

[dependencies.multiversx-sc]
version = "=0.48.0"

Expand Down
38 changes: 4 additions & 34 deletions esdt-safe/src/from_sovereign/events.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,11 @@
use transaction::{BatchId, Transaction, TxId};

multiversx_sc::imports!();

#[multiversx_sc::module]
pub trait EventsModule {
#[event("addRefundTransactionEvent")]
fn add_refund_transaction_event(
&self,
#[indexed] tx_id: TxId,
#[indexed] original_tx_id: TxId,
);

#[event("transferPerformedEvent")]
fn transfer_performed_event(
&self,
#[indexed] batch_id: BatchId,
#[indexed] tx_id: TxId,
tx: Transaction<Self::Api>,
);

#[event("transferFailedInvalidToken")]
fn transfer_failed_invalid_token(&self, #[indexed] batch_id: BatchId, #[indexed] tx_id: TxId);

#[event("transferFailedFrozenDestinationAccount")]
fn transfer_failed_frozen_destination_account(
&self,
#[indexed] batch_id: BatchId,
#[indexed] tx_id: TxId,
);

#[event("transferOverMaxAmount")]
fn transfer_over_max_amount(&self, #[indexed] batch_id: BatchId, #[indexed] tx_id: TxId);

#[event("transferFailedExecutionFailed")]
fn transfer_failed_execution_failed(
#[event("executedBridgeOp")]
fn execute_bridge_operation_event(
&self,
#[indexed] batch_id: BatchId,
#[indexed] tx_id: TxId,
#[indexed] hash_of_hashes: ManagedBuffer,
#[indexed] hash_of_bridge_op: ManagedBuffer,
);
}
27 changes: 12 additions & 15 deletions esdt-safe/src/from_sovereign/refund.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use transaction::{BatchId, PaymentsVec, Transaction, TxNonce};
use transaction::{BatchId, OperationEsdtPayment, PaymentsVec, Transaction, TxNonce};

multiversx_sc::imports!();

const NFT_AMOUNT: u32 = 1;

pub struct CheckMustRefundArgs<'a, M: ManagedTypeApi> {
pub token: &'a EsdtTokenPayment<M>,
pub token: &'a OperationEsdtPayment<M>,
pub roles: EsdtLocalRoleFlags,
pub dest: &'a ManagedAddress<M>,
pub batch_id: BatchId,
Expand All @@ -26,42 +26,40 @@ pub trait RefundModule:
&args.token.token_identifier,
args.token.token_nonce,
);
if token_balance < args.token.amount {

if token_balance < args.token.token_data.amount {
if args.token.token_nonce == 0 {
if !args.roles.has_role(&EsdtLocalRole::Mint) {
self.transfer_failed_invalid_token(args.batch_id, args.tx_nonce);

return true;
}
} else if !self.has_nft_roles(args.token, args.roles) {
self.transfer_failed_invalid_token(args.batch_id, args.tx_nonce);

return true;
}
}

if self.is_above_max_amount(&args.token.token_identifier, &args.token.amount) {
self.transfer_over_max_amount(args.batch_id, args.tx_nonce);

if self.is_above_max_amount(&args.token.token_identifier, &args.token.token_data.amount) {
return true;
}

if self.is_account_same_shard_frozen(args.sc_shard, args.dest, &args.token.token_identifier)
{
self.transfer_failed_frozen_destination_account(args.batch_id, args.tx_nonce);

return true;
}

false
}

fn has_nft_roles(&self, payment: &EsdtTokenPayment, roles: EsdtLocalRoleFlags) -> bool {
fn has_nft_roles(
&self,
payment: &OperationEsdtPayment<Self::Api>,
roles: EsdtLocalRoleFlags,
) -> bool {
if !roles.has_role(&EsdtLocalRole::NftCreate) {
return false;
}

if payment.amount > NFT_AMOUNT && !roles.has_role(&EsdtLocalRole::NftAddQuantity) {
if payment.token_data.amount > NFT_AMOUNT && !roles.has_role(&EsdtLocalRole::NftAddQuantity)
{
return false;
}

Expand Down Expand Up @@ -91,7 +89,6 @@ pub trait RefundModule:
tokens_to_refund: PaymentsVec<Self::Api>,
) -> Transaction<Self::Api> {
let tx_nonce = self.get_and_save_next_tx_id();
self.add_refund_transaction_event(tx_nonce, sov_tx.nonce);

// invert from and to
Transaction {
Expand Down
Loading

0 comments on commit 3094b60

Please sign in to comment.