-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from atomex-protocol/feat/atm-361-implement-ge…
…t-redeem-reward Get redeem reward and improve price manager
- Loading branch information
Showing
27 changed files
with
376 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { Currency } from '../../common'; | ||
import type { PriceManager } from '../../exchange'; | ||
import type { AtomexBlockchainProvider } from '../atomexBlockchainProvider'; | ||
import type { FeesInfo } from '../models/index'; | ||
|
||
export const getRedeemRewardInNativeCurrency = async ( | ||
currencyOrId: Currency | Currency['id'], | ||
redeemFee: FeesInfo, | ||
priceManager: PriceManager | ||
): Promise<FeesInfo> => { | ||
const nativeTokenPriceInUsd = await priceManager.getAveragePrice({ baseCurrency: currencyOrId, quoteCurrency: 'USD' }); | ||
if (!nativeTokenPriceInUsd) | ||
throw new Error(`Price for ${currencyOrId} in USD not found`); | ||
|
||
const maxRewardPercentValue = 30; | ||
const maxRewardPercent = 0.15; | ||
const maxRewardForRedeemDeviation = 0.05; | ||
|
||
const redeemFeeInUsd = redeemFee.estimated.multipliedBy(nativeTokenPriceInUsd); | ||
const k = maxRewardPercentValue / Math.log((1 - maxRewardPercent) / maxRewardForRedeemDeviation); | ||
const p = (1 - maxRewardPercent) / Math.exp(redeemFeeInUsd.toNumber() / k) + maxRewardPercent; | ||
|
||
const rewardForRedeem = redeemFee.estimated.multipliedBy(1 + p); | ||
const result: FeesInfo = { estimated: rewardForRedeem, max: rewardForRedeem }; | ||
|
||
return result; | ||
}; | ||
|
||
export const getRedeemRewardInToken = async ( | ||
currencyOrId: Currency | Currency['id'], | ||
redeemFee: FeesInfo, | ||
priceManager: PriceManager, | ||
blockchainProvider: AtomexBlockchainProvider | ||
): Promise<FeesInfo> => { | ||
const currency = typeof currencyOrId === 'string' ? blockchainProvider.getCurrency(currencyOrId) : currencyOrId; | ||
if (!currency) | ||
throw new Error(`Currency info not found for ${currencyOrId}`); | ||
|
||
const nativeCurrency = blockchainProvider.getNativeCurrencyInfo(currency.blockchain)?.currency; | ||
if (!nativeCurrency) | ||
throw new Error(`Native currency not found fir ${currency.blockchain}`); | ||
|
||
const nativeTokenPriceInCurrency = await priceManager.getAveragePrice({ baseCurrency: nativeCurrency, quoteCurrency: currencyOrId }); | ||
|
||
if (!nativeTokenPriceInCurrency) | ||
throw new Error(`Price for ${nativeCurrency.id} in ${currencyOrId} not found`); | ||
|
||
const inNativeToken = await getRedeemRewardInNativeCurrency(nativeCurrency.id, redeemFee, priceManager); | ||
|
||
return { | ||
estimated: inNativeToken.estimated.multipliedBy(nativeTokenPriceInCurrency), | ||
max: inNativeToken.max.multipliedBy(nativeTokenPriceInCurrency) | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.