-
Notifications
You must be signed in to change notification settings - Fork 0
feat(export): refactor code to allow for options #21
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,7 +63,7 @@ | |
| "inquirer": "8.0.0", | ||
| "node-fetch": "2.6.2", | ||
| "objects-to-csv": "^1.3.6", | ||
| "ora-classic": "^5.4.2", | ||
| "ora": "5.4.1", | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reverting to |
||
| "update-notifier": "5.1.0" | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,70 @@ | ||
| import inquirer, { Answers } from 'inquirer'; | ||
| import inquirer, { Answers, Separator } from 'inquirer'; | ||
| import fetch from 'node-fetch'; | ||
| import ora from 'ora-classic'; | ||
| import ora from 'ora'; | ||
|
|
||
| import { CONTRACT_VERSUM, getContractFromPlatform, PLATFORMS, TEZTOK_API, MESSAGES } from '@constants'; | ||
| import { CONTRACT_VERSUM, ERRORS, getContractFromPlatform, MESSAGES, PLATFORMS, TEZTOK_API } from '@constants'; | ||
| import { CollectorsType } from '@custom-types/collectors'; | ||
| import { validateContractAddress } from '@taquito/utils'; | ||
| import { SaveToFile } from '@utils/csv'; | ||
| import { error } from '@utils/logger'; | ||
| import { error, info } from '@utils/logger'; | ||
|
|
||
| type CollectorsType = { | ||
| holder_address: string; | ||
| amount: number; | ||
| }[]; | ||
| const handleAction = (address: string, token: string) => { | ||
| const query = ` | ||
| query GetTokenCollectors($address: String!, $token: String!) { | ||
| tokens_by_pk(fa2_address: $address, token_id: $token) { | ||
| holdings(where: { amount: { _gt: "0" } }) { | ||
| holder_address | ||
| amount | ||
| } | ||
| } | ||
| }`; | ||
|
|
||
| return fetch(TEZTOK_API, { method: 'POST', body: JSON.stringify({ query, variables: { address, token } }) }) | ||
| .then((e) => e.json()) | ||
| .then((e) => e.data.tokens_by_pk.holdings) | ||
| .then(async (collectors: CollectorsType) => { | ||
| // parse data | ||
| const data = collectors.map(({ holder_address, amount }) => ({ | ||
| address: holder_address, | ||
| amount, | ||
| })); | ||
| const filename = await SaveToFile(`token-collectors-${address}-${token}.csv`, data); | ||
| return `File saved ${filename}`; | ||
| }); | ||
| }; | ||
|
|
||
| export const action = (options: Record<string, string>) => { | ||
| // if options are present, bypass the user promp | ||
| if (options.contract && options.token) { | ||
| handleAction(options.contract, options.token) | ||
| .then((message) => info(message)) | ||
| .catch(() => { | ||
| error(ERRORS.ERROR_EXPORT_COLLECTOR); | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| export const action = () => { | ||
| // otherwise show user promp | ||
| const questions = [ | ||
| { | ||
| type: 'list', | ||
| name: 'platform', | ||
| message: MESSAGES.SELECT_PLATFORM, | ||
| choices: [PLATFORMS.VERSUM, PLATFORMS.HICETNUNC, PLATFORMS.FXHASH, PLATFORMS.OTHER], | ||
| choices: [PLATFORMS.VERSUM, PLATFORMS.HICETNUNC, PLATFORMS.FXHASH, new Separator(), PLATFORMS.OTHER], | ||
| default() { | ||
| return PLATFORMS.VERSUM; | ||
| }, | ||
| }, | ||
| { | ||
| type: 'input', | ||
| name: 'contract', | ||
| message: 'Enter contract address', | ||
| message: MESSAGES.ENTER_CONTRACT_ADDRESS, | ||
| when: (answers: Answers) => answers.platform === PLATFORMS.OTHER, | ||
| validate: async (input: string) => { | ||
| if ((await validateContractAddress(input)) === 3) { | ||
| return true; | ||
| } | ||
| error('\nInvalid contract address'); | ||
| return false; | ||
| return ERRORS.ERROR_INVALID_ADDRESS; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is how the inquirer API is meant to be used around validation. by returning a string it gives the users the hability to try again. |
||
| }, | ||
| default() { | ||
| return CONTRACT_VERSUM; | ||
|
|
@@ -42,41 +73,26 @@ export const action = () => { | |
| { | ||
| type: 'input', | ||
| name: 'token', | ||
| message: 'Enter token id', | ||
| message: MESSAGES.ENTER_TOKEN_ID, | ||
| default() { | ||
| return '0'; | ||
| }, | ||
| }, | ||
| ]; | ||
| const query = ` | ||
| query GetTokenCollectors($address: String!, $token: String!) { | ||
| tokens_by_pk(fa2_address: $address, token_id: $token) { | ||
| holdings(where: { amount: { _gt: "0" } }) { | ||
| holder_address | ||
| amount | ||
| } | ||
| } | ||
| }`; | ||
|
|
||
| inquirer.prompt(questions).then(({ platform, contract, token }: Record<string, string>) => { | ||
| // select contract address from platform | ||
| // select contract address from platform alias | ||
| const address: string = getContractFromPlatform(platform as PLATFORMS, token as string) || contract; | ||
| const spinner = ora(MESSAGES.FETCHING_DATA).start(); | ||
|
|
||
| fetch(TEZTOK_API, { method: 'POST', body: JSON.stringify({ query, variables: { address, token } }) }) | ||
| .then((e) => e.json()) | ||
| .then((e) => e.data.tokens_by_pk.holdings) | ||
| .then(async (collectors: CollectorsType) => { | ||
| // parse data | ||
| const data = collectors.map(({ holder_address, amount }) => ({ | ||
| address: holder_address, | ||
| amount, | ||
| })); | ||
| handleAction(address, token) | ||
| .then((message: string) => { | ||
| spinner.succeed(); | ||
| await SaveToFile(`collectors-${platform}-${address}-${token}.csv`, data); | ||
| info(message); | ||
| }) | ||
| .catch(() => { | ||
| spinner.fail(MESSAGES.ERROR_COLLECTOR_EXPORT); | ||
| spinner.fail(); | ||
| error(ERRORS.ERROR_EXPORT_COLLECTOR); | ||
| }); | ||
| }); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import inquirer, { Separator } from 'inquirer'; | ||
| import fetch from 'node-fetch'; | ||
| import ora from 'ora'; | ||
|
|
||
| import { ERRORS, getKeyFromPlatform, MESSAGES, PLATFORMS, TEZTOK_API } from '@constants'; | ||
| import { CollectorsType } from '@custom-types/collectors'; | ||
| import { validateAddress } from '@taquito/utils'; | ||
| import { SaveToFile } from '@utils/csv'; | ||
| import { error, info } from '@utils/logger'; | ||
|
|
||
| const handleAction = (address: string, platform?: PLATFORMS) => { | ||
| 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 | ||
| } | ||
| } | ||
| }`; | ||
|
|
||
| return fetch(TEZTOK_API, { method: 'POST', body: JSON.stringify({ query, variables: { address } }) }) | ||
| .then((e) => e.json()) | ||
| .then((e) => e.data.holdings) | ||
| .then(async (collectors: CollectorsType) => { | ||
| // parse data | ||
| const data = collectors | ||
| .map(({ holder_address, amount, token }) => ({ | ||
| address: holder_address, | ||
| amount, | ||
| platform: token ? token.platform : '', | ||
| token: token ? token.token_id : '', | ||
| })) | ||
| .filter((e) => { | ||
| if (platform === PLATFORMS.ALL) { | ||
| return true; | ||
| } | ||
| return e.platform === getKeyFromPlatform(platform as PLATFORMS); | ||
| }); | ||
| const filename = await SaveToFile(`wallet-collectors-${address}-${platform}.csv`, data); | ||
| return `File saved ${filename}`; | ||
| }); | ||
| }; | ||
|
|
||
| export const action = (options: Record<string, string>) => { | ||
| // if options are present, bypass the user promp | ||
| if (options.wallet) { | ||
| handleAction(options.wallet, (options.platform as PLATFORMS) || PLATFORMS.ALL) | ||
| .then((message) => info(message)) | ||
| .catch(() => { | ||
| error(ERRORS.ERROR_EXPORT_COLLECTOR); | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const questions = [ | ||
| { | ||
| type: 'input', | ||
| name: 'address', | ||
| message: MESSAGES.ENTER_USER_ADDRESS, | ||
| validate: async (input: string) => { | ||
| if ((await validateAddress(input)) === 3) { | ||
| return true; | ||
| } | ||
|
|
||
| return ERRORS.ERROR_INVALID_ADDRESS; | ||
| }, | ||
| // TODO: https://github.com/versumstudios/cli/issues/20 | ||
| // default() { | ||
| // return 'tz1gi68wGST7UtzkNpnnc354mpqCcVNQVcSw'; | ||
| // }, | ||
| }, | ||
| { | ||
| type: 'list', | ||
| name: 'platform', | ||
| message: MESSAGES.SELECT_PLATFORM, | ||
| choices: [ | ||
| PLATFORMS.VERSUM, | ||
| PLATFORMS.HICETNUNC, | ||
| PLATFORMS.FXHASH, | ||
| PLATFORMS.OBJKTCOM, | ||
| PLATFORMS.EIGHTBIDOU, | ||
| PLATFORMS.TYPED, | ||
| new Separator(), | ||
| PLATFORMS.ALL, | ||
| ], | ||
| default() { | ||
| return PLATFORMS.ALL; | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| inquirer.prompt(questions).then(({ address, platform }: Record<string, string>) => { | ||
| const spinner = ora(MESSAGES.FETCHING_DATA).start(); | ||
|
|
||
| handleAction(address, platform as PLATFORMS) | ||
| .then((message: string) => { | ||
| spinner.succeed(); | ||
| info(message); | ||
| }) | ||
| .catch(() => { | ||
| spinner.fail(); | ||
| error(ERRORS.ERROR_EXPORT_COLLECTOR); | ||
| }); | ||
| }); | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Started to add a list of documents that hopefully will help people (and us in the future) to keep track of everything.