Skip to content

Commit

Permalink
Added CoinGecko price feed
Browse files Browse the repository at this point in the history
  • Loading branch information
timbotronic committed May 21, 2022
1 parent 4267afc commit 771a882
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 19 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
6 changes: 3 additions & 3 deletions scripts/harvest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions static/tokens.ts
Original file line number Diff line number Diff line change
@@ -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' }
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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. */
Expand Down
20 changes: 20 additions & 0 deletions utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -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()
52 changes: 43 additions & 9 deletions utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
}
22 changes: 17 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -859,10 +859,12 @@ [email protected]:
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==
[email protected], 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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit 771a882

Please sign in to comment.