Skip to content

Commit

Permalink
use generated bridge limits json to provide min/max values
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboydiamonds committed Sep 24, 2024
1 parent 60138d4 commit 64f6816
Showing 1 changed file with 115 additions and 76 deletions.
191 changes: 115 additions & 76 deletions packages/rest-api/src/controllers/bridgeLimitsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { parseUnits } from '@ethersproject/units'
import { Synapse } from '../services/synapseService'
import { tokenAddressToToken } from '../utils/tokenAddressToToken'
import { formatBNToString } from '../utils/formatBNToString'
import BRIDGE_LIMITS_MAP from '../constants/limitsMap.ts'

export const bridgeLimitsController = async (req, res) => {
const errors = validationResult(req)
Expand All @@ -17,86 +18,104 @@ export const bridgeLimitsController = async (req, res) => {
const fromTokenInfo = tokenAddressToToken(fromChain, fromToken)
const toTokenInfo = tokenAddressToToken(toChain, toToken)

const upperLimitValue = parseUnits('1000000', fromTokenInfo.decimals)
const upperLimitBridgeQuotes = await Synapse.allBridgeQuotes(
Number(fromChain),
Number(toChain),
fromTokenInfo.address,
toTokenInfo.address,
upperLimitValue
)

const lowerLimitValues = ['0.01', '10']
let lowerLimitBridgeQuotes = null

for (const limit of lowerLimitValues) {
const lowerLimitAmount = parseUnits(limit, fromTokenInfo.decimals)

lowerLimitBridgeQuotes = await Synapse.allBridgeQuotes(
Number(fromChain),
Number(toChain),
fromTokenInfo.address,
toTokenInfo.address,
lowerLimitAmount
)

if (lowerLimitBridgeQuotes && lowerLimitBridgeQuotes.length > 0) {
break
}
let maxOriginAmount = null
let minOriginAmount = null

if (
BRIDGE_LIMITS_MAP[fromChain][fromTokenInfo.address].routes[toChain][
toTokenInfo.address
]
) {
minOriginAmount =
BRIDGE_LIMITS_MAP[fromChain][fromTokenInfo.address].routes[toChain][
toTokenInfo.address
].minOriginValue
maxOriginAmount =
BRIDGE_LIMITS_MAP[fromChain][fromTokenInfo.address].routes[toChain][
toTokenInfo.address
].maxOriginValue
}

const maxBridgeAmountQuote = upperLimitBridgeQuotes.reduce(
(maxQuote, currentQuote) => {
const currentMaxAmount = currentQuote.maxAmountOut
const maxAmount = maxQuote ? maxQuote.maxAmountOut : BigNumber.from(0)

return currentMaxAmount.gt(maxAmount) ? currentQuote : maxQuote
},
null
)

const minBridgeAmountQuote = lowerLimitBridgeQuotes.reduce(
(minQuote, currentQuote) => {
const currentFeeAmount = currentQuote.feeAmount
const minFeeAmount = minQuote ? minQuote.feeAmount : null

return !minFeeAmount || currentFeeAmount.lt(minFeeAmount)
? currentQuote
: minQuote
},
null
)

if (!maxBridgeAmountQuote || !minBridgeAmountQuote) {
return res.json({
maxOriginAmount: null,
minOriginAmount: null,
})
}

const maxAmountOriginQueryTokenOutInfo = tokenAddressToToken(
toChain,
maxBridgeAmountQuote.destQuery.tokenOut
)

const minAmountOriginQueryTokenOutInfo = tokenAddressToToken(
fromChain,
minBridgeAmountQuote.originQuery.tokenOut
)

const maxOriginAmount = formatBNToString(
maxBridgeAmountQuote.maxAmountOut,
maxAmountOriginQueryTokenOutInfo.decimals
)

const minOriginAmount = formatBNToString(
minBridgeAmountQuote.feeAmount,
minAmountOriginQueryTokenOutInfo.decimals
)
// const upperLimitValue = parseUnits('1000000', fromTokenInfo.decimals)
// const upperLimitBridgeQuotes = await Synapse.allBridgeQuotes(
// Number(fromChain),
// Number(toChain),
// fromTokenInfo.address,
// toTokenInfo.address,
// upperLimitValue
// )

// const lowerLimitValues = ['0.01', '10']
// let lowerLimitBridgeQuotes = null

// for (const limit of lowerLimitValues) {
// const lowerLimitAmount = parseUnits(limit, fromTokenInfo.decimals)

// lowerLimitBridgeQuotes = await Synapse.allBridgeQuotes(
// Number(fromChain),
// Number(toChain),
// fromTokenInfo.address,
// toTokenInfo.address,
// lowerLimitAmount
// )

// if (lowerLimitBridgeQuotes && lowerLimitBridgeQuotes.length > 0) {
// break
// }
// }

// const maxBridgeAmountQuote = upperLimitBridgeQuotes.reduce(
// (maxQuote, currentQuote) => {
// const currentMaxAmount = currentQuote.maxAmountOut
// const maxAmount = maxQuote ? maxQuote.maxAmountOut : BigNumber.from(0)

// return currentMaxAmount.gt(maxAmount) ? currentQuote : maxQuote
// },
// null
// )

// const minBridgeAmountQuote = lowerLimitBridgeQuotes.reduce(
// (minQuote, currentQuote) => {
// const currentFeeAmount = currentQuote.feeAmount
// const minFeeAmount = minQuote ? minQuote.feeAmount : null

// return !minFeeAmount || currentFeeAmount.lt(minFeeAmount)
// ? currentQuote
// : minQuote
// },
// null
// )

// if (!maxBridgeAmountQuote || !minBridgeAmountQuote) {
// return res.json({
// maxOriginAmount: null,
// minOriginAmount: null,
// })
// }

// const maxAmountOriginQueryTokenOutInfo = tokenAddressToToken(
// toChain,
// maxBridgeAmountQuote.destQuery.tokenOut
// )

// const minAmountOriginQueryTokenOutInfo = tokenAddressToToken(
// fromChain,
// minBridgeAmountQuote.originQuery.tokenOut
// )

// maxOriginAmount = formatBNToString(
// maxBridgeAmountQuote.maxAmountOut,
// maxAmountOriginQueryTokenOutInfo.decimals
// )

// minOriginAmount = formatBNToString(
// minBridgeAmountQuote.feeAmount,
// minAmountOriginQueryTokenOutInfo.decimals
// )

return res.json({
maxOriginAmount,
minOriginAmount,
maxOriginAmount: processAmount(maxOriginAmount),
minOriginAmount: processAmount(minOriginAmount),
})
} catch (err) {
res.status(500).json({
Expand All @@ -106,3 +125,23 @@ export const bridgeLimitsController = async (req, res) => {
})
}
}

const processAmount = (amount: string): number | null => {
const value = parseFloat(amount)

if (isNaN(value)) {
return null
}

if (value < 0.01) {
return value // If the value is less than 0.01, keep the same value
} else if (value >= 0.01 && value < 0.1) {
return 0.1 // If the value is between 0.01 and 0.1, use 0.1
} else if (value >= 0.1 && value < 0.5) {
return 0.5 // If the value is between 0.1 and 0.5, use 0.5
} else if (value >= 0.5 && value <= 1900000) {
return value // If the value is between 0.5 and 1900000, use that value
} else {
return null // If the value is above 1900000, set value to null
}
}

0 comments on commit 64f6816

Please sign in to comment.