Skip to content

Commit

Permalink
fixup! Add Rango Exchange plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
paullinator committed Jun 20, 2024
1 parent 5f916b8 commit 08408c4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const plugins = {
godex: makeGodexPlugin,
letsexchange: makeLetsExchangePlugin,
lifi: makeLifiPlugin,
rango: makeRangoPlugin,
sideshift: makeSideshiftPlugin,
spookySwap: makeSpookySwapPlugin,
swapuz: makeSwapuzPlugin,
Expand All @@ -38,7 +39,6 @@ const plugins = {
transfer: makeTransferPlugin,
velodrome: makeVelodromePlugin,
xrpdex,
rango: makeRangoPlugin
}

declare global {
Expand Down
71 changes: 39 additions & 32 deletions src/swap/defi/rango.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ const rangoTokens: {

let exchangeInfo: ExchangeInfo | undefined
let exchangeInfoLastUpdate = 0
let metaRequestPromise: Promise<void>

export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
const { io, log } = opts
Expand Down Expand Up @@ -224,10 +225,12 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
}
)

metaRequest
metaRequestPromise = metaRequest
.then(metaResponse => {
if (metaResponse.ok) {
return metaResponse.json()
} else {
return metaResponse.text()
}
})
.then(meta => {
Expand All @@ -244,15 +247,15 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
] = tokenSymbol
})
})
.catch(() => {})
.catch(e => {
console.error('Error fetching Rango meta', e)
})

const fetchSwapQuoteInner = async (
request: EdgeSwapRequestPlugin
): Promise<SwapOrder> => {
const {
fromCurrencyCode,
fromTokenId,
toCurrencyCode,
toTokenId,
nativeAmount,
fromWallet,
Expand All @@ -263,12 +266,14 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
throw new SwapCurrencyError(swapInfo, request)
}

await metaRequestPromise

const fromToken =
fromTokenId != null
? fromWallet.currencyConfig.allTokens[fromTokenId]
: undefined
let fromContractAddress
if (fromCurrencyCode === fromWallet.currencyInfo.currencyCode) {
if (fromTokenId === null) {
fromContractAddress = PARENT_TOKEN_CONTRACT_ADDRESS
} else {
fromContractAddress = fromToken?.networkLocation?.contractAddress
Expand All @@ -279,7 +284,7 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
? toWallet.currencyConfig.allTokens[toTokenId]
: undefined
let toContractAddress
if (toCurrencyCode === toWallet.currencyInfo.currencyCode) {
if (toTokenId === null) {
toContractAddress = PARENT_TOKEN_CONTRACT_ADDRESS
} else {
toContractAddress = toToken?.networkLocation?.contractAddress
Expand All @@ -305,7 +310,16 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
const toMainnetCode =
MAINNET_CODE_TRANSCRIPTION[toWallet.currencyInfo.pluginId]

if (fromMainnetCode === '' || toMainnetCode === '') {
if (fromMainnetCode == null || toMainnetCode == null) {
throw new SwapCurrencyError(swapInfo, request)
}

const fromSymbol =
rangoTokens[fromMainnetCode]?.[fromContractAddress.toLowerCase()]
const toSymbol =
rangoTokens[toMainnetCode]?.[toContractAddress.toLowerCase()]

if (fromSymbol == null || toSymbol == null) {
throw new SwapCurrencyError(swapInfo, request)
}

Expand Down Expand Up @@ -337,15 +351,6 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
}
}

const fromSymbol =
rangoTokens?.[fromMainnetCode]?.[fromContractAddress.toLowerCase()]
const toSymbol =
rangoTokens?.[toMainnetCode]?.[toContractAddress.toLowerCase()]

if (fromSymbol === undefined || toSymbol === undefined) {
throw new Error('Rango could not proceed with the exchange')
}

if (exchangeInfo != null) {
const { rango } = exchangeInfo.swap.plugins
rangoServers = rango?.rangoServers ?? rangoServers
Expand All @@ -368,9 +373,9 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
amount: nativeAmount,
disableEstimate: true,
slippage: DEFAULT_SLIPPAGE,
...(referrerAddress !== undefined &&
...(referrerAddress != null &&
referrerAddress !== '' &&
referrerFee !== undefined &&
referrerFee != null &&
referrerFee !== '' && { referrerAddress, referrerFee })
}

Expand Down Expand Up @@ -416,11 +421,11 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {

const spendInfo: EdgeSpendInfo = {
tokenId: null,
memos: [{ type: 'hex', value: approvalData}],
spendTargets: [
{
memo: approvalData,
nativeAmount: '0',
publicAddress: tx.approveTo
publicAddress: fromContractAddress
}
],
networkFeeOption: 'custom',
Expand All @@ -442,23 +447,35 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
preTx = await request.fromWallet.makeSpend(spendInfo)
}

const customNetworkFee = {
gasLimit: tx.gasLimit != null ? hexToDecimal(tx.gasLimit) : undefined,
gasPrice: tx.gasPrice != null ? div18(tx.gasPrice, '1000000000') : undefined,
maxFeePerGas: tx.maxFeePerGas ?? undefined,
maxPriorityFeePerGas: tx.maxPriorityFeePerGas ?? undefined
}

const networkFeeOption: EdgeSpendInfo['networkFeeOption'] =
(customNetworkFee.gasLimit != null || customNetworkFee.gasPrice != null) ? 'custom' : undefined

const spendInfo: EdgeSpendInfo = {
tokenId: request.fromTokenId,
memos: [{ type: 'hex', value: tx.txData}],
customNetworkFee,
spendTargets: [
{
memo: tx.txData,
nativeAmount: nativeAmount,
publicAddress: tx.txTo
}
],
networkFeeOption: 'custom',
networkFeeOption,
assetAction: {
assetActionType: 'swap'
},
savedAction: {
actionType: 'swap',
swapInfo,
isEstimate: false,
isEstimate: true,
toAsset: {
pluginId: toWallet.currencyInfo.pluginId,
tokenId: toTokenId,
Expand All @@ -475,16 +492,6 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
}
}

if (tx.gasPrice != null && tx.gasLimit != null) {
const gasPriceGwei = div18(tx.gasPrice, '1000000000')

const gasLimit = hexToDecimal(tx.gasLimit)
spendInfo.customNetworkFee = {
gasLimit,
gasPrice: gasPriceGwei
}
}

if (
tx.gasLimit != null &&
tx.maxFeePerGas != null &&
Expand Down
8 changes: 8 additions & 0 deletions test/testconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ export const asTestConfig = asObject({
PULSECHAIN_INIT: asCorePluginInit(asEvmApiKeys),

POLYGON_INIT: asCorePluginInit(asEvmApiKeys),
RANGO_EXCHANGE_INIT: asCorePluginInit(
asObject({
appId: asOptional(asString, 'edge'),
rangoApiKey: asString,
referrerAddress: asOptional(asString),
referrerFee: asOptional(asString)
}).withRest
),
SIDESHIFT_INIT: asCorePluginInit(
asObject({
affiliateId: asOptional(asString, '')
Expand Down

0 comments on commit 08408c4

Please sign in to comment.