Skip to content

Commit

Permalink
Improved code comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrantuk committed Oct 19, 2020
1 parent 2d7a231 commit 53ea142
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
38 changes: 34 additions & 4 deletions src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export class SOR {
this.processedCache = {};
}

/*
Fetch all public & active pools from Subgraph.
Will clear cached onChain pools and processed paths if new pools are different from cached.
SubgraphUrl can be passed to override default set in .env.
*/
async fetchSubgraphPools(SubgraphUrl: string = '') {
this.isSubgraphFetched = false;
let previous = _.cloneDeep(this.subgraphPools);
Expand All @@ -63,6 +68,11 @@ export class SOR {
this.isSubgraphFetched = true;
}

/*
Uses multicall contact to fetch all onchain balances, weights and fees for cached Subgraph pools.
Will clear cached processed paths if new pools are different from cached.
MulticallAddr can be passed to override default mainnet multicall address.
*/
async fetchOnChainPools(MulticallAddr: string = '') {
this.isOnChainFetched = false;

Expand Down Expand Up @@ -92,6 +102,9 @@ export class SOR {
this.isOnChainFetched = true;
}

/*
Find and cache cost of token.
*/
async setCostOutputToken(TokenOut: string, Cost: BigNumber = null) {
TokenOut = TokenOut.toLowerCase();

Expand All @@ -110,6 +123,11 @@ export class SOR {
}
}

/*
Checks a swap list against latest onchain pool info.
Will update any invalid swaps.
Normally used when using Subgraph balances only.
*/
async onChainCheck(
Swaps: Swap[][],
Total: BigNumber,
Expand All @@ -132,7 +150,7 @@ export class SOR {
this.provider
);

// Checks Subgraph swaps against Onchain pools info.
// Checks swaps against Onchain pools info.
// Will update any invalid swaps for valid.
if (SwapType === 'swapExactIn')
[Swaps, Total] = sor.checkSwapsExactIn(
Expand All @@ -156,6 +174,12 @@ export class SOR {
return [Swaps, Total];
}

/*
Main function to retrieve swap information.
Will always use onChain pools if available over Subgraph pools.
If using Subgraph pools by default swaps are checked using data retrieved from onChain.
Can be overridden with CheckOnChain.
*/
async getSwaps(
TokenIn: string,
TokenOut: string,
Expand All @@ -179,11 +203,17 @@ export class SOR {
costOutputToken = new BigNumber(0);
}

let pools, paths, epsOfInterest;
let pools: PoolDictionary,
paths: Path[],
epsOfInterest: EffectivePrice[];
// If token pair has been processed before use that info to speed up execution
let cache = this.processedCache[`${TokenIn}${TokenOut}${SwapType}`];

if (!cache) {
// Some function alter pools list directly but we want to keep original so make a copy to work from
// If not previously cached we must process all paths/prices.

// Always use onChain info if available
// Some functions alter pools list directly but we want to keep original so make a copy to work from
let poolsList;
if (this.isOnChainFetched)
poolsList = _.cloneDeep(this.onChainPools);
Expand Down Expand Up @@ -248,7 +278,7 @@ export class SOR {
// swapExactOut - total = total amount of TokenIn required for swap
let swaps, total;
[swaps, total] = sor.smartOrderRouterMultiHopEpsOfInterest(
_.cloneDeep(pools),
_.cloneDeep(pools), // Need to keep original pools for cache
paths,
SwapType,
SwapAmt,
Expand Down
33 changes: 0 additions & 33 deletions test/testScripts/example-simpleSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ async function simpleSwap() {
console.log(`Swaps: `);
console.log(swaps);

[swaps, amountOut] = await SOR.getSwaps(
tokenIn,
tokenOut,
swapType,
amountIn
);
console.log(`Total DAI Return (Quicker): ${amountOut.toString()}`);
console.log(`Swaps: `);
console.log(swaps);

// Here the on-chain check is cancelled by setting last parameter to false. Be aware this could lead to invalid swaps if Subgraph out of sync.
[swaps, amountIn] = await SOR.getSwaps(
tokenIn,
Expand Down Expand Up @@ -87,29 +77,6 @@ async function simpleSwap() {
console.log(`Swaps: `);
console.log(swaps);

console.log('Faster?');
[swaps, amountOut] = await SOR.getSwaps(
tokenIn,
tokenOut,
swapType,
amountIn
);
console.log(`Total DAI Return: ${amountOut.toString()}`);
console.log(`Swaps: `);
console.log(swaps);

await SOR.fetchOnChainPools();
console.log('Faster still?');
[swaps, amountOut] = await SOR.getSwaps(
tokenIn,
tokenOut,
swapType,
amountIn
);
console.log(`Total DAI Return: ${amountOut.toString()}`);
console.log(`Swaps: `);
console.log(swaps);

console.log('Refreshing Subgraph pools...');
// Refreshing subgraph pools
// - if new pools are different from previous onChain pools previously fetched will be ignored until also refreshed
Expand Down

0 comments on commit 53ea142

Please sign in to comment.