mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 20:54:07 -08:00
d6239d5bfb
* Update Compression node * Update Crypto node * Update DateTime node * Update EditImage node * Update EmailSend node * Update ExecuteWorkflow node * Update FTP node * Update Function node * Update FunctionItem node * Update ExecuteCommand node * Update OpenWeatherMap node * Update ReadBinaryFile node * Update ReadPdf node * Update RssFeedRead node & add URL validation * Update SpreadsheetFile node * Update Switch node * Update WriteBinaryFile node * Update Xml node * Update ActiveCampaign node * Update Airtable node * Update ApiTemplateIo node * Update Asana node * Update AwsLambda node * Update AwsSns node * Update AwsComprehend node * Update AwsRekognition node * Update AwsS3 node * Fix Error item * Update AwsSes node * Update AwsSqs node * Update Amqp node * Update Bitly node * Update Box node * Update Brandfetch node * Update CircleCi node * Update Clearbit node * Update ClickUp node * Update Cockpit node * Update CoinGecko node * Update Contentful node * Update ConvertKit node * Update Cortex node * Update CustomerIo node * Update DeepL node * Update Demio node * Update Disqus node * Update Drift node * Update Dropbox node * Update GetResponse node * Refactor & Update Ghost node * Update Github node * Update Gitlab node * Update GoogleAnalytics node * Update GoogleBooks node * Update GoogleCalendar node * Update GoogleDrive node * Update Gmail node * Update GoogleSheets node * Update GoogleSlides node * Update GoogleTasks node * Update Gotify node * Update GraphQL node * Update HackerNews node * Update Harvest node * Update HtmlExtract node * Update Hubspot node * Update Hunter node * Update Intercom node * Update Kafka node * Refactor & update Line node * Update LinkedIn node * Update Mailchimp node * Update Mandrill node * Update Matrix node * Update Mautic node * Update Medium node * Update MessageBird node * Update Mindee node * Update Mocean node * Update MondayCom node * Update MicrosoftExcel node * Update MicrosoftOneDrive node * Update MicrosoftOutlook node * Update Affinity node * Update Chargebee node * Update Discourse node * Update Freshdesk node * Update YouTube node * Update InvoiceNinja node * Update MailerLite node * Update Mailgun node * Update Mailjet node * Update Mattermost node * Update Nasa node * Update NextCloud node * Update OpenThesaurus node * Update Orbit node * Update PagerDuty node * Update PayPal node * Update Peekalink node * Update Phantombuster node * Update PostHog node * Update ProfitWell node * Refactor & Update Pushbullet node * Update QuickBooks node * Update Raindrop node * Update Reddit node * Update Rocketchat node * Update S3 node * Update Salesforce node * Update SendGrid node * Update SentryIo node * Update Shopify node * Update Signl4 node * Update Slack node * Update Spontit node * Update Spotify node * Update Storyblok node * Refactor & Update Strapi node * Refactor & Update Strava node * Update Taiga node * Refactor & update Tapfiliate node * Update Telegram node * Update TheHive node * Update Todoist node * Update TravisCi node * Update Trello node * Update Twilio node * Update Twist node * Update Twitter node * Update Uplead node * Update UProc node * Update Vero node * Update Webflow node * Update Wekan node * Update Wordpress node * Update Xero node * Update Yourls node * Update Zendesk node * Update ZohoCrm node * Refactor & Update Zoom node * Update Zulip node * Update Clockify node * Update MongoDb node * Update MySql node * Update MicrosoftTeams node * Update Stackby node * Refactor Discourse node * Support corner-case in Github node update * Support corner-case in Gitlab node update * Refactor & Update GoogleContacts node * Refactor Mindee node * Update Coda node * Lint fixes * Update Beeminder node * Update Google Firebase RealtimeDatabase node * Update HelpScout node * Update Mailcheck node * Update Paddle node * Update Pipedrive node * Update Pushover node * Update Segment node * Refactor & Update Vonage node * Added new conditions to warnings on execute batch cmd * Added keep only properties flag * Fixed code for keep only props * Added dependencies for image editing Co-authored-by: dali <servfrdali@yahoo.fr>
546 lines
14 KiB
TypeScript
546 lines
14 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodeExecutionData,
|
|
INodePropertyOptions,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
coinFields,
|
|
coinOperations,
|
|
} from './CoinDescription';
|
|
|
|
import {
|
|
eventFields,
|
|
eventOperations,
|
|
} from './EventDescription';
|
|
|
|
import {
|
|
coinGeckoApiRequest,
|
|
coinGeckoRequestAllItems,
|
|
} from './GenericFunctions';
|
|
|
|
import * as moment from 'moment-timezone';
|
|
|
|
export class CoinGecko implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'CoinGecko',
|
|
name: 'coinGecko',
|
|
icon: 'file:coinGecko.svg',
|
|
group: ['output'],
|
|
version: 1,
|
|
description: 'Consume CoinGecko API',
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
defaults: {
|
|
name: 'CoinGecko',
|
|
color: '#8bc53f',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
properties: [
|
|
{
|
|
displayName: 'Resource',
|
|
name: 'resource',
|
|
type: 'options',
|
|
options: [
|
|
{
|
|
name: 'Coin',
|
|
value: 'coin',
|
|
},
|
|
{
|
|
name: 'Event',
|
|
value: 'event',
|
|
},
|
|
],
|
|
default: 'coin',
|
|
},
|
|
...coinOperations,
|
|
...coinFields,
|
|
...eventOperations,
|
|
...eventFields,
|
|
],
|
|
};
|
|
|
|
methods = {
|
|
loadOptions: {
|
|
async getCurrencies(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const currencies = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
'/simple/supported_vs_currencies',
|
|
);
|
|
currencies.sort();
|
|
for (const currency of currencies) {
|
|
returnData.push({
|
|
name: currency.toUpperCase(),
|
|
value: currency,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
|
|
async getCoins(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const coins = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
'/coins/list',
|
|
);
|
|
for (const coin of coins) {
|
|
returnData.push({
|
|
name: coin.symbol.toUpperCase(),
|
|
value: coin.id,
|
|
});
|
|
}
|
|
returnData.sort((a, b) => {
|
|
if (a.name < b.name) { return -1; }
|
|
if (a.name > b.name) { return 1; }
|
|
return 0;
|
|
});
|
|
return returnData;
|
|
},
|
|
|
|
async getExchanges(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const exchanges = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
'/exchanges/list',
|
|
);
|
|
for (const exchange of exchanges) {
|
|
returnData.push({
|
|
name: exchange.name,
|
|
value: exchange.id,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
|
|
async getEventCountryCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const countryCodes = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
'/events/countries',
|
|
);
|
|
for (const code of countryCodes.data) {
|
|
if (!code.code) {
|
|
continue;
|
|
}
|
|
returnData.push({
|
|
name: code.country,
|
|
value: code.code,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
|
|
async getEventTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const eventTypes = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
'/events/types',
|
|
);
|
|
for (const type of eventTypes.data) {
|
|
returnData.push({
|
|
name: type,
|
|
value: type,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
},
|
|
};
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
const returnData: IDataObject[] = [];
|
|
const length = (items.length as unknown) as number;
|
|
const qs: IDataObject = {};
|
|
let responseData;
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
for (let i = 0; i < length; i++) {
|
|
try {
|
|
if (resource === 'coin') {
|
|
//https://www.coingecko.com/api/documentations/v3#/coins/get_coins__id_
|
|
//https://www.coingecko.com/api/documentations/v3#/contract/get_coins__id__contract__contract_address_
|
|
if (operation === 'get') {
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
qs.community_data = false;
|
|
qs.developer_data = false;
|
|
qs.localization = false;
|
|
qs.market_data = false;
|
|
qs.sparkline = false;
|
|
qs.tickers = false;
|
|
|
|
Object.assign(qs, options);
|
|
|
|
const searchBy = this.getNodeParameter('searchBy', i) as string;
|
|
|
|
if (searchBy === 'coinId') {
|
|
const coinId = this.getNodeParameter('coinId', i) as string;
|
|
|
|
responseData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/coins/${coinId}`,
|
|
{},
|
|
qs,
|
|
);
|
|
}
|
|
|
|
if (searchBy === 'contractAddress') {
|
|
const platformId = this.getNodeParameter('platformId', i) as string;
|
|
const contractAddress = this.getNodeParameter('contractAddress', i) as string;
|
|
|
|
responseData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/coins/${platformId}/contract/${contractAddress}`,
|
|
{},
|
|
qs,
|
|
);
|
|
}
|
|
}
|
|
//https://www.coingecko.com/api/documentations/v3#/coins/get_coins_list
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
let limit;
|
|
|
|
responseData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
'/coins/list',
|
|
{},
|
|
qs,
|
|
);
|
|
|
|
if (returnAll === false) {
|
|
limit = this.getNodeParameter('limit', i) as number;
|
|
responseData = responseData.splice(0, limit);
|
|
}
|
|
}
|
|
|
|
//https://www.coingecko.com/api/documentations/v3#/coins/get_coins_list
|
|
if (operation === 'market') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
const baseCurrency = this.getNodeParameter('baseCurrency', i) as string;
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
qs.vs_currency = baseCurrency;
|
|
|
|
Object.assign(qs, options);
|
|
|
|
if (options.price_change_percentage) {
|
|
qs.price_change_percentage = (options.price_change_percentage as string[]).join(',');
|
|
}
|
|
|
|
if (returnAll) {
|
|
responseData = await coinGeckoRequestAllItems.call(
|
|
this,
|
|
'',
|
|
'GET',
|
|
`/coins/markets`,
|
|
{},
|
|
qs,
|
|
);
|
|
} else {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
qs.per_page = limit;
|
|
|
|
responseData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/coins/markets`,
|
|
{},
|
|
qs,
|
|
);
|
|
}
|
|
}
|
|
|
|
//https://www.coingecko.com/api/documentations/v3#/simple/get_simple_price
|
|
//https://www.coingecko.com/api/documentations/v3#/simple/get_simple_token_price__id_
|
|
if (operation === 'price') {
|
|
|
|
const searchBy = this.getNodeParameter('searchBy', i) as string;
|
|
const quoteCurrencies = this.getNodeParameter('quoteCurrencies', i) as string[];
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
qs.vs_currencies = quoteCurrencies.join(',');
|
|
|
|
Object.assign(qs, options);
|
|
|
|
if (searchBy === 'coinId') {
|
|
const baseCurrencies = this.getNodeParameter('baseCurrencies', i) as string[];
|
|
|
|
qs.ids = baseCurrencies.join(',');
|
|
|
|
responseData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
'/simple/price',
|
|
{},
|
|
qs,
|
|
);
|
|
}
|
|
|
|
if (searchBy === 'contractAddress') {
|
|
const platformId = this.getNodeParameter('platformId', i) as string;
|
|
const contractAddresses = this.getNodeParameter('contractAddresses', i) as string;
|
|
|
|
qs.contract_addresses = contractAddresses;
|
|
|
|
responseData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/simple/token_price/${platformId}`,
|
|
{},
|
|
qs,
|
|
);
|
|
}
|
|
}
|
|
|
|
//https://www.coingecko.com/api/documentations/v3#/coins/get_coins__id__tickers
|
|
if (operation === 'ticker') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
const coinId = this.getNodeParameter('coinId', i) as string;
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
Object.assign(qs, options);
|
|
|
|
if (options.exchange_ids) {
|
|
qs.exchange_ids = (options.exchange_ids as string[]).join(',');
|
|
}
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await coinGeckoRequestAllItems.call(
|
|
this,
|
|
'tickers',
|
|
'GET',
|
|
`/coins/${coinId}/tickers`,
|
|
{},
|
|
qs,
|
|
);
|
|
} else {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/coins/${coinId}/tickers`,
|
|
{},
|
|
qs,
|
|
);
|
|
|
|
responseData = responseData.tickers;
|
|
responseData = responseData.splice(0, limit);
|
|
}
|
|
}
|
|
|
|
//https://www.coingecko.com/api/documentations/v3#/coins/get_coins__id__history
|
|
if (operation === 'history') {
|
|
|
|
const coinId = this.getNodeParameter('coinId', i) as string;
|
|
const date = this.getNodeParameter('date', i) as string;
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
Object.assign(qs, options);
|
|
|
|
qs.date = moment(date).format('DD-MM-YYYY');
|
|
|
|
responseData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/coins/${coinId}/history`,
|
|
{},
|
|
qs,
|
|
);
|
|
}
|
|
|
|
//https://www.coingecko.com/api/documentations/v3#/coins/get_coins__id__market_chart
|
|
//https://www.coingecko.com/api/documentations/v3#/contract/get_coins__id__contract__contract_address__market_chart_
|
|
if (operation === 'marketChart') {
|
|
|
|
let respData;
|
|
|
|
const searchBy = this.getNodeParameter('searchBy', i) as string;
|
|
const quoteCurrency = this.getNodeParameter('quoteCurrency', i) as string;
|
|
const days = this.getNodeParameter('days', i) as string;
|
|
|
|
qs.vs_currency = quoteCurrency;
|
|
qs.days = days;
|
|
|
|
if (searchBy === 'coinId') {
|
|
const coinId = this.getNodeParameter('baseCurrency', i) as string;
|
|
|
|
respData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/coins/${coinId}/market_chart`,
|
|
{},
|
|
qs,
|
|
);
|
|
}
|
|
|
|
if (searchBy === 'contractAddress') {
|
|
const platformId = this.getNodeParameter('platformId', i) as string;
|
|
const contractAddress = this.getNodeParameter('contractAddress', i) as string;
|
|
|
|
respData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/coins/${platformId}/contract/${contractAddress}/market_chart`,
|
|
{},
|
|
qs,
|
|
);
|
|
}
|
|
|
|
responseData = [];
|
|
for (let idx = 0; idx < respData.prices.length; idx++) {
|
|
const [time, price] = respData.prices[idx];
|
|
const marketCaps = respData.market_caps[idx][1];
|
|
const totalVolume = respData.total_volumes[idx][1];
|
|
responseData.push({ time: moment(time).toISOString(), price, marketCaps, totalVolume } as IDataObject);
|
|
}
|
|
}
|
|
|
|
//https://www.coingecko.com/api/documentations/v3#/coins/get_coins__id__ohlc
|
|
if (operation === 'candlestick') {
|
|
|
|
const baseCurrency = this.getNodeParameter('baseCurrency', i) as string;
|
|
const quoteCurrency = this.getNodeParameter('quoteCurrency', i) as string;
|
|
const days = this.getNodeParameter('days', i) as string;
|
|
|
|
qs.vs_currency = quoteCurrency;
|
|
qs.days = days;
|
|
|
|
responseData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/coins/${baseCurrency}/ohlc`,
|
|
{},
|
|
qs,
|
|
);
|
|
|
|
for (let idx = 0; idx < responseData.length; idx++) {
|
|
const [time, open, high, low, close] = responseData[idx];
|
|
responseData[idx] = { time: moment(time).toISOString(), open, high, low, close } as IDataObject;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (resource === 'event') {
|
|
//https://www.coingecko.com/api/documentations/v3#/events/get_events
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
Object.assign(qs, options);
|
|
|
|
if (returnAll) {
|
|
responseData = await coinGeckoRequestAllItems.call(
|
|
this,
|
|
'data',
|
|
'GET',
|
|
'/events',
|
|
{},
|
|
qs,
|
|
);
|
|
} else {
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
qs.per_page = limit;
|
|
|
|
responseData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
'/events',
|
|
{},
|
|
qs,
|
|
);
|
|
responseData = responseData.data;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (resource === 'simple') {
|
|
//https://www.coingecko.com/api/documentations/v3#/simple/get_simple_price
|
|
if (operation === 'price') {
|
|
|
|
const ids = this.getNodeParameter('ids', i) as string;
|
|
const currencies = this.getNodeParameter('currencies', i) as string[];
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
qs.ids = ids,
|
|
qs.vs_currencies = currencies.join(',');
|
|
|
|
Object.assign(qs, options);
|
|
|
|
responseData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
'/simple/price',
|
|
{},
|
|
qs,
|
|
);
|
|
}
|
|
|
|
//https://www.coingecko.com/api/documentations/v3#/simple/get_simple_token_price__id_
|
|
if (operation === 'tokenPrice') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
const contractAddresses = this.getNodeParameter('contractAddresses', i) as string;
|
|
const currencies = this.getNodeParameter('currencies', i) as string[];
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
qs.contract_addresses = contractAddresses;
|
|
qs.vs_currencies = currencies.join(',');
|
|
|
|
Object.assign(qs, options);
|
|
|
|
responseData = await coinGeckoApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/simple/token_price/${id}`,
|
|
{},
|
|
qs,
|
|
);
|
|
}
|
|
}
|
|
|
|
if (Array.isArray(responseData)) {
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
} else if (responseData !== undefined) {
|
|
returnData.push(responseData as IDataObject);
|
|
}
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
returnData.push({ error: error.message });
|
|
continue;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
}
|
|
}
|