Skip to content

Commit

Permalink
fix: use default values for symbol and decimals on erc20
Browse files Browse the repository at this point in the history
  • Loading branch information
antomor committed Feb 6, 2024
1 parent b1caf3c commit 403423f
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions src/relayServerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { BigNumber as BigNumberJs } from 'bignumber.js';
import { constants, BigNumber, type BigNumberish } from 'ethers';
import { MAX_ESTIMATED_GAS_DEVIATION } from './definitions/server.const';
import { getProvider } from './Utils';
import { ERC20__factory, PromiseOrValue } from '@rsksmart/rif-relay-contracts';
import {
ERC20,
ERC20__factory,
PromiseOrValue,
} from '@rsksmart/rif-relay-contracts';
import type ExchangeToken from './definitions/token.type';
import {
BigNumberishJs,
Expand Down Expand Up @@ -105,8 +109,8 @@ async function calculateFixedUsdFee(
precision = 18;
} else {
const tokenInstance = ERC20__factory.connect(tokenContract, provider);
symbol = await tokenInstance.symbol();
precision = await tokenInstance.decimals();
symbol = await callERC20Symbol(tokenInstance);
precision = await callERC20Decimals(tokenInstance);
}

const exchangeRate = await getExchangeRate(US_DOLLAR_SYMBOL, symbol);
Expand Down Expand Up @@ -270,6 +274,26 @@ async function validateIfTokenAmountIsAcceptable(
}
}

type ERC20OptionalMethod = 'symbol' | 'decimals' | 'name';

async function callERC20OptionalMethod<T>(
tokenInstance: ERC20,
methodName: ERC20OptionalMethod,
defaultValue: T
): Promise<T> {
return tokenInstance[methodName]
? ((await tokenInstance[methodName]()) as T)
: defaultValue;
}

async function callERC20Symbol(tokenInstance: ERC20, defaultValue = 'ERC20') {
return callERC20OptionalMethod(tokenInstance, 'symbol', defaultValue);
}

async function callERC20Decimals(tokenInstance: ERC20, defaultValue = 18) {
return callERC20OptionalMethod(tokenInstance, 'decimals', defaultValue);
}

async function convertTokenToGas(
tokenAmount: BigNumberishJs,
tokenContract: string,
Expand All @@ -279,11 +303,13 @@ async function convertTokenToGas(
if (tokenContract !== constants.AddressZero) {
const provider = getProvider();
const tokenInstance = ERC20__factory.connect(tokenContract, provider);
const symbol = await callERC20Symbol(tokenInstance, 'ERC20');
const decimals = await callERC20Decimals(tokenInstance, 18);
const token: ExchangeToken = {
instance: tokenInstance,
name: await tokenInstance.name(),
symbol: await tokenInstance.symbol(),
decimals: await tokenInstance.decimals(),
symbol,
decimals,
};

const xRate = await getXRateFor(token);
Expand Down Expand Up @@ -325,8 +351,8 @@ async function convertGasToTokenAndNative(
const token: ExchangeToken = {
instance: tokenInstance,
name: await tokenInstance.name(),
symbol: await tokenInstance.symbol(),
decimals: await tokenInstance.decimals(),
symbol: await callERC20Symbol(tokenInstance),
decimals: await callERC20Decimals(tokenInstance),
};

xRate = await getXRateFor(token);
Expand Down

0 comments on commit 403423f

Please sign in to comment.