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

Wegld fee #65

Merged
merged 79 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 75 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
4378754
Added new test file
andreiblt1304 Jun 25, 2024
d1f743c
Deploy test
andreiblt1304 Jun 25, 2024
1710be6
Modified setup + execute operation test
andreiblt1304 Jun 26, 2024
3fd293f
Added header verifier address method
andreiblt1304 Jun 26, 2024
defe670
Modified has_prefix function and test
andreiblt1304 Jun 26, 2024
fb815db
Fixed has_prefix error
andreiblt1304 Jun 26, 2024
9968276
Modified tests setup
andreiblt1304 Jun 26, 2024
ec7ce34
Fixed failing test
andreiblt1304 Jun 27, 2024
14e326f
Added mapper and inline function
andreiblt1304 Jun 28, 2024
36e870e
Modified mint to logic for wegld fee
andreiblt1304 Jun 28, 2024
d0aaea1
Added register_tokens endpoint
andreiblt1304 Jun 28, 2024
3f5de49
Modified wegld minting logic
andreiblt1304 Jun 28, 2024
6358d2e
Merge branch 'enshrine-esdt' into wegld-fee
andreiblt1304 Jun 28, 2024
752673e
Added wegld require for register tokens endpoint
andreiblt1304 Jun 28, 2024
2977ef2
Merge branch 'wegld-fee' of github.com:multiversx/mx-sovereign-sc int…
andreiblt1304 Jun 28, 2024
43e896c
Added wegld fee functions + reverted mint_tokens
andreiblt1304 Jun 28, 2024
a05a829
Modified execute endpoint for wegld fee implementation
andreiblt1304 Jun 28, 2024
1a77a32
Regenerated proxy
andreiblt1304 Jun 28, 2024
139528f
Removed from() in refund_wegld function
andreiblt1304 Jul 1, 2024
4a9db9f
Added wegld_ticker storage mapper
andreiblt1304 Jul 1, 2024
fd7ece4
Modified wegld mapper to save TokenIdentifier
andreiblt1304 Jul 1, 2024
04431bf
Renamed wegld mapper
andreiblt1304 Jul 1, 2024
a9bdc9f
Removed any use of wegld identifier constant
andreiblt1304 Jul 1, 2024
d8124c2
Regenerated enshrine proxy
andreiblt1304 Jul 1, 2024
79add8f
Renamed wegld method and modified logic
andreiblt1304 Jul 1, 2024
12d9950
Fixed comments
andreiblt1304 Jul 2, 2024
51f855f
Modified check for wegld fee function
andreiblt1304 Jul 2, 2024
d691d06
Modified execute_operation logic to throw event
andreiblt1304 Jul 2, 2024
b82ac41
Function rename
andreiblt1304 Jul 2, 2024
201397b
Modified inline function
andreiblt1304 Jul 2, 2024
6d65426
Added refund logic to verify function
andreiblt1304 Jul 2, 2024
12e326c
Modified init and fixed build error
andreiblt1304 Jul 2, 2024
59c2665
Modified called event and wegld function
andreiblt1304 Jul 2, 2024
6693b9a
Regenerated proxy
andreiblt1304 Jul 2, 2024
33a7aab
Renamed init arg
andreiblt1304 Jul 2, 2024
1f5bff4
Regenerated proxy
andreiblt1304 Jul 2, 2024
098b067
Added burn for sovereign tokens
andreiblt1304 Jul 3, 2024
83139b9
Modified wegld fee function
andreiblt1304 Jul 3, 2024
e1c516b
Added bool return value to fee function
andreiblt1304 Jul 3, 2024
a1b2738
Added register token call
andreiblt1304 Jul 3, 2024
1bff454
Removed clone call
andreiblt1304 Jul 3, 2024
4cfc7c6
Modified wegld fee calculation
andreiblt1304 Jul 3, 2024
87cafe7
Reverted condition in execute_operations
andreiblt1304 Jul 3, 2024
32fb9ac
Added else scenario for wegld fee
andreiblt1304 Jul 3, 2024
07d92fc
Added require check + changed return value
andreiblt1304 Jul 3, 2024
e426908
Modified wegld fee and inline function logic
andreiblt1304 Jul 4, 2024
74c97f7
Switched returned types
andreiblt1304 Jul 4, 2024
015c2d4
Added default function for OperationEsdtPayment
andreiblt1304 Jul 4, 2024
5352b44
Added get_wegld_payment function
andreiblt1304 Jul 4, 2024
8ed2d0a
Modified get_wegld_payment to return index
andreiblt1304 Jul 4, 2024
a26458a
Added new endpoint for WEGLD deposit
andreiblt1304 Jul 4, 2024
ae5d975
Modified prefix function and added sov prefix to init
andreiblt1304 Jul 4, 2024
86f397e
Added inline prefix check function
andreiblt1304 Jul 4, 2024
b6a8743
Modified inline function and used new has_sov_prefix function
andreiblt1304 Jul 4, 2024
3dcf59c
Switched from OptionalValue to Option
andreiblt1304 Jul 4, 2024
9eee6b9
Modified get_wegld logic to return BigUint and isize index
andreiblt1304 Jul 4, 2024
9b4a669
Regenerated proxy
andreiblt1304 Jul 4, 2024
f49be51
Modified function to check sovereign prefix
andreiblt1304 Jul 4, 2024
0a6a31f
Renamed sovereign token mapper
andreiblt1304 Jul 4, 2024
62aa434
Modified wegld fee and mint & burn logic
andreiblt1304 Jul 4, 2024
7f058e6
Separated lib.rs match statement
andreiblt1304 Jul 4, 2024
906fb92
Reverted create_tx file
andreiblt1304 Jul 4, 2024
aaf36e7
Modified mapper identifier and removed require
andreiblt1304 Jul 5, 2024
28736eb
Called has_sov_prefix instead of has_prefix
andreiblt1304 Jul 5, 2024
5d3b2f4
Modified token verification logic
andreiblt1304 Jul 5, 2024
ff22b99
Regenerated proxy
andreiblt1304 Jul 5, 2024
13e4765
Renamed endpoint
andreiblt1304 Jul 5, 2024
13bac6a
Removed require statement
andreiblt1304 Jul 5, 2024
5db58c6
Added panic in execute operations
andreiblt1304 Jul 8, 2024
98e86fd
Modified init argument
andreiblt1304 Jul 8, 2024
65d1e45
Merge branch 'enshrine-esdt' of github.com:multiversx/mx-sovereign-sc…
andreiblt1304 Jul 8, 2024
db647b9
Removed sc_panic and added return
andreiblt1304 Jul 9, 2024
a8ddcc8
Modified tokens wegld registration and removed function
andreiblt1304 Jul 9, 2024
5882849
Modified get_sovereign_prefix call
andreiblt1304 Jul 9, 2024
6aef775
Removed require and added if
andreiblt1304 Jul 10, 2024
f660493
Added constants for prefix checks
andreiblt1304 Jul 11, 2024
9ad4b0e
Modified init endpoint if condition
andreiblt1304 Jul 11, 2024
6785627
Renamed optional parameters in init
andreiblt1304 Jul 12, 2024
4b92c04
Regenerated proxy
andreiblt1304 Jul 12, 2024
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
10 changes: 10 additions & 0 deletions common/transaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ impl<M: ManagedTypeApi> From<OperationEsdtPayment<M>> for EsdtTokenPayment<M> {
}
}

