Skip to content

Commit

Permalink
feat: update payload for events
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikari-Shinji-re authored and RyukTheCoder committed Jan 27, 2025
1 parent a112171 commit 36a11b6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 5 deletions.
6 changes: 6 additions & 0 deletions queue-manager/rango-preset/src/actions/checkStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
getCurrentStep,
getCurrentStepTx,
getCurrentStepTxType,
getLastSuccessfulStepInput,
getLastSuccessfulStepInputUsd,
getLastSuccessfulStepOutputUsd,
inMemoryTransactionsData,
resetNetworkStatus,
setCurrentStepTx,
Expand Down Expand Up @@ -222,7 +225,10 @@ async function checkTransactionStatus({
notifier({
event: {
type: StepEventType.SUCCEEDED,
inputAmount: getLastSuccessfulStepInput(swap),
inputAmountUsd: getLastSuccessfulStepInputUsd(swap),
outputAmount: currentStep.outputAmount ?? '',
outputAmountUsd: getLastSuccessfulStepOutputUsd(swap),
},
swap,
step: currentStep,
Expand Down
6 changes: 6 additions & 0 deletions queue-manager/rango-preset/src/actions/scheduleNextStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import type { PendingSwapStep } from 'rango-types';
import {
getCurrentStep,
getLastSuccessfulStep,
getLastSuccessfulStepInput,
getLastSuccessfulStepInputUsd,
getLastSuccessfulStepOutputUsd,
isTxAlreadyCreated,
} from '../helpers';
import { notifier } from '../services/eventEmitter';
Expand Down Expand Up @@ -76,8 +79,11 @@ export function scheduleNextStep({
: {
event: {
type: StepEventType.SUCCEEDED,
inputAmount: getLastSuccessfulStepInput(swap),
inputAmountUsd: getLastSuccessfulStepInputUsd(swap),
outputAmount:
getLastSuccessfulStep(swap.steps)?.outputAmount ?? '',
outputAmountUsd: getLastSuccessfulStepOutputUsd(swap),
},
}),
swap: swap,
Expand Down
60 changes: 60 additions & 0 deletions queue-manager/rango-preset/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
getEvmProvider,
splitWalletNetwork,
} from '@rango-dev/wallets-shared';
import BigNumber from 'bignumber.js';
import { TransactionType } from 'rango-sdk';
import { PendingSwapNetworkStatus, SignerError } from 'rango-types';

Expand Down Expand Up @@ -1518,3 +1519,62 @@ export function isApprovalTX(step: Step): boolean {

return approvalTx;
}

export function getTokenAmountInUsd(
amount: string | number,
usdPrice: string | number
): string {
const usdValue = new BigNumber(amount).multipliedBy(usdPrice);
if (isNaN(usdValue.toNumber())) {
return '';
}
return usdValue.toString();
}

export function getSwapInputUsd(swap: PendingSwap): string {
return getTokenAmountInUsd(
swap.inputAmount,
swap.steps[0].fromUsdPrice ?? ''
);
}

export function getSwapOutputUsd(swap: PendingSwap): string {
const lastStep = swap.steps[swap.steps.length - 1];

return getTokenAmountInUsd(
lastStep.outputAmount ?? '',
lastStep.toUsdPrice ?? ''
);
}

export function getLastSuccessfulStepInput(swap: PendingSwap): string {
const lastSuccessfulStepIndex = swap.steps.findLastIndex(
(step) => step.status === 'success'
);

if (lastSuccessfulStepIndex < 0) {
return '';
}

return lastSuccessfulStepIndex === 0
? swap.inputAmount
: swap.steps[lastSuccessfulStepIndex - 1].outputAmount ?? '';
}

export function getLastSuccessfulStepInputUsd(swap: PendingSwap): string {
const lastSuccessfulStep = getLastSuccessfulStep(swap.steps);

return getTokenAmountInUsd(
getLastSuccessfulStepInput(swap),
lastSuccessfulStep?.fromUsdPrice ?? ''
);
}

export function getLastSuccessfulStepOutputUsd(swap: PendingSwap): string {
const lastSuccessfulStep = getLastSuccessfulStep(swap.steps);

return getTokenAmountInUsd(
lastSuccessfulStep?.outputAmount ?? '',
lastSuccessfulStep?.toUsdPrice ?? ''
);
}
18 changes: 14 additions & 4 deletions queue-manager/rango-preset/src/services/eventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
getCurrentStepTx,
getFailedStep,
getLastSuccessfulStep,
getSwapInputUsd,
getSwapOutputUsd,
isApprovalCurrentStepTx,
} from '../helpers';
import { getCurrentBlockchainOfOrNull } from '../shared';
Expand Down Expand Up @@ -140,7 +142,7 @@ function getEventPayload(
return result;
}

function emitRouteEvent(stepEvent: StepEvent, route: Route) {
function emitRouteEvent(stepEvent: StepEvent, route: Route, swap: PendingSwap) {
let routeEvent: RouteEvent | undefined;
const { type } = stepEvent;
switch (type) {
Expand All @@ -150,9 +152,17 @@ function emitRouteEvent(stepEvent: StepEvent, route: Route) {
case StepEventType.FAILED:
routeEvent = { ...stepEvent, type: RouteEventType.FAILED };
break;
case StepEventType.SUCCEEDED:
routeEvent = { ...stepEvent, type: RouteEventType.SUCCEEDED };
case StepEventType.SUCCEEDED: {
routeEvent = {
...stepEvent,
type: RouteEventType.SUCCEEDED,
inputAmount: swap.inputAmount,
inputAmountUsd: getSwapInputUsd(swap),
outputAmount: swap.steps[swap.steps.length - 1].outputAmount ?? '',
outputAmountUsd: getSwapOutputUsd(swap),
};
break;
}
default:
break;
}
Expand Down Expand Up @@ -267,6 +277,6 @@ export function notifier(params: NotifierParams) {
emitStepEvent({ ...event, message, messageSeverity }, route, step);
}
if (params.event.type === StepEventType.FAILED || !params.step) {
emitRouteEvent({ ...event, message, messageSeverity }, route);
emitRouteEvent({ ...event, message, messageSeverity }, route, params.swap);
}
}
5 changes: 4 additions & 1 deletion queue-manager/rango-preset/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,15 @@ export type FailedRouteEventPayload = {
export type FailedStepEventPayload = FailedRouteEventPayload;

export type SucceededRouteEventPayload = {
inputAmount: string;
inputAmountUsd: string;
outputAmount: string;
outputAmountUsd: string;
};

export type SucceededStepEventPayload = SucceededRouteEventPayload;

export type OutputRevealedEventPayload = SucceededRouteEventPayload;
export type OutputRevealedEventPayload = { outputAmount: string };

export type StepExecutionEventPayload = {
status:
Expand Down

0 comments on commit 36a11b6

Please sign in to comment.