Skip to content

Commit

Permalink
Review adjustement + some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Corantin committed Apr 5, 2022
1 parent 0bcbf4b commit 6aadcfc
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/react-app/src/models/token-amount.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { TokenModel } from './token.model';

export type TokenAmountModel = {
parsedAmount: number;
parsedAmountStable?: number; // maybe create new type to handle it or use with PairModel
usdValue?: number; // Only set when fetching from getBalanceOf
token: TokenModel;
};
5 changes: 3 additions & 2 deletions packages/react-app/src/services/quest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,11 @@ export async function getBalanceOf(
const balance = (await erc20Contract.balanceOf(address)) as BigNumber;
tokenInfo.amount = balance.toString();
const { price } = await fetchRoutePairWithStable(tokenInfo.token);
const parsedAmount = fromBigNumber(balance, tokenInfo.decimals);
return {
token: tokenInfo,
parsedAmount: fromBigNumber(balance, tokenInfo.decimals),
parsedAmountStable: +price,
parsedAmount,
usdValue: +price * parsedAmount,
};
}
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions packages/react-app/src/services/uniswap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getNetwork } from 'src/networks';
import { UniswapPairsEntityQuery } from 'src/queries/uniswap-pairs-entity.query';
import { TOKENS } from 'src/constants';
import { arrayDistinct } from 'src/utils/array.util';
import { ChainId, Fetcher, Route } from '@uniswap/sdk';
import { Fetcher, Route } from '@uniswap/sdk';
import Web3 from 'web3';
import { Logger } from '../utils/logger';

Expand Down Expand Up @@ -52,7 +52,7 @@ export async function fetchPairWithStables(tokenA: string): Promise<PairModel[]
}

export async function fetchRoutePairWithStable(tokenA: string) {
const chainId = ChainId.RINKEBY; // TODO We need get the current chain here
const { chainId, isTestNetwork } = getNetwork();

tokenA = Web3.utils.toChecksumAddress(tokenA, chainId);

Expand All @@ -72,7 +72,7 @@ export async function fetchRoutePairWithStable(tokenA: string) {

if (!pairsWithStables || pairsWithStables.length === 0) {
// throw new Error('Token dont have pair with stables knowed');
return { price: 0, priceInverted: 0 };
return { price: isTestNetwork ? 1 : 0, priceInverted: 0 }; // Fallback to 1 for dev or 0 for production
}

const commonStable = pairsWithStables[0].token1?.token;
Expand Down
1 change: 0 additions & 1 deletion packages/react-app/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
declare module '@1hive/1hive-ui';
declare module 'use-wallet';
declare module 'styled-components';
declare module 'coinmarketcap-api';
4 changes: 0 additions & 4 deletions packages/react-app/src/utils/contract.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,4 @@ export function getQuestContractInterface() {
return new ethers.utils.Interface(getContractsJson().Quest.abi);
}

// export function getBalanceCheckerContract() {
// return new ethers.utils.Interface(getContractsJson().Quest.abi);
// }

// #endregion
6 changes: 3 additions & 3 deletions packages/react-app/src/utils/web3.utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BigNumber, ethers, ethers as ethersUtil } from 'ethers';
import { noop } from 'lodash-es';
import { TokenAmountModel } from 'src/models/token-amount.model';
import { getNetwork } from 'src/networks';
import Web3 from 'web3';
import { toWei } from 'web3-utils';
import { IS_DEV } from '../constants';
Expand Down Expand Up @@ -157,11 +158,10 @@ export function fromBigNumber(bigNumber: BigNumber | string, decimals: number |
}

export function getDefaultProvider() {
const { httpProvider } = getNetwork();
let ethOrWeb = (window as any).ethereum ?? (window as any).web3;
if (!ethOrWeb) {
ethOrWeb = new Web3.providers.HttpProvider(
`https://rinkeby.infura.io/v3/${env('INFURA_API_KEY')}`,
);
ethOrWeb = new Web3.providers.HttpProvider(`${httpProvider}/${env('INFURA_API_KEY')}`);
}

return ethOrWeb && new ethersUtil.providers.Web3Provider(ethOrWeb);
Expand Down

0 comments on commit 6aadcfc

Please sign in to comment.