Skip to content

Commit

Permalink
feat: calculate rebalance rewards for each transaction (#68)
Browse files Browse the repository at this point in the history
* feat: calculate rebalance rewards for each transaction

* feat: apply simple caching for better performance
  • Loading branch information
ryenguyen7411 authored Aug 20, 2022
1 parent ea94e54 commit 8ee6b17
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/modules/common-params/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type SkybridgeParams<

price: string;
rebateRate: string;
rebalanceRewards: string;
};

export const SkybridgeTermsMessage = {
Expand Down
20 changes: 20 additions & 0 deletions src/modules/generic-details/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { SkybridgeParams, SkybridgeStatus } from '../common-params';
import type { SkybridgeResource } from '../resources';
import { fromApiCoin, SkybridgeCoin } from '../coins';
import { SkybridgeBridge } from '../bridges';
import { estimateSwapRewards } from '../generic-rewards';

type ServerReturnType<R extends SkybridgeResource, M extends SkybridgeMode> = {
items: Array<
Expand Down Expand Up @@ -41,12 +42,14 @@ type ReturnType<R extends SkybridgeResource, M extends SkybridgeMode> = Pick<
| 'feeCurrency'
| 'addressSending'
| 'isSkypoolsSwap'
| 'rebalanceRewards'
> & {
txDepositId: SkybridgeParams<R, M>['txDepositId'] | null;
txReceivingId: SkybridgeParams<R, M>['txReceivingId'] | null;
};

const bridgeCache = new Map<string, SkybridgeBridge>();
const rebalanceRewardsCache = new Map<string, string>();

export const getDetails = async <R extends SkybridgeResource, M extends SkybridgeMode>({
resource,
Expand Down Expand Up @@ -109,6 +112,22 @@ export const getDetails = async <R extends SkybridgeResource, M extends Skybridg
throw new Error(`"${hash}" is not a withdrawal, it is a swap.`);
}

const rebalanceRewards: string = await (async () => {
const cache = rebalanceRewardsCache.get(hash);
if (cache) return cache;

const swapRewardsResult = await estimateSwapRewards({
context,
amountDesired: result.data.amountIn,
currencyDeposit: fromApiCoin({ bridge: result.bridge, coin: result.data.currencyIn as any }),
currencyReceiving: fromApiCoin({ bridge: result.bridge, coin: result.data.currencyOut as any }),
});

rebalanceRewardsCache.set(hash, swapRewardsResult.amountReceiving);
return swapRewardsResult.amountReceiving;
})();


return ({
addressReceiving: result.data.addressOut,
addressDeposit: result.data.addressDeposit,
Expand All @@ -128,6 +147,7 @@ export const getDetails = async <R extends SkybridgeResource, M extends Skybridg
coin: (result.data.feeCurrency as any) || null,
}),
feeTotal: result.data.fee,
rebalanceRewards,
isSkypoolsSwap: resource === 'swap' && result.data.skypools === true,
} as unknown) as ReturnType<R, M>;
};

0 comments on commit 8ee6b17

Please sign in to comment.