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(relayer): Introduce eth->gear token transfer relayer #124

Merged
merged 37 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c722b39
Add method to fetch deposit events from ERC20Treasury
mertwole Sep 9, 2024
8188931
Re-arrange modules
mertwole Sep 9, 2024
354b91e
deposit event extractor
mertwole Sep 9, 2024
92e0788
Checkpoint extractor
mertwole Sep 9, 2024
acb1166
Newtype for ethereum slot
mertwole Sep 9, 2024
48c59c3
Add message_sender
mertwole Sep 12, 2024
d01fe5f
message sender draft
mertwole Sep 12, 2024
9e1aa3e
Update balance metric
mertwole Sep 12, 2024
4f95998
Add todo
mertwole Sep 12, 2024
55affee
Merge branch 'main' into md-eth-gear-tt-relayer
mertwole Oct 8, 2024
bd235e9
Fix merge
mertwole Oct 8, 2024
18fd443
Compose relayer
mertwole Oct 8, 2024
0f216c8
Split message sender to parts
mertwole Oct 9, 2024
394e31b
Merge branch 'main' into md-eth-gear-tt-relayer
mertwole Oct 11, 2024
8573a85
Fix merge
mertwole Oct 11, 2024
ab7c4bf
Move code from utils to ethereum_beacon_client
mertwole Oct 11, 2024
c7d0816
Fix clippy
mertwole Oct 11, 2024
aac57a7
Move beacon client to a separate module
mertwole Oct 11, 2024
641e471
Inject beacon client dependency
mertwole Oct 11, 2024
ff69924
Finish deposit_event_extractor
mertwole Oct 14, 2024
0f5a7b8
Finalize BeaconClient
mertwole Oct 14, 2024
ad9afe6
Implement TODOs
mertwole Oct 14, 2024
61b3461
Compose cli args
mertwole Oct 14, 2024
c2f0f32
Rename endpoint to beacon_endpoint
mertwole Oct 15, 2024
ca56a35
Small fixes
mertwole Oct 18, 2024
15af9f5
Merge branch 'main' into md-eth-gear-tt-relayer
mertwole Oct 18, 2024
3a93c58
Move some code to utils in beacon client
mertwole Oct 21, 2024
750bc1e
Finalize
mertwole Oct 21, 2024
70ddf58
Fix clippy
mertwole Oct 21, 2024
34c9e7a
Add temporary fix for error Block not found
mertwole Oct 21, 2024
75b2084
Merge branch 'main' into md-eth-gear-tt-relayer
mertwole Oct 22, 2024
d8efe86
Merge branch 'main' into md-eth-gear-tt-relayer
mertwole Oct 23, 2024
dcc738c
Add hex_utils module
mertwole Oct 30, 2024
3e8be22
Gclient -> GClient
mertwole Oct 30, 2024
9bccb00
BeaconClient::connect -> new
mertwole Oct 30, 2024
a837a5e
Fix warn
mertwole Oct 30, 2024
afee900
Fix clippy
mertwole Oct 30, 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
196 changes: 99 additions & 97 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion ethereum/client/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::process::Command;

fn main() {
println!("cargo::rerun-if-changed=*");
println!("cargo::rerun-if-changed=../src");
println!("cargo::rerun-if-changed=../lib");

Command::new("forge")
.arg("build")
Expand Down
6 changes: 6 additions & 0 deletions ethereum/client/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ sol! {
}
}

sol!(
#[sol(rpc)]
IERC20Treasury,
"../out/IERC20Treasury.sol/IERC20Treasury.json"
);