impl<M: ManagedTypeApi> Default for OperationEsdtPayment<M> {
fn default() -> Self {
OperationEsdtPayment {
token_identifier: TokenIdentifier::from(ManagedBuffer::new()),
token_nonce: 0,
token_data: StolenFromFrameworkEsdtTokenData::default(),
}
}
}

// Temporary until Clone is implemented for EsdtTokenData
#[derive(
TopDecode, TopEncode, NestedDecode, NestedEncode, TypeAbi, Debug, ManagedVecItem, Clone,
Expand Down
23 changes: 22 additions & 1 deletion common/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub trait UtilsModule: bls_signature::BlsSignatureModule {
list
}

fn has_sov_token_prefix(&self, token_id: &TokenIdentifier) -> bool {
fn has_prefix(&self, token_id: &TokenIdentifier) -> bool {
let dash = b'-';
let buffer = token_id.as_managed_buffer();
let mut array_buffer = [0u8; 32];
Expand All @@ -80,4 +80,25 @@ pub trait UtilsModule: bls_signature::BlsSignatureModule {

false
}

fn has_sov_prefix(&self, token_id: &TokenIdentifier, chain_prefix: ManagedBuffer) -> bool {
if !self.has_prefix(token_id) {
return false;
}

let dash = b'-';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have this as a const instead.

let buffer = token_id.as_managed_buffer();
let mut array_buffer = [0u8; 32];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"32" can also be a const, like "MAX_TOKEN_ID_LEN"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also do the same in the above function btw.

let slice = buffer.load_to_byte_array(&mut array_buffer);

if let Some(index) = slice.iter().position(|&b| b == dash) {
let prefix = ManagedBuffer::from(&slice[..index]);

if prefix == chain_prefix {
return true;
}
}

false
}
}
6 changes: 6 additions & 0 deletions enshrine-esdt-safe/src/common/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ use multiversx_sc::imports::*;
pub trait CommonStorage {
#[storage_mapper("isSovereignChain")]
fn is_sovereign_chain(&self) -> SingleValueMapper<bool>;

#[storage_mapper("wegldIdentifier")]
fn wegld_identifier(&self) -> SingleValueMapper<TokenIdentifier>;

#[storage_mapper("sovereignTokensPrefix")]
fn sovereign_tokens_prefix(&self) -> SingleValueMapper<ManagedBuffer>;
}
18 changes: 18 additions & 0 deletions enshrine-esdt-safe/src/enshrine_esdt_safe_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ where
{
pub fn init<
Arg0: ProxyArg<bool>,
Arg1: ProxyArg<Option<TokenIdentifier<Env::Api>>>,
Arg2: ProxyArg<Option<ManagedBuffer<Env::Api>>>,
>(
self,
is_sovereign_chain: Arg0,
wegld_identifier: Arg1,
sov_token_prefix: Arg2,
) -> TxTypedDeploy<Env, From, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_deploy()
.argument(&is_sovereign_chain)
.argument(&wegld_identifier)
.argument(&sov_token_prefix)
.original_result()
}
}
Expand Down Expand Up @@ -181,6 +187,18 @@ where
.original_result()
}

