Skip to content

Commit 511657a

Browse files
committed
Rango: Add Sui support
1 parent d1fc009 commit 511657a

File tree

1 file changed

+61
-26
lines changed

1 file changed

+61
-26
lines changed

src/swap/defi/rango.ts

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
SwapBelowLimitError,
2323
SwapCurrencyError
2424
} from 'edge-core-js/types'
25+
import { base64 } from 'rfc4648'
2526

2627
import { div18 } from '../../util/biggystringplus'
2728
import {
@@ -84,7 +85,9 @@ const MAINNET_CODE_TRANSCRIPTION: StringMap = {
8485
solana: 'SOLANA',
8586
thorchainrune: 'THOR',
8687
// tron: 'TRON', // Currently only centralized bridges available, so won't return a quote.
87-
zksync: 'ZKSYNC'
88+
zksync: 'ZKSYNC',
89+
// thorchainrune: 'THOR',
90+
sui: 'SUI'
8891
}
8992

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

236+
const asSuiTransaction = asObject({
237+
type: asValue('SUI'),
238+
unsignedPtbBase64: asString
239+
})
240+
233241
const asSwapResponse = asObject({
234242
resultType: asRoutingResultType,
235243
route: asEither(asSwapSimulationResult, asNull),
@@ -238,6 +246,7 @@ const asSwapResponse = asObject({
238246
asEvmTransaction,
239247
asSolanaTransaction,
240248
asCosmosTransaction,
249+
asSuiTransaction,
241250
asNull
242251
),
243252
// Common tracking fields that might be in the response
@@ -368,7 +377,10 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
368377
referrerFee != null &&
369378
referrerFee !== ''
370379
) {
371-
referrer = { referrerAddress: referrerAddress.toLowerCase(), referrerFee }
380+
referrer = {
381+
referrerAddress: referrerAddress.toLowerCase(),
382+
referrerFee
383+
}
372384
}
373385

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

573585
let preTx: EdgeTransaction | undefined
574586
let spendInfo: EdgeSpendInfo
587+
const providersStr = providers?.join(' -> ')
588+
const metadataNotes = `DEX Providers: ${providersStr}`
589+
590+
const swapOrderBase = {
591+
expirationDate: new Date(Date.now() + EXPIRATION_MS),
592+
fromNativeAmount: nativeAmount,
593+
metadataNotes,
594+
minReceiveAmount: route.outputAmountMin,
595+
preTx,
596+
request,
597+
swapInfo
598+
}
575599

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

627651
break
628652
}
653+
case 'SUI': {
654+
const { unsignedPtbBase64 } = asSuiTransaction(tx)
655+
const unsignedTx = base64.parse(unsignedPtbBase64)
656+
657+
const makeTxParams: MakeTxParams = {
658+
type: 'MakeTx',
659+
unsignedTx,
660+
metadata: {
661+
assetAction: {
662+
assetActionType: 'swap'
663+
},
664+
savedAction: {
665+
actionType: 'swap',
666+
swapInfo,
667+
isEstimate: true,
668+
toAsset: {
669+
pluginId: toWallet.currencyInfo.pluginId,
670+
tokenId: toTokenId,
671+
nativeAmount: route.outputAmount
672+
},
673+
fromAsset: {
674+
pluginId: fromWallet.currencyInfo.pluginId,
675+
tokenId: fromTokenId,
676+
nativeAmount
677+
},
678+
payoutAddress: toAddress,
679+
payoutWalletId: toWallet.id,
680+
refundAddress: fromAddress
681+
}
682+
}
683+
}
629684

685+
return { ...swapOrderBase, makeTxParams }
686+
}
630687
case 'COSMOS': {
631688
const cosmosTransaction = asCosmosTransaction(tx)
632689
log(`COSMOS transaction:`, cosmosTransaction)
@@ -680,18 +737,8 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
680737
const metadataNotes = `DEX Providers: ${providersStr}`
681738

682739
// Return SwapOrder with makeTxParams directly (not spendInfo)
683-
return {
684-
expirationDate: new Date(Date.now() + EXPIRATION_MS),
685-
fromNativeAmount: nativeAmount,
686-
metadataNotes,
687-
minReceiveAmount: route.outputAmountMin,
688-
preTx,
689-
request,
690-
makeTxParams,
691-
swapInfo
692-
}
740+
return { ...swapOrderBase, makeTxParams, metadataNotes }
693741
}
694-
695742
default: {
696743
const evmTransaction = asEvmTransaction(tx)
697744
if (evmTransaction.txData == null) {
@@ -783,19 +830,7 @@ export function makeRangoPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
783830
}
784831
}
785832

786-
const providersStr = providers?.join(' -> ')
787-
const metadataNotes = `DEX Providers: ${providersStr}`
788-
789-
return {
790-
expirationDate: new Date(Date.now() + EXPIRATION_MS),
791-
fromNativeAmount: nativeAmount,
792-
metadataNotes,
793-
minReceiveAmount: route.outputAmountMin,
794-
preTx,
795-
request,
796-
spendInfo,
797-
swapInfo
798-
}
833+
return { ...swapOrderBase, spendInfo }
799834
}
800835

801836
const out: EdgeSwapPlugin = {

0 commit comments

Comments
 (0)