impl ContentMessage {
pub fn to_bytes(&self) -> Vec<u8> {
let mut ret: Vec<u8> = Vec::with_capacity(32 + 32 + 20 + self.data.len());
Expand Down
103 changes: 94 additions & 9 deletions ethereum/client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{marker::PhantomData, str::FromStr};

use abi::IRelayer::MerkleRoot;

use alloy::{
contract::Event,
network::{Ethereum, EthereumWallet},
Expand All @@ -21,22 +19,22 @@ use alloy::{
Transport,
},
};

use primitive_types::{H160, H256};
use reqwest::Url;

pub use error::Error;

use crate::abi::{
ContentMessage, IMessageQueue, IMessageQueue::IMessageQueueInstance, IRelayer,
IRelayer::IRelayerInstance,
};
pub use alloy::primitives::TxHash;

#[cfg(test)]
mod tests;

mod abi;
use abi::{
ContentMessage, IERC20Treasury, IMessageQueue, IMessageQueue::IMessageQueueInstance, IRelayer,
IRelayer::IRelayerInstance, IRelayer::MerkleRoot,
};

pub mod error;
pub use error::Error;

type ProviderType = FillProvider<
JoinFill<
Expand Down Expand Up @@ -64,6 +62,16 @@ pub struct MerkleRootEntry {
pub block_number: u64,
}

#[derive(Debug, Clone)]
pub struct DepositEventEntry {
pub from: H160,
pub to: H256,
pub token: H160,
pub amount: primitive_types::U256,

pub tx_hash: TxHash,
}

#[derive(Debug)]
pub enum TxStatus {
Finalized,
Expand Down Expand Up @@ -121,6 +129,11 @@ impl EthApi {
})
}

// TODO: Don't expose provider here.
pub fn raw_provider(&self) -> &ProviderType {
&self.contracts.provider
}

pub async fn get_approx_balance(&self) -> Result<f64, Error> {
self.contracts.get_approx_balance(self.public_key).await
}
Expand Down Expand Up @@ -158,10 +171,44 @@ impl EthApi {
self.contracts.fetch_merkle_roots_in_range(from, to).await
}

pub async fn fetch_deposit_events(
&self,
contract_address: H160,
block: u64,
) -> Result<Vec<DepositEventEntry>, Error> {
Ok(self
.contracts
.fetch_deposit_events(Address::from_slice(contract_address.as_bytes()), block)
.await?
.into_iter()
.map(
|(
IERC20Treasury::Deposit {
from,
to,
token,
amount,
},
tx_hash,
)| DepositEventEntry {
from: H160(*from.0),
to: H256(to.0),
token: H160(*token.0),
amount: primitive_types::U256::from_little_endian(&amount.to_le_bytes_vec()),
tx_hash,
},
)
.collect())
}

pub async fn block_number(&self) -> Result<u64, Error> {
self.contracts.block_number().await
}

pub async fn finalized_block_number(&self) -> Result<u64, Error> {
self.contracts.finalized_block_number().await
}

#[allow(clippy::too_many_arguments)]
pub async fn provide_content_message(
&self,
Expand Down Expand Up @@ -258,6 +305,17 @@ where
self.provider.get_block_number().await.map_err(|e| e.into())
}

pub async fn finalized_block_number(&self) -> Result<u64, Error> {
Ok(self
.provider
.get_block_by_number(BlockNumberOrTag::Finalized, false)
.await
.map_err(Error::ErrorInHTTPTransport)?
.ok_or(Error::ErrorFetchingBlock)?
.header
.number)
}

pub async fn fetch_merkle_roots(&self, depth: u64) -> Result<Vec<MerkleRootEntry>, Error> {
let current_block: u64 = self.provider.get_block_number().await?;

Expand Down Expand Up @@ -291,6 +349,33 @@ where
.collect())
}

pub async fn fetch_deposit_events(
&self,
contract_address: Address,
block: u64,
) -> Result<Vec<(IERC20Treasury::Deposit, TxHash)>, Error> {
let filter = Filter::new()
.address(contract_address)
.event_signature(IERC20Treasury::Deposit::SIGNATURE_HASH)
.from_block(block)
.to_block(block);

let event: Event<T, P, IERC20Treasury::Deposit, Ethereum> =
Event::new(self.provider.clone(), filter);

let logs = event.query().await.map_err(Error::ErrorQueryingEvent)?;

logs.into_iter()
.map(|(event, log)| {
Ok((
event,
log.transaction_hash
.ok_or(Error::ErrorFetchingTransaction)?,
))
})
.collect()
}

#[allow(clippy::too_many_arguments)]
pub async fn provide_content_message(
&self,
Expand Down
2 changes: 1 addition & 1 deletion gear-rpc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type GearHeader = sp_runtime::generic::Header<u32, sp_runtime::traits::BlakeTwo2

#[derive(Clone)]
pub struct GearApi {
api: gsdk::Api,
pub api: gsdk::Api,
}

impl GearApi {
Expand Down
1 change: 1 addition & 0 deletions relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ clap.workspace = true
derive_more.workspace = true
dotenv.workspace = true
erc20-relay-client.workspace = true
ethereum-common.workspace = true
futures.workspace = true
gear-core.workspace = true
gclient.workspace = true
Expand Down
Loading
Loading