Skip to content

Commit

Permalink
feat(export): add collector export (#17)
Browse files Browse the repository at this point in the history
* feat(export): add export collectors

* chore(export): add platform param

* refactor(export): rename function

Co-authored-by: d-pettersson <david.pettersson@code.berlin>
  • Loading branch information
somaticbits and d-pettersson committed Jul 24, 2022
1 parent 0281a03 commit f721109
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 7 deletions.
97 changes: 97 additions & 0 deletions src/actions/export-collectors.ts
@@ -0,0 +1,97 @@
import inquirer from 'inquirer';
import fetch from 'node-fetch';
import ora from 'ora-classic';

import { TEZTOK_API, PLATFORMS, MESSAGES, getTezTokPlatform } from '@constants';
import { validateAddress } from '@taquito/utils';
import { SaveToFile } from '@utils/csv';
import { error } from '@utils/logger';

type CollectorsType = {
holder_address: string;
amount: number;
token?: { platform: string; tokenId: string };
}[];

const mapCollectors = (collectors: CollectorsType) => {
return collectors.map(({ holder_address, amount, token }) => ({
address: holder_address,
amount,
platform: token ? `${token.platform}` : '',
}));
};

export const action = (hasPlatform: boolean) => {
const questions = [
{
type: 'list',
name: 'format',
message: 'Select data format',
choices: ['unique', 'full'],
default() {
return 'unique';
},
},
{
type: 'list',
name: 'platform',
message: MESSAGES.SELECT_PLATFORM,
choices: [PLATFORMS.VERSUM, PLATFORMS.HICETNUNC, PLATFORMS.FXHASH,],
when: hasPlatform,
default() {
return 'unique';
},
},
{
type: 'input',
name: 'address',
message: 'Enter address',
validate: async (input: string) => {
if ((await validateAddress(input)) === 3) {
return true;
}
error('\nInvalid address');
return false;
},
default() {
return 'tz1...';
},
},
];

const query = `
query GetUserCollectors($address: String!) {
holdings(where: {token: {artist_address: {_eq: $address}}, amount: {_gt: "0"}, holder_address: {_nregex: "^KT*", _neq: "tz1burnburnburnburnburnburnburjAYjjX"}}) {
amount
holder_address
token {
platform
fa2_address
token_id
}
}
}`;
inquirer.prompt(questions).then(({ format, address, platform }: Record<string, string>) => {
const spinner = ora(MESSAGES.FETCHING_DATA).start();
fetch(TEZTOK_API, { method: 'POST', body: JSON.stringify({ query, variables: { address } }) })
.then((e) => e.json())
.then((e) => e.data.holdings)
.then(async (collectors: CollectorsType) => {
const data = hasPlatform
? mapCollectors(collectors).filter((e) => e.platform === getTezTokPlatform(platform as PLATFORMS))
: mapCollectors(collectors);
spinner.succeed();
if (format === 'unique') {
// Use set to remove duplicates and convert to object[]
const uniqueData = new Set(data.map((e) => e.address));
const csvData = [...uniqueData].map((e) => ({ address: e }));
await SaveToFile(`collectors-${format}-${address}.csv`, csvData);
return;
}
await SaveToFile(`collectors-${format}-${address}.csv`, data);
})
.catch(() => {
spinner.fail(MESSAGES.ERROR_COLLECTOR_EXPORT);
});
});
};
10 changes: 4 additions & 6 deletions src/actions/export-token-collectors.ts
Expand Up @@ -2,7 +2,7 @@ import inquirer, { Answers } from 'inquirer';
import fetch from 'node-fetch';
import ora from 'ora-classic';

import { CONTRACT_VERSUM, getContractFromPlatform, PLATFORMS, TEZTOK_API } from '@constants';
import { CONTRACT_VERSUM, getContractFromPlatform, PLATFORMS, TEZTOK_API, MESSAGES } from '@constants';
import { validateContractAddress } from '@taquito/utils';
import { SaveToFile } from '@utils/csv';
import { error } from '@utils/logger';
Expand All @@ -17,7 +17,7 @@ export const action = () => {
{
type: 'list',
name: 'platform',
message: 'Select platform',
message: MESSAGES.SELECT_PLATFORM,
choices: [PLATFORMS.VERSUM, PLATFORMS.HICETNUNC, PLATFORMS.FXHASH, PLATFORMS.OTHER],
default() {
return PLATFORMS.VERSUM;
Expand Down Expand Up @@ -61,7 +61,7 @@ export const action = () => {
inquirer.prompt(questions).then(({ platform, contract, token }: Record<string, string>) => {
// select contract address from platform
const address: string = getContractFromPlatform(platform as PLATFORMS, token as string) || contract;
const spinner = ora('Fetching data...').start();
const spinner = ora(MESSAGES.FETCHING_DATA).start();

fetch(TEZTOK_API, { method: 'POST', body: JSON.stringify({ query, variables: { address, token } }) })
.then((e) => e.json())
Expand All @@ -76,9 +76,7 @@ export const action = () => {
await SaveToFile(`collectors-${platform}-${address}-${token}.csv`, data);
})
.catch(() => {
const errorMsg = 'Error exporting collectors';
error(errorMsg);
spinner.fail(errorMsg);
spinner.fail(MESSAGES.ERROR_COLLECTOR_EXPORT);
});
});
};
11 changes: 10 additions & 1 deletion src/commands/export.ts
@@ -1,7 +1,16 @@
import { Command } from 'commander';

import { action as exportCollectorsAction } from '@actions/export-collectors';
import { action as exportTokenCollectorsAction } from '@actions/export-token-collectors';

export const command = new Command('export');
export const command = new Command('export').description('export token data to csv');
// usage: versum export token-collectors
command.command('token-collectors').description('exports token collectors').action(exportTokenCollectorsAction);
//usage: versum export collectors
command
.command('collectors')
.description('exports collectors')
.option('-p, --platform', 'select platform to export from')
.action((option) => {
exportCollectorsAction(option.platform);
});
22 changes: 22 additions & 0 deletions src/constants.ts
Expand Up @@ -28,6 +28,7 @@ export enum PLATFORMS {
VERSUM = 'versum',
HICETNUNC = 'hicetnunc',
FXHASH = 'fxhash',
OBJKT = 'objkt',
OTHER = 'other',
}

Expand All @@ -48,3 +49,24 @@ export const getContractFromPlatform = (platform: PLATFORMS, token_id: string) =
return false;
}
};

export const getTezTokPlatform = (platform: PLATFORMS) => {
switch (platform) {
case PLATFORMS.VERSUM:
return 'VERSUM';
case PLATFORMS.HICETNUNC:
return 'HEN';
case PLATFORMS.FXHASH:
return 'FXHASH';
case PLATFORMS.OBJKT:
return 'OBJKT';
default:
return false;
}
};

export enum MESSAGES {
SELECT_PLATFORM = 'Select platform',
ERROR_COLLECTOR_EXPORT = 'Error exporting collectors',
FETCHING_DATA = 'Fetching data...',
}

0 comments on commit f721109

Please sign in to comment.