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

feat(gear-programs): vft-gateway & bridging-payment #84

Merged
merged 38 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
076d1ba
initial grc20-gateway and bridge-payment implementation
LouiseMedova Jun 24, 2024
9d06fd3
TO DO comments
LouiseMedova Jun 24, 2024
eeec0cb
implementation of handle_reply and critical in grc20-gateway
LouiseMedova Jun 25, 2024
108a83c
several fixes according to review & naming update
LouiseMedova Jul 3, 2024
35fc280
fixed build with updated sails
LouiseMedova Jul 11, 2024
cccc45e
changef sails-rtl to sails
LouiseMedova Jul 14, 2024
57c772f
merged relayer & Cargo.toml
LouiseMedova Jul 15, 2024
dde0881
Update gear-programs/bridging-payment/src/wasm/build.rs
LouiseMedova Jul 16, 2024
472a25b
Update Cargo.toml
LouiseMedova Jul 16, 2024
dbccf41
Update Cargo.toml
LouiseMedova Jul 16, 2024
a8304c7
Update gear-programs/bridging-payment/src/wasm/Cargo.toml
LouiseMedova Jul 16, 2024
7c7aa55
Update gear-programs/vft-gateway/src/services/mod.rs
LouiseMedova Jul 16, 2024
814d133
added functionality to bridge-payment to transfer tokens and make req…
LouiseMedova Aug 1, 2024
7391125
fixed build
LouiseMedova Aug 1, 2024
5d2a43a
updated contracts to v1.5.0
LouiseMedova Aug 14, 2024
76b90d7
merged main
LouiseMedova Aug 15, 2024
5e6ba15
finished merge
LouiseMedova Aug 15, 2024
fb6c00d
fixed warnings
LouiseMedova Aug 16, 2024
27118e5
added admins functions
LouiseMedova Aug 21, 2024
21701ff
Update gear-programs/bridging-payment/src/services/msg_tracker.rs
LouiseMedova Aug 21, 2024
39f5e98
Update gear-programs/bridging-payment/src/services/mod.rs
LouiseMedova Aug 21, 2024
fc5ebfd
Update gear-programs/vft-gateway/src/wasm/tests/utils.rs
LouiseMedova Aug 21, 2024
1117ef7
Update gear-programs/vft-gateway/src/wasm/tests/utils.rs
LouiseMedova Aug 21, 2024
2129d3c
Update gear-programs/vft-gateway/src/services/mod.rs
LouiseMedova Aug 21, 2024
c4aacb1
Update gear-programs/vft-gateway/src/services/mod.rs
LouiseMedova Aug 21, 2024
a032af6
Update gear-programs/vft-gateway/src/services/msg_tracker.rs
LouiseMedova Aug 21, 2024
5e33c7a
Update gear-programs/vft-gateway/src/services/token_operations.rs
LouiseMedova Aug 21, 2024
58fe8dc
fixed accroding review
LouiseMedova Aug 21, 2024
db82f77
fixed test
LouiseMedova Aug 21, 2024
db04eaa
Merge branch 'main' into lm-vara-contracts
LouiseMedova Aug 21, 2024
33cbc35
added query methods
LouiseMedova Aug 23, 2024
3e48e3e
fixed tests
LouiseMedova Aug 23, 2024
1546c17
fix clippy
LouiseMedova Aug 23, 2024
fc3b5ce
fix clippy
LouiseMedova Aug 23, 2024
00778e1
fix clippy
LouiseMedova Aug 26, 2024
e8aaf38
fixed tests
LouiseMedova Aug 26, 2024
11f6a23
fixed tests
LouiseMedova Aug 26, 2024
1a3ba49
fmt
LouiseMedova Aug 26, 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
4,383 changes: 3,434 additions & 949 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ members = [
"relayer",
"circuits/*",
"ethereum-common",
"gear-programs/*",
"gear-programs/bridging-payment",
"gear-programs/bridging-payment/src/wasm",
"gear-programs/vft-gateway",
"gear-programs/vft-gateway/src/wasm",
]

