Skip to content

Commit

Permalink
Whitelist plugins for ChangeHero plugin
Browse files Browse the repository at this point in the history
We can't rely on their blockchain names to match ours so we need to whitelist them manually
  • Loading branch information
peachbits committed Oct 6, 2023
1 parent 927b366 commit 2ebd1bf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- changed: Restrict ChangeHero trading to whitelisted plugins
- changed: Replace deprecated currency codes in `SwapCurrencyError` with requests

## 0.21.9 (2023-10-02)
Expand Down
56 changes: 45 additions & 11 deletions src/swap/changehero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import {

import {
checkInvalidCodes,
getCodes,
getCodesWithTranscription,
getMaxSwappable,
InvalidCurrencyCodes,
makeSwapPluginQuote,
SwapOrder
} from '../swap-helpers'
import { convertRequest, getAddress } from '../util/utils'
import { EdgeSwapRequestPlugin } from './types'
import { EdgeSwapRequestPlugin, StringMap } from './types'

const pluginId = 'changehero'

Expand All @@ -43,6 +43,33 @@ const asInitOptions = asObject({
apiKey: asString
})

const MAINNET_CODE_TRANSCRIPTION: StringMap = {
ethereum: 'ethereum',
binancesmartchain: 'binance_smart_chain',
solana: 'solana',
algorand: 'algorand',
avalanche: 'avalanche_(c-chain)',
bitcoincash: 'bitcoin_cash',
bitcoinsv: 'bitcoin_sv',
bitcoin: 'bitcoin',
tron: 'tron',
polygon: 'polygon',
dash: 'dash',
digibyte: 'digibyte',
dogecoin: 'doge',
polkadot: 'polkadot',
ethereumclassic: 'ethereum_classic',
optimism: 'optimism',
hedera: 'hedera',
litecoin: 'litecoin',
qtum: 'qtum',
stellar: 'stellar',
monero: 'monero',
ripple: 'ripple',
tezos: 'tezos',
zcash: 'zcash'
}

// See https://changehero.io/currencies for list of supported currencies
const INVALID_CURRENCY_CODES: InvalidCurrencyCodes = {
from: {
Expand Down Expand Up @@ -95,11 +122,7 @@ function checkReply(
reply.error.code === -32602 ||
(reply.error.message?.includes('Invalid currency:') ?? false)
) {
throw new SwapCurrencyError(
swapInfo,
request.fromCurrencyCode,
request.toCurrencyCode
)
throw new SwapCurrencyError(swapInfo, request)
}
throw new Error('ChangeHero error: ' + JSON.stringify(reply.error))
}
Expand Down Expand Up @@ -134,11 +157,22 @@ export function makeChangeHeroPlugin(
getAddress(request.fromWallet),
getAddress(request.toWallet)
])
const { fromCurrencyCode, toCurrencyCode } = getCodes(request)

// The chain codes are undocumented but ChangeHero uses Edge pluginIds for these values (confirmed via Slack)
const fromMainnetCode = request.fromWallet.currencyInfo.pluginId
const toMainnetCode = request.toWallet.currencyInfo.pluginId
// Supported chains must be whitelisted
if (
MAINNET_CODE_TRANSCRIPTION[request.fromWallet.currencyInfo.pluginId] ==
null ||
MAINNET_CODE_TRANSCRIPTION[request.toWallet.currencyInfo.pluginId] == null
) {
throw new SwapCurrencyError(swapInfo, request)
}

const {
fromCurrencyCode,
toCurrencyCode,
fromMainnetCode,
toMainnetCode
} = getCodesWithTranscription(request, MAINNET_CODE_TRANSCRIPTION)

const quoteAmount =
request.quoteFor === 'from'
Expand Down

0 comments on commit 2ebd1bf

Please sign in to comment.