Skip to content

Commit

Permalink
Added separate onChainCheck function.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrantuk committed Oct 19, 2020
1 parent 032745c commit 048c9f1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 42 deletions.
2 changes: 0 additions & 2 deletions src/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ export async function getAllPublicSwapPools(SubgraphUrl: string = '') {
}
`;

console.log(`Using: ${SubgraphUrl === '' ? SUBGRAPH_URL : SubgraphUrl}`);

const response = await fetch(
SubgraphUrl === '' ? SUBGRAPH_URL : SubgraphUrl,
{
Expand Down
97 changes: 62 additions & 35 deletions src/wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JsonRpcProvider } from '@ethersproject/providers';
import { BigNumber } from './utils/bignumber';
import { SubGraphPools } from './types';
import { SubGraphPools, Swap } from './types';
import _ from 'lodash';
const sor = require('./index');

Expand All @@ -11,7 +11,8 @@ export class SOR {
subgraphPoolsFormatted;
onChainPools;
provider: JsonRpcProvider;
multicallAddress: String = '0xeefba1e63905ef1d7acba5a8513c70307c1ce441';
// Default multi address for mainnet
multicallAddress: string = '0xeefba1e63905ef1d7acba5a8513c70307c1ce441';
gasPrice: BigNumber;
// avg Balancer swap cost. Can be updated manually if required.
swapCost: BigNumber = new BigNumber('100000');
Expand Down Expand Up @@ -83,13 +84,61 @@ export class SOR {
}
}

async onChainCheck(
Swaps: Swap[][],
Total: BigNumber,
SwapType: string,
TokenIn: string,
TokenOut: string,
SwapAmt: BigNumber,
MulticallAddr: string
): Promise<[Swap[][], BigNumber]> {
// Gets pools used in swaps
let poolsToCheck: SubGraphPools = sor.getPoolsFromSwaps(
Swaps,
this.subgraphPools
);

// Get onchain info for swap pools
let onChainPools = await sor.getAllPoolDataOnChain(
poolsToCheck,
MulticallAddr === '' ? this.multicallAddress : MulticallAddr,
this.provider
);

// Checks Subgraph swaps against Onchain pools info.
// Will update any invalid swaps for valid.
if (SwapType === 'swapExactIn')
[Swaps, Total] = sor.checkSwapsExactIn(
Swaps,
TokenIn,
TokenOut,
SwapAmt,
Total,
onChainPools
);
else
[Swaps, Total] = sor.checkSwapsExactOut(
Swaps,
TokenIn,
TokenOut,
SwapAmt,
Total,
onChainPools
);

return [Swaps, Total];
}

async getSwaps(
TokenIn: string,
TokenOut: string,
SwapType: string,
SwapAmt: BigNumber,
CheckOnChain: boolean = true
) {
CheckOnChain: boolean = true,
MulticallAddr: string = ''
): Promise<[Swap[][], BigNumber]> {
console.time('getSwaps');
if (!this.isSubgraphFetched) {
console.error('ERROR: Must fetch pools before getting a swap.');
return;
Expand Down Expand Up @@ -167,41 +216,19 @@ export class SOR {

// Perform onChain check of swaps if using Subgraph balances
if (!this.isOnChainFetched && CheckOnChain && swaps.length > 0) {
// Gets pools used in swaps
let poolsToCheck: SubGraphPools = sor.getPoolsFromSwaps(
[swaps, total] = await this.onChainCheck(
swaps,
this.subgraphPools
);

// Get onchain info for swap pools
let onChainPools = await sor.getAllPoolDataOnChain(
poolsToCheck,
this.multicallAddress,
this.provider
total,
SwapType,
TokenIn,
TokenOut,
SwapAmt,
MulticallAddr === '' ? this.multicallAddress : MulticallAddr
);

// Checks Subgraph swaps against Onchain pools info.
// Will update any invalid swaps for valid.
if (SwapType === 'swapExactIn')
[swaps, total] = sor.checkSwapsExactIn(
swaps,
TokenIn,
TokenOut,
SwapAmt,
total,
onChainPools
);
else
[swaps, total] = sor.checkSwapsExactOut(
swaps,
TokenIn,
TokenOut,
SwapAmt,
total,
onChainPools
);
}

console.timeEnd('getSwaps');

return [swaps, total];
}
}
7 changes: 2 additions & 5 deletions test/testScripts/example-simpleSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ async function simpleSwap() {
tokenIn,
tokenOut,
swapType,
amountIn,
maxNoPools
amountIn
);
console.log(`Total DAI Return: ${amountOut.toString()}`);
console.log(`Swaps: `);
Expand All @@ -56,7 +55,6 @@ async function simpleSwap() {
tokenOut,
'swapExactOut',
amountOut,
maxNoPools,
false
);
console.log(`Total USDC In: ${amountIn.toString()}`);
Expand Down Expand Up @@ -88,8 +86,7 @@ async function simpleSwap() {
tokenIn,
tokenOut,
swapType,
amountIn,
maxNoPools
amountIn
);
console.log(`Total DAI Return: ${amountOut.toString()}`);
console.log(`Swaps: `);
Expand Down

0 comments on commit 048c9f1

Please sign in to comment.