Add only swaps token router api - endpoints, sync logic, and documentation#245
Add only swaps token router api - endpoints, sync logic, and documentation#245
Conversation
| { | ||
| "name": "onlyswaps-token-routes-api", | ||
| "version": "0.0.1", | ||
| "description": "Token mapping indexer for Only Swaps protocol", |
There was a problem hiding this comment.
| "description": "Token mapping indexer for Only Swaps protocol", | |
| "description": "Token mapping indexer for the only swaps protocol", |
|
|
||
| router.get("/", async (req, res) => { | ||
| try { | ||
| const { srcChainId, dstChainId } = req.query; |
There was a problem hiding this comment.
I'm always a little nervous using capital letters in query params as the spec doesn't require case sensitivity
There was a problem hiding this comment.
in practice it's probably fine though
|
|
||
| res.json({ | ||
| ...token, | ||
| networkName: network?.name |
There was a problem hiding this comment.
if there's no network here, it's probably a 500
| @@ -0,0 +1,95 @@ | |||
| import { NetworkConfig } from "../types"; | |||
|
|
|||
| export const networks: NetworkConfig[] = [ | |||
There was a problem hiding this comment.
Initially I was going to suggest that we make this configurable via a config file, but on second thoughts it might be nicer to have it concretely here in typescript so adding new networks requires adding them here.
It's possible we could even feed the contract deployment job from here.
Then again, we could instead centralise the list of networks somewhere else and feed this app from it. Can't decide which is nicer hmm
| private db!: Low<Database>; | ||
|
|
||
| async initialize() { | ||
| const file = path.join(__dirname, "../../data/db.json"); |
There was a problem hiding this comment.
this filepath seems odd - would suggest feeding from an env var or using something like ~/onlyswaps/token-api/db.json as a default
| const totalBlocks = currentBlock - fromBlock; | ||
| logger.info(`[${networkName}] Syncing from block ${fromBlock} to ${currentBlock} (${totalBlocks} blocks)`); | ||
|
|
||
| const chunkSize = Number(process.env.BLOCK_CHUNK_SIZE) || 10000; |
There was a problem hiding this comment.
would suggest centralising the reading of env vars into a config object to make it obvious what can/can't be configured, and pass the config object in the constructor
| // Get TokenMappingRemoved events | ||
| const removedEvents = await provider.getLogs({ | ||
| address: contract.target, | ||
| topics: [ethers.id("TokenMappingRemoved(uint256,address,address)")], |
There was a problem hiding this comment.
this repeats the ABI in our const at the top - could maybe separate it out into its own const so we only have to change it in one place
| await this.db.updateNetworkBlockNumber(chainId, end); | ||
|
|
||
| // Add delay between chunks to avoid rate limiting | ||
| const delayMs = Number(process.env.REQUEST_DELAY_MS) || 200; |
| logger.info(`[${networkName}] Sync complete. Last block: ${currentBlock}`); | ||
| } | ||
|
|
||
| private sleep(ms: number): Promise<void> { |
There was a problem hiding this comment.
can extract to a util function somewhere rather than method
| } | ||
| } | ||
|
|
||
| async syncHistoricalEvents(chainId: number, fromBlock: number = 0) { |
There was a problem hiding this comment.
the logic in this func is nice and clear, but it's very long - if it's possible to split it down into smaller chunks without making artificial functions it might be even clearer
PR adds Only Swaps token routes API with endpoints, events syncing logic, and documentation.
Features:
data/db.jsonKnown Issues and Limitations:
onlyswaps-token-routes-api/src/config/networks.tsfile.How to Use:
.envand populate the parameters: RPC URLs, router addresses, and start blocks similar to.env.examplenpm run devin development mode to launch indexer + API. See README for production mode.READMEexamples, e.g., List all token mappings (optionally filtered by srcChainId and dstChainId) -curl -s "http://localhost:3000/api/mappings/?srcChainId=1&dstChainId=10" | jq