-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract common bridging payment code
- Loading branch information
1 parent
d3fb836
commit 19fb48b
Showing
28 changed files
with
331 additions
and
497 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "bridging_payment_common" | ||
version.workspace = true | ||
edition.workspace = true | ||
|
||
[dependencies] | ||
sails-rs.workspace = true | ||
parity-scale-codec.workspace = true | ||
scale-info.workspace = true | ||
gstd.workspace = true | ||
|
||
[features] | ||
default = [] | ||
gtest_macros = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
gear-programs/bridging-payment-common/src/gtest_macros.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#[macro_export] | ||
macro_rules! create_function { | ||
($name:ident, $method:expr) => { | ||
fn $name(&self, from: u64, error: bool); | ||
}; | ||
|
||
($name:ident, $method:expr, $($param_name:ident: $param_type:ty),*) => { | ||
fn $name(&self, from: u64, $($param_name: $param_type,)* error: bool); | ||
}; | ||
} | ||
|
||
pub use create_function; | ||
|
||
#[macro_export] | ||
macro_rules! implement_function { | ||
($name:ident, $prefix:expr, $method:expr) => { | ||
fn $name(&self, from: u64, error: bool) { | ||
let payload = [ | ||
$prefix.encode(), | ||
$method.encode(), | ||
] | ||
.concat(); | ||
let result = self.send_bytes(from, payload); | ||
|
||
if error { | ||
assert!(result.main_failed()); | ||
} else { | ||
assert!(!result.main_failed()); | ||
} | ||
} | ||
}; | ||
($name:ident, $prefix:expr, $method:expr, $($param_name:ident: $param_type:ty),*; $with_value:expr) => { | ||
fn $name(&self, from: u64, $($param_name: $param_type,)* error: bool) { | ||
let payload = [ | ||
$prefix.encode(), | ||
$method.encode(), | ||
($($param_name,)*).encode(), | ||
] | ||
.concat(); | ||
|
||
let result = if $with_value { | ||
self.send_bytes_with_value(from, payload, FEE) | ||
} else { | ||
self.send_bytes(from, payload) | ||
}; | ||
|
||
if error { | ||
assert!(result.main_failed()); | ||
} else { | ||
assert!(!result.main_failed()); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
pub use implement_function; | ||
|
||
#[macro_export] | ||
macro_rules! create_query_function { | ||
// Match functions with parameters | ||
($fn_name:ident, $return_type:ty, $($param_name:ident: $param_type:ty),*) => { | ||
fn $fn_name(&self, $($param_name: $param_type),*) -> $return_type; | ||
}; | ||
// Match functions without parameters | ||
($fn_name:ident, $return_type:ty) => { | ||
fn $fn_name(&self) -> $return_type; | ||
}; | ||
} | ||
|
||
pub use create_query_function; | ||
|
||
#[macro_export] | ||
macro_rules! implement_token_query { | ||
($fn_name:ident, $query_name:expr, $return_type:ty) => { | ||
fn $fn_name(&self) -> $return_type { | ||
let query = ["Vft".encode(), $query_name.encode()].concat(); | ||
let result = self.send_bytes(ADMIN_ID, query.clone()); | ||
|
||
let log_entry = result | ||
.log() | ||
.iter() | ||
.find(|log_entry| log_entry.destination() == ADMIN_ID.into()) | ||
.expect("Unable to get query reply"); | ||
|
||
let query_reply = <(String, String, $return_type)>::decode(&mut log_entry.payload()) | ||
.expect("Unable to decode reply"); | ||
query_reply.2 | ||
} | ||
}; | ||
|
||
($fn_name:ident, $query_name:expr, $return_type:ty, $($param_name:ident: $param_type:ty),*) => { | ||
fn $fn_name(&self, $($param_name: $param_type),*) -> $return_type { | ||
let query = ["Vft".encode(), $query_name.encode(), ($($param_name),*).encode()].concat(); | ||
let result = self.send_bytes(ADMIN_ID, query.clone()); | ||
|
||
let log_entry = result | ||
.log() | ||
.iter() | ||
.find(|log_entry| log_entry.destination() == ADMIN_ID.into()) | ||
.expect("Unable to get query reply"); | ||
|
||
let query_reply = <(String, String, $return_type)>::decode(&mut log_entry.payload()) | ||
.expect("Unable to decode reply"); | ||
query_reply.2 | ||
} | ||
}; | ||
} | ||
|
||
pub use implement_token_query; | ||
|
||
#[macro_export] | ||
macro_rules! create_ft_mock { | ||
($name:ident, $handle_result:expr) => { | ||
#[derive(Debug)] | ||
pub struct $name; | ||
|
||
impl WasmProgram for $name { | ||
fn init(&mut self, _payload: Vec<u8>) -> Result<Option<Vec<u8>>, &'static str> { | ||
Ok(None) | ||
} | ||
|
||
fn handle(&mut self, _payload: Vec<u8>) -> Result<Option<Vec<u8>>, &'static str> { | ||
$handle_result | ||
} | ||
|
||
fn handle_reply(&mut self, _payload: Vec<u8>) -> Result<(), &'static str> { | ||
unimplemented!() | ||
} | ||
|
||
fn handle_signal(&mut self, _payload: Vec<u8>) -> Result<(), &'static str> { | ||
unimplemented!() | ||
} | ||
|
||
fn state(&mut self) -> Result<Vec<u8>, &'static str> { | ||
unimplemented!() | ||
} | ||
} | ||
}; | ||
} | ||
|
||
pub use create_ft_mock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![no_std] | ||
|
||
pub mod error; | ||
#[cfg(feature = "gtest_macros")] | ||
pub mod gtest_macros; | ||
pub mod refund; | ||
pub mod utils; | ||
|
||
use sails_rs::prelude::*; | ||
|
||
#[derive(Encode, Decode, TypeInfo)] | ||
pub enum BridgingPaymentEvents { | ||
TeleportVaraToEth { | ||
nonce: U256, | ||
sender: ActorId, | ||
amount: U256, | ||
receiver: H160, | ||
eth_token_id: H160, | ||
}, | ||
DepositVaraToTreasury { | ||
sender: ActorId, | ||
amount: U256, | ||
receiver: H160, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use gstd::{exec, msg, ActorId}; | ||
|
||
pub fn process_refund(sender: ActorId, attached_value: u128, fee: u128) { | ||
let refund = attached_value - fee; | ||
if refund >= exec::env_vars().existential_deposit { | ||
handle_refund(sender, refund); | ||
} | ||
} | ||
|
||
fn handle_refund(actor_id: ActorId, amount: u128) { | ||
msg::send_with_gas(actor_id, "", 0, amount).expect("Error in refund"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use gstd::{vec::Vec, ActorId, MessageId}; | ||
|
||
use crate::error::Error; | ||
|
||
#[macro_export] | ||
macro_rules! maybe_event_or_panic_async { | ||
($self:expr, $expr:expr) => {{ | ||
let result: Result<Option<BridgingPaymentEvents>, Error> = $expr().await; | ||
match result { | ||
Ok(Some(value)) => { | ||
if let Err(e) = $self.notify_on(value) { | ||
panic!("Error in depositing events: {:?}", e); | ||
} | ||
} | ||
Ok(None) => {} | ||
Err(e) => { | ||
panic!("Message processing failed with error: {:?}", e); | ||
} | ||
} | ||
}}; | ||
} | ||
|
||
pub use maybe_event_or_panic_async; | ||
|
||
#[macro_export] | ||
macro_rules! event_or_panic_async { | ||
($self:expr, $expr:expr) => {{ | ||
let result: Result<BridgingPaymentEvents, Error> = $expr().await; | ||
match result { | ||
Ok(value) => { | ||
if let Err(e) = $self.notify_on(value) { | ||
panic!("Error in depositing events: {:?}", e); | ||
} | ||
} | ||
Err(e) => { | ||
panic!("Message processing failed with error: {:?}", e); | ||
} | ||
} | ||
}}; | ||
} | ||
|
||
pub use event_or_panic_async; | ||
|
||
pub async fn send_message_with_gas_for_reply( | ||
handle_reply_hook: fn(msg_id: MessageId), | ||
destination: ActorId, | ||
message: Vec<u8>, | ||
gas_to_send: u64, | ||
gas_deposit: u64, | ||
reply_timeout: u32, | ||
msg_id: MessageId, | ||
) -> Result<(), Error> { | ||
gstd::msg::send_bytes_with_gas_for_reply(destination, message, gas_to_send, 0, gas_deposit) | ||
.map_err(|_| Error::SendFailure)? | ||
.up_to(Some(reply_timeout)) | ||
.map_err(|_| Error::ReplyTimeout)? | ||
.handle_reply(move || handle_reply_hook(msg_id)) | ||
.map_err(|_| Error::ReplyHook)? | ||
.await | ||
.map_err(|_| Error::ReplyFailure)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
gear-programs/bridging-payment-vara-supply/src/services/error.rs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.