Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(export): add collector export (#17)
* 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
1 parent
0281a03
commit f721109
Showing
4 changed files
with
133 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters