From 8ee6b17f03bb2738e05e5c16b21cd75e193c4838 Mon Sep 17 00:00:00 2001 From: Rye Nguyen Date: Sat, 20 Aug 2022 22:37:09 +0700 Subject: [PATCH] feat: calculate rebalance rewards for each transaction (#68) * feat: calculate rebalance rewards for each transaction * feat: apply simple caching for better performance --- src/modules/common-params/index.ts | 1 + src/modules/generic-details/index.ts | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/modules/common-params/index.ts b/src/modules/common-params/index.ts index 0a618b7..ed336ed 100644 --- a/src/modules/common-params/index.ts +++ b/src/modules/common-params/index.ts @@ -52,6 +52,7 @@ export type SkybridgeParams< price: string; rebateRate: string; + rebalanceRewards: string; }; export const SkybridgeTermsMessage = { diff --git a/src/modules/generic-details/index.ts b/src/modules/generic-details/index.ts index 4cf5d22..2929865 100644 --- a/src/modules/generic-details/index.ts +++ b/src/modules/generic-details/index.ts @@ -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 = { items: Array< @@ -41,12 +42,14 @@ type ReturnType = Pick< | 'feeCurrency' | 'addressSending' | 'isSkypoolsSwap' + | 'rebalanceRewards' > & { txDepositId: SkybridgeParams['txDepositId'] | null; txReceivingId: SkybridgeParams['txReceivingId'] | null; }; const bridgeCache = new Map(); +const rebalanceRewardsCache = new Map(); export const getDetails = async ({ resource, @@ -109,6 +112,22 @@ export const getDetails = 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, @@ -128,6 +147,7 @@ export const getDetails = async ; };