From 1be44ed729773e6fc7e9e139784d9f6bd3a24367 Mon Sep 17 00:00:00 2001 From: Paul V Puey Date: Fri, 10 Nov 2023 17:31:58 -0800 Subject: [PATCH] Add rune support --- src/swap/defi/thorchain.ts | 62 ++++++++++++++++++++++++++++++++------ src/swap/types.ts | 13 +++++++- 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/swap/defi/thorchain.ts b/src/swap/defi/thorchain.ts index aa6b9348..4389c3e7 100644 --- a/src/swap/defi/thorchain.ts +++ b/src/swap/defi/thorchain.ts @@ -19,6 +19,7 @@ import { EdgeSwapQuote, EdgeSwapRequest, EdgeTransaction, + EdgeTxSwap, SwapBelowLimitError, SwapCurrencyError } from 'edge-core-js/types' @@ -40,7 +41,7 @@ import { promiseWithTimeout, QueryParams } from '../../util/utils' -import { EdgeSwapRequestPlugin } from '../types' +import { EdgeSwapRequestPlugin, MakeTxParams } from '../types' import { getEvmApprovalData, getEvmTokenData } from './defiUtils' const pluginId = 'thorchain' @@ -527,6 +528,16 @@ export function makeThorchainPlugin( let publicAddress = thorAddress let approvalData let memoType: EdgeMemo['type'] + + const swapData: EdgeTxSwap = { + isEstimate, + payoutAddress: toAddress, + payoutCurrencyCode: toCurrencyCode, + payoutNativeAmount: toNativeAmount, + payoutWalletId: toWallet.id, + plugin: { ...swapInfo } + } + if (EVM_CURRENCY_CODES[fromMainnetCode]) { memoType = 'hex' if (fromMainnetCode !== fromCurrencyCode) { @@ -562,6 +573,30 @@ export function makeThorchainPlugin( } else { memo = Buffer.from(memo).toString('hex') } + } else if (fromWallet.currencyInfo.pluginId === 'thorchainrune') { + const makeTxParams: MakeTxParams = { + type: 'MakeTxDeposit', + assets: [ + { + amount: fromNativeAmount, + asset: 'THOR.RUNE', + decimals: THOR_LIMIT_UNITS + } + ], + memo, + metadata: {}, + swapData + } + + return { + canBePartial, + maxFulfillmentSeconds, + request, + makeTxParams, + swapInfo, + fromNativeAmount, + expirationDate: new Date(Date.now() + EXPIRATION_MS) + } } else { memoType = 'text' // Cannot yet do tokens on non-EVM chains @@ -615,14 +650,7 @@ export function makeThorchainPlugin( } ], - swapData: { - isEstimate, - payoutAddress: toAddress, - payoutCurrencyCode: toCurrencyCode, - payoutNativeAmount: toNativeAmount, - payoutWalletId: toWallet.id, - plugin: { ...swapInfo } - }, + swapData, otherParams: { outputSort: 'targets' } @@ -678,6 +706,22 @@ const getPool = ( tokenCode: string, pools: Pool[] ): Pool => { + if (mainnetCode === 'THOR' && tokenCode === 'RUNE') { + // Create a fake pool for rune. Use BTC pool to find rune USD price + const btcPool = pools.find(pool => pool.asset === 'BTC.BTC') + + if (btcPool == null) { + throw new SwapCurrencyError(swapInfo, request) + } + const { assetPrice, assetPriceUSD } = btcPool + const pool: Pool = { + asset: 'THOR.RUNE', + assetPrice: '1', + assetPriceUSD: div(assetPriceUSD, assetPrice, 16) + } + return pool + } + const pool = pools.find(pool => { const [asset] = pool.asset.split('-') return asset === `${mainnetCode}.${tokenCode}` diff --git a/src/swap/types.ts b/src/swap/types.ts index 1f6ba3ef..061ab653 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -39,4 +39,15 @@ export type MakeTxParams = * UNIX time (seconds) to expire the DEX swap if it hasn't executed */ expiration?: number - } \ No newline at end of file + } + | { + type: 'MakeTxDeposit' + assets: Array<{ + amount: string + asset: string + decimals: string + }> + memo: string + metadata?: EdgeMetadata + swapData?: EdgeTxSwap + }