Skip to content

Commit

Permalink
Add rune support
Browse files Browse the repository at this point in the history
  • Loading branch information
paullinator committed Nov 11, 2023
1 parent bc8e690 commit 1be44ed
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
62 changes: 53 additions & 9 deletions src/swap/defi/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
EdgeSwapQuote,
EdgeSwapRequest,
EdgeTransaction,
EdgeTxSwap,
SwapBelowLimitError,
SwapCurrencyError
} from 'edge-core-js/types'
Expand All @@ -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'
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -615,14 +650,7 @@ export function makeThorchainPlugin(
}
],

swapData: {
isEstimate,
payoutAddress: toAddress,
payoutCurrencyCode: toCurrencyCode,
payoutNativeAmount: toNativeAmount,
payoutWalletId: toWallet.id,
plugin: { ...swapInfo }
},
swapData,
otherParams: {
outputSort: 'targets'
}
Expand Down Expand Up @@ -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}`
Expand Down
13 changes: 12 additions & 1 deletion src/swap/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,15 @@ export type MakeTxParams =
* UNIX time (seconds) to expire the DEX swap if it hasn't executed
*/
expiration?: number
}
}
| {
type: 'MakeTxDeposit'
assets: Array<{
amount: string
asset: string
decimals: string
}>
memo: string
metadata?: EdgeMetadata
swapData?: EdgeTxSwap
}

0 comments on commit 1be44ed

Please sign in to comment.