Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 61 additions & 26 deletions src/swap/defi/rango.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
SwapBelowLimitError,
SwapCurrencyError
} from 'edge-core-js/types'
import { base64 } from 'rfc4648'

import { div18 } from '../../util/biggystringplus'
import {
Expand Down Expand Up @@ -84,7 +85,9 @@ const MAINNET_CODE_TRANSCRIPTION: StringMap = {
solana: 'SOLANA',
thorchainrune: 'THOR',
// tron: 'TRON', // Currently only centralized bridges available, so won't return a quote.
zksync: 'ZKSYNC'
zksync: 'ZKSYNC',
// thorchainrune: 'THOR',
sui: 'SUI'
}

const RANGO_SERVERS_DEFAULT = ['https://api.rango.exchange']
Expand Down Expand Up @@ -230,6 +233,11 @@ const asCosmosTransaction = asObject({
// expectedOutput: asOptional(asString) // Unused
})

const asSuiTransaction = asObject({
type: asValue('SUI'),
unsignedPtbBase64: asString
})

const asSwapResponse = asObject({
resultType: asRoutingResultType,
route: asEither(asSwapSimulationResult, asNull),
Expand All @@ -238,6 +246,7 @@ const asSwapResponse = asObject({
asEvmTransaction,
asSolanaTransaction,
asCosmosTransaction,
asSuiTransaction,
asNull
),
// Common tracking fields that might be in the response
Expand Down Expand Up @@ -368,7 +377,10 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
referrerFee != null &&
referrerFee !== ''
) {
referrer = { referrerAddress: referrerAddress.toLowerCase(), referrerFee }
referrer = {
referrerAddress: referrerAddress.toLowerCase(),
referrerFee
}
}

const swapParameters = {
Expand Down Expand Up @@ -572,6 +584,18 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {

let preTx: EdgeTransaction | undefined
let spendInfo: EdgeSpendInfo
const providersStr = providers?.join(' -> ')
const metadataNotes = `DEX Providers: ${providersStr}`

const swapOrderBase = {
expirationDate: new Date(Date.now() + EXPIRATION_MS),
fromNativeAmount: nativeAmount,
metadataNotes,
minReceiveAmount: route.outputAmountMin,
preTx,
request,
swapInfo
}

switch (tx.type) {
case 'SOLANA': {
Expand Down Expand Up @@ -626,7 +650,40 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {

break
}
case 'SUI': {
const { unsignedPtbBase64 } = asSuiTransaction(tx)
const unsignedTx = base64.parse(unsignedPtbBase64)

const makeTxParams: MakeTxParams = {
type: 'MakeTx',
unsignedTx,
metadata: {
assetAction: {
assetActionType: 'swap'
},
savedAction: {
actionType: 'swap',
swapInfo,
isEstimate: true,
toAsset: {
pluginId: toWallet.currencyInfo.pluginId,
tokenId: toTokenId,
nativeAmount: route.outputAmount
},
fromAsset: {
pluginId: fromWallet.currencyInfo.pluginId,
tokenId: fromTokenId,
nativeAmount
},
payoutAddress: toAddress,
payoutWalletId: toWallet.id,
refundAddress: fromAddress
}
}
}

return { ...swapOrderBase, makeTxParams }
}
case 'COSMOS': {
const cosmosTransaction = asCosmosTransaction(tx)
log(`COSMOS transaction:`, cosmosTransaction)
Expand Down Expand Up @@ -680,18 +737,8 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
const metadataNotes = `DEX Providers: ${providersStr}`

// Return SwapOrder with makeTxParams directly (not spendInfo)
return {
expirationDate: new Date(Date.now() + EXPIRATION_MS),
fromNativeAmount: nativeAmount,
metadataNotes,
minReceiveAmount: route.outputAmountMin,
preTx,
request,
makeTxParams,
swapInfo
}
return { ...swapOrderBase, makeTxParams, metadataNotes }
}

default: {
const evmTransaction = asEvmTransaction(tx)
if (evmTransaction.txData == null) {
Expand Down Expand Up @@ -783,19 +830,7 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
}
}

const providersStr = providers?.join(' -> ')
const metadataNotes = `DEX Providers: ${providersStr}`

return {
expirationDate: new Date(Date.now() + EXPIRATION_MS),
fromNativeAmount: nativeAmount,
metadataNotes,
minReceiveAmount: route.outputAmountMin,
preTx,
request,
spendInfo,
swapInfo
}
return { ...swapOrderBase, spendInfo }
}

const out: EdgeSwapPlugin = {
Expand Down
Loading