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 3 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
2,639 changes: 2,081 additions & 558 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 @@ -4,10 +4,13 @@ members = [
"ethereum/client",
"gear-rpc-client",
"prover",
"relayer",
# "relayer",
LouiseMedova marked this conversation as resolved.
Show resolved Hide resolved
"circuits/*",
"ethereum-common",
"gear-programs/*",
"gear-programs/grc20-gateway/app",
"gear-programs/grc20-gateway/wasm",
"gear-programs/bridging-payment/app",
"gear-programs/bridging-payment/wasm",
]

resolver = "2"
Expand All @@ -27,7 +30,6 @@ 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" }

plonky2 = { git = "https://github.com/gear-tech/plonky2.git", rev = "4a620f4d79efe9233d0e7682df5a2fc625b5420e" }
Expand Down Expand Up @@ -80,7 +82,10 @@ unroll = "0.1.5"

# Gear/Substrate deps
gstd = { version = "1.4.1", features = ["nightly"] }
gear-core = "1.4.1"
gclient = "1.4.1"
gear-wasm-builder = { version = "1.4.1", default-features = false }
gwasm-builder = { package = "gear-wasm-builder", version = "1.1.1" }
LouiseMedova marked this conversation as resolved.
Show resolved Hide resolved
gsdk = { git = "https://github.com/gear-tech/gear.git", branch = "dn-pallet-gear-bridge" }
pallet-gear-bridge-rpc-runtime-api = { git = "https://github.com/gear-tech/gear.git", branch = "dn-pallet-gear-bridge", default-features = false, features = [
"std",
Expand Down Expand Up @@ -131,6 +136,13 @@ alloy-sol-type-parser = { git = "https://github.com/alloy-rs/core.git", package
"std",
] }

sails-idl-gen = { git = "https://github.com/gear-tech/sails" }
sails-client-gen = { git = "https://github.com/gear-tech/sails" }
sails-rtl = { git = "https://github.com/gear-tech/sails" }

gbuiltin-bridge = { git = "https://github.com/gear-tech/gear", branch = "dn-pallet-gear-bridge" }
LouiseMedova marked this conversation as resolved.
Show resolved Hide resolved
git-download = "0.1"
LouiseMedova marked this conversation as resolved.
Show resolved Hide resolved

[patch.crates-io]
alloy-primitives = { git = "https://github.com/alloy-rs/core.git", package = "alloy-primitives", rev = "1bac7678797fcd1bee2f2580825724b4165b12c1" }
alloy-sol-types = { git = "https://github.com/alloy-rs/core.git", package = "alloy-sol-types", rev = "1bac7678797fcd1bee2f2580825724b4165b12c1" }
23 changes: 0 additions & 23 deletions gear-programs/bridging-payment/Cargo.toml

This file was deleted.

15 changes: 15 additions & 0 deletions gear-programs/bridging-payment/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "bridge-payment-app"
version = "0.1.0"
edition = "2021"

[dependencies]
sails-rtl.workspace = true
parity-scale-codec.workspace = true
scale-info.workspace = true
gstd.workspace = true
gbuiltin-bridge.workspace = true

[build-dependencies]
git-download.workspace = true
sails-client-gen.workspace = true
11 changes: 11 additions & 0 deletions gear-programs/bridging-payment/app/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::{env, path::PathBuf};

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

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

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

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

use sails_rtl::gstd::{gprogram, GStdExecContext};
pub mod services;
use services::{BridgePayment, InitConfig};
#[derive(Default)]
pub struct Program;

#[gprogram]
impl Program {
pub fn new(init_config: InitConfig) -> Self {
BridgePayment::<GStdExecContext>::seed(init_config, GStdExecContext::new());
Self
}

pub fn bridge_payment(&self) -> BridgePayment<GStdExecContext> {
BridgePayment::new(GStdExecContext::new())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include!(concat!(env!("OUT_DIR"), "/grc20-gateway.rs"));
193 changes: 193 additions & 0 deletions gear-programs/bridging-payment/app/src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
use gstd::MessageId;
use sails_rtl::{
gstd::{gservice, msg, ExecContext},
prelude::*,
};

use gstd::exec;
mod grc20_gateway;
mod utils;
pub struct BridgePayment<ExecContext> {
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,
sender: ActorId,
LouiseMedova marked this conversation as resolved.
Show resolved Hide resolved
amount: U256,
receiver: [u8; 20],
eth_token_id: [u8; 20],
) {
let data = self.data();
let config = self.config();

let attached_value = msg::value();

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

// return remainder
let refund = self.config().fee - attached_value;
LouiseMedova marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
}
}

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/app/src/services/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use super::{grc20_gateway, Config};
use gstd::{msg, prelude::collections::HashMap, MessageId};
use sails_rtl::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,
amount: U256,
receiver: [u8; 20],
eth_token_id: [u8; 20],
config: &Config,
) -> Result<Result<U256, grc20_gateway::Error>, Error> {
let bytes: Vec<u8> = grc20_gateway::grc_20_gateway_io::TeleportVaraToEth::encode_call(
sender,
amount,
receiver,
eth_token_id,
);

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, grc20_gateway::Error> =
grc20_gateway::grc_20_gateway_io::TeleportVaraToEth::decode_reply(&reply_bytes)
.map_err(|_| Error::RequestToGateWayDecodeError)?;

Ok(reply)
}
3 changes: 0 additions & 3 deletions gear-programs/bridging-payment/build.rs

This file was deleted.

Loading