diff --git a/src/swap/defi/rango.ts b/src/swap/defi/rango.ts index bdb78e11..c92dcdf6 100644 --- a/src/swap/defi/rango.ts +++ b/src/swap/defi/rango.ts @@ -22,6 +22,7 @@ import { SwapBelowLimitError, SwapCurrencyError } from 'edge-core-js/types' +import { base64 } from 'rfc4648' import { div18 } from '../../util/biggystringplus' import { @@ -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'] @@ -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), @@ -238,6 +246,7 @@ const asSwapResponse = asObject({ asEvmTransaction, asSolanaTransaction, asCosmosTransaction, + asSuiTransaction, asNull ), // Common tracking fields that might be in the response @@ -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 = { @@ -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': { @@ -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) @@ -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) { @@ -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 = {