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: mayan integration #322

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions Cargo.lock

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

59 changes: 30 additions & 29 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,42 @@ documentation = "https://github.com/gemwalletcom"
[workspace]
resolver = "2"
members = [
"apps/api",
"apps/daemon",
"apps/parser",
"apps/setup",
"apps/api",
"apps/daemon",
"apps/parser",
"apps/setup",

"bin/img-downloader",
"bin/generate",
"bin/img-downloader",
"bin/generate",

"bin/uniffi-bindgen",
"gemstone",
"bin/uniffi-bindgen",
"gemstone",

"crates/primitives",
"crates/blockchain",
"crates/fiat",
"crates/cacher",
"crates/name_resolver",
"crates/api_connector",
"crates/settings",
"crates/settings_chain",
"crates/pricer",
"crates/chain_primitives",
"crates/primitives",
"crates/blockchain",
"crates/fiat",
"crates/cacher",
"crates/name_resolver",
"crates/api_connector",
"crates/settings",
"crates/settings_chain",
"crates/pricer",
"crates/chain_primitives",

"crates/security_*",
"crates/gem_*",
"crates/security_*",
"crates/gem_*",

"crates/localizer",
"crates/job_runner",
"crates/localizer",
"crates/job_runner",
]

default-members = [
"apps/api",
"apps/daemon",
"apps/parser",
"apps/setup",
"bin/generate",
"gemstone",
"apps/api",
"apps/daemon",
"apps/parser",
"apps/setup",
"bin/generate",
"gemstone",
]

[workspace.dependencies]
Expand Down Expand Up @@ -73,6 +73,7 @@ lazy_static = "1.4.0"
futures-util = "0.3.30"
uuid = { version = "1.8.0", features = ["v4"] }
rand = { version = "0.8.5" }
rand_core = { version = "0.6.4" }

# db
diesel = { version = "2.2.3", features = ["postgres", "chrono", "serde_json"] }
Expand Down Expand Up @@ -116,5 +117,5 @@ rust-embed = { version = "8.5.0" }

# numbers
rusty-money = { git = "https://github.com/varunsrin/rusty_money.git", rev = "bbc0150", features = [
"iso",
"iso",
] }
4 changes: 4 additions & 0 deletions crates/gem_evm/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ impl FromStr for EthereumAddress {
impl EthereumAddress {
pub const LEN: usize = 20;

pub fn zero() -> Self {
Self { bytes: vec![0u8; Self::LEN] }
}

pub fn parse(str: &str) -> Option<EthereumAddress> {
Self::from_str(str).ok()
}
Expand Down
16 changes: 16 additions & 0 deletions crates/gem_evm/src/jsonrpc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloy_primitives::U256;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -35,6 +36,17 @@ impl TransactionObject {
data: format!("0x{}", hex::encode(data)),
}
}

pub fn new_call_with_value(from: &str, to: &str, data: Vec<u8>, value: &str) -> Self {
Self {
from: Some(from.to_string()),
to: to.to_string(),
gas: None,
gas_price: None,
value: Some(value.to_string()),
data: format!("0x{}", hex::encode(data)),
}
}
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -73,6 +85,8 @@ pub enum EthereumRpc {
GasPrice,
GetBalance(&'static str),
Call(TransactionObject, BlockParameter),
GetTransactionReceipt(String),
EstimateGas(TransactionObject),
}

impl EthereumRpc {
Expand All @@ -81,6 +95,8 @@ impl EthereumRpc {
EthereumRpc::GasPrice => "eth_gasPrice",
EthereumRpc::GetBalance(_) => "eth_getBalance",
EthereumRpc::Call(_, _) => "eth_call",
EthereumRpc::GetTransactionReceipt(_) => "eth_getTransactionReceipt",
EthereumRpc::EstimateGas(_) => "eth_estimateGas",
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/gem_evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pub mod erc20;
pub mod erc2612;
pub mod jsonrpc;
pub mod lido;
pub mod mayan;
pub mod permit2;
pub mod uniswap;
69 changes: 69 additions & 0 deletions crates/gem_evm/src/mayan/fee_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use alloy_core::sol;
use alloy_primitives::{Address, U256};

sol! {
/// @notice Fee Manager interface for managing protocol fees and treasury operations
#[derive(Debug, PartialEq)]
interface IFeeManager {
/// @notice Calculates the protocol fee in basis points
/// @param amountIn The input amount for the swap
/// @param tokenIn The input token address
/// @param tokenOut The output token identifier
/// @param destChain The destination chain identifier
/// @param referrerBps The referrer's basis points
/// @return The protocol fee in basis points
function calcProtocolBps(
uint64 amountIn,
address tokenIn,
bytes32 tokenOut,
uint16 destChain,
uint8 referrerBps
) external view returns (uint8);

/// @notice Returns the current fee collector address
/// @return The address of the fee collector (treasury or contract)
function feeCollector() external view returns (address);

/// @notice Changes the operator to a new address
/// @param nextOperator The address of the new operator
function changeOperator(address nextOperator) external;

/// @notice Allows the next operator to claim the operator role
function claimOperator() external;

/// @notice Sweeps ERC20 tokens from the contract
/// @param token The token address to sweep
/// @param amount The amount to sweep
/// @param to The recipient address
function sweepToken(address token, uint256 amount, address to) external;

/// @notice Sweeps ETH from the contract
/// @param amount The amount of ETH to sweep
/// @param to The recipient address
function sweepEth(uint256 amount, address payable to) external;

/// @notice Sets the base fee in basis points
/// @param baseBps The new base fee in basis points
function setBaseBps(uint8 baseBps) external;

/// @notice Sets the treasury address
/// @param treasury The new treasury address
function setTreasury(address treasury) external;
}

/// @notice Fee Manager contract state and events
#[derive(Debug, PartialEq)]
contract FeeManager {
/// @notice The current operator address
address public operator;

/// @notice The next operator address
address public nextOperator;

/// @notice The base fee in basis points
uint8 public baseBps;

/// @notice The treasury address
address public treasury;
}
}
88 changes: 88 additions & 0 deletions crates/gem_evm/src/mayan/forwarder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use alloy_core::sol;

sol! {
/// @title MayanForwarder Interface
#[derive(Debug, PartialEq)]
interface IMayanForwarder {
/// @notice Guardian address
function guardian() external view returns (address);

/// @notice Next guardian address
function nextGuardian() external view returns (address);

/// @notice Check if protocol is supported for swaps
function swapProtocols(address protocol) external view returns (bool);

/// @notice Check if protocol is supported for Mayan operations
function mayanProtocols(address protocol) external view returns (bool);

/// @notice Forward ETH to Mayan protocol
function forwardEth(address mayanProtocol, bytes calldata protocolData) external payable;

/// @notice Forward ERC20 tokens to Mayan protocol
function forwardERC20(
address tokenIn,
uint256 amountIn,
PermitParams calldata permitParams,
address mayanProtocol,
bytes calldata protocolData
) external payable;

/// @notice Swap ETH to token and forward to Mayan protocol
function swapAndForwardEth(
uint256 amountIn,
address swapProtocol,
bytes calldata swapData,
address middleToken,
uint256 minMiddleAmount,
address mayanProtocol,
bytes calldata mayanData
) external payable;

/// @notice Swap ERC20 token and forward to Mayan protocol
function swapAndForwardERC20(
address tokenIn,
uint256 amountIn,
PermitParams calldata permitParams,
address swapProtocol,
bytes calldata swapData,
address middleToken,
uint256 minMiddleAmount,
address mayanProtocol,
bytes calldata mayanData
) external payable;

/// @notice Rescue ERC20 tokens
function rescueToken(address token, uint256 amount, address to) external;

/// @notice Rescue ETH
function rescueEth(uint256 amount, address payable to) external;

/// @notice Change guardian
function changeGuardian(address newGuardian) external;

/// @notice Claim guardian role
function claimGuardian() external;

/// @notice Set swap protocol status
function setSwapProtocol(address swapProtocol, bool enabled) external;

/// @notice Set Mayan protocol status
function setMayanProtocol(address mayanProtocol, bool enabled) external;

/// Events
event ForwardedEth(address mayanProtocol, bytes protocolData);
event ForwardedERC20(address token, uint256 amount, address mayanProtocol, bytes protocolData);
event SwapAndForwardedEth(uint256 amountIn, address swapProtocol, address middleToken, uint256 middleAmount, address mayanProtocol, bytes mayanData);
event SwapAndForwardedERC20(address tokenIn, uint256 amountIn, address swapProtocol, address middleToken, uint256 middleAmount, address mayanProtocol, bytes mayanData);

/// Structs
struct PermitParams {
uint256 value;
uint256 deadline;
uint8 v;
bytes32 r;
bytes32 s;
}
}
}
3 changes: 3 additions & 0 deletions crates/gem_evm/src/mayan/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod fee_manager;
pub mod forwarder;
pub mod swift;
Loading