| 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557 |
| // SPDX-License-Identifier: NOLICENSE
/***
* @author Original: Zenos Pavlakou
* @author Modified: Alexandre Tolstenko
* @notice reference https://cryptomultisender.com/blog/our-airdrop-smart-contract
*/
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
pragma solidity ^0.8.9;
contract CryptoMultisender is Ownable {
using SafeMath for uint256;
mapping (address => uint256) public tokenTrialDrops;
mapping (address => bool) public isPremiumMember;
mapping (address => bool) public isAffiliate;
mapping (string => address) public affiliateCodeToAddr;
mapping (string => bool) public affiliateCodeExists;
mapping (address => string) public affiliateCodeOfAddr;
mapping (address => string) public isAffiliatedWith;
uint256 public premiumMemberFee;
uint256 public rate;
uint256 public dropUnitPrice;
event TokenAirdrop(address indexed by, address indexed tokenAddress, uint256 totalTransfers);
event EthAirdrop(address indexed by, uint256 totalTransfers, uint256 ethValue);
event RateChanged(uint256 from, uint256 to);
event RefundIssued(address indexed to, uint256 totalWei);
event ERC20TokensWithdrawn(address token, address sentTo, uint256 value);
event CommissionPaid(address indexed to, uint256 value);
event NewPremiumMembership(address indexed premiumMember);
event NewAffiliatePartnership(address indexed newAffiliate, string indexed affiliateCode);
event AffiliatePartnershipRevoked(address indexed affiliate, string indexed affiliateCode);
event PremiumMemberFeeUpdated(uint256 newFee);
constructor() {
rate = 10000;
dropUnitPrice = 1e14;
premiumMemberFee = 25e16;
}
/**
* Allows the owner of this contract to change the fee for users to become premium members.
*
* @param _fee The new fee.
*
* @return True if the fee is changed successfully. False otherwise.
* */
function setPremiumMemberFee(uint256 _fee) public onlyOwner returns(bool) {
require(_fee > 0 && _fee != premiumMemberFee);
premiumMemberFee = _fee;
emit PremiumMemberFeeUpdated(_fee);
return true;
}
function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len;
while (_i != 0) {
k = k-1;
uint8 temp = (48 + uint8(_i - _i / 10 * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}
/**
* Used to give change to users who accidentally send too much ETH to payable functions.
*
* @param _price The service fee the user has to pay for function execution.
**/
function giveChange(uint256 _price) internal {
if(msg.value > _price) {
uint256 change = msg.value.sub(_price);
payable(msg.sender).transfer(change);
}
}
/**
* Ensures that the correct affiliate code is used and also ensures that affiliate partners
* are not able to 'jack' commissions from existing users who they are not affiliated with.
*
* @param _afCode The affiliate code provided by the user.
*
* @return The correct affiliate code or void.
**/
function processAffiliateCode(string memory _afCode) internal returns(string memory) {
if(stringsAreEqual(isAffiliatedWith[msg.sender], "void") || !isAffiliate[affiliateCodeToAddr[_afCode]]) {
isAffiliatedWith[msg.sender] = "void";
return "void";
}
if(!stringsAreEqual(_afCode, "") && stringsAreEqual(isAffiliatedWith[msg.sender],"")
&& affiliateCodeExists[_afCode]) {
if(affiliateCodeToAddr[_afCode] == msg.sender) {
return "void";
}
isAffiliatedWith[msg.sender] = _afCode;
}
if(stringsAreEqual(_afCode,"") && !stringsAreEqual(isAffiliatedWith[msg.sender],"")) {
_afCode = isAffiliatedWith[msg.sender];
}
if(stringsAreEqual(_afCode,"") || !affiliateCodeExists[_afCode]) {
isAffiliatedWith[msg.sender] = "void";
_afCode = "void";
}
return _afCode;
}
/**
* Allows the owner of this contract to grant users with premium membership.
*
* @param _addr The address of the user who is being granted premium membership.
*
* @return True if premium membership is granted successfully. False otherwise.
**/
function grantPremiumMembership(address _addr) public onlyOwner returns(bool) {
require(!isPremiumMember[_addr], "Is already premiumMember member");
isPremiumMember[_addr] = true;
emit NewPremiumMembership(_addr);
return true;
}
/**
* Allows users to become premium members.
*
* @param _afCode If a user has been refferred by an affiliate partner, they can provide
* an affiliate code so the partner gets commission.
*
* @return True if user successfully becomes premium member. False otherwise.
**/
function becomePremiumMember(string memory _afCode) public payable returns(bool) {
require(!isPremiumMember[msg.sender], "Is already premiumMember member");
require(
msg.value >= premiumMemberFee,
string(abi.encodePacked(
"premiumMember fee is: ", uint2str(premiumMemberFee), ". Not enough ETH sent. ", uint2str(msg.value)
))
);
isPremiumMember[msg.sender] = true;
_afCode = processAffiliateCode(_afCode);
giveChange(premiumMemberFee);
if(!stringsAreEqual(_afCode,"void") && isAffiliate[affiliateCodeToAddr[_afCode]]) {
payable(owner()).transfer(premiumMemberFee.mul(80).div(100));
uint256 commission = premiumMemberFee.mul(20).div(100);
payable(affiliateCodeToAddr[_afCode]).transfer(commission);
emit CommissionPaid(affiliateCodeToAddr[_afCode], commission);
} else {
payable(owner()).transfer(premiumMemberFee);
}
emit NewPremiumMembership(msg.sender);
return true;
}
/**
* Allows the owner of this contract to add an affiliate partner.
*
* @param _addr The address of the new affiliate partner.
* @param _code The affiliate code.
*
* @return True if the affiliate has been added successfully. False otherwise.
**/
function addAffiliate(address _addr, string memory _code) public onlyOwner returns(bool) {
require(!isAffiliate[_addr], "Address is already an affiliate.");
require(_addr != address(0));
require(!affiliateCodeExists[_code]);
affiliateCodeExists[_code] = true;
isAffiliate[_addr] = true;
affiliateCodeToAddr[_code] = _addr;
affiliateCodeOfAddr[_addr] = _code;
emit NewAffiliatePartnership(_addr,_code);
return true;
}
/**
* Allows the owner of this contract to remove an affiliate partner.
*
* @param _addr The address of the affiliate partner.
*
* @return True if affiliate partner is removed successfully. False otherwise.
**/
function removeAffiliate(address _addr) public onlyOwner returns(bool) {
require(isAffiliate[_addr]);
isAffiliate[_addr] = false;
affiliateCodeToAddr[affiliateCodeOfAddr[_addr]] = address(0);
emit AffiliatePartnershipRevoked(_addr, affiliateCodeOfAddr[_addr]);
affiliateCodeOfAddr[_addr] = "No longer an affiliate partner";
return true;
}
/**
* Checks whether or not an ERC20 token has used its free trial of 100 drops. This is a constant
* function which does not alter the state of the contract and therefore does not require any gas
* or a signature to be executed.
*
* @param _addressOfToken The address of the token being queried.
*
* @return true if the token being queried has not used its 100 first free trial drops, false
* otherwise.
* */
function tokenHasFreeTrial(address _addressOfToken) public view returns(bool) {
return tokenTrialDrops[_addressOfToken] < 100;
}
/**
* Checks how many remaining free trial drops a token has.
*
* @param _addressOfToken the address of the token being queried.
*
* @return the total remaining free trial drops of a token.
* */
function getRemainingTrialDrops(address _addressOfToken) public view returns(uint256) {
if(tokenHasFreeTrial(_addressOfToken)) {
uint256 maxTrialDrops = 100;
return maxTrialDrops.sub(tokenTrialDrops[_addressOfToken]);
}
return 0;
}
/**
* Allows for the price of drops to be changed by the owner of the contract. Any attempt made by
* any other account to invoke the function will result in a loss of gas and the price will remain
* untampered.
*
* @return true if function executes successfully, false otherwise.
* */
function setRate(uint256 _newRate) public onlyOwner returns(bool) {
require(
_newRate != rate
&& _newRate > 0
);
emit RateChanged(rate, _newRate);
rate = _newRate;
uint256 eth = 1 ether;
dropUnitPrice = eth.div(rate);
return true;
}
/**
* Allows for the allowance of a token from its owner to this contract to be queried.
*
* As part of the ERC20 standard all tokens which fall under this category have an allowance
* function which enables owners of tokens to allow (or give permission) to another address
* to spend tokens on behalf of the owner. This contract uses this as part of its protocol.
* Users must first give permission to the contract to transfer tokens on their behalf, however,
* this does not mean that the tokens will ever be transferrable without the permission of the
* owner. This is a security feature which was implemented on this contract. It is not possible
* for the owner of this contract or anyone else to transfer the tokens which belong to others.
*
* @param _addr The address of the token's owner.
* @param _addressOfToken The contract address of the ERC20 token.
*
* @return The ERC20 token allowance from token owner to this contract.
* */
function getTokenAllowance(address _addr, address _addressOfToken) public view returns(uint256) {
IERC20 token = IERC20(_addressOfToken);
return token.allowance(_addr, address(this));
}
fallback() external payable {
revert();
}
receive() external payable {
revert();
}
/**
* Checks if two strings are the same.
*
* @param _a String 1
* @param _b String 2
*
* @return True if both strings are the same. False otherwise.
**/
function stringsAreEqual(string memory _a, string memory _b) internal pure returns(bool) {
bytes32 hashA = keccak256(abi.encodePacked(_a));
bytes32 hashB = keccak256(abi.encodePacked(_b));
return hashA == hashB;
}
/**
* Allows for the distribution of Ether to be transferred to multiple recipients at
* a time. This function only facilitates batch transfers of constant values (i.e., all recipients
* will receive the same amount of tokens).
*
* @param _recipients The list of addresses which will receive tokens.
* @param _value The amount of tokens all addresses will receive.
* @param _afCode If the user is affiliated with a partner, they will provide this code so that
* the parter is paid commission.
*
* @return true if function executes successfully, false otherwise.
* */
function singleValueEthAirrop(address[] memory _recipients, uint256 _value, string memory _afCode) public payable returns(bool) {
uint256 price = _recipients.length.mul(dropUnitPrice);
uint256 totalCost = _value.mul(_recipients.length).add(price);
require(
msg.value >= totalCost|| isPremiumMember[msg.sender],
"Not enough ETH sent with transaction!"
);
_afCode = processAffiliateCode(_afCode);
if(!isPremiumMember[msg.sender]) {
distributeCommission(_recipients.length, _afCode);
}
giveChange(totalCost);
for(uint i=0; i<_recipients.length; i++) {
if(_recipients[i] != address(0)) {
payable(_recipients[i]).transfer(_value);
}
}
emit EthAirdrop(msg.sender, _recipients.length, _value.mul(_recipients.length));
return true;
}
function _getTotalEthValue(uint256[] memory _values) internal pure returns(uint256) {
uint256 totalVal = 0;
for(uint i = 0; i < _values.length; i++) {
totalVal = totalVal.add(_values[i]);
}
return totalVal;
}
/**
* Allows for the distribution of Ether to be transferred to multiple recipients at
* a time.
*
* @param _recipients The list of addresses which will receive tokens.
* @param _values The corresponding amounts that the recipients will receive
* @param _afCode If the user is affiliated with a partner, they will provide this code so that
* the parter is paid commission.
*
* @return true if function executes successfully, false otherwise.
* */
function multiValueEthAirdrop(address[] memory _recipients, uint256[] memory _values, string memory _afCode) public payable returns(bool) {
require(_recipients.length == _values.length, "Total number of recipients and values are not equal");
uint256 totalEthValue = _getTotalEthValue(_values);
uint256 price = _recipients.length.mul(dropUnitPrice);
uint256 totalCost = totalEthValue.add(price);
require(
msg.value >= totalCost || isPremiumMember[msg.sender],
"Not enough ETH sent with transaction!"
);
_afCode = processAffiliateCode(_afCode);
if(!isPremiumMember[msg.sender]) {
distributeCommission(_recipients.length, _afCode);
}
giveChange(totalCost);
for(uint i = 0; i < _recipients.length; i++) {
if(_recipients[i] != address(0) && _values[i] > 0) {
payable(_recipients[i]).transfer(_values[i]);
}
}
emit EthAirdrop(msg.sender, _recipients.length, totalEthValue);
return true;
}
/**
* Allows for the distribution of an ERC20 token to be transferred to multiple recipients at
* a time. This function only facilitates batch transfers of constant values (i.e., all recipients
* will receive the same amount of tokens).
*
* @param _addressOfToken The contract address of an ERC20 token.
* @param _recipients The list of addresses which will receive tokens.
* @param _value The amount of tokens all addresses will receive.
* @param _afCode If the user is affiliated with a partner, they will provide this code so that
* the parter is paid commission.
*
* @return true if function executes successfully, false otherwise.
* */
function singleValueTokenAirdrop(address _addressOfToken, address[] memory _recipients, uint256 _value, string memory _afCode) public payable returns(bool) {
IERC20 token = IERC20(_addressOfToken);
uint256 price = _recipients.length.mul(dropUnitPrice);
require(
msg.value >= price || tokenHasFreeTrial(_addressOfToken) || isPremiumMember[msg.sender],
"Not enough ETH sent with transaction!"
);
giveChange(price);
_afCode = processAffiliateCode(_afCode);
for(uint i = 0; i < _recipients.length; i++) {
if(_recipients[i] != address(0)) {
token.transferFrom(msg.sender, _recipients[i], _value);
}
}
if(tokenHasFreeTrial(_addressOfToken)) {
tokenTrialDrops[_addressOfToken] = tokenTrialDrops[_addressOfToken].add(_recipients.length);
} else {
if(!isPremiumMember[msg.sender]) {
distributeCommission(_recipients.length, _afCode);
}
}
emit TokenAirdrop(msg.sender, _addressOfToken, _recipients.length);
return true;
}
/**
* Allows for the distribution of an ERC20 token to be transferred to multiple recipients at
* a time. This function facilitates batch transfers of differing values (i.e., all recipients
* can receive different amounts of tokens).
*
* @param _addressOfToken The contract address of an ERC20 token.
* @param _recipients The list of addresses which will receive tokens.
* @param _values The corresponding values of tokens which each address will receive.
* @param _afCode If the user is affiliated with a partner, they will provide this code so that
* the parter is paid commission.
*
* @return true if function executes successfully, false otherwise.
* */
function multiValueTokenAirdrop(address _addressOfToken, address[] memory _recipients, uint256[] memory _values, string memory _afCode) public payable returns(bool) {
IERC20 token = IERC20(_addressOfToken);
require(_recipients.length == _values.length, "Total number of recipients and values are not equal");
uint256 price = _recipients.length.mul(dropUnitPrice);
require(
msg.value >= price || tokenHasFreeTrial(_addressOfToken) || isPremiumMember[msg.sender],
"Not enough ETH sent with transaction!"
);
giveChange(price);
_afCode = processAffiliateCode(_afCode);
for(uint i = 0; i < _recipients.length; i++) {
if(_recipients[i] != address(0) && _values[i] > 0) {
token.transferFrom(msg.sender, _recipients[i], _values[i]);
}
}
if(tokenHasFreeTrial(_addressOfToken)) {
tokenTrialDrops[_addressOfToken] = tokenTrialDrops[_addressOfToken].add(_recipients.length);
} else {
if(!isPremiumMember[msg.sender]) {
distributeCommission(_recipients.length, _afCode);
}
}
emit TokenAirdrop(msg.sender, _addressOfToken, _recipients.length);
return true;
}
/**
* Send the owner and affiliates commissions.
**/
function distributeCommission(uint256 _drops, string memory _afCode) internal {
if(!stringsAreEqual(_afCode,"void") && isAffiliate[affiliateCodeToAddr[_afCode]]) {
uint256 profitSplit = _drops.mul(dropUnitPrice).div(2);
payable(owner()).transfer(profitSplit);
payable(affiliateCodeToAddr[_afCode]).transfer(profitSplit);
emit CommissionPaid(affiliateCodeToAddr[_afCode], profitSplit);
} else {
payable(owner()).transfer(_drops.mul(dropUnitPrice));
}
}
/**
* Allows for any ERC20 tokens which have been mistakenly sent to this contract to be returned
* to the original sender by the owner of the contract. Any attempt made by any other account
* to invoke the function will result in a loss of gas and no tokens will be transferred out.
*
* @param _addressOfToken The contract address of an ERC20 token.
* @param _recipient The address which will receive tokens.
* @param _value The amount of tokens to refund.
*
* @return true if function executes successfully, false otherwise.
* */
function withdrawERC20Tokens(address _addressOfToken, address _recipient, uint256 _value) public onlyOwner returns(bool){
require(
_addressOfToken != address(0)
&& _recipient != address(0)
&& _value > 0
);
IERC20 token = IERC20(_addressOfToken);
token.transfer(_recipient, _value);
emit ERC20TokensWithdrawn(_addressOfToken, _recipient, _value);
return true;
}
} |