Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BSC support for Thorchain swaps #307

Merged
merged 3 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- added: Support BSC for Thorchain swaps

## 1.1.1 (2023-11-24)

- fixed: Return proper min amount error with Thorchain
Expand Down
16 changes: 10 additions & 6 deletions src/swap/defi/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export const EVM_CURRENCY_CODES: { [cc: string]: boolean } = {
AVAX: true,
BCH: false,
BNB: false,
BSC: true,
BTC: false,
DOGE: false,
ETC: true,
Expand All @@ -147,6 +148,7 @@ export const EVM_CURRENCY_CODES: { [cc: string]: boolean } = {
export const MAINNET_CODE_TRANSCRIPTION: { [cc: string]: ChainTypes } = {
avalanche: 'AVAX',
binancechain: 'BNB',
binancesmartchain: 'BSC',
bitcoin: 'BTC',
bitcoincash: 'BCH',
dogecoin: 'DOGE',
Expand Down Expand Up @@ -545,7 +547,7 @@ export function makeThorchainPlugin(

if (EVM_CURRENCY_CODES[fromMainnetCode]) {
memoType = 'hex'
if (fromMainnetCode !== fromCurrencyCode) {
if (fromTokenId != null) {
if (router == null)
throw new Error(`Missing router address for ${fromMainnetCode}`)
if (sourceTokenContractAddress == null)
Expand Down Expand Up @@ -606,7 +608,7 @@ export function makeThorchainPlugin(
} else {
memoType = 'text'
// Cannot yet do tokens on non-EVM chains
if (fromMainnetCode !== fromCurrencyCode) {
if (fromTokenId != null) {
throw new SwapCurrencyError(swapInfo, request)
}
}
Expand Down Expand Up @@ -663,11 +665,12 @@ export function makeThorchainPlugin(
}

if (EVM_CURRENCY_CODES[fromMainnetCode]) {
if (fromMainnetCode === fromCurrencyCode) {
if (fromTokenId == null) {
// For mainnet coins of EVM chains, use gasLimit override since makeSpend doesn't
// know how to estimate an ETH spend with extra data
const gasLimit = getGasLimit(fromMainnetCode, fromCurrencyCode)
const gasLimit = getGasLimit(fromMainnetCode, fromTokenId)
if (gasLimit != null) {
spendInfo.networkFeeOption = 'custom'
spendInfo.customNetworkFee = {
...spendInfo.customNetworkFee,
gasLimit
Expand Down Expand Up @@ -999,6 +1002,7 @@ type ChainTypes =
| 'BTC'
| 'ETH'
| 'BCH'
| 'BSC'
| 'DOGE'
| 'LTC'
| 'AVAX'
Expand Down Expand Up @@ -1133,10 +1137,10 @@ const getQuote = async (

export const getGasLimit = (
chain: ChainTypes,
asset: string
tokenId: string | undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use ? to make tokenId optional?

): string | undefined => {
if (EVM_CURRENCY_CODES[chain]) {
if (chain === asset) {
if (tokenId == null) {
return EVM_SEND_GAS
} else {
return EVM_TOKEN_SEND_GAS
Expand Down
2 changes: 1 addition & 1 deletion src/swap/defi/thorchainDa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ export function makeThorchainDaPlugin(
if (fromMainnetCode === fromCurrencyCode) {
// For mainnet coins of EVM chains, use gasLimit override since makeSpend doesn't
// know how to estimate an ETH spend with extra data
const gasLimit = getGasLimit(fromMainnetCode, fromCurrencyCode)
const gasLimit = getGasLimit(fromMainnetCode, fromTokenId)
if (gasLimit != null) {
spendInfo.customNetworkFee = {
...spendInfo.customNetworkFee,
Expand Down
Loading