pub fn register_new_token_id<
Arg0: ProxyArg<MultiValueEncoded<Env::Api, TokenIdentifier<Env::Api>>>,
>(
self,
tokens: Arg0,
) -> TxTypedCall<Env, From, To, (), Gas, ()> {
self.wrapped_tx
.raw_call("registerNewTokenID")
.argument(&tokens)
.original_result()
}

pub fn set_max_tx_batch_size<
Arg0: ProxyArg<usize>,
>(
Expand Down
114 changes: 100 additions & 14 deletions enshrine-esdt-safe/src/from_sovereign/transfer_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use transaction::{GasLimit, Operation, OperationData, OperationEsdtPayment, Oper

const CALLBACK_GAS: GasLimit = 10_000_000; // Increase if not enough
const TRANSACTION_GAS: GasLimit = 30_000_000;
const DEFAULT_ISSUE_COST: u64 = 50000000000000000;

#[multiversx_sc::module]
pub trait TransferTokensModule:
Expand All @@ -22,8 +23,10 @@ pub trait TransferTokensModule:
{
#[endpoint(executeBridgeOps)]
fn execute_operations(&self, hash_of_hashes: ManagedBuffer, operation: Operation<Self::Api>) {
let is_sovereign_chain = self.is_sovereign_chain().get();

require!(
!self.is_sovereign_chain().get(),
!is_sovereign_chain,
"Invalid method to call in current chain"
);

Expand All @@ -36,13 +39,70 @@ pub trait TransferTokensModule:
sc_panic!("Operation is not registered");
}

let are_tokens_registered =
self.verify_operation_tokens_issue_paid(operation.tokens.clone());

if !are_tokens_registered {
self.emit_transfer_failed_events(
&hash_of_hashes,
&OperationTuple {
op_hash: operation_hash.clone(),
operation: operation.clone(),
},
);

return;
}

let minted_operation_tokens = self.mint_tokens(&operation.tokens);
let operation_tuple = OperationTuple {
op_hash: operation_hash,
operation,
op_hash: operation_hash.clone(),
operation: operation.clone(),
};

self.distribute_payments(hash_of_hashes, operation_tuple, minted_operation_tokens);
self.distribute_payments(
hash_of_hashes.clone(),
operation_tuple,
minted_operation_tokens,
);
}

#[endpoint(registerNewTokenID)]
#[payable("*")]
fn register_new_token_id(&self, tokens: MultiValueEncoded<TokenIdentifier>) {
let call_payment = self.call_value().single_esdt().clone();
let wegld_identifier = self.wegld_identifier().get();

require!(
call_payment.token_identifier == wegld_identifier,
"WEGLD is the only token accepted as register fee"
);

require!(
call_payment.amount == DEFAULT_ISSUE_COST * tokens.len() as u64,
"WEGLD fee amount is not met"
);

for token_id in tokens {
self.register_token(token_id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this makes token issue, it won't work. You can only make one legacy async call per call, which is what the token issue framework uses.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

register_token just saves the TokenIdentifier to the paid_issued_tokens mapper

}
}

fn verify_operation_tokens_issue_paid(
&self,
tokens: ManagedVec<OperationEsdtPayment<Self::Api>>,
) -> bool {
for token in tokens.iter() {
if !self.has_sov_prefix(&token.token_identifier, self.get_sovereign_prefix()) {
continue;
}

if !self.paid_issued_tokens().contains(&token.token_identifier) {
return false;
}
}

true
}

fn mint_tokens(
Expand All @@ -52,7 +112,8 @@ pub trait TransferTokensModule:
let mut output_payments = ManagedVec::new();

for operation_token in operation_tokens.iter() {
if !self.has_sov_token_prefix(&operation_token.token_identifier) {
let sov_prefix = self.get_sovereign_prefix();
if !self.has_sov_prefix(&operation_token.token_identifier, sov_prefix) {
output_payments.push(operation_token.clone());
continue;
}
Expand Down Expand Up @@ -113,7 +174,6 @@ pub trait TransferTokensModule:

output_payments
}

fn distribute_payments(
&self,
hash_of_hashes: ManagedBuffer,
Expand Down Expand Up @@ -195,6 +255,7 @@ pub trait TransferTokensModule:
);
}
ManagedAsyncCallResult::Err(_) => {
self.burn_sovereign_tokens(&operation_tuple.operation);
self.emit_transfer_failed_events(hash_of_hashes, operation_tuple);
}
}
Expand All @@ -208,6 +269,21 @@ pub trait TransferTokensModule:
.sync_call();
}

fn burn_sovereign_tokens(&self, operation: &Operation<Self::Api>) {
for token in operation.tokens.iter() {
let sov_prefix = self.get_sovereign_prefix();
if !self.has_sov_prefix(&token.token_identifier, sov_prefix) {
continue;
}

self.send().esdt_local_burn(
&token.token_identifier,
token.token_nonce,
&token.token_data.amount,
);
}
}

fn emit_transfer_failed_events(
&self,
hash_of_hashes: &ManagedBuffer,
Expand All @@ -218,14 +294,6 @@ pub trait TransferTokensModule:
operation_tuple.op_hash.clone(),
);

for operation_token in &operation_tuple.operation.tokens {
self.send().esdt_local_burn(
&operation_token.token_identifier,
operation_token.token_nonce,
&operation_token.token_data.amount,
);
}

// deposit back mainchain tokens into user account
let sc_address = self.blockchain().get_sc_address();
let tx_nonce = self.get_and_save_next_tx_id();
Expand Down Expand Up @@ -268,9 +336,27 @@ pub trait TransferTokensModule:
}
}

