From 027b1b5127d6a8afd519254d322571487f7feec2 Mon Sep 17 00:00:00 2001 From: Paul V Puey Date: Mon, 15 May 2023 21:40:03 -0700 Subject: [PATCH] Implement lifi withdrawal script --- package.json | 1 + src/bin/lifiReporter.ts | 80 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/bin/lifiReporter.ts diff --git a/package.json b/package.json index b613193..f0d8987 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "clean": "rimraf lib dist .parcel-cache", "fix": "npm run lint -- --fix", "fio:promo": "node -r sucrase/register src/bin/fioPromo/fioPromo.ts", + "lifi": "node -r sucrase/register src/bin/lifiReporter.ts", "precommit": "lint-staged && npm run prepare", "prepare": "./scripts/prepare.sh && npm-run-all clean -p build.*", "start": "node -r sucrase/register src/indexQuery.ts", diff --git a/src/bin/lifiReporter.ts b/src/bin/lifiReporter.ts new file mode 100644 index 0000000..2cc4d78 --- /dev/null +++ b/src/bin/lifiReporter.ts @@ -0,0 +1,80 @@ +import { asArray, asNumber, asObject, asString } from 'cleaners' +import fetch from 'node-fetch' + +const asIntegrators = asObject({ + feeBalances: asArray( + asObject({ + tokenBalances: asArray( + asObject({ + amountUsd: asString, + token: asObject({ + name: asString, + symbol: asString, + address: asString, + chainId: asNumber + }) + }) + ) + }) + ) +}) + +const asTransactionRequest = asObject({ + transactionRequest: asObject({ + data: asString, + to: asString + }) +}) + +const url = 'https://li.quest' + +const main = async (): Promise => { + const response = await fetch(`${url}/v1/integrators/edgeapp`) + if (!response.ok) { + const text = await response.text() + throw new Error(text) + } + + const minAmount = Number(process.argv[2] ?? 100) + + const result = await response.json() + const integrators = asIntegrators(result) + let balUsd = 0 + const tokenAddresses: { [chainId: string]: string[] } = {} + console.log(JSON.stringify(integrators, null, 2)) + integrators.feeBalances.forEach(fb => { + fb.tokenBalances.forEach(tb => { + const amount = Number(tb.amountUsd) + if (amount >= minAmount) { + balUsd += amount + if (tokenAddresses[tb.token.chainId] === undefined) { + tokenAddresses[tb.token.chainId] = [] + } + tokenAddresses[tb.token.chainId].push(tb.token.address) + console.log( + `chainId:${tb.token.chainId} ${tb.token.symbol} (${tb.token.address}): $${tb.amountUsd}` + ) + } + }) + }) + console.log(`Total: $${balUsd}\n`) + for (const chainId in tokenAddresses) { + console.log(`\n**********************************`) + console.log(`chainId:${chainId}\n`) + const tokens = tokenAddresses[chainId].join(',') + const response = await fetch( + `${url}/v1/integrators/edgeapp/withdraw/${chainId}?tokenAddresses=${tokens}` + ) + + if (!response.ok) { + const text = await response.text() + throw new Error(text) + } + + const result = asTransactionRequest(await response.json()) + console.log(`to address: ${result.transactionRequest.to}`) + console.log(`data: ${result.transactionRequest.data}`) + } +} + +main().catch(e => console.log(e))