Skip to content

Commit

Permalink
Updated to make costOutputToken a separate method to improve speed.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrantuk committed Oct 15, 2020
1 parent 029c987 commit 97a8267
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ export class SOR {
gasPrice: BigNumber;
// avg Balancer swap cost. Can be updated manually if required.
swapCost: BigNumber = new BigNumber('100000');
tokenCost;

constructor(Provider: JsonRpcProvider, GasPrice: BigNumber) {
this.isSubgraphFetched = false;
this.isOnChainFetched = false;
this.provider = Provider;
this.gasPrice = GasPrice;
this.tokenCost = {};
}

async fetchSubgraphPools() {
Expand Down Expand Up @@ -54,6 +56,24 @@ export class SOR {
this.isOnChainFetched = true;
}

async setCostOutputToken(TokenOut: string, Cost: BigNumber = null) {
TokenOut = TokenOut.toLowerCase();

if (Cost === null) {
// This calculates the cost to make a swap which is used as an input to SOR to allow it to make gas efficient recommendations
const costOutputToken = await sor.getCostOutputToken(
TokenOut,
this.gasPrice,
this.swapCost,
this.provider
);

this.tokenCost[TokenOut] = costOutputToken;
} else {
this.tokenCost[TokenOut] = Cost;
}
}

async getSwaps(
TokenIn: string,
TokenOut: string,
Expand All @@ -76,13 +96,10 @@ export class SOR {
TokenIn = TokenIn.toLowerCase();
TokenOut = TokenOut.toLowerCase();

// This calculates the cost to make a swap which is used as an input to SOR to allow it to make gas efficient recommendations
const costOutputToken = await sor.getCostOutputToken(
TokenOut,
this.gasPrice,
this.swapCost,
this.provider
);
let costOutputToken = this.tokenCost[TokenOut];
if (costOutputToken === undefined) {
costOutputToken = new BigNumber(0);
}

// Retrieves all pools that contain both tokenIn & tokenOut, i.e. pools that can be used for direct swaps
// Retrieves intermediate pools along with tokens that are contained in these.
Expand Down
5 changes: 5 additions & 0 deletions test/testScripts/example-simpleSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ async function simpleSwap() {
let amountIn = new BigNumber('1000000'); // 1 USDC, Always pay attention to Token Decimals. i.e. In this case USDC has 6 decimals.
const maxNoPools = 4; // This determines the max no of pools the SOR will use to swap.

// 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)
await SOR.setCostOutputToken(tokenOut);

// If getSwaps is called after fetchSubgraphPools() but before fetchOnChainPools() then Subgraph balances are used.
// These can potentially be innacurate. By default getSwaps will do a final check of swaps using on-chain info.
let [swaps, amountOut] = await SOR.getSwaps(
Expand Down

0 comments on commit 97a8267

Please sign in to comment.