/
airdrops.js
235 lines (198 loc) · 7.96 KB
/
airdrops.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* eslint-disable no-await-in-loop */
/* global actions, api */
const UTILITY_TOKEN_SYMBOL = 'BEE';
const UTILITY_TOKEN_PRECISION = 8;
const CONTRACT_NAME = 'airdrops';
actions.createSSC = async () => {
const tableExists = await api.db.tableExists('pendingAirdrops');
if (tableExists === false) {
await api.db.createTable('pendingAirdrops', ['airdropId', 'symbol']);
await api.db.createTable('params');
const params = {};
params.feePerTransaction = '0.1';
params.maxTransactionsPerBlock = 50;
params.maxAirdropsPerBlock = 1;
await api.db.insert('params', params);
}
};
actions.updateParams = async (payload) => {
if (api.assert(api.sender === api.owner, 'not authorized')) {
const {
feePerTransaction,
maxTransactionsPerBlock,
maxAirdropsPerBlock,
} = payload;
const params = await api.db.findOne('params', {});
if (feePerTransaction) {
if (!api.assert(typeof feePerTransaction === 'string' && !api.BigNumber(feePerTransaction).isNaN() && api.BigNumber(feePerTransaction).gte(0), 'invalid feePerTransaction')) return;
params.feePerTransaction = feePerTransaction;
}
if (maxTransactionsPerBlock) {
if (!api.assert(Number.isInteger(maxTransactionsPerBlock) && maxTransactionsPerBlock >= 1, 'invalid maxTransactionsPerBlock')) return;
params.maxTransactionsPerBlock = maxTransactionsPerBlock;
}
if (maxAirdropsPerBlock) {
if (!api.assert(Number.isInteger(maxAirdropsPerBlock) && maxAirdropsPerBlock >= 1, 'invalid maxAirdropsPerBlock')) return;
params.maxAirdropsPerBlock = maxAirdropsPerBlock;
}
await api.db.update('params', params);
}
};
const parseAirdrop = async (list, precision) => {
const params = await api.db.findOne('params', {});
const airdrop = {};
airdrop.list = [];
airdrop.fee = '0';
airdrop.quantity = '0';
airdrop.isValid = false;
// loop through list and validate
for (let i = 0; i < list.length; i += 1) {
const { 0: to, 1: quantity } = list[i];
if (api.assert(to, `list[${i}]: account name cannot be undefined`)
&& api.assert(api.isValidAccountName(to), `list[${i}]: invalid account name`)
&& api.assert(quantity, `list[${i}]: quantity cannot be undefined`)
&& api.assert(!api.BigNumber(quantity).isNaN(), `list[${i}]: invalid quantity`)
&& api.assert(api.BigNumber(quantity).gt(0), `list[${i}]: quantity must be positive`)
&& api.assert(api.BigNumber(quantity).dp() <= precision, `list[${i}]: quantity precision mismatch`)) {
airdrop.list.push({
to,
quantity,
});
// add this quantity to the total quantity of tokens to airdrop
airdrop.quantity = api.BigNumber(airdrop.quantity).plus(quantity);
} else break;
}
// calculate total fee
airdrop.fee = api.BigNumber(params.feePerTransaction).times(airdrop.list.length)
.toFixed(UTILITY_TOKEN_PRECISION);
// list validation, check if all values from list are valid & pushed into airdrop.list
if (api.assert(list.length > 0, 'list cannot be empty') && list.length === airdrop.list.length) {
airdrop.isValid = true;
}
return airdrop;
};
const hasValidType = (token, type) => {
if (type === 'transfer') {
return true;
}
// check if staking is enabled
if (type === 'stake' && api.assert(token.stakingEnabled === true, 'staking not enabled')) {
return true;
}
return false;
};
const transferIsSuccessful = (result, action, from, to, symbol, quantity) => {
if (result.errors === undefined
&& result.events && result.events.find(el => el.contract === 'tokens'
&& el.event === action
&& el.data.from === from
&& el.data.to === to
&& api.BigNumber(el.data.quantity).eq(quantity)
&& el.data.symbol === symbol) !== undefined) {
return true;
}
return false;
};
actions.newAirdrop = async (payload) => {
const {
symbol,
type,
list,
isSignedWithActiveKey,
} = payload;
if (api.assert(isSignedWithActiveKey === true, 'you must use a custom_json signed with your active key')
&& api.assert(symbol && typeof symbol === 'string'
&& type && typeof type === 'string'
&& list && Array.isArray(list), 'invalid params')
&& api.assert(type === 'transfer' || type === 'stake', 'invalid type')) {
const token = await api.db.findOneInTable('tokens', 'tokens', { symbol });
// get api.sender's utility and airdrop token balances
const utilityToken = await api.db.findOneInTable('tokens', 'balances', { account: api.sender, symbol: UTILITY_TOKEN_SYMBOL });
const nativeToken = await api.db.findOneInTable('tokens', 'balances', { account: api.sender, symbol });
if (api.assert(token !== null, 'symbol does not exist')
&& hasValidType(token, type)) {
const airdrop = await parseAirdrop(list, token.precision);
if (airdrop.isValid
&& api.assert(utilityToken && utilityToken.balance
&& api.BigNumber(utilityToken.balance).gte(airdrop.fee), 'you must have enough tokens to cover the airdrop fee')
&& api.assert(nativeToken && nativeToken.balance
&& api.BigNumber(nativeToken.balance).gte(airdrop.quantity), 'you must have enough tokens to do the airdrop')) {
// validations completed
// lock airdrop tokens by transfering them to contract
const tokenTransfer = await api.executeSmartContract('tokens', 'transferToContract', {
to: CONTRACT_NAME, symbol, quantity: airdrop.quantity,
});
if (transferIsSuccessful(tokenTransfer, 'transferToContract', api.sender, CONTRACT_NAME, symbol, airdrop.quantity)) {
// deduct fee from sender's utility token balance
const feeTransfer = await api.executeSmartContract('tokens', 'transfer', {
to: 'null', symbol: UTILITY_TOKEN_SYMBOL, quantity: airdrop.fee, isSignedWithActiveKey,
});
if (transferIsSuccessful(feeTransfer, 'transfer', api.sender, 'null', UTILITY_TOKEN_SYMBOL, airdrop.fee)) {
const res = await api.db.insert('pendingAirdrops', {
airdropId: api.transactionId,
symbol,
type,
list: airdrop.list,
blockNumber: api.blockNumber,
});
api.emit('newAirdrop', { airdropId: res.airdropId });
} else {
// if fee transfer was failed, return native balance to api.sender
await api.transferTokens(api.sender, symbol, airdrop.quantity, 'user');
}
}
}
}
}
};
const processAirdrop = async (airdrop, maxTransactionsPerBlock) => {
const {
airdropId,
list,
symbol,
type,
} = airdrop;
let count = 0;
while (count < maxTransactionsPerBlock && list[count] !== undefined) {
const { to, quantity } = list[count];
if (type === 'transfer') {
// transfer tokens
await api.transferTokens(to, symbol, quantity, 'user');
} else if (type === 'stake') {
// stake tokens
await api.executeSmartContract('tokens', 'stakeFromContract', {
to, symbol, quantity,
});
}
count += 1;
}
api.BigNumber(count).plus(1);
airdrop.list.splice(0, count);
if (airdrop.list.length > 0) {
// if limit has been reached & transactions are still remaining, update airdrop
await api.db.update('pendingAirdrops', airdrop);
} else {
// if no other transactions are remaining, delete airdrop
await api.db.remove('pendingAirdrops', airdrop);
}
api.emit('airdropDistribution', {
airdropId,
transactions: count,
});
};
actions.checkPendingAirdrops = async () => {
if (api.assert(api.sender === 'null', 'not authorized')) {
const params = await api.db.findOne('params', {});
const pendingAirdrops = await api.db.find('pendingAirdrops',
{
blockNumber: { $lt: api.blockNumber },
},
params.maxAirdropsPerBlock,
0,
[{ index: '_id', descending: false }]);
for (let i = 0; i < pendingAirdrops.length; i += 1) {
const airdrop = pendingAirdrops[i];
await processAirdrop(airdrop, params.maxTransactionsPerBlock);
}
}
};