-
Notifications
You must be signed in to change notification settings - Fork 2
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
Wegld fee #65
Changes from 66 commits
4378754
d1f743c
1710be6
3fd293f
defe670
fb815db
9968276
ec7ce34
14e326f
36e870e
d0aaea1
3f5de49
6358d2e
752673e
2977ef2
43e896c
a05a829
1a77a32
139528f
4a9db9f
fd7ece4
04431bf
a9bdc9f
d8124c2
79add8f
12d9950
51f855f
d691d06
b82ac41
201397b
6d65426
12e326c
59c2665
6693b9a
33a7aab
1f5bff4
098b067
83139b9
e1c516b
a1b2738
1bff454
4cfc7c6
87cafe7
32fb9ac
07d92fc
e426908
74c97f7
015c2d4
5352b44
8ed2d0a
a26458a
ae5d975
86f397e
b6a8743
3dcf59c
9eee6b9
9b4a669
f49be51
0a6a31f
62aa434
7f058e6
906fb92
aaf36e7
28736eb
5d3b2f4
ff22b99
13e4765
13bac6a
5db58c6
98e86fd
65d1e45
db647b9
a8ddcc8
5882849
6aef775
f660493
9ad4b0e
6785627
4b92c04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]; | ||
|
@@ -80,4 +80,23 @@ pub trait UtilsModule: bls_signature::BlsSignatureModule { | |
|
||
false | ||
} | ||
|
||
fn has_sov_prefix(&self, token_id: &TokenIdentifier, chain_prefix: ManagedBuffer) -> bool { | ||
require!(self.has_prefix(token_id), "Token does not have prefix"); | ||
|
||
let dash = b'-'; | ||
let buffer = token_id.as_managed_buffer(); | ||
let mut array_buffer = [0u8; 32]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "32" can also be a const, like "MAX_TOKEN_ID_LEN" There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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" | ||
); | ||
|
||
|
@@ -36,13 +39,66 @@ 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(), | ||
}, | ||
); | ||
} | ||
|
||
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(registerTokens)] | ||
andreiblt1304 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[payable("*")] | ||
fn register_tokens(&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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
|
||
fn verify_operation_tokens_issue_paid( | ||
&self, | ||
tokens: ManagedVec<OperationEsdtPayment<Self::Api>>, | ||
) -> bool { | ||
require!(!tokens.is_empty(), "Tokens array should not be empty"); | ||
andreiblt1304 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for token in tokens.iter() { | ||
if !self.was_token_registered(&token.token_identifier) { | ||
return false; | ||
} | ||
} | ||
|
||
true | ||
} | ||
|
||
fn mint_tokens( | ||
|
@@ -52,7 +108,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; | ||
} | ||
|
@@ -113,7 +170,6 @@ pub trait TransferTokensModule: | |
|
||
output_payments | ||
} | ||
|
||
fn distribute_payments( | ||
&self, | ||
hash_of_hashes: ManagedBuffer, | ||
|
@@ -195,6 +251,7 @@ pub trait TransferTokensModule: | |
); | ||
} | ||
ManagedAsyncCallResult::Err(_) => { | ||
self.burn_sovereign_tokens(&operation_tuple.operation); | ||
self.emit_transfer_failed_events(hash_of_hashes, operation_tuple); | ||
} | ||
} | ||
|
@@ -208,6 +265,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, | ||
|
@@ -218,14 +290,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(); | ||
|
@@ -268,9 +332,33 @@ pub trait TransferTokensModule: | |
} | ||
} | ||
|
||
#[inline] | ||
fn was_token_registered(&self, token_id: &TokenIdentifier<Self::Api>) -> bool { | ||
self.has_sov_prefix(token_id, self.get_sovereign_prefix()) | ||
&& self.paid_issued_tokens().contains(token_id) | ||
} | ||
|
||
#[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>>; | ||
} |
There was a problem hiding this comment.
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.