#[inline]
fn get_sovereign_prefix(&self) -> ManagedBuffer {
self.sovereign_tokens_prefix().get()
}

#[inline]
fn register_token(&self, token_id: TokenIdentifier<Self::Api>) {
self.paid_issued_tokens().insert(token_id);
}

#[inline]
fn is_wegld(&self, token_id: &TokenIdentifier<Self::Api>) -> bool {
token_id.eq(&self.wegld_identifier().get())
}

#[storage_mapper("pending_hashes")]
fn pending_hashes(&self, hash_of_hashes: &ManagedBuffer) -> UnorderedSetMapper<ManagedBuffer>;

#[storage_mapper("header_verifier_address")]
fn header_verifier_address(&self) -> SingleValueMapper<ManagedAddress>;

#[storage_mapper("mintedTokens")]
fn paid_issued_tokens(&self) -> UnorderedSetMapper<TokenIdentifier<Self::Api>>;
}
27 changes: 26 additions & 1 deletion enshrine-esdt-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,34 @@ pub trait EnshrineEsdtSafe:
+ common::storage::CommonStorage
{
#[init]
fn init(&self, is_sovereign_chain: bool) {
fn init(
&self,
is_sovereign_chain: bool,
wegld_identifier: Option<TokenIdentifier>,
sov_token_prefix: Option<ManagedBuffer>,
) {
self.is_sovereign_chain().set(is_sovereign_chain);
self.set_paused(true);

if !is_sovereign_chain {
match wegld_identifier {
Some(identifier) => {
require!(
identifier.is_valid_esdt_identifier(),
"Sent Identifier is not valid"
);

self.wegld_identifier().set(identifier);
}

None => sc_panic!("WEGLG identifier must be set in Mainchain"),
}

match sov_token_prefix {
Some(prefix) => self.sovereign_tokens_prefix().set(prefix),
None => sc_panic!("Sovereign Token Prefix must be set in Mainchain"),
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can revert condition here to do some de-nesting, i.e.

if is_sovereign_chain {
    return;
}

// code that was inside if

Also, since you need "Some" for both Option, why not take "TokenIdentifier" and "ManagedBuffer" directly in the init call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree with the de-nesting but I don't quite understand what you mean by taking those directly in the init call

}

#[only_owner]
Expand Down
4 changes: 1 addition & 3 deletions enshrine-esdt-safe/src/to_sovereign/create_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ pub trait CreateTxModule:

current_token_data.amount = payment.amount.clone();

if self.is_sovereign_chain().get()
|| self.has_sov_token_prefix(&payment.token_identifier)
{
if self.is_sovereign_chain().get() || self.has_prefix(&payment.token_identifier) {
self.send().esdt_local_burn(
&payment.token_identifier,
payment.token_nonce,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const RECEIVER_ADDRESS: TestAddress = TestAddress::new("receiver");

const NFT_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("NFT-123456");
const FUNGIBLE_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("CROWD-123456");
const PREFIX_NFT_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("SOV-NFT-123456");
const PREFIX_NFT_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("sov-NFT-123456");

fn world() -> ScenarioWorld {
let mut blockchain = ScenarioWorld::new();
Expand Down
5 changes: 3 additions & 2 deletions enshrine-esdt-safe/wasm-enshrine-esdt-safe-full/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

// Init: 1
// Upgrade: 1
// Endpoints: 27
// Endpoints: 28
// Async Callback: 1
// Promise callbacks: 1
// Total number of exported functions: 31
// Total number of exported functions: 32

#![no_std]

Expand All @@ -28,6 +28,7 @@ multiversx_sc_wasm_adapter::endpoints! {
addSigners => add_signers
removeSigners => remove_signers
executeBridgeOps => execute_operations
registerNewTokenID => register_new_token_id
setMaxTxBatchSize => set_max_tx_batch_size
setMaxTxBatchBlockDuration => set_max_tx_batch_block_duration
getCurrentTxBatch => get_current_tx_batch
Expand Down
5 changes: 3 additions & 2 deletions enshrine-esdt-safe/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

// Init: 1
// Upgrade: 1
// Endpoints: 27
// Endpoints: 28
// Async Callback: 1
// Promise callbacks: 1
// Total number of exported functions: 31
// Total number of exported functions: 32

#![no_std]

Expand All @@ -28,6 +28,7 @@ multiversx_sc_wasm_adapter::endpoints! {
addSigners => add_signers
removeSigners => remove_signers
executeBridgeOps => execute_operations
registerNewTokenID => register_new_token_id
setMaxTxBatchSize => set_max_tx_batch_size
setMaxTxBatchBlockDuration => set_max_tx_batch_block_duration
getCurrentTxBatch => get_current_tx_batch
Expand Down
Loading