Skip to content

Commit

Permalink
refactor: Move fee estimation to core (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben authored Feb 3, 2022
1 parent 69f4d1a commit bb2c0a4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 66 deletions.
13 changes: 10 additions & 3 deletions core/src/auctions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Auction, AuctionInitialInfo, Notifier } from './types';
import type { Auction, AuctionInitialInfo, AuctionTransaction, Notifier } from './types';
import BigNumber from './bignumber';
import fetchAuctionsByCollateralType, { fetchAuctionStatus } from './fetch';
import { getExchangeRateBySymbol, getUniswapCalleeBySymbol, getUniswapParametersByCollateral } from './uniswap';
Expand All @@ -14,6 +14,7 @@ import {
import { getSupportedCollateralTypes } from './addresses';
import { getClipperNameByCollateralType } from './contracts';
import convertNumberTo32Bytes from './helpers/convertNumberTo32Bytes';
import { enrichAuctionWithTransactionFees, getApproximateTransactionFees } from './fees';

const enrichAuctionWithActualNumbers = async function (
network: string,
Expand Down Expand Up @@ -107,7 +108,7 @@ export const fetchAllInitialAuctions = async function (network: string): Promise
return auctionGroups.flat();
};

export const fetchAllAuctions = async function (network: string): Promise<Auction[]> {
export const fetchAllAuctions = async function (network: string): Promise<AuctionTransaction[]> {
const auctions = await fetchAllInitialAuctions(network);

// enrich them with statuses
Expand All @@ -120,7 +121,13 @@ export const fetchAllAuctions = async function (network: string): Promise<Auctio
const auctionsWithPriceDrop = await Promise.all(auctionsWithStatuses.map(enrichAuctionWithPriceDrop));

// enrich them with market values
return await Promise.all(auctionsWithPriceDrop.map(a => enrichAuctionWithMarketValues(a, network)));
const auctionsWithMarketValue = await Promise.all(
auctionsWithPriceDrop.map(a => enrichAuctionWithMarketValues(a, network))
);

// enrich with profit and fee calculation
const fees = await getApproximateTransactionFees(network);
return await Promise.all(auctionsWithMarketValue.map(a => enrichAuctionWithTransactionFees(a, fees)));
};

export const restartAuction = async function (
Expand Down
10 changes: 9 additions & 1 deletion core/src/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ import { ethers } from 'ethers';
import BigNumber from './bignumber';
import { getNetworkConfigByType } from './constants/NETWORKS';
import { ETH_NUMBER_OF_DIGITS } from './constants/UNITS';
import memoizee from 'memoizee';

const API_URL = 'https://ethgasstation.info/json/ethgasAPI.json';
const TRANSACTION_SPEED = 'fast';

const getCurrentGasPrice = async function (): Promise<BigNumber> {
const GAS_CACHE = 10 * 1000;

const _getCurrentGasPrice = async function (): Promise<BigNumber> {
const gasData = await fetch(API_URL).then(r => r.json());
const gasPriceString = ethers.utils.formatUnits(gasData[TRANSACTION_SPEED] / 10, 'gwei');
return new BigNumber(gasPriceString);
};

const getCurrentGasPrice = memoizee(_getCurrentGasPrice, {
maxAge: GAS_CACHE,
promise: true,
});

export const getGasPrice = async function (network: string): Promise<BigNumber> {
const networkConfig = getNetworkConfigByType(network);
if (networkConfig.gasPrice) {
Expand Down
27 changes: 13 additions & 14 deletions frontend/store/auctions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Auction, AuctionTransaction, TransactionFees } from 'auctions-core/src/types';
import type { Auction, AuctionTransaction } from 'auctions-core/src/types';
import { ActionContext } from 'vuex';
import { message } from 'ant-design-vue';
import {
Expand All @@ -8,7 +8,6 @@ import {
enrichAuctionWithPriceDropAndMarketValue,
} from 'auctions-core/src/auctions';
import { checkAllExchangeRates } from 'auctions-core/src/uniswap';
import { enrichAuctionWithTransactionFees } from 'auctions-core/src/fees';
import { checkAllCalcParameters } from 'auctions-core/src/params';
import { checkAllSupportedCollaterals } from 'auctions-core/src/addresses';
import getWallet from '~/lib/wallet';
Expand All @@ -21,7 +20,7 @@ let refetchIntervalId: ReturnType<typeof setInterval> | undefined;
let updateAuctionsPricesIntervalId: ReturnType<typeof setInterval> | undefined;

interface State {
auctionStorage: Record<string, Auction>;
auctionStorage: Record<string, AuctionTransaction>;
isFetching: boolean;
isBidding: boolean;
error: string | null;
Expand All @@ -40,11 +39,8 @@ export const getters = {
listAuctions(state: State) {
return Object.values(state.auctionStorage);
},
listAuctionTransactions(state: State, getters: any, _rootState: any, rootGetters: any): AuctionTransaction[] {
const fees: TransactionFees = rootGetters['fees/fees'];
const auctions = Object.values(state.auctionStorage).map(auction =>
enrichAuctionWithTransactionFees(auction, fees)
);
listAuctionTransactions(state: State, getters: any, _rootState: any): AuctionTransaction[] {
const auctions = Object.values(state.auctionStorage);
auctions.forEach(auction => {
auction.isRestarting = getters.isAuctionRestarting(auction.id);
});
Expand All @@ -68,11 +64,14 @@ export const getters = {
};

export const mutations = {
setAuctions(state: State, auctions: Auction[]) {
const newAuctionStorage = auctions.reduce((auctionStorage: Record<string, Auction>, auction: Auction) => {
auctionStorage[auction.id] = auction;
return auctionStorage;
}, {});
setAuctions(state: State, auctions: AuctionTransaction[]) {
const newAuctionStorage = auctions.reduce(
(auctionStorage: Record<string, AuctionTransaction>, auction: AuctionTransaction) => {
auctionStorage[auction.id] = auction;
return auctionStorage;
},
{}
);
const finishedAuctionIds = Object.keys(state.auctionStorage).filter(auctionID => {
return !newAuctionStorage[auctionID];
});
Expand All @@ -85,7 +84,7 @@ export const mutations = {
...newAuctionStorage,
};
},
setAuction(state: State, auction: Auction) {
setAuction(state: State, auction: AuctionTransaction) {
state.auctionStorage[auction.id] = auction;
},
setAuctionFinish(state: State, { id, transactionAddress }: { id: string; transactionAddress: string }) {
Expand Down
48 changes: 0 additions & 48 deletions frontend/store/fees.ts

This file was deleted.

0 comments on commit bb2c0a4

Please sign in to comment.