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-treasury program implementation #127

Merged
merged 16 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ members = [
"gear-programs/bridging-payment/src/wasm",
"gear-programs/vft-gateway",
"gear-programs/vft-gateway/src/wasm",
"gear-programs/vft-treasury",
"gear-programs/vft-treasury/src/wasm",
"gear-programs/*",
"gear-programs/checkpoint-light-client/io",
"utils-prometheus",
Expand All @@ -37,6 +39,8 @@ bridging_payment = { path = "gear-programs/bridging-payment" }
bridging_payment_wasm = { path = "gear-programs/bridging_payment/src/wasm" }
vft-gateway-app = { path = "gear-programs/vft-gateway" }
vft_gateway_wasm = { path = "gear-programs/vft-gateway/src/wasm" }
vft-treasury-app = { path = "gear-programs/vft-treasury" }
vft_treasury_wasm = { path = "gear-programs/vft-treasury/src/wasm" }
gear_proof_storage = { path = "gear-programs/proof-storage" }
checkpoint_light_client-io = { path = "gear-programs/checkpoint-light-client/io", default-features = false }
utils-prometheus = { path = "utils-prometheus" }
Expand Down
22 changes: 22 additions & 0 deletions gear-programs/vft-treasury/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "vft-treasury-app"
version.workspace = true
edition.workspace = true

[dependencies]
sails-rs.workspace = true
parity-scale-codec.workspace = true
scale-info.workspace = true
gstd = { workspace = true, features = ["debug"] }
gbuiltin-eth-bridge.workspace = true

[build-dependencies]
git-download.workspace = true
sails-client-gen.workspace = true

[dev-dependencies]
gclient.workspace = true
tokio.workspace = true
blake2.workspace = true
gear-core.workspace = true
primitive-types.workspace = true
24 changes: 24 additions & 0 deletions gear-programs/vft-treasury/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use sails_client_gen::ClientGenerator;
use std::{env, path::PathBuf};

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

#[cfg(not(target_family = "windows"))]
let idl_file_path = out_dir_path.join("vft.idl");
#[cfg(not(target_family = "windows"))]
git_download::repo("https://github.com/gear-foundation/standards")
.branch_name("master")
.add_file("extended-vft/wasm/extended_vft.idl", &idl_file_path)
.exec()
.unwrap();

// use local copy of `vft.idl` to build on windows
#[cfg(target_family = "windows")]
let idl_file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("vft.idl");

ClientGenerator::from_idl_path(&idl_file_path)
.generate_to(client_rs_file_path)
.unwrap();
}
19 changes: 19 additions & 0 deletions gear-programs/vft-treasury/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![no_std]

use sails_rs::{gstd::GStdExecContext, prelude::*};
pub mod services;
use services::{InitConfig, VftTreasury};
#[derive(Default)]
pub struct Program;

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

pub fn vft_treasury(&self) -> VftTreasury<GStdExecContext> {
VftTreasury::new(GStdExecContext::new())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use super::{msg_tracker_mut, utils, Config, Error, MessageStatus};
use gstd::MessageId;
use sails_rs::prelude::*;

pub async fn send_message_to_bridge_builtin(
gear_bridge_builtin: ActorId,
receiver_contract_address: H160,
receiver: H160,
token_id: H160,
amount: U256,
config: &Config,
msg_id: MessageId,
) -> Result<U256, Error> {
msg_tracker_mut()
.update_message_status(msg_id, MessageStatus::SendingMessageToBridgeBuiltin)
.expect("no message found");

let payload_bytes = Payload {
receiver,
token_id,
amount,
}
.pack();

let bytes = gbuiltin_eth_bridge::Request::SendEthMessage {
destination: receiver_contract_address,
payload: payload_bytes,
}
.encode();

utils::set_critical_hook(msg_id);
utils::send_message_with_gas_for_reply(
gear_bridge_builtin,
bytes,
config.gas_to_send_request_to_builtin,
config.gas_for_reply_deposit,
config.reply_timeout,
msg_id,
)
.await?;
msg_tracker_mut().check_bridge_reply(&msg_id)
}

#[derive(Debug, Decode, Encode, TypeInfo)]
pub struct Payload {
pub receiver: H160,
pub token_id: H160,
pub amount: U256,
}

impl Payload {
pub fn pack(self) -> Vec<u8> {
let mut packed = Vec::with_capacity(20 + 20 + 32); // H160 is 20 bytes, U256 is 32 bytes

packed.extend_from_slice(self.receiver.as_bytes());
packed.extend_from_slice(self.token_id.as_bytes());

let mut amount_bytes = [0u8; 32];
self.amount.to_big_endian(&mut amount_bytes);
packed.extend_from_slice(&amount_bytes);

packed
}
}
23 changes: 23 additions & 0 deletions gear-programs/vft-treasury/src/services/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use sails_rs::prelude::*;

#[derive(Debug, Encode, Decode, TypeInfo, Clone, PartialEq, Eq)]
pub enum Error {
SendFailure(String),
ReplyFailure(String),
BuiltinDecode,
ReplyTimeout,
DuplicateAddressMapping,
NoCorrespondingEthAddress,
ReplyHook(String),
MessageNotFound,
InvalidMessageStatus,
MessageFailed,
BridgeBuiltinMessageFailed,
TokensRefunded,
NotEthClient,
NotAdmin,
NotBridgingClient,
NotEnoughGas,
TransferFailed,
TransferTokensDecode,
}
Loading
Loading