Skip to content

Commit

Permalink
fix: fix issue with exchange rate requester
Browse files Browse the repository at this point in the history
  • Loading branch information
Verisana committed Aug 22, 2023
1 parent 8661990 commit ac97f99
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 48 deletions.
57 changes: 57 additions & 0 deletions src/dex/curve-v1-factory/curve-v1-factory-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,63 @@ describe('CurveV1Factory', function () {
}
});
});

describe(`renBTC-wibBTC`, () => {
const srcTokenSymbol = 'renBTC';
const destTokenSymbol = 'wibBTC';
const amountsForSell = [
0n,
1n * BI_POWS[tokens[srcTokenSymbol].decimals - 1],
2n * BI_POWS[tokens[srcTokenSymbol].decimals - 1],
3n * BI_POWS[tokens[srcTokenSymbol].decimals - 1],
4n * BI_POWS[tokens[srcTokenSymbol].decimals - 1],
5n * BI_POWS[tokens[srcTokenSymbol].decimals - 1],
6n * BI_POWS[tokens[srcTokenSymbol].decimals - 1],
7n * BI_POWS[tokens[srcTokenSymbol].decimals - 1],
8n * BI_POWS[tokens[srcTokenSymbol].decimals - 1],
9n * BI_POWS[tokens[srcTokenSymbol].decimals - 1],
10n * BI_POWS[tokens[srcTokenSymbol].decimals - 1],
];

it('getPoolIdentifiers and getPricesVolume SELL', async function () {
await testPricingOnNetwork(
curveV1Factory,
network,
dexKey,
blockNumber,
srcTokenSymbol,
destTokenSymbol,
SwapSide.SELL,
amountsForSell,
);
});

it('getTopPoolsForToken', async function () {
// We have to check without calling initializePricing, because
// pool-tracker is not calling that function
const newCurveV1Factory = new CurveV1Factory(
network,
dexKey,
dexHelper,
);
if (newCurveV1Factory.updatePoolState) {
await newCurveV1Factory.updatePoolState();
}
const poolLiquidity = await newCurveV1Factory.getTopPoolsForToken(
tokens[srcTokenSymbol].address,
10,
);
console.log(`${srcTokenSymbol} Top Pools:`, poolLiquidity);

if (!newCurveV1Factory.hasConstantPriceLargeAmounts) {
checkPoolsLiquidity(
poolLiquidity,
Tokens[network][srcTokenSymbol].address,
dexKey,
);
}
});
});
});
describe('Polygon', () => {
const network = Network.POLYGON;
Expand Down
102 changes: 58 additions & 44 deletions src/dex/curve-v1-factory/curve-v1-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ export class CurveV1Factory
// This is only to start timer, each pool is initialized with updated state
this.poolManager.initializePollingPools();
await this.fetchFactoryPools(blockNumber);
await this.poolManager.fetchLiquiditiesFromApi();
await this.poolManager.updatePollingPoolsInBatch();
this.logger.info(`${this.dexKey}: successfully initialized`);
}

Expand Down Expand Up @@ -340,7 +342,7 @@ export class CurveV1Factory
// Variable initializeInitialState is only for poolTracker. We don't want to keep state updated with scheduler
// We just want to initialize factory pools and send request to CurveAPI
// Other values are not used
initializeInitialState: boolean = true,
initializeInitialState: boolean = false,
) {
if (this.areFactoryPoolsFetched) {
return;
Expand Down Expand Up @@ -770,57 +772,69 @@ export class CurveV1Factory
)
: amountsWithUnit;

const results = pools.map(
(pool): PoolPrices<CurveV1FactoryData> | null => {
const state = pool.getState();
const results = await Promise.all(
pools.map(
async (pool): Promise<PoolPrices<CurveV1FactoryData> | null> => {
let state = pool.getState();
if (!state) {
await this.poolManager.updateManuallyPollingPools(
pool.baseStatePoolPolling
? [pool.baseStatePoolPolling, pool]
: [pool],
);
state = pool.getState();
if (!state) {
return null;
}
}

if (!state) {
return null;
}
if (state.balances.every(b => b === 0n)) {
this.logger.trace(
`${this.dexKey} on ${this.dexHelper.config.data.network}: State balances equal to 0 in pool ${pool.address}`,
);
return null;
}

if (state.balances.every(b => b === 0n)) {
this.logger.trace(
`${this.dexKey} on ${this.dexHelper.config.data.network}: State balances equal to 0 in pool ${pool.address}`,
const poolData = pool.getPoolData(
srcTokenAddress,
destTokenAddress,
);
return null;
}

const poolData = pool.getPoolData(srcTokenAddress, destTokenAddress);
if (poolData === null) {
this.logger.error(
`${pool.fullName}: one or both tokens can not be exchanged in pool ${pool.address}: ${srcTokenAddress} -> ${destTokenAddress}`,
);
return null;
}

if (poolData === null) {
this.logger.error(
`${pool.fullName}: one or both tokens can not be exchanged in pool ${pool.address}: ${srcTokenAddress} -> ${destTokenAddress}`,
);
return null;
}
let outputs: bigint[] = this.poolManager
.getPriceHandler(pool.implementationAddress)
.getOutputs(
state,
amountsWithUnitAndFee,
poolData.i,
poolData.j,
poolData.underlyingSwap,
);

let outputs: bigint[] = this.poolManager
.getPriceHandler(pool.implementationAddress)
.getOutputs(
state,
amountsWithUnitAndFee,
poolData.i,
poolData.j,
poolData.underlyingSwap,
outputs = applyTransferFee(
outputs,
side,
transferFees.destDexFee,
this.DEST_TOKEN_DEX_TRANSFERS,
);

outputs = applyTransferFee(
outputs,
side,
transferFees.destDexFee,
this.DEST_TOKEN_DEX_TRANSFERS,
);

return {
prices: [0n, ...outputs.slice(1)],
unit: outputs[0],
data: poolData,
exchange: this.dexKey,
poolIdentifier: pool.poolIdentifier,
gasCost: POOL_EXCHANGE_GAS_COST,
poolAddresses: [pool.address],
};
},
return {
prices: [0n, ...outputs.slice(1)],
unit: outputs[0],
data: poolData,
exchange: this.dexKey,
poolIdentifier: pool.poolIdentifier,
gasCost: POOL_EXCHANGE_GAS_COST,
poolAddresses: [pool.address],
};
},
),
);

return results.filter(
Expand Down
12 changes: 11 additions & 1 deletion src/dex/curve-v1-factory/curve-v1-pool-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class CurveV1FactoryPoolManager {
filteredPoolsByLiquidity.sort((a, b) => +a.isMetaPool - +b.isMetaPool),
);

this.statePollingManager.updatePoolsInBatch(
return this.statePollingManager.updatePoolsInBatch(
this.logger,
this.dexHelper,
pools,
Expand All @@ -105,6 +105,16 @@ export class CurveV1FactoryPoolManager {
);
}

async updateManuallyPollingPools(pools: PoolPollingBase[]) {
return this.statePollingManager.updatePoolsInBatch(
this.logger,
this.dexHelper,
pools,
undefined,
undefined,
);
}

async initializeIndividualPollingPoolState(
identifier: string,
isSrcFeeOnTransferTokenToBeExchanged: boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,17 @@ export class CustomBasePoolForFactory extends PoolPollingBase {

let exchangeRateCurrent: (bigint | undefined)[] | undefined;

let lastEndIndex = lastIndex + 1;
let lastEndIndex = lastIndex + this.poolConstants.COINS.length;
if (this.useLending) {
exchangeRateCurrent = new Array(this.useLending.length).fill(undefined);
let trueUseLendingCount = this.useLending.filter(el => el).length;
const exchangeRateResults = multiOutputs.slice(
lastEndIndex,
// Filter false elements before checking length
lastEndIndex + this.useLending.filter(el => el).length,
lastEndIndex + trueUseLendingCount,
) as bigint[];

lastEndIndex += this.useLending.length;
lastEndIndex += trueUseLendingCount;
// We had array with booleans and I filtered of `false` and sent request.
// So, now I must map that results to original indices. That is the reason of this complication
const indicesToFill = this.useLending.reduce<number[]>((acc, curr, i) => {
Expand Down
5 changes: 5 additions & 0 deletions tests/constants-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ export const Tokens: {
address: '0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E',
decimals: 18,
},
wibBTC: {
address: '0x8751d4196027d4e6da63716fa7786b5174f04c15',
decimals: 18,
},
},
[Network.ROPSTEN]: {
DAI: {
Expand Down Expand Up @@ -918,6 +922,7 @@ export const Holders: {
PSP: '0xE5E5440a1CE69C5cf67BFFA74d185e57c31b43E5',
crvUSD: '0xA920De414eA4Ab66b97dA1bFE9e6EcA7d4219635',
GHO: '0x844Dc85EdD8492A56228D293cfEbb823EF3E10EC',
wibBTC: '0xFbdCA68601f835b27790D98bbb8eC7f05FDEaA9B',
},
[Network.ROPSTEN]: {
ETH: '0x43262A12d8610AA70C15DbaeAC321d51613c9071',
Expand Down

0 comments on commit ac97f99

Please sign in to comment.