Skip to content

Commit

Permalink
feat(server): add /settings api for settings shared with frontend (#…
Browse files Browse the repository at this point in the history
…637)

* feat(server): create api to return shared settings with client

* feat(ui-ux): add lazy query for settings

* feat(ui-ux): get txn fee from settings api
  • Loading branch information
lykalabrada authored Mar 3, 2023
1 parent 8f4782b commit 934b018
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 5 deletions.
4 changes: 4 additions & 0 deletions apps/server/.env
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ USDC_ADDRESS="0xB200af2b733B831Fbb3d98b13076BC33F605aD58"
WBTC_ADDRESS="0xD723D679d1A3b23d0Aafe4C0812f61DDA84fc043"
ETHEREUM_WALLET_PRIVATE_KEY="0xbdf16a59fe0d5491874778730f7652f753e0c1341722a0dd315a6f83461ac9cb"
APP_VERSION=0.0.0
# Transaction fees
ETH_FEE_PERCENTAGE="0"
DFC_FEE_PERCENTAGE="0.003"
DEFICHAIN_DUST_UTXO="0.001"
# Supported Tokens
SUPPORTED_EVM_TOKENS="WBTC,ETH"
SUPPORTED_DFC_TOKENS="BTC,ETH"
2 changes: 2 additions & 0 deletions apps/server/src/AppConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ export function appConfig() {
network: process.env.DEFICHAIN_NETWORK,
transferFee: process.env.DFC_FEE_PERCENTAGE,
dustUTXO: process.env.DEFICHAIN_DUST_UTXO,
supportedTokens: process.env.SUPPORTED_DFC_TOKENS,
},
ethereum: {
rpcUrl: process.env.ETHEREUM_RPC_URL,
transferFee: process.env.ETH_FEE_PERCENTAGE,
supportedTokens: process.env.SUPPORTED_EVM_TOKENS,
contracts: {
bridgeProxy: {
address: process.env.BRIDGE_PROXY_ADDRESS,
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/AppModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DeFiChainModule } from './defichain/DeFiChainModule';
import { EthereumModule } from './ethereum/EthereumModule';
import { EthersModule } from './modules/EthersModule';
import { HealthModule } from './modules/HealthModule';
import { SettingsModule } from './settings/SettingsModule';
import { VersionModule } from './version/VersionModule';

@Module({
Expand Down Expand Up @@ -38,6 +39,7 @@ import { VersionModule } from './version/VersionModule';
HealthModule,
VersionModule,
BalancesModule,
SettingsModule,
],
controllers: [],
providers: [
Expand Down
28 changes: 28 additions & 0 deletions apps/server/src/settings/SettingsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Controller, Get } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { SupportedDFCTokenSymbols, SupportedEVMTokenSymbols } from 'src/AppConfig';

import { SettingsModel } from './SettingsInterface';

@Controller('settings')
export class SettingsController {
constructor(private configService: ConfigService) {}

@Get()
public getSettings(): SettingsModel {
const supportedDfcTokens = this.configService.getOrThrow('defichain.supportedTokens');
const supportedEvmTokens = this.configService.getOrThrow('ethereum.supportedTokens');
const settings = {
defichain: {
transferFee: this.configService.getOrThrow('defichain.transferFee') as `${number}`,
supportedTokens: supportedDfcTokens.split(',') as Array<keyof typeof SupportedDFCTokenSymbols>,
},
ethereum: {
transferFee: this.configService.getOrThrow('ethereum.transferFee') as `${number}`,
supportedTokens: supportedEvmTokens.split(',') as Array<keyof typeof SupportedEVMTokenSymbols>,
},
};

return settings;
}
}
11 changes: 11 additions & 0 deletions apps/server/src/settings/SettingsInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SupportedDFCTokenSymbols, SupportedEVMTokenSymbols } from 'src/AppConfig';

interface Settings {
transferFee: `${number}` | number;
supportedTokens: Array<keyof typeof SupportedEVMTokenSymbols | keyof typeof SupportedDFCTokenSymbols>;
}

export interface SettingsModel {
defichain: Settings;
ethereum: Settings;
}
8 changes: 8 additions & 0 deletions apps/server/src/settings/SettingsModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';

import { SettingsController } from './SettingsController';

@Module({
controllers: [SettingsController],
})
export class SettingsModule {}
21 changes: 17 additions & 4 deletions apps/web/src/hooks/useTransferFee.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import BigNumber from "bignumber.js";
import { useEffect, useState } from "react";
import { useNetworkContext } from "@contexts/NetworkContext";
import { useLazyBridgeSettingsQuery } from "@store/index";
import { Network } from "types";

/**
* Computes transfer fee
* Any changes to the fee logic can be updated here
*/

const EVM_TO_DFC_FEE_PERCENTAGE = 0;
const DFC_TO_EVM_FEE_PERCENTAGE = 0.003;
export default function useTransferFee(transferAmount: string | number) {
const { selectedNetworkA, selectedTokensA } = useNetworkContext();

const [trigger] = useLazyBridgeSettingsQuery();
const [dfcFee, setDfcFee] = useState<`${number}` | number>(0);
const [evmFee, setEvmFee] = useState<`${number}` | number>(0);

useEffect(() => {
async function getBridgeSettings() {
const { data } = await trigger({});
if (data?.defichain.transferFee) setDfcFee(data?.defichain.transferFee);
if (data?.ethereum.transferFee) setEvmFee(data?.ethereum.transferFee);
}
getBridgeSettings();
}, [selectedTokensA]);

const isSendingFromEvm = selectedNetworkA.name === Network.Ethereum;
const feeSymbol = selectedTokensA.tokenA.name;
const fee = new BigNumber(transferAmount || 0).multipliedBy(
isSendingFromEvm ? EVM_TO_DFC_FEE_PERCENTAGE : DFC_TO_EVM_FEE_PERCENTAGE
isSendingFromEvm ? evmFee : dfcFee
);

return [fee, feeSymbol];
Expand Down
8 changes: 7 additions & 1 deletion apps/web/src/store/defichain.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createApi, fetchBaseQuery, retry } from "@reduxjs/toolkit/query/react";
import { FetchArgs } from "@reduxjs/toolkit/dist/query/fetchBaseQuery";
import { AddressDetails, BridgeVersion } from "types";
import { AddressDetails, BridgeSettings, BridgeVersion } from "types";
import { HttpStatusCode } from "axios";

const staggeredBaseQueryWithBailOut = retry(
Expand Down Expand Up @@ -109,6 +109,12 @@ export const bridgeApi = createApi({
method: "GET",
}),
}),
bridgeSettings: builder.query<BridgeSettings, any>({
query: ({ baseUrl }) => ({
url: `${baseUrl}/settings`,
method: "GET",
}),
}),
allocateDfcFund: builder.mutation<{ transactionHash: string }, any>({
query: ({ baseUrl, txnHash }) => ({
url: `${baseUrl}/${PATH_ETHEREUM}/allocateDFCFund`,
Expand Down
3 changes: 3 additions & 0 deletions apps/web/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const useLazyBridgeStatusQuery = () =>
useWrappedLazyQuery(bridgeApi.useLazyBridgeStatusQuery, true);
const useLazyBridgeVersionQuery = () =>
useWrappedLazyQuery(bridgeApi.useLazyBridgeVersionQuery);
const useLazyBridgeSettingsQuery = () =>
useWrappedLazyQuery(bridgeApi.useLazyBridgeSettingsQuery);

export {
useGenerateAddressMutation,
Expand All @@ -32,5 +34,6 @@ export {
useBalanceDfcMutation,
useLazyBridgeStatusQuery,
useLazyBridgeVersionQuery,
useLazyBridgeSettingsQuery,
bridgeApi,
};
10 changes: 10 additions & 0 deletions apps/web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export interface BridgeVersion {
v: string;
}

interface Settings {
transferFee: `${number}` | number;
supportedTokens: string[];
}

export interface BridgeSettings {
defichain: Settings;
ethereum: Settings;
}

export enum SelectionType {
"Network" = "Network",
"Token" = "Token",
Expand Down

0 comments on commit 934b018

Please sign in to comment.