-
Notifications
You must be signed in to change notification settings - Fork 3
/
mcoin.ts
197 lines (168 loc) · 6.85 KB
/
mcoin.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env node
import * as program from "commander";
import MeetupToken from "./src/MeetupToken";
program
.option('-k, --key <key>', 'Meetup API key')
.option('-m, --meetupName <meetupName>', 'Meetup name. eg SydEthereum')
.option('-h, --wshost <wsHost>', 'Host of WS-RPC server listening interface (default: "localhost")')
.option('-p, --wsport <wsPort>', 'Post of WS-RPC server listening interface (default: "8546")')
.option('-o, --owner <owner>', 'Address of contract owner')
.option('-c, --contract <contract>', 'Contract address of the Meetup token')
.option('-b, --contractBlock <contractBlock>', 'Block the Meetup token contract was deployed')
.option('-s, --symbol <symbol>', 'Symbol of the Mettup token (default "SET")')
.option('-t, --tokenName <tokenName>', 'Name of the Meetup token (default "Transferable Sydney Ethereum Token")')
.option('-o, --verbose <level>', '0 trace, 1 debug, 2 info, 3 warn, 4 error (default 2)');
program
.command('deploy')
.description('deploy new Meetup token contract')
.action(async function(command, eventId)
{
const meetupToken = initMeetupToken();
const tokenConfig = loadTokenConfig();
try
{
const contract = await meetupToken.deployTokenContract(
tokenConfig.symbol,
tokenConfig.tokenName
);
console.log(`Contract address for newly deployed meetup token is ${contract}`);
process.exit();
}
catch (err)
{
console.error(`Could not deploy new contract for meetup. Error: ${err.message}`);
process.exit(1);
}
});
program
.command('members')
.description('Issue tokens to new members of the Meetup')
.action(async function(command, eventId)
{
const meetupToken = initMeetupToken();
try
{
const newMembers = await meetupToken.issueTokensToNewMembers();
console.log(`Tokens issued to ${newMembers.length} new members`);
process.exit();
}
catch (err)
{
console.error(`Could not issue tokens to new members of the ${meetupToken.meetup.urlname} meetup. Error: ${err.message}`);
process.exit(1);
}
});
program
.command('event <id>')
.description('Issue tokens to members who attended a Meetup event with Meetup event id')
.action(async (eventId) =>
{
const meetupToken = initMeetupToken();
if (!eventId) {
console.error(`Error: id of the Meetup event must be an argument to the event.`);
process.exit(1);
}
try {
const members = await meetupToken.issueTokensToMembersAtEvent(eventId);
console.log(`${members.length} members were issued tokens`);
process.exit();
}
catch (err) {
console.error(`Error: could not issue token to members that attended Meetup event with id ${eventId}.`);
process.exit(1);
}
});
program.parse(process.argv);
// display help if no commands passed into program. The first 2 arguments are node and mcoin.js
if (process.argv.length < 3) {
program.outputHelp();
process.exit(2);
}
function loadMeetupConfig(): {
key: string,
meetupName: string
}
{
// set the NODE_ENV environment so the meetup config file can be loaded
process.env.NODE_ENV = 'meetup';
// load the meetup.yaml file in the config folder
const config = require('config').util.loadFileConfigs();
const returnOptions = {
key: program.key || config.key,
meetupName: program.meetupName || config.name
};
if (!returnOptions.key) {
console.log(`Error: Meetup API key must be set with the --key option or in the config/meetup.yaml config file.`);
console.log(`You can generate a Meetup API key at https://secure.meetup.com/meetup_api/key/`);
process.exit(1);
}
else if (!returnOptions.meetupName)
{
console.log(`Error: Meetup name must be set with the --name option or in the config/meetup.yaml config file. eg SydEthereum`);
process.exit(1);
}
return returnOptions;
}
function loadTokenConfig(): {
wsurl: string,
contractOwner: string,
contractAddress?: string,
contractAddressBlock?: number,
symbol: string,
tokenName: string,
issueAmounts: {
newMember: number,
attendEvent: number,
speakAtEvent: number,
hostEvent: number,
sponsorEvent: number
}
}
{
// set the NODE_ENV environment so the token config file can be loaded
process.env.NODE_ENV = 'token';
// load the token.yaml file in the config folder
const config = require('config').util.loadFileConfigs();
// use the program options in preference to the configuration file
const contractOwner = program.owner || config.contractOwner;
if (!contractOwner) {
console.log(`Error: owner of the token contract must be set with the --owner option or in the config/token.yaml config file.`);
console.log(`The owner is the address that the contract was or will be created from. It's the externally owned account that is sending the Ethereum transaction.`);
process.exit(1);
}
// use the program options in preference to the configuration file or geth defaults
const wshost = program.wshost || config.wshost || 'localhost';
const wsport = program.wsport || config.wsport || '8546';
if (!config.amounts) {
config.amounts = {};
}
return {
wsurl: `ws://${wshost}:${wsport.toString()}`,
contractOwner: contractOwner,
contractAddress: program.contract || config.contractAddress,
contractAddressBlock: program.contractBlock || config.contractAddressBlock,
symbol: program.symbol || config.symbol || 'SET',
tokenName: program.tokenName || config.tokenName || 'Transferable Sydney Ethereum Token',
issueAmounts: {
newMember: program.newMember || config.issueAmounts.newMember || 1000,
attendEvent: program.attendEvent || config.issueAmounts.attendEvent || 2000,
speakAtEvent: program.speakAtEvent || config.issueAmounts.speakAtEvent || 3000,
hostEvent: program.hostEvent || config.issueAmounts.hostEvent || 5000,
sponsorEvent: program.sponsorEvent || config.issueAmounts.sponsorEvent || 10000
}
};
}
function initMeetupToken(): MeetupToken
{
const meetupConfig = loadMeetupConfig();
const tokenConfig = loadTokenConfig();
return new MeetupToken({
apiKey: meetupConfig.key,
urlname: meetupConfig.meetupName,
contractAddress: tokenConfig.contractAddress,
contractAddressBlock: tokenConfig.contractAddressBlock,
contractOwner: tokenConfig.contractOwner,
wsURL: tokenConfig.wsurl,
issueAmounts: tokenConfig.issueAmounts
});
}