/
export-token-collectors.ts
82 lines (77 loc) · 2.43 KB
/
export-token-collectors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import inquirer, { Answers } from 'inquirer';
import fetch from 'node-fetch';
import ora from 'ora-classic';
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';
type CollectorsType = {
holder_address: string;
amount: number;
}[];
export const action = () => {
const questions = [
{
type: 'list',
name: 'platform',
message: MESSAGES.SELECT_PLATFORM,
choices: [PLATFORMS.VERSUM, PLATFORMS.HICETNUNC, PLATFORMS.FXHASH, PLATFORMS.OTHER],
default() {
return PLATFORMS.VERSUM;
},
},
{
type: 'input',
name: 'contract',
message: '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;
},
default() {
return CONTRACT_VERSUM;
},
},
{
type: 'input',
name: 'token',
message: '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
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,
}));
spinner.succeed();
await SaveToFile(`collectors-${platform}-${address}-${token}.csv`, data);
})
.catch(() => {
spinner.fail(MESSAGES.ERROR_COLLECTOR_EXPORT);
});
});
};