Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added coingecko API #13

Merged
merged 1 commit into from
Nov 9, 2018
Merged
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
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
"roots": [
"<rootDir>/src"
],
testEnvironment: 'node',
"transform": {
"^.+\\.ts$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js|ts)$",
"moduleFileExtensions": ["ts", "js", "json"]
};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"scripts": {
"dev": "ts-node-dev -r tsconfig-paths/register --files src/index.ts",
"build": "tsc"
"build": "tsc",
"test": "jest"
},
"dependencies": {
"axios": "^0.17.1",
Expand All @@ -19,10 +20,11 @@
"devDependencies": {
"@types/bytebuffer": "^5.0.37",
"@types/dotenv": "^4.0.3",
"@types/jest": "^22.2.3",
"@types/jest": "^23.3.9",
"@types/mssql": "^4.0.7",
"@types/node": "^9.6.6",
"@types/winston": "^2.3.9",
"ts-jest": "^23.10.4",
"ts-node-dev": "^1.0.0-pre.30",
"tsconfig-paths": "^3.6.0"
},
Expand Down
8 changes: 8 additions & 0 deletions src/router/coin/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getOnlyPrice, countRatio } from '../'

describe('coin/index.ts', () => {
it('gets only price of coin', async () => {
const { price, image } = await getOnlyPrice('sbd', 'usd')
expect(price).toBeTruthy()
})
})
34 changes: 18 additions & 16 deletions src/router/coin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,31 @@ import axios from 'axios'
import crypto from './convert'

let getOnlyPrice = async (coin: string, currency: string) => {
console.log(crypto[coin])
let res
if (!!crypto[coin]) {
let res = await axios.get(
`https://api.coinmarketcap.com/v1/ticker/${crypto[coin]}/?convert=${currency}`
)
let price: number = res.data[0][`price_${currency.toLowerCase()}`] || 0
return price
// let res = await axios.get(
// `https://api.coinmarketcap.com/v1/ticker/${crypto[coin]}/?convert=${currency}`
// )
// let price: number = res.data[0][`price_${currency.toLowerCase()}`] || 0
res = await axios.get(`https://api.coingecko.com/api/v3/coins/${crypto[coin]}`)
} else {
let res = await axios.get(
`https://api.coinmarketcap.com/v1/ticker/${coin}/?convert=${currency}`
)
let price: number = res.data[0][`price_${currency.toLowerCase()}`] || 0
return price
// let res = await axios.get(
// `https://api.coinmarketcap.com/v1/ticker/${coin}/?convert=${currency}`
// )
// let price: number = res.data[0][`price_${currency.toLowerCase()}`] || 0
res = await axios.get(`https://api.coingecko.com/api/v3/coins/${coin}`)
}
let price: number = res.data['market_data']['current_price'][currency]
let image: string = res.data['image']['thumb']
return { price, image }
}

