Skip to content

Commit ce07833

Browse files
committed
refactor: remove code duplications from future price methods
1 parent f15e883 commit ce07833

File tree

1 file changed

+20
-60
lines changed

1 file changed

+20
-60
lines changed

src/llammas/LlammaTemplate.ts

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,21 @@ export class LlammaTemplate {
342342
return await crvusd.contracts[this.controller].contract.calculate_debt_n1(_collateral, _debt, N, crvusd.constantOptions);
343343
}
344344

345+
private async _calcPrices(_n1: ethers.BigNumber, _n2: ethers.BigNumber): Promise<string[]> {
346+
const contract = crvusd.contracts[this.address].contract
347+
return (await Promise.all([
348+
contract.p_oracle_up(_n1, crvusd.constantOptions),
349+
contract.p_oracle_down(_n2, crvusd.constantOptions),
350+
]) as ethers.BigNumber[]).map((_p) => ethers.utils.formatUnits(_p));
351+
352+
// TODO switch to multicall
353+
// const contract = crvusd.contracts[this.address].multicallContract;
354+
// const [_price1, _price2] = await crvusd.multicallProvider.all([
355+
// contract.price_oracle_up(_n1),
356+
// contract.price_oracle_down(_n2),
357+
// ]);
358+
}
359+
345360
public async createLoanMaxRecv(collateral: number | string, N: number): Promise<string> {
346361
if (N < this.minTicks) throw Error(`N must be >= ${this.minTicks}`);
347362
if (N > this.maxTicks) throw Error(`N must be <= ${this.maxTicks}`);
@@ -366,18 +381,7 @@ export class LlammaTemplate {
366381
public async createLoanPrices(collateral: number | string, debt: number | string, N: number): Promise<string[]> {
367382
const [_n1, _n2] = await this._createLoanTicks(collateral, debt, N);
368383

369-
const contract = crvusd.contracts[this.address].contract
370-
return (await Promise.all([
371-
contract.p_oracle_up(_n1, crvusd.constantOptions),
372-
contract.p_oracle_down(_n2, crvusd.constantOptions),
373-
]) as ethers.BigNumber[]).map((_p) => ethers.utils.formatUnits(_p));
374-
375-
// TODO switch to multicall
376-
// const contract = crvusd.contracts[this.address].multicallContract;
377-
// const [_price1, _price2] = await crvusd.multicallProvider.all([
378-
// contract.price_oracle_up(_n1),
379-
// contract.price_oracle_down(_n2),
380-
// ]);
384+
return await this._calcPrices(_n1, _n2);
381385
}
382386

383387
public async createLoanIsApproved(collateral: number | string): Promise<boolean> {
@@ -454,18 +458,7 @@ export class LlammaTemplate {
454458
public async borrowMorePrices(collateral: number | string, debt: number | string): Promise<string[]> {
455459
const [_n1, _n2] = await this._borrowMoreTicks(collateral, debt);
456460

457-
const contract = crvusd.contracts[this.address].contract
458-
return (await Promise.all([
459-
contract.p_oracle_up(_n1, crvusd.constantOptions),
460-
contract.p_oracle_down(_n2, crvusd.constantOptions),
461-
]) as ethers.BigNumber[]).map((_p) => ethers.utils.formatUnits(_p));
462-
463-
// TODO switch to multicall
464-
// const contract = crvusd.contracts[this.address].multicallContract;
465-
// const [_price1, _price2] = await crvusd.multicallProvider.all([
466-
// contract.price_oracle_up(_n1),
467-
// contract.price_oracle_down(_n2),
468-
// ]);
461+
return await this._calcPrices(_n1, _n2);
469462
}
470463

471464
public async borrowMoreHealth(collateral: number | string, debt: number | string, full = true, address = ""): Promise<string> {
@@ -548,18 +541,7 @@ export class LlammaTemplate {
548541
public async addCollateralPrices(collateral: number | string, address = ""): Promise<string[]> {
549542
const [_n1, _n2] = await this._addCollateralTicks(collateral, address);
550543

551-
const contract = crvusd.contracts[this.address].contract
552-
return (await Promise.all([
553-
contract.p_oracle_up(_n1, crvusd.constantOptions),
554-
contract.p_oracle_down(_n2, crvusd.constantOptions),
555-
]) as ethers.BigNumber[]).map((_p) => ethers.utils.formatUnits(_p));
556-
557-
// TODO switch to multicall
558-
// const contract = crvusd.contracts[this.address].multicallContract;
559-
// const [_price1, _price2] = await crvusd.multicallProvider.all([
560-
// contract.price_oracle_up(_n1),
561-
// contract.price_oracle_down(_n2),
562-
// ]);
544+
return await this._calcPrices(_n1, _n2);
563545
}
564546

565547
public async addCollateralHealth(collateral: number | string, full = true, address = ""): Promise<string> {
@@ -643,18 +625,7 @@ export class LlammaTemplate {
643625
public async removeCollateralPrices(collateral: number | string): Promise<string[]> {
644626
const [_n1, _n2] = await this._removeCollateralTicks(collateral);
645627

646-
const contract = crvusd.contracts[this.address].contract
647-
return (await Promise.all([
648-
contract.p_oracle_up(_n1, crvusd.constantOptions),
649-
contract.p_oracle_down(_n2, crvusd.constantOptions),
650-
]) as ethers.BigNumber[]).map((_p) => ethers.utils.formatUnits(_p));
651-
652-
// TODO switch to multicall
653-
// const contract = crvusd.contracts[this.address].multicallContract;
654-
// const [_price1, _price2] = await crvusd.multicallProvider.all([
655-
// contract.price_oracle_up(_n1),
656-
// contract.price_oracle_down(_n2),
657-
// ]);
628+
return await this._calcPrices(_n1, _n2);
658629
}
659630

660631
public async removeCollateralHealth(collateral: number | string, full = true, address = ""): Promise<string> {
@@ -714,18 +685,7 @@ export class LlammaTemplate {
714685
public async repayPrices(debt: number | string): Promise<string[]> {
715686
const [_n1, _n2] = await this._repayTicks(debt);
716687

717-
const contract = crvusd.contracts[this.address].contract
718-
return (await Promise.all([
719-
contract.p_oracle_up(_n1, crvusd.constantOptions),
720-
contract.p_oracle_down(_n2, crvusd.constantOptions),
721-
]) as ethers.BigNumber[]).map((_p) => ethers.utils.formatUnits(_p));
722-
723-
// TODO switch to multicall
724-
// const contract = crvusd.contracts[this.address].multicallContract;
725-
// const [_price1, _price2] = await crvusd.multicallProvider.all([
726-
// contract.price_oracle_up(_n1),
727-
// contract.price_oracle_down(_n2),
728-
// ]);
688+
return await this._calcPrices(_n1, _n2);
729689
}
730690

731691
public async repayIsApproved(debt: number | string): Promise<boolean> {

0 commit comments

Comments
 (0)