resolver = "2"
Expand All @@ -27,8 +30,10 @@ plonky2_u32 = { path = "./circuits/plonky2_u32" }
ethereum-client = { path = "./ethereum/client" }
ethereum-common = { path = "ethereum-common", default-features = false }

grc20_gateway = { path = "gear-programs/grc20-gateway" }
bridging_payment = { path = "gear-programs/bridging-payment" }
bridge_payment_wasm = { path = "gear-programs/bridging_payment/src/wasm" }
LouiseMedova marked this conversation as resolved.
Show resolved Hide resolved
vft-gateway = { path = "gear-programs/vft-gateway" }
vft_gateway_wasm = { path = "gear-programs/vft-gateway/src/wasm" }
gear_proof_storage = { path = "gear-programs/proof-storage" }

plonky2 = { git = "https://github.com/gear-tech/plonky2.git", rev = "4a620f4d79efe9233d0e7682df5a2fc625b5420e" }
Expand Down Expand Up @@ -56,6 +61,7 @@ ethereum-types = { version = "0.14.1", default-features = false, features = [
ff = { version = "0.13.0", features = ["derive"] }
futures = { version = "0.3.30", features = ["executor"] }
futures-util = "0.3.28"
git-download = "0.1"
hex = { version = "0.4.3", default-features = false }
hex-literal = "0.4.1"
itertools = "0.10.5"
Expand All @@ -72,6 +78,9 @@ rand = { version = "0.8.5", default-features = false, features = ["getrandom"] }
rand_chacha = "0.3.1"
rayon = "1.5.3"
reqwest = "0.11.24"
sails-idl-gen = { git = "https://github.com/gear-tech/sails" }
sails-client-gen = { git = "https://github.com/gear-tech/sails" }
sails = { git = "https://github.com/gear-tech/sails" }
scale-info = { version = "2.10", default-features = false }
serde = { version = "1.0", features = ["alloc", "derive"] }
serde_json = "1.0"
Expand All @@ -84,7 +93,10 @@ tree_hash_derive = { git = "https://github.com/gear-tech/tree_hash.git", branch
unroll = "0.1.5"

# Gear/Substrate deps
gstd = { version = "1.4.1", features = ["nightly"] }
gbuiltin-bridge = { git = "https://github.com/gear-tech/gear", branch = "dn-pallet-gear-bridge" }
LouiseMedova marked this conversation as resolved.
Show resolved Hide resolved
gstd = { version = "1.4.2", features = ["nightly"] }
gwasm-builder = { package = "gear-wasm-builder", version = "1.4.2" }

gear-wasm-builder = { version = "1.4.1", default-features = false }
gsdk = { git = "https://github.com/gear-tech/gear.git", branch = "dn-pallet-gear-eth-bridge" }
gclient = { git = "https://github.com/gear-tech/gear.git", branch = "dn-pallet-gear-eth-bridge" }
Expand Down
18 changes: 5 additions & 13 deletions gear-programs/bridging-payment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ version.workspace = true
edition.workspace = true

[dependencies]
grc20_gateway = { workspace = true, features = ["std"] }

gstd.workspace = true
sails.workspace = true
parity-scale-codec.workspace = true
scale-info.workspace = true
primitive-types.workspace = true
gstd.workspace = true
gbuiltin-bridge.workspace = true

[build-dependencies]
gear-wasm-builder.workspace = true

[lib]
crate-type = ["rlib"]
name = "bridging_payment"

[features]
std = []
default = ["std"]
git-download.workspace = true
sails-client-gen.workspace = true
10 changes: 9 additions & 1 deletion gear-programs/bridging-payment/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
use std::{env, path::PathBuf};

fn main() {
gear_wasm_builder::build();
let out_dir_path = PathBuf::from(env::var("OUT_DIR").unwrap());

let idl_file_path = PathBuf::from("../vft-gateway/src/wasm/vft-gateway.idl");

let client_rs_file_path = out_dir_path.join("vft-gateway.rs");

sails_client_gen::generate_client_from_idl(&idl_file_path, client_rs_file_path).unwrap();
}
40 changes: 14 additions & 26 deletions gear-programs/bridging-payment/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
#![no_std]

use gstd::ActorId;
use parity_scale_codec::{Decode, Encode};
use primitive_types::U256;
use scale_info::TypeInfo;
use sails::gstd::{gprogram, GStdExecContext};
pub mod services;
use services::{BridgePayment, InitConfig};
#[derive(Default)]
pub struct Program;

#[cfg(not(feature = "std"))]
mod wasm;
#[gprogram]
impl Program {
pub fn new(init_config: InitConfig) -> Self {
BridgePayment::<GStdExecContext>::seed(init_config, GStdExecContext::new());
Self
}

#[derive(Debug, Decode, Encode, TypeInfo)]
pub struct InitMessage {
pub grc20_gateway: ActorId,
pub fee: u128,
}

#[derive(Debug, Decode, Encode, TypeInfo)]
pub enum AdminMessage {
SetFee(u128),
ReclaimFees,
}

#[derive(Debug, Decode, Encode, TypeInfo)]
pub struct UserReply {
pub nonce: U256,
}

#[derive(Debug, Decode, Encode, TypeInfo)]
pub struct State {
pub fee: u128,
pub fn bridge_payment(&self) -> BridgePayment<GStdExecContext> {
BridgePayment::new(GStdExecContext::new())
}
}
193 changes: 193 additions & 0 deletions gear-programs/bridging-payment/src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
use gstd::MessageId;
use sails::{
gstd::{gservice, msg, ExecContext},
prelude::*,
};

use gstd::exec;
mod utils;
mod vft_gateway;
pub struct BridgePayment<ExecContext> {
LouiseMedova marked this conversation as resolved.
Show resolved Hide resolved
exec_context: ExecContext,
}

#[derive(Encode, Decode, TypeInfo)]
pub enum BridgePaymentEvents {
TeleportVaraToEth {
nonce: U256,
sender: ActorId,
amount: U256,
receiver: H160,
eth_token_id: H160,
},
}

static mut DATA: Option<BridgePaymentData> = None;
static mut CONFIG: Option<Config> = None;

#[derive(Debug)]
pub struct BridgePaymentData {
admin_address: ActorId,
grc20_gateway_address: ActorId,
}

#[derive(Debug, Decode, Encode, TypeInfo)]
pub struct InitConfig {
admin_address: ActorId,
grc20_gateway_address: ActorId,
config: Config,
}

impl InitConfig {
pub fn new(admin_address: ActorId, grc20_gateway_address: ActorId, config: Config) -> Self {
Self {
admin_address,
grc20_gateway_address,
config,
}
}
}

#[derive(Debug, Decode, Encode, TypeInfo)]
pub struct Config {
fee: u128,
gas_for_reply_deposit: u64,
gas_to_send_request_to_gateway: u64,
}

impl Config {
pub fn new(fee: u128, gas_for_reply_deposit: u64, gas_to_send_request_to_gateway: u64) -> Self {
Self {
fee,
gas_for_reply_deposit,
gas_to_send_request_to_gateway,
}
}
}
impl<T> BridgePayment<T>
where
T: ExecContext,
{
pub fn seed(config: InitConfig, exec_context: T) {
unsafe {
DATA = Some(BridgePaymentData {
admin_address: exec_context.actor_id(),
grc20_gateway_address: config.grc20_gateway_address,
});
CONFIG = Some(config.config);
}
}
pub fn new(exec_context: T) -> Self {
Self { exec_context }
}

fn data(&self) -> &BridgePaymentData {
unsafe {
DATA.as_ref()
.expect("BridgePaymentData::seed() should be called")
}
}

fn data_mut(&mut self) -> &mut BridgePaymentData {
unsafe {
DATA.as_mut()
.expect("BridgePaymentData::seed() should be called")
}
}

fn config(&self) -> &Config {
unsafe {
CONFIG
.as_ref()
.expect("BridgePaymentData::seed() should be called")
}
}

fn config_mut(&self) -> &mut Config {
unsafe {
CONFIG
.as_mut()
.expect("BridgePaymentData::seed() should be called")
}
}

pub fn handle_reply() {}
}

#[gservice(events = BridgePaymentEvents)]
impl<T> BridgePayment<T>
where
T: ExecContext,
{
pub fn set_fee(&mut self, fee: u128) {
let data = self.data();
if data.admin_address != self.exec_context.actor_id() {
panic!("Not admin");
}
let config: &mut Config = self.config_mut();
config.fee = fee;
}

pub fn reclaim_fee(&mut self) {
let data = self.data();
if data.admin_address != self.exec_context.actor_id() {
panic!("Not admin");
}
let fee_balance = exec::value_available();
msg::send_with_gas(data.admin_address.into(), "", 0, fee_balance)
.expect("Failed to reclaim fees");
}

pub async fn request_to_gateway(
&mut self,
amount: U256,
receiver: H160,
vara_token_id: ActorId,
) {
let data = self.data();
let config = self.config();

let attached_value = msg::value();

if attached_value < config.fee {
panic!("Not enough fee");
}
let sender = self.exec_context.actor_id();
match utils::send_message_to_gateway(
data.grc20_gateway_address,
sender,
vara_token_id,
amount,
receiver,
config,
)
.await
{
Ok(Ok((nonce, eth_token_id))) => {
self.notify_on(BridgePaymentEvents::TeleportVaraToEth {
nonce,
sender,
amount,
receiver: receiver.into(),
eth_token_id,
})
.expect("Error in depositing event");

// return remainder
let refund = attached_value - self.config().fee;
if refund >= exec::env_vars().existential_deposit {
handle_refund(self.exec_context.actor_id(), refund);
}
}

_ => {
// Handle refund for the full attached value on any error
handle_refund(self.exec_context.actor_id(), attached_value);
mertwole marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}

fn handle_refund(actor_id: ActorId, amount: u128) {
msg::send_with_gas(actor_id.into(), "", 0, amount).expect("Error in refund");
}
50 changes: 50 additions & 0 deletions gear-programs/bridging-payment/src/services/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use super::{vft_gateway, vft_gateway::vft_gateway::io as vft_gateway_io, Config};
use gstd::{msg, prelude::collections::HashMap, MessageId};
use sails::prelude::*;

#[derive(Debug, Encode, Decode, TypeInfo)]
pub enum Error {
RequestToGateWaySendError,
RequestToGateWayReplyError,
RequestToGateWayDecodeError,
RequestToBuiltinReplyError,
PayloadSizeError,
MintTokensSendError,
MintTokensReplyError,
MintTokensDecodeError,
ReplyTimeoutError,
TokensRefundedError,
}

pub async fn send_message_to_gateway(
gateway_address: ActorId,
sender: ActorId,
vara_token_id: ActorId,
amount: U256,
receiver: H160,
config: &Config,
) -> Result<Result<(U256, H160), vft_gateway::Error>, Error> {
let bytes: Vec<u8> = vft_gateway::vft_gateway::io::TransferVaraToEth::encode_call(
sender,
vara_token_id,
amount,
receiver,
);

let reply_bytes = msg::send_bytes_with_gas_for_reply(
gateway_address.into(),
bytes,
config.gas_to_send_request_to_gateway,
0,
config.gas_for_reply_deposit,
)
.map_err(|_| Error::RequestToGateWaySendError)?
.await
.map_err(|_| Error::RequestToGateWayReplyError)?;

let reply: Result<(U256, H160), vft_gateway::Error> =
vft_gateway_io::TransferVaraToEth::decode_reply(&reply_bytes)
.map_err(|_| Error::RequestToGateWayDecodeError)?;

Ok(reply)
}
1 change: 1 addition & 0 deletions gear-programs/bridging-payment/src/services/vft_gateway.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include!(concat!(env!("OUT_DIR"), "/vft-gateway.rs"));
Loading