mirror of
https://github.com/apotdevin/thunderhub.git
synced 2025-02-21 14:04:03 +01:00
feat: add bitcoin price query
This commit is contained in:
parent
ecad5380f2
commit
86152341b2
8 changed files with 5806 additions and 6 deletions
|
@ -23,6 +23,7 @@
|
|||
"dependencies": {
|
||||
"@types/graphql-depth-limit": "^1.1.2",
|
||||
"@types/graphql-iso-date": "^3.3.3",
|
||||
"@types/node-fetch": "^2.5.3",
|
||||
"apollo-server": "^2.9.7",
|
||||
"dotenv": "^8.2.0",
|
||||
"graphql": "^14.5.8",
|
||||
|
|
15
src/schemaTypes/query/data/bitcoinPrice.ts
Normal file
15
src/schemaTypes/query/data/bitcoinPrice.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { GraphQLObjectType, GraphQLString, GraphQLFloat } from "graphql";
|
||||
|
||||
export const BitcoinPriceType = new GraphQLObjectType({
|
||||
name: "bitcoinPriceType",
|
||||
fields: () => {
|
||||
return {
|
||||
price: {
|
||||
type: GraphQLFloat
|
||||
},
|
||||
symbol: {
|
||||
type: GraphQLString
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
|
@ -1,6 +1,19 @@
|
|||
import { GraphQLObjectType, GraphQLBoolean, GraphQLString } from "graphql";
|
||||
import { GraphQLInt } from "graphql";
|
||||
|
||||
export const PartnerNodeType = new GraphQLObjectType({
|
||||
name: "partnerNodeType",
|
||||
fields: () => {
|
||||
return {
|
||||
alias: { type: GraphQLString },
|
||||
capacity: { type: GraphQLString },
|
||||
channelCount: { type: GraphQLInt },
|
||||
color: { type: GraphQLString },
|
||||
lastUpdate: { type: GraphQLString }
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
export const ChannelType = new GraphQLObjectType({
|
||||
name: "channelType",
|
||||
fields: () => {
|
||||
|
@ -26,7 +39,8 @@ export const ChannelType = new GraphQLObjectType({
|
|||
timeOnline: { type: GraphQLInt },
|
||||
transactionId: { type: GraphQLString },
|
||||
transactionVout: { type: GraphQLInt },
|
||||
unsettledBalance: { type: GraphQLInt }
|
||||
unsettledBalance: { type: GraphQLInt },
|
||||
partnerNodeInfo: { type: PartnerNodeType }
|
||||
};
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { GraphQLList } from "graphql";
|
||||
import { getChannels as getLnChannels } from "ln-service";
|
||||
import { getChannels as getLnChannels, getNode } from "ln-service";
|
||||
import { logger } from "../../../helpers/logger";
|
||||
import { ChannelType } from "../../../schemaTypes/query/info/channels";
|
||||
import { requestLimiter } from "../../../helpers/rateLimiter";
|
||||
|
@ -42,10 +42,16 @@ export const getChannels = {
|
|||
|
||||
try {
|
||||
const channelList: ChannelListProps = await getLnChannels({
|
||||
lnd: lnd
|
||||
lnd
|
||||
});
|
||||
|
||||
const channels = channelList.channels.map((channel, index) => {
|
||||
const channels = channelList.channels.map(async channel => {
|
||||
const nodeInfo = await getNode({
|
||||
lnd,
|
||||
is_omitting_channels: true,
|
||||
public_key: channel.partner_public_key
|
||||
});
|
||||
|
||||
return {
|
||||
capacity: channel.capacity,
|
||||
commitTransactionFee: channel.commit_transaction_fee,
|
||||
|
@ -68,7 +74,14 @@ export const getChannels = {
|
|||
timeOnline: channel.time_online,
|
||||
transactionId: channel.transaction_id,
|
||||
transactionVout: channel.transaction_vout,
|
||||
unsettledBalance: channel.unsettled_balance
|
||||
unsettledBalance: channel.unsettled_balance,
|
||||
partnerNodeInfo: {
|
||||
alias: nodeInfo.alias,
|
||||
capacity: nodeInfo.capacity,
|
||||
channelCount: nodeInfo.channel_count,
|
||||
color: nodeInfo.color,
|
||||
lastUpdate: nodeInfo.updated_at
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
|
|
45
src/schemas/query/data/bitcoinPrice.ts
Normal file
45
src/schemas/query/data/bitcoinPrice.ts
Normal file
|
@ -0,0 +1,45 @@
|
|||
import { logger } from "../../../helpers/logger";
|
||||
import { requestLimiter } from "../../../helpers/rateLimiter";
|
||||
import { GraphQLString } from "graphql";
|
||||
import { BitcoinPriceType } from "../../../schemaTypes/query/data/bitcoinPrice";
|
||||
import fetch from "node-fetch";
|
||||
|
||||
const url = "https://blockchain.info/ticker";
|
||||
|
||||
export const getBitcoinPrice = {
|
||||
type: BitcoinPriceType,
|
||||
args: {
|
||||
currency: {
|
||||
type: GraphQLString
|
||||
}
|
||||
},
|
||||
resolve: async (root: any, params: any, context: any) => {
|
||||
await requestLimiter(context.ip, params, "bitcoinPrice", 1, "1s");
|
||||
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
const json = await response.json();
|
||||
|
||||
if (!params.currency && "EUR" in json) {
|
||||
const value = json.EUR;
|
||||
|
||||
return {
|
||||
price: value.last,
|
||||
symbol: value.symbol
|
||||
};
|
||||
} else if (params.currency in json) {
|
||||
const value = json[params.currency];
|
||||
|
||||
return {
|
||||
price: value.last,
|
||||
symbol: value.symbol
|
||||
};
|
||||
} else {
|
||||
throw new Error("Problem getting Bitcoin price.");
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error("Error getting bitcoin price: %o", error);
|
||||
throw new Error("Problem getting Bitcoin price.");
|
||||
}
|
||||
}
|
||||
};
|
5
src/schemas/query/data/index.ts
Normal file
5
src/schemas/query/data/index.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { getBitcoinPrice } from "./bitcoinPrice";
|
||||
|
||||
export const dataQueries = {
|
||||
getBitcoinPrice
|
||||
};
|
|
@ -1,9 +1,11 @@
|
|||
import { channelQueries } from "./channels";
|
||||
import { generalQueries } from "./general";
|
||||
import { invoiceQueries } from "./invoices";
|
||||
import { dataQueries } from "./data";
|
||||
|
||||
export const query = {
|
||||
...channelQueries,
|
||||
...generalQueries,
|
||||
...invoiceQueries
|
||||
...invoiceQueries,
|
||||
...dataQueries
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue