Skip to content

Commit

Permalink
relayer fee configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
salman01zp committed Aug 9, 2023
1 parent 3f5fc25 commit 4fd8689
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 36 deletions.
8 changes: 4 additions & 4 deletions crates/relayer-config/src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ pub struct EvmChainConfig {
/// TxQueue configuration
#[serde(skip_serializing, default)]
pub tx_queue: TxQueueConfig,
/// TxWithdrawFee configuration
/// Relayer fee configuration
#[serde(skip_serializing, default)]
pub withdraw_fee_config: WithdrawFeeConfig,
pub relayer_fee_config: RelayerFeeConfig,
/// Block poller/listening configuration
#[serde(skip_serializing, default)]
pub block_poller: Option<BlockPollerConfig>,
Expand All @@ -82,14 +82,14 @@ pub struct EvmChainConfig {
/// Transaction withdraw fee configuration.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all(serialize = "camelCase", deserialize = "kebab-case"))]
pub struct WithdrawFeeConfig {
pub struct RelayerFeeConfig {
/// Relayer profit percent per transaction fee for relaying
pub relayer_profit_percent: f64,
/// Maximum refund amount per transaction relaying
pub max_refund_amount: f64,
}

impl Default for WithdrawFeeConfig {
impl Default for RelayerFeeConfig {
fn default() -> Self {
Self {
relayer_profit_percent: 5.,
Expand Down
22 changes: 11 additions & 11 deletions crates/tx-relay/src/evm/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use webb::evm::ethers::utils::{format_units, parse_units};
use webb_chains_info::chain_info_by_chain_id;
use webb_price_oracle_backends::PriceBackend;
use webb_proposals::TypedChainId;
use webb_relayer_config::evm::WithdrawFeeConfig;
use webb_relayer_config::evm::RelayerFeeConfig;
use webb_relayer_context::RelayerContext;
use webb_relayer_utils::Result;

Expand Down Expand Up @@ -97,7 +97,7 @@ pub async fn get_evm_fee_info(
// Need to recalculate estimated fee with the gas amount that was passed in. We use
// cached exchange rate so that this matches calculation on the client.
fee_info.estimated_fee = calculate_transaction_fee(
&chain_config.withdraw_fee_config,
&chain_config.relayer_fee_config,
fee_info.gas_price,
gas_amount,
fee_info.native_token_price,
Expand All @@ -107,7 +107,7 @@ pub async fn get_evm_fee_info(
// Recalculate max refund in case relayer balance changed.
fee_info.max_refund = max_refund(
chain_id,
&chain_config.withdraw_fee_config,
&chain_config.relayer_fee_config,
fee_info.native_token_price,
fee_info.native_token_decimals,
ctx,
Expand All @@ -117,7 +117,7 @@ pub async fn get_evm_fee_info(
} else {
let fee_info = generate_fee_info(
chain_id,
&chain_config.withdraw_fee_config,
&chain_config.relayer_fee_config,
vanchor,
gas_amount,
ctx,
Expand All @@ -136,7 +136,7 @@ pub async fn get_evm_fee_info(
/// Generate new fee info by fetching relevant data from remote APIs and doing calculations.
async fn generate_fee_info(
chain_id: TypedChainId,
withdraw_fee_config: &WithdrawFeeConfig,
relayer_fee_config: &RelayerFeeConfig,
vanchor: Address,
gas_amount: U256,
ctx: &RelayerContext,
Expand Down Expand Up @@ -179,7 +179,7 @@ async fn generate_fee_info(
.await?;

let estimated_fee = calculate_transaction_fee(
withdraw_fee_config,
relayer_fee_config,
gas_price,
gas_amount,
native_token_price,
Expand All @@ -200,7 +200,7 @@ async fn generate_fee_info(
refund_exchange_rate,
max_refund: max_refund(
chain_id,
withdraw_fee_config,
relayer_fee_config,
native_token_price,
native_token_decimals,
ctx,
Expand All @@ -216,7 +216,7 @@ async fn generate_fee_info(

async fn max_refund(
chain_id: TypedChainId,
withdraw_fee_config: &WithdrawFeeConfig,
relayer_fee_config: &RelayerFeeConfig,
native_token_price: f64,
native_token_decimals: u8,
ctx: &RelayerContext,
Expand All @@ -226,7 +226,7 @@ async fn max_refund(
let relayer_balance = provider.get_balance(wallet.address(), None).await?;

// Get the maximum refund amount in USD from the config.
let max_refund_amount = withdraw_fee_config.max_refund_amount;
let max_refund_amount = relayer_fee_config.max_refund_amount;

// Calculate the maximum refund amount per relay transaction in `nativeToken`.
// Ensuring that refund <= relayer balance
Expand All @@ -243,7 +243,7 @@ async fn max_refund(
///
/// The algorithm is explained at https://www.notion.so/hicommonwealth/Private-Tx-Relay-Support-v1-f5522b04d6a349aab1bbdb0dd83a7fb4#6bb2b4920e3f42d69988688c6fa54e6e
fn calculate_transaction_fee(
withdraw_fee_config: &WithdrawFeeConfig,
relayer_fee_config: &RelayerFeeConfig,
gas_price: U256,
gas_amount: U256,
native_token_price: f64,
Expand All @@ -261,7 +261,7 @@ fn calculate_transaction_fee(
// Step 3: Calculate the profit that the relayer should make, and add it to the tx fee in USD.
// This is the total amount of USD that the relayer should receive.
let relay_tx_fee =
(withdraw_fee_config.relayer_profit_percent / 100.0) * tx_fee_usd;
(relayer_fee_config.relayer_profit_percent / 100.0) * tx_fee_usd;
let total_fee_with_profit_in_usd = tx_fee_usd + relay_tx_fee;
// Step 4: Convert the total fee to `wrappedToken` using the exchange rate for the underlying
// wrapped token.
Expand Down
24 changes: 12 additions & 12 deletions tests/lib/localTestnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ import {
import {
ChainInfo,
Contract,
defaultWithdrawConfigValue,
defaultRelayerFeeConfigValue,
EnabledContracts,
EventsWatcher,
LinkedAnchor,
ProposalSigningBackend,
SmartAnchorUpdatesConfig,
WithdrawFeeConfig,
RelayerFeeConfig,
} from './webbRelayer';
import { ConvertToKebabCase } from './tsHacks';
import { hexToU8a, u8aToHex } from '@polkadot/util';
Expand All @@ -56,7 +56,7 @@ export type GanacheAccounts = {
export type ExportedConfigOptions = {
signatureVBridge?: VBridge<VAnchor>;
proposalSigningBackend?: ProposalSigningBackend;
withdrawConfig?: WithdrawFeeConfig;
relayerFeeConfig?: RelayerFeeConfig;
relayerWallet?: Wallet;
linkedAnchors?: LinkedAnchor[];
blockConfirmations?: number;
Expand Down Expand Up @@ -483,7 +483,7 @@ export class LocalChain {
privateKey: (wallet as ethers.Wallet).privateKey,
contracts: contracts,
txQueue: opts.txQueueConfig ?? defaultEvmTxQueueConfig,
withdrawFeeConfig: opts.withdrawConfig ?? defaultWithdrawConfigValue,
relayerFeeConfig: opts.relayerFeeConfig ?? defaultRelayerFeeConfigValue,
};
return chainInfo;
}
Expand All @@ -502,7 +502,7 @@ export class LocalChain {
privateKey: opts.privateKey ?? '',
contracts: [],
txQueue: defaultEvmTxQueueConfig,
withdrawFeeConfig: defaultWithdrawConfigValue,
relayerFeeConfig: defaultRelayerFeeConfigValue,
};
for (const contract of this.opts.enabledContracts) {
if (contract.contract == 'VAnchor') {
Expand Down Expand Up @@ -533,14 +533,14 @@ export class LocalChain {
'smart-anchor-updates'?: ConvertToKebabCase<SmartAnchorUpdatesConfig>;
};
type ConvertedTxQueueConfig = ConvertToKebabCase<TxQueueConfig>;
type ConvertedWithdrawFeeConfig = ConvertToKebabCase<WithdrawFeeConfig>;
type ConvertedRelayerFeeConfig = ConvertToKebabCase<RelayerFeeConfig>;
type ConvertedConfig = Omit<
ConvertToKebabCase<typeof config>,
'contracts' | 'tx-queue' | 'withdraw-fee-config'
'contracts' | 'tx-queue' | 'relayer-fee-config'
> & {
contracts: ConvertedContract[];
'tx-queue': ConvertedTxQueueConfig;
'withdraw-fee-config'?: ConvertedWithdrawFeeConfig;
'relayer-fee-config'?: ConvertedRelayerFeeConfig;
};
type FullConfigFile = {
evm: {
Expand All @@ -562,9 +562,9 @@ export class LocalChain {
'max-sleep-interval': config.txQueue.maxSleepInterval,
'polling-interval': config.txQueue.pollingInterval,
},
'withdraw-fee-config': {
'relayer-fee-percent': config.withdrawFeeConfig.relayerFeePercent,
'max-refund-amount': config.withdrawFeeConfig.maxRefundAmount,
'relayer-fee-config': {
'relayer-fee-percent': config.relayerFeeConfig.relayerFeePercent,
'max-refund-amount': config.relayerFeeConfig.maxRefundAmount,
},
contracts: config.contracts.map(
(contract): ConvertedContract => ({
Expand Down Expand Up @@ -686,7 +686,7 @@ export type FullChainInfo = ChainInfo & {
privateKey: string;
blockConfirmations: number;
txQueue: TxQueueConfig;
withdrawFeeConfig: WithdrawFeeConfig;
relayerFeeConfig: RelayerFeeConfig;
};

export async function setupVanchorEvmTx(
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/webbRelayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ export interface EvmEtherscanConfig {
[key: string]: EtherscanApiConfig;
}

export interface WithdrawFeeConfig {
export interface RelayerFeeConfig {
relayerFeePercent: number;
maxRefundAmount: number;
}
Expand Down Expand Up @@ -552,7 +552,7 @@ export interface EnabledContracts {
}

// Default WithdrawlFeeConfig for the tx relaying.
export const defaultWithdrawConfigValue: WithdrawFeeConfig = {
export const defaultRelayerFeeConfigValue: RelayerFeeConfig = {
maxRefundAmount: 0,
relayerFeePercent: 0,
};
Expand Down
4 changes: 2 additions & 2 deletions tests/sim/smartAnchorUpdates.sim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { BigNumber, ethers } from 'ethers';
import temp from 'temp';
import { LocalChain } from '../lib/localTestnet.js';
import {
defaultWithdrawConfigValue,
defaultRelayerFeeConfigValue,
EnabledContracts,
LinkedAnchor,
WebbRelayer,
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('Smart Anchor Updates Simulation', function () {
const path = `${tmpDirPath}/${chain.name}.json`;
await chain.writeConfig(path, {
signatureVBridge,
withdrawConfig: defaultWithdrawConfigValue,
relayerFeeConfig: defaultRelayerFeeConfigValue,
relayerWallet: relayerWallet,
linkedAnchors: myLinkedAnchors,
smartAnchorUpdates: {
Expand Down
4 changes: 2 additions & 2 deletions tests/test/evm/relayerTxTransfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { BigNumber, ethers } from 'ethers';
import temp from 'temp';
import { LocalChain } from '../../lib/localTestnet.js';
import {
defaultWithdrawConfigValue,
defaultRelayerFeeConfigValue,
EnabledContracts,
EvmFeeInfo,
EvmEtherscanConfig,
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('Relayer transfer assets', function () {
// save the chain configs.
await localChain1.writeConfig(`${tmpDirPath}/${localChain1.name}.json`, {
signatureVBridge,
withdrawConfig: defaultWithdrawConfigValue,
relayerFeeConfig: defaultRelayerFeeConfigValue,
relayerWallet: relayerWallet1,
});

Expand Down
6 changes: 3 additions & 3 deletions tests/test/evm/vanchorPrivateTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { BigNumber, ethers } from 'ethers';
import temp from 'temp';
import { LocalChain, setupVanchorEvmTx } from '../../lib/localTestnet.js';
import {
defaultWithdrawConfigValue,
defaultRelayerFeeConfigValue,
EnabledContracts,
EvmFeeInfo,
EvmEtherscanConfig,
Expand Down Expand Up @@ -152,13 +152,13 @@ describe('Vanchor Private Tx relaying with mocked governor', function () {
// save the chain configs.
await localChain1.writeConfig(`${tmpDirPath}/${localChain1.name}.json`, {
signatureVBridge,
withdrawConfig: defaultWithdrawConfigValue,
relayerFeeConfig: defaultRelayerFeeConfigValue,
relayerWallet: relayerWallet1,
txQueueConfig: { maxSleepInterval: 1500, pollingInterval: 7000 },
});
await localChain2.writeConfig(`${tmpDirPath}/${localChain2.name}.json`, {
signatureVBridge,
withdrawConfig: defaultWithdrawConfigValue,
relayerFeeConfig: defaultRelayerFeeConfigValue,
relayerWallet: relayerWallet2,
txQueueConfig: { maxSleepInterval: 1500, pollingInterval: 7000 },
});
Expand Down

0 comments on commit 4fd8689

Please sign in to comment.