let countRatio = async (coin1: string, coin2: string) => {
let data: number[] = await Promise.all([
await getOnlyPrice(coin1, 'USD'),
await getOnlyPrice(coin2, 'USD')
let data: { price: number; image: string }[] = await Promise.all([
await getOnlyPrice(coin1, 'usd'),
await getOnlyPrice(coin2, 'usd')
])

return data[0] / data[1]
return { price: data[0].price / data[1].price, images: [data[0].image, data[1].image] }
}

export { getOnlyPrice, countRatio }
89 changes: 28 additions & 61 deletions src/router/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,22 @@ const convert = async (client: Discord.Client, msg: Discord.Message, args: strin
const coin2 = args[2].toLowerCase()
const isCoin1Crypto = coin1 in crypto
const isCoin2Crypto = coin2 in crypto
console.log(`${isCoin1Crypto} ${isCoin2Crypto}`)
if (isCoin1Crypto && isCoin2Crypto) {
// Crypto to Crypto
countRatio(coin1, coin2).then(data => {
if (data === 0) {
if (!data.price) {
errorMsg(msg, 'Invalid coin/currency')
} else {
const replyMessage = new Discord.RichEmbed()
.setColor(0x00ae86)
.setTitle(`${coin1} -> ${coin2}`)
.setThumbnail(`${data.images[1]}`)
.setImage(`${data.images[0]}`)
.addField(`${coin1}`, `${number}`, true)
.addField(`${coin2}`, `${data.price * number}`, true)

msg.channel.send({
embed: {
color: 3447003,
description: `${coin1} -> ${coin2}`,
fields: [
{
name: `${coin1}`,
value: `${number}`
},
{
name: `${coin2}`,
value: `${data * number}`
}
],
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: '© superoo7'
}
}
embed: replyMessage
})
}
})
Expand All @@ -46,29 +35,18 @@ const convert = async (client: Discord.Client, msg: Discord.Message, args: strin
// crypto, currency
getOnlyPrice(coin1, coin2)
.then(data => {
if (data === 0) {
if (!data.price) {
errorMsg(msg, 'Invalid coin/currency')
} else {
const replyMessage = new Discord.RichEmbed()
.setColor(0x00ae86)
.setTitle(`${coin1} -> ${coin2}`)
.setThumbnail(`${data.image}`)
.addField(`${coin1}`, `${number}`, true)
.addField(`${coin2}`, `${data.price * number}`, true)

msg.channel.send({
embed: {
color: 3447003,
description: `${coin1} -> ${coin2}`,
fields: [
{
name: `${coin1}`,
value: `${number}`
},
{
name: `${coin2}`,
value: `${data * number}`
}
],
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: '© superoo7'
}
}
embed: replyMessage
})
}
})
Expand All @@ -77,29 +55,18 @@ const convert = async (client: Discord.Client, msg: Discord.Message, args: strin
// Fiat to Crypto
getOnlyPrice(coin2, coin1)
.then(data => {
if (data === 0) {
if (!data.price) {
msg.reply('Invalid coin/currency')
} else {
const replyMessage = new Discord.RichEmbed()
.setColor(0x00ae86)
.setTitle(`${coin1} -> ${coin2}`)
.setThumbnail(`${data.image}`)
.addField(`${coin1}`, `${number}`, true)
.addField(`${coin2}`, `${number / data.price}`, true)

msg.channel.send({
embed: {
color: 3447003,
description: `${coin1} -> ${coin2}`,
fields: [
{
name: `${coin1}`,
value: `${number}`
},
{
name: `${coin2}`,
value: `${number / data}`
}
],
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: '© superoo7'
}
}
embed: replyMessage
})
}
})
Expand Down
4 changes: 2 additions & 2 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ let router = async (client: Discord.Client, msg: Discord.Message) => {
price(msg, ['sbd'])
break
case 's/sbd':
infoMsg(msg, `Steem to SBD ratio is ${await countRatio('steem', 'sbd')}`)
convert(client, msg, ['1', 'steem', 'sbd'])
break
case 'sbd/s':
infoMsg(msg, `SBD to Steem ratio is ${await countRatio('steem', 'sbd')}`)
convert(client, msg, ['1', 'sbd', 'steem'])
break
case 'convert':
convert(client, msg, args)
Expand Down
9 changes: 7 additions & 2 deletions src/router/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ const price = async (msg: Discord.Message, args: string[]) => {
let coin = args[0]
await getOnlyPrice(coin, 'usd')
.then(price => {
if (price === 0) {
if (!price.price) {
errorMsg(msg, `${coin} is not available`)
return
} else {
infoMsg(msg, `${coin} is at $${price}`)
const replyMessage = new Discord.RichEmbed()
.setColor(0x00ae86)
.setTitle(`${coin} price`)
.setThumbnail(`${price.image}`)
.addField('Price', `$${price.price}`)
msg.channel.send({ embed: replyMessage })
return
}
})
Expand Down
47 changes: 39 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/events/-/events-1.2.0.tgz#81a6731ce4df43619e5c8c945383b3e62a89ea86"

"@types/jest@^22.2.3":
version "22.2.3"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-22.2.3.tgz#0157c0316dc3722c43a7b71de3fdf3acbccef10d"
"@types/jest@^23.3.9":
version "23.3.9"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-23.3.9.tgz#c16b55186ee73ae65e001fbee69d392c51337ad1"

"@types/json5@^0.0.29":
version "0.0.29"
Expand Down Expand Up @@ -578,6 +578,12 @@ browserify-aes@^1.0.6:
inherits "^2.0.1"
safe-buffer "^5.0.1"

bs-logger@0.x:
version "0.2.5"
resolved "https://registry.yarnpkg.com/bs-logger/-/bs-logger-0.2.5.tgz#1d82f0cf88864e1341cd9262237f8d0748a49b22"
dependencies:
fast-json-stable-stringify "^2.0.0"

bs58@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/bs58/-/bs58-4.0.1.tgz#be161e76c354f6f788ae4071f63f34e8c4f0a42a"
Expand All @@ -590,7 +596,7 @@ bser@^2.0.0:
dependencies:
node-int64 "^0.4.0"

buffer-from@^1.0.0, buffer-from@^1.1.0:
buffer-from@1.x, buffer-from@^1.0.0, buffer-from@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"

Expand Down Expand Up @@ -1315,7 +1321,7 @@ fast-deep-equal@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.0.0.tgz#96256a3bc975595eb36d82e9929d060d893439ff"

fast-json-stable-stringify@^2.0.0:
fast-json-stable-stringify@2.x, fast-json-stable-stringify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2"

Expand Down Expand Up @@ -2350,6 +2356,12 @@ json-stringify-safe@~5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"

json5@2.x:
version "2.1.0"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.0.tgz#e7a0c62c48285c628d20a10b85c89bb807c32850"
dependencies:
minimist "^1.2.0"

json5@^0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"
Expand Down Expand Up @@ -2552,7 +2564,7 @@ lru-cache@^4.0.1:
pseudomap "^1.0.2"
yallist "^2.1.2"

make-error@^1.1.1:
make-error@1.x, make-error@^1.1.1:
version "1.3.5"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.5.tgz#efe4e81f6db28cadd605c70f29c831b58ef776c8"

Expand Down Expand Up @@ -2750,7 +2762,7 @@ mixin-deep@^1.2.0:
for-in "^1.0.2"
is-extendable "^1.0.1"

mkdirp@^0.5.0, mkdirp@^0.5.1:
mkdirp@0.x, mkdirp@^0.5.0, mkdirp@^0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
dependencies:
Expand Down Expand Up @@ -3460,7 +3472,7 @@ secp256k1@^3.3.1:
version "5.5.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab"

semver@^5.1.0:
semver@^5.1.0, semver@^5.5:
version "5.6.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.6.0.tgz#7e74256fbaa49c75aa7c7a205cc22799cac80004"

Expand Down Expand Up @@ -3907,6 +3919,19 @@ trim-right@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"

ts-jest@^23.10.4:
version "23.10.4"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-23.10.4.tgz#a7a953f55c9165bcaa90ff91014a178e87fe0df8"
dependencies:
bs-logger "0.x"
buffer-from "1.x"
fast-json-stable-stringify "2.x"
json5 "2.x"
make-error "1.x"
mkdirp "0.x"
semver "^5.5"
yargs-parser "10.x"

ts-node-dev@^1.0.0-pre.30:
version "1.0.0-pre.30"
resolved "https://registry.yarnpkg.com/ts-node-dev/-/ts-node-dev-1.0.0-pre.30.tgz#11446f273d8adee9fa4b8de6661ef5885abb5431"
Expand Down Expand Up @@ -4209,6 +4234,12 @@ yallist@^3.0.0, yallist@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.2.tgz#8452b4bb7e83c7c188d8041c1a837c773d6d8bb9"

yargs-parser@10.x:
version "10.1.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"
dependencies:
camelcase "^4.1.0"

yargs-parser@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-8.1.0.tgz#f1376a33b6629a5d063782944da732631e966950"
Expand Down