Skip to content
Draft
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
200 changes: 200 additions & 0 deletions docs/newrates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/**
* New rates server should use the following API
* POST /api/v3/rates
* BODY {
* "targetFiat": "USD",
* "crypto": [
* {
* "isoDate": "2025-06-23T15:52:30.000Z",
* "pluginId": "bitcoin",
* },
* {
* "isoDate": "2025-06-23T15:52:30.000Z",
* "pluginId": "ethereum"
* },
* {
* "isoDate": "2025-06-23T15:52:30.000Z",
* "pluginId": "ethereum",
* "tokenId": "dac17f958d2ee523a2206206994597c13d831ec7",
* }
* ],
* "fiat": [{"fiatCode": "GBP"}, {"fiatCode": "EUR"}]
* }
*
* Response would be
* {
* "targetFiat": "USD",
* "crypto": [
* {
* "isoDate": "2025-06-23T15:52:30.000Z",
* "pluginId": "bitcoin",
* "rate": 103102.23
* },
* {
* "isoDate": "2025-06-23T15:52:30.000Z",
* "pluginId": "ethereum"
* "rate": 2304.23
* },
* {
* "isoDate": "2025-06-23T15:52:30.000Z",
* "pluginId": "ethereum",
* "tokenId": "dac17f958d2ee523a2206206994597c13d831ec7",
* "rate": 1.002
* }
* ],
* "fiat": [
* {
* "fiatCode": "GBP",
* "rate": 1.32
* }, {
* "fiatCode": "EUR"
* "rate": 1.08
* }
* ]
* }
*/

/**
* If no date is provided, rate queries should round down to the nearest minute.
* If a date is provided, rate queries should round down to the nearest 5 min interval
* since most historical rates are only accurate to 5 minutes
*/

/**
* Crypto rates are stored in the `rates_data`
* database.
*
* Doc id uses isoDate as the docId
*/

const ratesDocExample = {
_id: '2025-06-23T15:55:00.000Z',
chains: {
bitcoin: {
currencyCode: 'BTC',
Copy link
Copy Markdown
Member Author

@paullinator paullinator Jul 7, 2025

Choose a reason for hiding this comment

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

Change to:

{
  bitcoin: {
   '': {
      cc: 'BTC',
      USD: 104124
    },
    '0xasdlkfj': {
    }
}

USD: 101234.09
},
ethereum: {
currencyCode: 'ETH',
USD: 2310.09
}
},
tokens: {
dac17f958d2ee523a2206206994597c13d831ec7: {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Change to pluginId_tokenId as key

currencyCode: 'USDT',
USD: 1.0001
},
a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48: {
currencyCode: 'USDC',
USD: 1.0002
}
},
fiat: {
CAD: { USD: 0.73 },
EUR: { USD: 1.15 },
GBP: { USD: 1.35 }
}
}

/**
* To prevent double querying assets that exist on
* multiple chains, a document will be used
* to map tokenId assets to another asset on a main chain
* like Ethereum.
*
* The examples below maps Arbitrum and Zksync to Ethereum and
* USDT/USDC on Solana to the same assets on Ethereum.
*
* These docs will be in the `rates_mappings` database
*/

// PluginID mappings should be manually edited, not autogenerated since
// these ONLY refer to Edge pluginIds and not partner slugs
const pluginIdMappings = {
_id: 'pluginIdMappings',
data: {
// Map arbitrum and zksync to ethereum since they are the same asset
arbitrum: 'ethereum',
zksync: 'ethereum'
}
}

/**
* Token mappings should be auto generated from partner APIs. ie. the
* coingecko /api/v3/coins/list?include_platform=true endpoint will return all the
* contract addresses on all chains for each asset supported.
*
* The mappings below should be refreshed once per day
*/
const tokenMappings = {
_id: 'tokenMappings',
data: {
// USDT from solana to ethereum
Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB: {
sourceChain: 'solana', // Only used for easy visual reference
destChain: 'ethereum', // Only used for easy visual reference
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should we determine a pluginId ranking to determine which chain is considered the source or trust the coingecko api to remain constant and use the first platform in the array?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What Paul, William, Matt said. mapping keep growing but always look up main db first, mapping 2nd.

currencyCode: 'USDT', // Only used for easy visual reference
tokenId: 'dac17f958d2ee523a2206206994597c13d831ec7'
},
// USDC from solana to ethereum
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v: {
sourceChain: 'solana', // Only used for easy visual reference
destChain: 'ethereum', // Only used for easy visual reference
currencyCode: 'USDC', // Only used for easy visual reference
tokenId: 'a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
}
}
}

/**
* For each rate provider there will be a document that maps
* the rate provider's unique identifiers to either pluginIds or tokenIds
* The pluginIdMap should be manually entered as it references Edge
* pluginIds. The tokenIdMap should be auto generated from partner APIs
* since it only references contract addresses. For the tokenIds, use the first
* chain that is returned by the partner API. All other chains would first
* get translated by the tokenMappings doc above.
*/

const coinmarketcapPluginIdMap = {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Both cmc and coingecko utilize platform ids which (in 99% of cases) don't match mainnet asset ids. This map should be platform ID and then we can add the mainnet assets to the tokenId mapping below

_id: 'coinmarketcapPluginIdMap',
data: {
// Mapping from Edge pluginIds to CMC ids of main chain assets
bitcoin: 1,
ethereum: 1027,
ripple: 52
}
}

const coinmarketcapTokenIdMap = {
_id: 'coinmarketcapTokenIdMap',
data: {
// Maps the contract address to the CMC id
dac17f958d2ee523a2206206994597c13d831ec7: {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

chains with numbered tokenIds may have collisions

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Use pluginId_tokenId as doc key instead

id: 825,
slug: 'tether'
},
a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48: {
id: 3408,
slug: 'usd-coin'
}
}
}

/**
* Used to map legacy currency codes to pluginId/tokenId which
* would be used for a one time conversion of old db data to
* the new format. This needs to be hand edited :(
*/
const legacyCurrencyCodeMap = {
_id: 'legacyCurrencyCodeMap',
data: {
BTC: { pluginId: 'bitcoin', tokenId: null },
ETH: { pluginId: 'ethereum', tokenId: null },
XRP: { pluginId: 'ripple', tokenId: null },
USDC: {
pluginId: 'ethereum',
tokenId: 'a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
}
}
}
Loading
Loading