Skip to content

Commit

Permalink
refactor: remove setting of total swap costs
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Aug 25, 2021
1 parent b8d7a7e commit b1b6eab
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 54 deletions.
13 changes: 0 additions & 13 deletions src/swapCostCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ export class SwapCostCalculator {
private tokenPriceCache: Record<string, string> = {
ZERO_ADDRESS: BONE.toString(),
};
private swapCostOverride: Record<string, string> = {};

private initializeCache(): void {
this.tokenPriceCache = {
ZERO_ADDRESS: BONE.toString(),
[WETHADDR[this.chainId].toLowerCase()]: BONE.toString(),
};
this.swapCostOverride = {};
}

constructor(chainId: number) {
Expand Down Expand Up @@ -82,14 +80,6 @@ export class SwapCostCalculator {
this.tokenPriceCache[tokenAddress.toLowerCase()] = tokenPrice;
}

/**
* We only include this function as a hack as the tests expect to be able to set the swap fee directly
* @deprecated Use `setNativeAssetPriceInToken`
*/
setSwapCostOverride(tokenAddress: string, swapCost: string): void {
this.swapCostOverride[tokenAddress.toLowerCase()] = swapCost;
}

/**
* Calculate the cost of spending a certain amount of gas in terms of a token.
* This allows us to determine whether an increased amount of tokens gained
Expand All @@ -106,9 +96,6 @@ export class SwapCostCalculator {
gasPriceWei: BigNumber,
swapGasCost: BigNumber
): Promise<BigNumber> {
if (this.swapCostOverride[tokenAddress.toLowerCase()]) {
return bnum(this.swapCostOverride[tokenAddress.toLowerCase()]);
}
return calculateTotalSwapCost(
await this.getNativeAssetPriceInToken(tokenAddress, tokenDecimals),
swapGasCost,
Expand Down
25 changes: 1 addition & 24 deletions src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class SOR {
disabledOptions: DisabledOptions;

private poolCacher: PoolCacher;
private swapCostCalculator: SwapCostCalculator;
swapCostCalculator: SwapCostCalculator;

constructor(
provider: BaseProvider,
Expand Down Expand Up @@ -94,29 +94,6 @@ export class SOR {
);
}

/*
Find and cache cost of token.
If cost is passed then it manually sets the value.
*/
async setCostOutputToken(
tokenOut: string,
tokenDecimals: number,
cost: BigNumber = null
): Promise<BigNumber> {
if (cost !== null) {
this.swapCostCalculator.setSwapCostOverride(
tokenOut,
cost.toString()
);
}
return this.swapCostCalculator.convertGasCostToToken(
tokenOut,
tokenDecimals,
this.gasPrice,
this.swapCost
);
}

/*
Saves updated pools data to internal onChainBalanceCache.
If isOnChain is true will retrieve all required onChain data. (false is advised to only be used for testing)
Expand Down
19 changes: 12 additions & 7 deletions test/lib/testHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
PoolFilter,
SwapV2,
} from '../../src/types';
import { bnum } from '../../src/utils/bignumber';
import { bnum, BONE } from '../../src/utils/bignumber';
import * as fs from 'fs';
import { assert } from 'chai';
import { getAddress } from '@ethersproject/address';
Expand Down Expand Up @@ -360,18 +360,23 @@ export async function getFullSwap(

let swapTypeCorrect = SwapTypes.SwapExactIn;

// We're wanting to set the value of costOutputToken so we calculate
// a native asset price which will give the desired value
const effectiveNativeAssetPrice = costOutputToken
.div(gasPrice)
.div(swapCost)
.div(BONE)
.toString();
if (swapType === 'swapExactIn')
await sor.setCostOutputToken(
await sor.swapCostCalculator.setNativeAssetPriceInToken(
tokenOut,
returnAmountDecimals,
costOutputToken
effectiveNativeAssetPrice
);
else {
swapTypeCorrect = SwapTypes.SwapExactOut;
await sor.setCostOutputToken(
await sor.swapCostCalculator.setNativeAssetPriceInToken(
tokenIn,
returnAmountDecimals,
costOutputToken
effectiveNativeAssetPrice
);
}

Expand Down
16 changes: 6 additions & 10 deletions test/testScripts/swapExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,13 @@ async function getSwap(
const sor = new SOR(provider, gasPrice, maxNoPools, networkId, poolsSource);

// This calculates the cost to make a swap which is used as an input to sor to allow it to make gas efficient recommendations.
// Can be set once and will be used for further swap calculations.
// Defaults to 0 if not called or can be set manually using: await sor.setCostOutputToken(tokenOut, manualPriceBn)
// Note - tokenOut for SwapExactIn, tokenIn for SwapExactOut
let cost: BigNumber;
if (swapType === SwapTypes.SwapExactOut)
cost = await sor.setCostOutputToken(tokenIn.address, tokenIn.decimals);
else
cost = await sor.setCostOutputToken(
tokenOut.address,
tokenOut.decimals
);
const outputToken =
swapType === SwapTypes.SwapExactOut ? tokenIn : tokenOut;
const cost = await sor.getCostOutputToken(
outputToken.address,
outputToken.decimals
);

// Will get onChain data for pools list
await sor.fetchPools(queryOnChain);
Expand Down

0 comments on commit b1b6eab

Please sign in to comment.