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 Rango Exchange #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/demo/partners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export default {
type: 'fiat',
color: '#99A5DE'
},
rango: { type: 'swap', color: '#1C3CF1' },
safello: {
type: 'fiat',
color: deprecated
Expand Down
168 changes: 168 additions & 0 deletions src/partners/rango.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import {
asArray,
asNumber,
asObject,
asOptional,
asString,
asValue
} from 'cleaners'

import { PartnerPlugin, PluginParams, PluginResult, StandardTx } from '../types'
import { datelog } from '../util'

const asRangoPluginParams = asObject({
settings: asObject({
latestIsoDate: asOptional(asString, '0')
}),
apiKeys: asObject({
apiKey: asOptional(asString)
})
})

const asSwapperType = asValue('DEX', 'BRIDGE', 'AGGREGATOR', 'OFF_CHAIN')

const asSwapperData = asObject({
swapperId: asString,
swapperLogo: asString,
swapperTitle: asString,
swapperType: asSwapperType
})

const asBlockchainData = asObject({
blockchain: asString,
displayName: asString,
logo: asString,
shortName: asString,
type: asString
})

const asTokenData = asObject({
blockchainData: asBlockchainData,
symbol: asString,
decimals: asNumber,
name: asOptional(asString),
expectedAmount: asNumber,
logo: asString,
price: asOptional(asNumber),
realAmount: asNumber
})

const asSwapStatus = asValue('running', 'failed', 'success', 'unknown')
const asStepSummary = asObject({
feeUsd: asNumber,
status: asSwapStatus,
stepNumber: asNumber,
fromToken: asTokenData,
toToken: asTokenData,
swapper: asOptional(asSwapperData)
})

const asTransaction = asObject({
requestId: asString,
transactionTime: asString,
status: asSwapStatus,
stepsSummary: asArray(asStepSummary)
})

const asRangoResult = asObject({ transactions: asArray(asTransaction) })

type RangoResult = ReturnType<typeof asRangoResult>

const QUERY_LOOKBACK = 1000 * 60 * 60 * 24 * 5 // 5 days
const OFFSET = 50

export const queryRango = async (
pluginParams: PluginParams
): Promise<PluginResult> => {
const ssFormatTxs: StandardTx[] = []

const { settings, apiKeys } = asRangoPluginParams(pluginParams)
const { apiKey } = apiKeys
let { latestIsoDate } = settings

let previousTimestamp = new Date(latestIsoDate).getTime() - QUERY_LOOKBACK
if (previousTimestamp < 0) previousTimestamp = 0
const previousLatestIsoDate = new Date(previousTimestamp).toISOString()

let done = false
let oldestIsoDate = new Date(99999999999999).toISOString()

let page = 0

while (!done) {
const url = `https://api.rango.exchange/scanner/tx/filter?offset=${OFFSET}&page=${page}&apiKey=${apiKey}`
let jsonObj: RangoResult
try {
const result = await fetch(url, {
method: 'GET'
})
const resultJson: ReturnType<typeof asRangoResult> = await result.json()
jsonObj = asRangoResult(resultJson)
} catch (e) {
datelog(e)
break
}
const txs = jsonObj.transactions
for (const rawTx of txs) {
const { requestId, status, stepsSummary, transactionTime } = rawTx

if (status !== 'success') {
continue
}
const time = new Date(transactionTime)

const depositToken = stepsSummary[0].fromToken
const payoutToken = stepsSummary[stepsSummary.length - 1].toToken
const payoutUsdValue =
payoutToken.price != null
? payoutToken.realAmount * payoutToken.price
: -1

const ssTx: StandardTx = {
status: 'complete',
orderId: requestId,
depositTxid: undefined,
depositAddress: undefined,
depositCurrency: depositToken.symbol,
depositAmount: depositToken.realAmount,
payoutTxid: undefined,
payoutAddress: undefined,
payoutCurrency: payoutToken.symbol,
payoutAmount: payoutToken.realAmount,
timestamp: time.getTime(),
isoDate: time.toISOString(),
usdValue: payoutUsdValue,
rawTx
}

ssFormatTxs.push(ssTx)

if (ssTx.isoDate > latestIsoDate) {
latestIsoDate = ssTx.isoDate
}
if (ssTx.isoDate < oldestIsoDate) {
oldestIsoDate = ssTx.isoDate
}
if (ssTx.isoDate < previousLatestIsoDate && !done) {
datelog(`rango done: date ${ssTx.isoDate} < ${previousLatestIsoDate}`)
done = true
}
}

if (txs.length < OFFSET) {
break
}
page++
}
const out: PluginResult = {
settings: { latestIsoDate },
transactions: ssFormatTxs
}
return out
}

export const rango: PartnerPlugin = {
queryFunc: queryRango,
pluginName: 'Rango Exchange',
pluginId: 'rango'
}
2 changes: 2 additions & 0 deletions src/queryEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { lifi } from './partners/lifi'
import { moonpay } from './partners/moonpay'
import { paybis } from './partners/paybis'
import { paytrie } from './partners/paytrie'
import { rango } from './partners/rango'
import { safello } from './partners/safello'
import { sideshift } from './partners/sideshift'
import { simplex } from './partners/simplex'
Expand Down Expand Up @@ -58,6 +59,7 @@ const plugins = [
moonpay,
paybis,
paytrie,
rango,
safello,
sideshift,
simplex,
Expand Down