Skip to content

Commit 7333de8

Browse files
authored
Merge pull request #27 from SpringRole/airdrop_update
Airdrop update
2 parents 3836f87 + 3ee4794 commit 7333de8

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

contracts/AirDrop.sol

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,39 @@ contract AirDrop is Ownable {
5959
}
6060

6161
/*
62-
Airdrop function which take up a array of address and amount and call the
63-
transfer function to send the token
62+
Airdrop function which take up a array of address,token amount and eth amount and call the
63+
transfer function to send the token plus send eth to the address is balance is 0
6464
*/
65-
function doAirDrop(address[] _address, uint256 _amount) onlyOwner public returns (bool) {
65+
function doAirDrop(address[] _address, uint256 _amount, uint256 _ethAmount) onlyOwner public returns (bool) {
6666
uint256 count = _address.length;
6767
for (uint256 i = 0; i < count; i++)
6868
{
6969
/* calling transfer function from contract */
7070
tokenInstance.transfer(_address [i],_amount);
71+
if((_address [i].balance == 0) && (this.balance >= _ethAmount))
72+
{
73+
require(_address [i].send(_ethAmount));
74+
}
7175
}
7276
}
77+
78+
79+
function transferEthToOnwer() onlyOwner public returns (bool) {
80+
require(owner.send(this.balance));
81+
}
82+
83+
/*
84+
function to add eth to the contract
85+
*/
86+
function() payable {
87+
88+
}
89+
90+
/*
91+
function to kill contract
92+
*/
93+
94+
function kill() onlyOwner {
95+
selfdestruct(owner);
96+
}
7397
}

test/2_airdrop.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ contract('Airdrop', function(accounts) {
55

66
var airdropInstance;
77
var tokenInstance;
8+
var eth_contract_balance = 5*(10**18);
9+
// random address as need 0 eth balance account to test
10+
var zero_balance = '0xf59b7E0F657B0DCBaD411465F5A534121fF42B58';
11+
var zero_balance2 = '0xf59b7E0F657B0DCBaD411465F5A534121fF42B57';
812

913
before(function () {
1014
return AirDrop.deployed().then(function(instance) {
1115
airdropInstance = instance;
1216
return InviteToken.deployed();
1317
}).then(function(token) {
1418
tokenInstance = token;
19+
airdropInstance.send(eth_contract_balance,{from:accounts[0]});
1520
});
1621
});
1722

@@ -22,6 +27,10 @@ contract('Airdrop', function(accounts) {
2227
});
2328
});
2429

30+
it("should have balance 5 eth", function() {
31+
assert.equal(eth_contract_balance,web3.eth.getBalance(airdropInstance.address).toNumber() ,"should have balance 5 eth");
32+
});
33+
2534
it("airdrop should fail as no balance is assigned", function() {
2635
var address = [accounts[1],accounts[2]];
2736
return airdropInstance.doAirDrop(address,1).then(function(instance){
@@ -33,16 +42,32 @@ contract('Airdrop', function(accounts) {
3342

3443
it("do 1 token airdrop to 2 different address", function() {
3544
var amount = 1;
36-
var address = [accounts[1],accounts[2]];
45+
var eth_amount = 100;
3746

3847
// Get initial balances of first and second account.
3948
var account_one = accounts[1];
40-
var account_two = accounts[2];
49+
var account_two = zero_balance;
50+
var account_three = zero_balance2;
4151

4252
var account_one_starting_balance;
4353
var account_two_starting_balance;
54+
var account_three_starting_balance;
55+
4456
var account_one_ending_balance;
4557
var account_two_ending_balance;
58+
var account_three_ending_balance;
59+
60+
var account_one_eth_starting_balance = web3.eth.getBalance(account_one).toNumber();
61+
var account_two_eth_starting_balance = web3.eth.getBalance(account_two).toNumber();
62+
var account_three_eth_starting_balance = web3.eth.getBalance(account_two).toNumber();
63+
64+
var account_one_eth_ending_balance;
65+
var account_two_eth_ending_balance;
66+
var account_three_eth_ending_balance;
67+
68+
var address = [account_one,account_two];
69+
70+
var address_batch_2 = [account_three];
4671

4772
return tokenInstance.mint(10000)
4873
.then(function(instance){
@@ -57,20 +82,41 @@ contract('Airdrop', function(accounts) {
5782
return tokenInstance.balanceOf.call(account_two);
5883
}).then(function(balance){
5984
account_two_starting_balance = balance.toNumber();
85+
// check balance for account 2
86+
return tokenInstance.balanceOf.call(account_three);
87+
}).then(function(balance){
88+
account_three_starting_balance = balance.toNumber();
6089
// do airdrop
61-
return airdropInstance.doAirDrop(address,amount);
90+
return airdropInstance.doAirDrop(address,amount,eth_amount);
91+
}).then(function(){
92+
// do airdrop batch 2 with high eth amount
93+
return airdropInstance.doAirDrop(address_batch_2,amount,eth_contract_balance);
6294
}).then(function(){
6395
// check balance for account 1
6496
return tokenInstance.balanceOf.call(account_one);
6597
}).then(function(balance){
98+
account_one_ending_balance = balance.toNumber();
6699
// check balance for account 2
67-
account_one_ending_balance = balance.toNumber();
68100
return tokenInstance.balanceOf.call(account_two);
69101
}).then(function(balance){
70-
account_two_ending_balance = balance.toNumber();
102+
account_two_ending_balance = balance.toNumber();
103+
// check balance for account 3
104+
return tokenInstance.balanceOf.call(account_three);
105+
}).then(function(balance){
106+
account_three_ending_balance = balance.toNumber();
71107
// check if balance has updated or not
72108
assert.equal(account_one_ending_balance, account_one_starting_balance + amount, "Amount wasn't sent to the receiver one");
73109
assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver two");
110+
assert.equal(account_three_ending_balance, account_three_starting_balance + amount, "Amount wasn't correctly sent to the receiver three");
111+
// fetch final eth balance
112+
account_one_eth_ending_balance = web3.eth.getBalance(account_one).toNumber();
113+
account_two_eth_ending_balance = web3.eth.getBalance(account_two).toNumber();
114+
account_three_eth_ending_balance = web3.eth.getBalance(account_three).toNumber();
115+
116+
// check if eth is updated for 2nd and remains the same for first
117+
assert.equal(account_one_eth_ending_balance, account_one_eth_starting_balance, "Eth balance is not the same");
118+
assert.equal(account_two_eth_ending_balance, account_two_eth_starting_balance + eth_amount, "Eth balance was not update correctly");
119+
assert.equal(account_three_eth_ending_balance, account_three_eth_starting_balance, "Eth balance is not the same when eth amount passed was more then balance");
74120
});
75121
});
76122
});

0 commit comments

Comments
 (0)