Skip to content

Commit 8626ffd

Browse files
committed
Add support for Binance Beacon Chain tokens for Letsexchange
1 parent 971e6d1 commit 8626ffd

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/partners/letsexchange.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { EVM_CHAIN_IDS } from '../util/chainIds'
2525

2626
const MAX_RETRIES = 5
2727
const QUERY_INTERVAL_MS = 1000 * 60 * 60 * 24 * 30 // 30 days in milliseconds
28+
const QUERY_ROLLBACK_MS = 1000 * 60 * 60 * 24 * 7 // 7 days in milliseconds
2829
const LETSEXCHANGE_START_DATE = '2022-02-01T00:00:00.000Z'
2930

3031
/**
@@ -214,6 +215,16 @@ const NATIVE_TOKEN_ADDRESSES = new Set([
214215
'xmr1'
215216
])
216217

218+
// Mapping of chain plugin IDs and currency code to contract address.
219+
const LETSEXCHANGE_NETWORK_CURRENCY_CODE_TO_CONTRACT_ADDRESS: Record<
220+
string,
221+
Record<string, string>
222+
> = {
223+
binance: {
224+
COTI: 'COTI-CBB'
225+
}
226+
}
227+
217228
// In-memory cache for currency contract addresses
218229
// Key format: `${code}_${network_code}` (both lowercase)
219230
interface CoinInfo {
@@ -302,6 +313,13 @@ function getAssetInfo(
302313

303314
// Determine tokenId from the contract address in the response
304315
let tokenId: EdgeTokenId = null
316+
const overrideContractAddress =
317+
LETSEXCHANGE_NETWORK_CURRENCY_CODE_TO_CONTRACT_ADDRESS[chainPluginId]?.[
318+
currencyCode
319+
]
320+
if (overrideContractAddress != null) {
321+
contractAddress = overrideContractAddress
322+
}
305323

306324
if (contractAddress == null) {
307325
// Try to lookup contract address from cache
@@ -354,7 +372,7 @@ export async function queryLetsExchange(
354372
}
355373

356374
// Query from the saved date forward in 30-day chunks (oldest to newest)
357-
let windowStart = new Date(latestIsoDate).getTime() - QUERY_INTERVAL_MS
375+
let windowStart = new Date(latestIsoDate).getTime() - QUERY_ROLLBACK_MS
358376
const now = Date.now()
359377
let done = false
360378
let newTxStart: number = 0

src/util/asEdgeTokenId.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const asEdgeTokenId = asEither(asString, asNull)
55

66
export type TokenType =
77
| 'simple'
8+
| 'binance'
89
| 'evm'
910
| 'cosmos'
1011
| 'xrpl'
@@ -19,7 +20,7 @@ export const tokenTypes: Record<string, TokenType> = {
1920
avalanche: 'evm',
2021
axelar: 'cosmos',
2122
base: 'evm',
22-
binance: null,
23+
binance: 'binance',
2324
binancesmartchain: 'evm',
2425
bitcoin: null,
2526
bitcoincash: null,
@@ -107,6 +108,14 @@ export const createTokenId = (
107108
return contractAddress.toLowerCase().replace(/^0x/, '')
108109
}
109110

111+
case 'binance': {
112+
const regex = /^[A-Z0-9]{3,8}-[0-9A-F]{3}$/
113+
if (!regex.test(contractAddress)) {
114+
throw new Error('Invalid binance contract address')
115+
}
116+
return contractAddress
117+
}
118+
110119
// Cosmos token support:
111120
case 'cosmos': {
112121
// Regexes inspired by a general regex in https://github.com/cosmos/cosmos-sdk

0 commit comments

Comments
 (0)