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: load shield fees instead of hardcoding it #78

Merged
merged 5 commits into from
Feb 2, 2023
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
14 changes: 12 additions & 2 deletions components/ReviewTransactionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
import { Tooltip } from '@chakra-ui/tooltip';
import { ethers } from 'ethers';
import { parseUnits } from 'ethers/lib/utils.js';
import { useNetwork } from 'wagmi';
import { TokenListContextItem } from '@/contexts/TokenContext';
import useNotifications from '@/hooks/useNotifications';
import { useRailgunProvider } from '@/hooks/useRailgunProvider';
import useRailgunTx from '@/hooks/useRailgunTx';
import { shortenAddress } from '@/utils/address';

Expand All @@ -36,8 +38,16 @@ const ReviewTransactionModal = ({
}: ReviewTransactionModalProps) => {
const { txNotify, notifyUser } = useNotifications();
const { shield, isShielding } = useRailgunTx();
const bigNumberAmount = parseUnits(amount! || '0', token?.decimals);
const feeAmount = parseUnits(amount! || '0', token?.decimals).div('400');
const { shieldingFees } = useRailgunProvider();
const { chain } = useNetwork();
const tokenAmount = amount;
const tokenDecimals = token?.decimals;

const bigNumberAmount = parseUnits(tokenAmount! || '0', tokenDecimals);
const shieldFee = shieldingFees[chain?.id || 1];
const feeAmount = parseUnits(tokenAmount! || '0', tokenDecimals)
wildmolasses marked this conversation as resolved.
Show resolved Hide resolved
.mul(shieldFee)
.div(10000);

const doSubmit: React.FormEventHandler = async () => {
if (!token.address || !amount || !token?.decimals || !recipient) throw new Error('bad form');
Expand Down
29 changes: 27 additions & 2 deletions hooks/useRailgunProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
import { useEffect, useState } from 'react';
import { setProviderForNetwork } from '@railgun-community/quickstart';
import { BigNumber } from 'ethers';
import { useNetwork, useProvider } from 'wagmi';
import { networks } from '@/utils/networks';
import { loadProviders } from '@/utils/railgun';

// Fee is in bips, e.g. a value of 25 is a 0.25% fee.
interface ShieldFee {
[chainId: number]: BigNumber;
}

const fallbackShieldingFees: ShieldFee = {};
Object.keys(networks).forEach((chainId) => {
// Current fees are 0.25% everywhere, so we initialize with that
fallbackShieldingFees[Number(chainId)] = BigNumber.from('25');
});

export const useRailgunProvider = () => {
const [isProviderLoaded, setProviderLoaded] = useState<Boolean>(false);
const [shieldingFees, setShieldingFees] = useState<ShieldFee>(fallbackShieldingFees);
const { chain } = useNetwork();
const network = networks[chain?.id || 1];
const provider = useProvider();

useEffect(() => {
const fn = async () => {
setProviderLoaded(false);
await loadProviders();
const res = await loadProviders();

// Set the shield fees for each network.
const shieldingFeesFromNetwork = res.reduce((acc, response) => {
const newFee = response.providerInfo.feesSerialized?.shield;
return {
...acc,
[response.chainId]: BigNumber.from(newFee || fallbackShieldingFees[response.chainId]),
};
}, {});
setShieldingFees(shieldingFeesFromNetwork);

// Provider is done loading.
alexkeating marked this conversation as resolved.
Show resolved Hide resolved
setProviderLoaded(true);
};
fn();
Expand All @@ -23,5 +48,5 @@ export const useRailgunProvider = () => {
setProviderForNetwork(network.railgunNetworkName, provider);
}, [provider, network.railgunNetworkName]);

return { isProviderLoaded };
return { isProviderLoaded, shieldingFees };
};
9 changes: 7 additions & 2 deletions utils/railgun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ export const loadProviders = async () => {
// Whether to forward debug logs from Fallback Provider.
const shouldDebug = true;
return Promise.all(
Object.values(networks).map(({ railgunNetworkName, fallbackProviders }) => {
return loadProvider(fallbackProviders, railgunNetworkName, shouldDebug);
Object.keys(networks).map(async (chainIdString) => {
const chainId = Number(chainIdString);
const { railgunNetworkName, fallbackProviders } = networks[chainId];
return {
chainId,
providerInfo: await loadProvider(fallbackProviders, railgunNetworkName, shouldDebug),
};
})
);
};
Expand Down