From 771a882dc83c11e212413cfb1d0205c09520413f Mon Sep 17 00:00:00 2001 From: timbotronic Date: Sat, 21 May 2022 09:52:59 +0100 Subject: [PATCH] Added CoinGecko price feed --- package.json | 5 +++++ scripts/harvest.ts | 6 +++--- static/tokens.ts | 5 +++++ tsconfig.json | 4 ++-- utils/fetch.ts | 20 ++++++++++++++++++ utils/helpers.ts | 52 ++++++++++++++++++++++++++++++++++++++-------- yarn.lock | 22 +++++++++++++++----- 7 files changed, 95 insertions(+), 19 deletions(-) create mode 100644 static/tokens.ts create mode 100644 utils/fetch.ts diff --git a/package.json b/package.json index 0bf0a03..134b82a 100644 --- a/package.json +++ b/package.json @@ -17,11 +17,16 @@ "big.js": "^6.1.1", "bitcoinjs-lib": "^6.0.1", "bn.js": "^5.2.0", + "colors": "^1.4.0", "dotenv": "^16.0.1", + "node-fetch": "2", "prettier": "^2.6.2", "semver": "^7.3.7", "sudo-fs-promise": "^1.0.1", "ts-node": "^10.7.0", "typescript": "^4.6.4" + }, + "resolutions": { + "async": "2.6.4" } } diff --git a/scripts/harvest.ts b/scripts/harvest.ts index 2ad6aa3..f6cb206 100644 --- a/scripts/harvest.ts +++ b/scripts/harvest.ts @@ -7,11 +7,11 @@ async function main() { const ktContext = await setup() await ktContext.printStats() const karContext = await karuraApi() - await karContext.printStats() + const kintHarvest = await ktContext.getKintPending() const ksmHarvest = Number(kintHarvest) * Number(await karContext.getKsmKintPrice()) - console.log(`🌾 Harvestable Amount: ${kintHarvest} KINT / ${ksmHarvest.toFixed(2)} KSM`) - console.log('=============================') + await karContext.printStats(kintHarvest, ksmHarvest) + const rl = readline.createInterface({ input, output }) const answer1 = await rl.question( diff --git a/static/tokens.ts b/static/tokens.ts new file mode 100644 index 0000000..03ce832 --- /dev/null +++ b/static/tokens.ts @@ -0,0 +1,5 @@ +export const kint = { Token: 'KINT' } +export const ksm = { Token: 'KSM' } +export const kbtc = { Token: 'KBTC' } +export const kusd = { Token: 'KUSD' } +export const kar = { Token: 'KAR' } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 1dfbc91..1d05046 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,7 @@ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ /* Language and Environment */ - "target": "es2018", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ @@ -24,7 +24,7 @@ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ /* Modules */ - "module": "commonjs", /* Specify what module code is generated. */ + "module": "commonJs", /* Specify what module code is generated. */ // "rootDir": "./", /* Specify the root folder within your source files. */ // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ diff --git a/utils/fetch.ts b/utils/fetch.ts new file mode 100644 index 0000000..e969fa7 --- /dev/null +++ b/utils/fetch.ts @@ -0,0 +1,20 @@ +const fetch = require('node-fetch') +const cgUri = 'https://api.coingecko.com/api/v3' + +export const getCgPrice = async (asset: string) => { + try { + const resp = await fetch(cgUri + `/simple/price?ids=${asset}&vs_currencies=usd`, { method: 'GET' }) + const json = await resp.json() + return json[asset].usd + } catch (e) { + console.log("coingecko call failed with: ", e) + return -1 + } +} + +async function main() { + const price = await getCgPrice('kintsugi') + console.log(price) +} + +// main() diff --git a/utils/helpers.ts b/utils/helpers.ts index 975acfd..1010792 100644 --- a/utils/helpers.ts +++ b/utils/helpers.ts @@ -1,18 +1,20 @@ import 'dotenv/config' import { ApiPromise, WsProvider, Keyring } from '@polkadot/api' import { payments } from 'bitcoinjs-lib' +import { getCgPrice } from '../utils/fetch' import Big from 'big.js' import { options } from '@acala-network/api' -import { parseConfigFileTextToJson } from 'typescript' -// const fs = require('fs') -// var fs = require('@mh-cbon/sudo-fs'); +// import * as tokens from "../static/tokens" +const fetch = require('node-fetch') var fs = require('sudo-fs-promise') +var colors = require('colors') const kint = { Token: 'KINT' } const ksm = { Token: 'KSM' } const kbtc = { Token: 'KBTC' } const kusd = { Token: 'KUSD' } const kar = { Token: 'KAR' } +const cgUri = 'https://api.coingecko.com/api/v3' const setupKeys = async (api: ApiPromise) => { let signer @@ -294,14 +296,27 @@ export const karuraApi = async () => { return Number(kintPrice / ksmPrice).toFixed(5) } - const printStats = async () => { - // console.log(signer.addressRaw) - // console.log(signer.publicKey) - // console.log(sig) + const printStats = async (kintHarvest, ksmHarvest) => { + const kintPrice = await getCgPrice('kintsugi') + const ksmPrice = await getCgPrice('kusama') + + const kintInKsm = await getKsmKintPrice() + const kintInUsd = Number(kintInKsm) * ksmPrice + + const diff = calcPercentages(kintPrice, kintInUsd) + console.log(`🏠 KAR Address: ${address}`) - console.log(`🚗 KAR Balance: ${await getKarBalance()}`) - console.log(`🧮 KAR/KSM Price: ${await getKsmKintPrice()}`) + console.log(`🚗 KAR Balance (for fees): ${await getKarBalance()}`) + console.log(`🧮 Karura KINT Price: ${kintInKsm} KSM / $${kintInUsd.toFixed(2)}`) + printPercentages(kintPrice, kintInUsd) + console.log( + `🌾 Harvestable Amount: ${kintHarvest} KINT / ${ksmHarvest.toFixed(2)} KSM / $${( + kintPrice * Number(kintHarvest) + ).toFixed(2)}` + ) + console.log('=============================') } + // bridge from kintsugi const bridgeToKint = async (amount) => { @@ -366,3 +381,22 @@ const submitTx = async (tx, signer) => { return details } + +const printPercentages = (num1: number, num2: number) => { + const percent = calcPercentages(num1, num2) + + process.stdout.write(`🦎 Chain vs CoinGecko price: `) + if (percent > 0) { + process.stdout.write(colors.green(`${percent.toFixed(2)}%\n`)) + } else { + process.stdout.write(colors.red(`${percent.toFixed(2)}%\n`)) + } +} + +const calcPercentages = (num1: number, num2: number) => { + const diff = num2 - num1 + const ratio = diff / num1 + const percent = ratio * 100 + + return percent +} diff --git a/yarn.lock b/yarn.lock index 193c269..75e60eb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -859,10 +859,12 @@ array-uniq@1.0.2: resolved "https://registry.yarnpkg.com/array-uniq/-/array-uniq-1.0.2.tgz#5fcc373920775723cfd64d65c64bef53bf9eba6d" integrity sha512-GVYjmpL05al4dNlKJm53mKE4w9OOLiuVHWorsIA3YVz+Hu0hcn6PtE3Ydl0EqU7v+7ABC4mjjWsnLUxbpno+CA== -async@^1.5.2: - version "1.5.2" - resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" - integrity sha512-nSVgobk4rv61R9PUSDtYt7mPVB2olxNR5RWJcAsH676/ef11bUZwvu7+RGYrYauVdDPcO519v68wRhXQtxsV9w== +async@2.6.4, async@^1.5.2: + version "2.6.4" + resolved "https://registry.yarnpkg.com/async/-/async-2.6.4.tgz#706b7ff6084664cd7eae713f6f965433b5504221" + integrity sha512-mzo5dfJYwAn29PeiJ0zvwTo04zj8HDJj0Mn8TD7sno7q12prdbnasKJHhkm2c1LgrhlJ0teaea8860oxi51mGA== + dependencies: + lodash "^4.17.14" async@^3.2.3: version "3.2.3" @@ -1043,6 +1045,11 @@ color-name@~1.1.4: resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2" integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== +colors@^1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/colors/-/colors-1.4.0.tgz#c50491479d4c1bdaed2c9ced32cf7c7dc2360f78" + integrity sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA== + combined-stream@^1.0.8: version "1.0.8" resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" @@ -1449,6 +1456,11 @@ lodash.set@^4.3.2: resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" integrity sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM= +lodash@^4.17.14: + version "4.17.21" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" + integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== + lru-cache@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94" @@ -1591,7 +1603,7 @@ nock@^13.2.4: lodash.set "^4.3.2" propagate "^2.0.0" -node-fetch@^2.6.7: +node-fetch@2, node-fetch@^2.6.7: version "2.6.7" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad" integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==