Skip to content

Commit

Permalink
format code using extension Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge-Lopes committed Sep 15, 2022
1 parent 0aad034 commit 1cc00ca
Showing 1 changed file with 49 additions and 30 deletions.
79 changes: 49 additions & 30 deletions contract/src/stopLoss.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import {
import { Far, E } from '@endo/far';
import { AmountMath } from '@agoric/ertp';
import { offerTo } from '@agoric/zoe/src/contractSupport/index.js';
import { assertBoundaryShape, assertExecutionMode, assertAllocationStatePhase, assertScheduledOrActive, assertInitialBoundariesRange } from './assertionHelper.js';
import {
assertBoundaryShape,
assertExecutionMode,
assertAllocationStatePhase,
assertScheduledOrActive,
assertInitialBoundariesRange,
} from './assertionHelper.js';
import { makeBoundaryWatcher } from './boundaryWatcher.js';
import { makeNotifierKit } from '@agoric/notifier';
import { ALLOCATION_PHASE, BOUNDARY_WATCHER_STATUS } from './constants.js';
import { makeTracer } from '@agoric/inter-protocol/src/makeTracer.js';


const tracer = makeTracer('StopLoss');

/**
Expand All @@ -23,7 +28,7 @@ const tracer = makeTracer('StopLoss');
*/
const start = async (zcf) => {
const {
/** @type XYKAMMPublicFacet */ ammPublicFacet,
/** @type XYKAMMPublicFacet */ ammPublicFacet,
/** @type Issuer */ centralIssuer,
/** @type Issuer */ secondaryIssuer,
/** @type Issuer */ lpTokenIssuer,
Expand All @@ -39,18 +44,20 @@ const start = async (zcf) => {
const secondaryBrand = zcf.getBrandForIssuer(secondaryIssuer);
const lpTokenBrand = zcf.getBrandForIssuer(lpTokenIssuer);

const getStateSnapshot = phase => {
const getStateSnapshot = (phase) => {
return harden({
phase: phase,
lpBalance: stopLossSeat.getAmountAllocated('LpToken', lpTokenBrand),
liquidityBalance: {
central: stopLossSeat.getAmountAllocated('Central', centralBrand),
secondary: stopLossSeat.getAmountAllocated('Secondary', secondaryBrand),
}
},
});
};

const { updater, notifier } = makeNotifierKit(getStateSnapshot(ALLOCATION_PHASE.IDLE));
const { updater, notifier } = makeNotifierKit(
getStateSnapshot(ALLOCATION_PHASE.IDLE),
);

// phaseSnapshot used for assertAllocationStatePhase
let phaseSnapshot = ALLOCATION_PHASE.IDLE;
Expand All @@ -59,21 +66,27 @@ const start = async (zcf) => {
const allocationState = getStateSnapshot(allocationPhase);
updater.updateState(allocationState);
phaseSnapshot = allocationPhase;
}
};

assertBoundaryShape(boundaries, centralBrand, secondaryBrand);

const init = async () => {
let fromCentralPriceAuthority;

if (ammPublicFacet) {
const { fromCentral } = await E(ammPublicFacet).getPriceAuthorities(secondaryBrand);
const { fromCentral } = await E(ammPublicFacet).getPriceAuthorities(
secondaryBrand,
);
fromCentralPriceAuthority = fromCentral;
} else {
fromCentralPriceAuthority = devPriceAuthority;
}

await isPriceInsideInitialBoundaries(fromCentralPriceAuthority, boundaries, secondaryBrand);
await isPriceInsideInitialBoundaries(
fromCentralPriceAuthority,
boundaries,
secondaryBrand,
);

const boundaryWatcher = makeBoundaryWatcher({
fromCentralPriceAuthority,
Expand All @@ -87,18 +100,22 @@ const start = async (zcf) => {
return boundaryWatcher;
};

const isPriceInsideInitialBoundaries = async (fromCentralPriceAuthority, boundaries, secondaryBrand) => {
const isPriceInsideInitialBoundaries = async (
fromCentralPriceAuthority,
boundaries,
secondaryBrand,
) => {
const amountIn = boundaries.lower.denominator;
const quote = await E(fromCentralPriceAuthority).quoteGiven(amountIn, secondaryBrand);
const quote = await E(fromCentralPriceAuthority).quoteGiven(
amountIn,
secondaryBrand,
);
const quoteAmountOut = getAmountOut(quote);
assertInitialBoundariesRange(boundaries, quoteAmountOut)
}
assertInitialBoundariesRange(boundaries, quoteAmountOut);
};

// Initiate listening
const {
boundaryWatcherPromise,
updateBoundaries,
} = await init();
const { boundaryWatcherPromise, updateBoundaries } = await init();

const schedule = async () => {
// Wait for the price boundary being violated
Expand All @@ -119,7 +136,7 @@ const start = async (zcf) => {
};

// Schedule a trigger for LP token removal
schedule().catch(error => {
schedule().catch((error) => {
updateAllocationState(ALLOCATION_PHASE.ERROR);
tracer('Schedule encountered an error', error);
}); // Notify user
Expand Down Expand Up @@ -163,7 +180,7 @@ const start = async (zcf) => {
Secondary: null,
},
});

await removeLiquidityFromAmm();
assertAllocationStatePhase(phaseSnapshot, ALLOCATION_PHASE.REMOVED);

Expand Down Expand Up @@ -200,20 +217,18 @@ const start = async (zcf) => {
const makeWithdrawLpTokensInvitation = () => {
const withdrawLpTokens = (creatorSeat) => {
assertProposalShape(creatorSeat, {
want: {LpToken: null},
want: { LpToken: null },
});

assertAllocationStatePhase(phaseSnapshot, ALLOCATION_PHASE.ACTIVE);

const lpTokenAmountAllocated = stopLossSeat.getAmountAllocated(
'LpToken',
lpTokenBrand,
)
);

creatorSeat.incrementBy(
stopLossSeat.decrementBy(
harden({LpToken: lpTokenAmountAllocated}),
),
stopLossSeat.decrementBy(harden({ LpToken: lpTokenAmountAllocated })),
);

zcf.reallocate(creatorSeat, stopLossSeat);
Expand All @@ -229,8 +244,9 @@ const start = async (zcf) => {
};

const removeLiquidityFromAmm = async () => {
const removeLiquidityInvitation =
await E(ammPublicFacet).makeRemoveLiquidityInvitation();
const removeLiquidityInvitation = await E(
ammPublicFacet,
).makeRemoveLiquidityInvitation();

const lpTokensLockedAmount = stopLossSeat.getAmountAllocated(
'LpToken',
Expand Down Expand Up @@ -264,18 +280,21 @@ const start = async (zcf) => {
} catch (error) {
updateAllocationState(ALLOCATION_PHASE.ERROR);
tracer('removeLiquidityFromAmm encounted an error: ', error);
return
};
return;
}

const [amounts, removeOfferResult] = await Promise.all([deposited, E(liquiditySeat).getOfferResult()]);
const [amounts, removeOfferResult] = await Promise.all([
deposited,
E(liquiditySeat).getOfferResult(),
]);
tracer('Amounts from removal', amounts);

updateAllocationState(ALLOCATION_PHASE.REMOVED);

return removeOfferResult;
};

const updateConfiguration = async boundaries => {
const updateConfiguration = async (boundaries) => {
assertScheduledOrActive(phaseSnapshot);
return await updateBoundaries(boundaries);
};
Expand Down

0 comments on commit 1cc00ca

Please sign in to comment.