Last active
February 7, 2018 23:23
-
-
Save safead/5fb624547ed983770c5981a0662701c1 to your computer and use it in GitHub Desktop.
SAFE Pre-Sale truffle tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var SAFEToken = artifacts.require('./SAFEToken.sol'); | |
var SAFEPreSale = artifacts.require('./SAFEPreSale.sol'); | |
var expectThrow = async (promise) => { | |
const errMsg = 'Expected throw not received'; | |
try{ | |
await promise; | |
}catch (err){ | |
assert(err.toString().includes('Exception while processing transaction: revert'), errMsg); | |
return; | |
} | |
assert.fail(errMsg); | |
} | |
function big(val){ | |
return new web3.BigNumber(val); | |
} | |
function gasLog(val){ | |
advancedLog && console.log("\t[ gas cost: " + val + ' ]'); | |
} | |
function discount(val, percentage){ | |
return val.mul(1000).div(big(1000 - percentage)); | |
} | |
function findInLogs(logs, eventName, address){ | |
for(var i = 0; i < logs.length; i++){ | |
if(logs[i]['event'] !== eventName) continue; | |
if(address && logs[i].args['_to'] !== address) continue; | |
return logs[i].args; | |
} | |
return null; | |
} | |
var Contract, PreSale, i, res, transLog, balance, | |
advancedLog = false, | |
tokensForOneEther = big(713), | |
bountyWalletPercentage = big(19), | |
tokenWalletPercentage = big(499), | |
referralPercentage = big(23), | |
referredDiscount = big(31), | |
currentDiscount = big(0), | |
totalBalance = { | |
totalSupply: big(0), | |
wallet: big(0), | |
buyers: big(0), | |
tokenWallet: big(0), | |
bountyWallet: big(0), | |
referrerWallet: big(0) | |
}, | |
toFreeze = big(12.5); | |
contract('SAFEPreSale', function(accounts){ | |
console.log("contract connected, accounts:\n\n", accounts); | |
beforeEach(async function(){ | |
Contract = await SAFEToken.deployed(); | |
PreSale = await SAFEPreSale.deployed(); | |
}); | |
var buyersBalances = {}, | |
totalbuyers = 0, | |
deals = [ | |
{ | |
acc: accounts[1], | |
value: big(0.003) | |
}, | |
{ | |
acc: accounts[2], | |
value: big(0.3) | |
}, | |
{ | |
acc: accounts[3], | |
value: big(3) | |
}, | |
{ | |
acc: accounts[3], | |
value: big(5) | |
}, | |
{ | |
acc: accounts[4], | |
value: big(0.1211) | |
}, | |
{ | |
acc: accounts[5], | |
value: big(5.0000005) | |
}, | |
{ | |
acc: accounts[5], | |
value: big(1.00000033333333) | |
}, | |
{ | |
acc: accounts[5], | |
value: big(11.11122333444) | |
}, | |
], | |
earlyBuyerDiscount = big(500), | |
earlyBuyerMinValue = big(10), | |
earlyBuyerDealValue = big(10), | |
earlyBuyer = accounts[6], | |
wallet = accounts[7], | |
tokenWallet = accounts[8], | |
bountyWallet = accounts[9]; | |
deals.forEach(function(x){ | |
if(!buyersBalances[x.acc]){ | |
buyersBalances[x.acc] = big(0); | |
totalbuyers++; | |
} | |
}); | |
console.log(" \n--- TEST PARAMETERS: --- \n\n" + | |
"-----------------------------------------\nwallet: " + | |
wallet + "\ntokenWallet: " + tokenWallet + ' [' + (tokenWalletPercentage.toNumber() / 10) + "%]\n" + | |
'bountyWallet: ' + bountyWallet + ' [' + (bountyWalletPercentage.toNumber() / 10) + "%]\n" + | |
'Total buyers: ' + totalbuyers + "\n" + | |
'Total investments: ' + deals.length + "\n" + | |
'Amount to freeze in the final: ' + toFreeze + | |
"\n-----------------------------------\n"); | |
it('ether: wallet balance', async function(){ | |
var balance = await web3.eth.getBalance(wallet); | |
totalBalance.wallet = totalBalance.wallet.plus(web3.fromWei(balance, 'ether')); | |
advancedLog && console.log("\t initial wallet for ether balance", web3.fromWei(balance, 'ether').toNumber()); | |
}); | |
it('mint: add PreSale contract as minting agent', async function(){ | |
res = await Contract.setMintAgent(PreSale.address, true); | |
transLog = res.logs.find(element => element.event.match('MintingAgentChanged')); | |
assert.strictEqual(transLog.args._addr, PreSale.address); | |
assert.strictEqual(transLog.args._state, true); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('bounty: set bountyWallet ' + bountyWallet, async function(){ | |
res = await PreSale.setBountyWallet(bountyWallet, bountyWalletPercentage.toNumber()); | |
transLog = res.logs.find(element => element.event.match('BountyWalletSet')); | |
assert.strictEqual(transLog.args._addr, bountyWallet); | |
assert.strictEqual(transLog.args._perc.toNumber(), bountyWalletPercentage.toNumber()); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('bounty: set bountyWallet with zero address (should revert)', async function(){ expectThrow( | |
PreSale.setBountyWallet(0x0, 100) | |
)}); | |
it('bounty: set bountyWallet with wrong percentage (should revert)', async function(){ expectThrow( | |
PreSale.setBountyWallet(bountyWallet, 1001) | |
)}); | |
it('bounty: set bountyWallet from wrong address (should revert)', async function(){ expectThrow( | |
PreSale.setBountyWallet(bountyWallet, bountyWalletPercentage.toNumber(), {from: accounts[5]}) | |
)}); | |
it('bounty: check bountyWallet from wrong address (should revert)', async function(){ expectThrow( | |
PreSale.getBountyWallet.call({from: accounts[5]}) | |
)}); | |
it('bounty: check bountyWallet [' + bountyWallet + '] from owner address', async function(){ | |
res = await PreSale.getBountyWallet.call(); | |
assert.strictEqual(res[0], bountyWallet); | |
assert.strictEqual(bountyWalletPercentage.toNumber(), res[1].toNumber()); | |
}); | |
it('wallet: set ' + wallet + ' & check WalletSet event', async function(){ | |
res = await PreSale.setWallet(wallet); | |
transLog = res.logs.find(element => element.event.match('WalletSet')); | |
assert.strictEqual(transLog.args._addr, wallet); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('wallet: set with zero address (should revert)', async function(){ expectThrow( | |
PreSale.setWallet(0x0) | |
)}); | |
it('wallet: set from wrong address (should revert)', async function(){ expectThrow( | |
PreSale.setWallet(wallet, {from: accounts[5]}) | |
)}); | |
it('wallet: check from wrong address (should revert)', async function(){ expectThrow( | |
PreSale.getWallet.call({from: accounts[5]}) | |
)}); | |
it('wallet: check from owner address', async function(){ | |
res = await PreSale.getWallet.call(); | |
assert.strictEqual(res, wallet); | |
}); | |
it('token wallet: set & check TokenWalletSet event', async function(){ | |
res = await PreSale.setTokenWallet(tokenWallet, tokenWalletPercentage.toNumber()); | |
transLog = res.logs.find(element => element.event.match('TokenWalletSet')); | |
assert.strictEqual(transLog.args._addr, tokenWallet); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('token wallet: set with zero address (should revert)', async function(){ expectThrow( | |
PreSale.setTokenWallet(0x0, 100) | |
)}); | |
it('token wallet: set from wrong address (should revert)', async function(){ expectThrow( | |
PreSale.setTokenWallet(tokenWallet, 100, {from: accounts[5]}) | |
)}); | |
it('token wallet: set with wrong percentage (should revert)', async function(){ expectThrow( | |
PreSale.setTokenWallet(tokenWallet, 1001) | |
)}); | |
it('token wallet: check from wrong address (should revert)', async function(){ expectThrow( | |
PreSale.getTokenWallet.call({from: accounts[5]}) | |
)}); | |
it('token wallet: check [' + tokenWallet + '] from owner address', async function(){ | |
res = await PreSale.getTokenWallet.call(); | |
assert.strictEqual(res[0], tokenWallet); | |
assert.strictEqual(tokenWalletPercentage.toNumber(), res[1].toNumber()); | |
}); | |
it('tokensForOneEther: set from wrong address (should revert)', async function(){ expectThrow( | |
PreSale.changeTokensForOneEther(tokensForOneEther, {from: accounts[5]}) | |
)}); | |
it('tokensForOneEther: set', async function(){ | |
res = await PreSale.changeTokensForOneEther(tokensForOneEther); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('tokensForOneEther: check from any account', async function(){ | |
res = await PreSale.tokensForOneEther.call(); | |
assert.strictEqual(res.toNumber(), tokensForOneEther.toNumber()); | |
}); | |
it('discount: set from wrong address (should revert)', async function(){ expectThrow( | |
PreSale.changeDiscount(50, {from: accounts[5]}) | |
)}); | |
it('discount: set 25%', async function(){ | |
currentDiscount = big(250); | |
res = await PreSale.changeDiscount(currentDiscount); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('discount: check from any account', async function(){ | |
res = await PreSale.discount.call(); | |
assert.strictEqual(res.toNumber(), currentDiscount.toNumber()); | |
}); | |
it('referral: change referral & referred %', async function(){ | |
res = await PreSale.changeReferralRules(referralPercentage, referredDiscount); | |
gasLog(res.receipt.gasUsed); | |
res = await PreSale.referralPercentage.call(); | |
assert.strictEqual(res.toNumber(), referralPercentage.toNumber()); | |
res = await PreSale.referredDiscount.call(); | |
assert.strictEqual(res.toNumber(), referredDiscount.toNumber()); | |
}); | |
it('early buyer: add new with too big percentage (should revert)', async function(){ expectThrow( | |
PreSale.addEarlyBuyer(earlyBuyer, big(1001), big(0)) | |
)}); | |
it('early buyer: add new with zero percentage (should revert)', async function(){ expectThrow( | |
PreSale.addEarlyBuyer(earlyBuyer, big(0), big(0)) | |
)}); | |
it('early buyer: add new', async function(){ | |
res = await PreSale.addEarlyBuyer(earlyBuyer, earlyBuyerDiscount, earlyBuyerMinValue); | |
transLog = res.logs.find(element => element.event.match('NewEarlyBuyer')); | |
assert.strictEqual(transLog.args._addr, earlyBuyer); | |
assert.strictEqual(transLog.args._discount.toNumber(), earlyBuyerDiscount.toNumber()); | |
assert.strictEqual(transLog.args._minValue.toNumber(), earlyBuyerMinValue.toNumber()); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('early buyer: get info from any address (should revert)', async function(){ expectThrow( | |
PreSale.getEarlyBuyer(earlyBuyer, {from: accounts[5]}) | |
)}); | |
it('early buyer: get info', async function(){ | |
res = await PreSale.getEarlyBuyer(earlyBuyer); | |
assert.strictEqual(res[0].toNumber(), earlyBuyerDiscount.toNumber()); | |
assert.strictEqual(res[1].toNumber(), earlyBuyerMinValue.toNumber()); | |
}); | |
it('early buyer: delete', async function(){ | |
res = await PreSale.deleteEarlyBuyer(earlyBuyer); | |
transLog = res.logs.find(element => element.event.match('DeletedEarlyBuyer')); | |
assert.strictEqual(transLog.args._addr, earlyBuyer); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('early buyer: get info (should revert)', async function(){ expectThrow( | |
PreSale.getEarlyBuyer(earlyBuyer) | |
)}); | |
it('early buyer: add again', async function(){ | |
res = await PreSale.addEarlyBuyer(earlyBuyer, earlyBuyerDiscount, earlyBuyerMinValue.toNumber()); | |
transLog = res.logs.find(element => element.event.match('NewEarlyBuyer')); | |
assert.strictEqual(transLog.args._addr, earlyBuyer); | |
assert.strictEqual(transLog.args._discount.toNumber(), earlyBuyerDiscount.toNumber()); | |
assert.strictEqual(transLog.args._minValue.toNumber(), earlyBuyerMinValue.toNumber()); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('early buyer: buy less then minimum', async function(){ | |
var value = earlyBuyerMinValue.div(2); | |
totalBalance.wallet = totalBalance.wallet.plus(value); | |
var percentage = big(1000).minus(tokenWalletPercentage).minus(bountyWalletPercentage); | |
var buyerTokensValue = discount(value.mul(tokensForOneEther), currentDiscount); | |
var tokenWalletValue = buyerTokensValue.mul(tokenWalletPercentage).div(percentage); | |
var bountyWalletValue = buyerTokensValue.mul(bountyWalletPercentage).div(percentage); | |
totalBalance.bountyWallet = totalBalance.bountyWallet.plus(bountyWalletValue); | |
totalBalance.tokenWallet = totalBalance.tokenWallet.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(buyerTokensValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(bountyWalletValue); | |
advancedLog && console.log("\t + early investor [" + earlyBuyer + '] (paid ' + value.toNumber() + ' ether), tokens: ' + buyerTokensValue.toNumber() + ' [' + (currentDiscount / 10) + '% discount]' + | |
"\n\t bountyWallet [" + bountyWallet + '] tokens: ' + bountyWalletValue + | |
"\n\t tokenWallet [" + tokenWallet + '] tokens: ' + tokenWalletValue + | |
"\n\t tokens totalSupply: " + totalBalance.totalSupply.toNumber()); | |
res = await PreSale.sendTransaction({ from: earlyBuyer, value: web3.toWei(value, 'ether') }); | |
transLog = findInLogs(res.logs, 'PurchasedTokens'); | |
assert(transLog); | |
assert.strictEqual(transLog._purchaser, earlyBuyer); | |
assert.strictEqual(transLog._ether.toNumber(), web3.toWei(value, 'ether').toNumber()); | |
assert.strictEqual(transLog._tokens.toNumber(), web3.toWei(buyerTokensValue, 'ether').toNumber()); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('early buyer: buy as promised', async function(){ | |
totalBalance.wallet = totalBalance.wallet.plus(earlyBuyerDealValue); | |
var percentage = big(1000).minus(tokenWalletPercentage).minus(bountyWalletPercentage); | |
var buyerTokensValue = discount(earlyBuyerDealValue.mul(tokensForOneEther), earlyBuyerDiscount); | |
var tokenWalletValue = buyerTokensValue.mul(tokenWalletPercentage).div(percentage); | |
var bountyWalletValue = buyerTokensValue.mul(bountyWalletPercentage).div(percentage); | |
totalBalance.bountyWallet = totalBalance.bountyWallet.plus(bountyWalletValue); | |
totalBalance.tokenWallet = totalBalance.tokenWallet.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(buyerTokensValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(bountyWalletValue); | |
advancedLog && console.log("\t + early investor [" + earlyBuyer + '] (paid ' + earlyBuyerDealValue.toNumber() + ' ether), tokens: ' + buyerTokensValue.toNumber() + ' [' + (earlyBuyerDiscount / 10) + '% discount]' + | |
"\n\t bountyWallet [" + bountyWallet + '] tokens: ' + bountyWalletValue + | |
"\n\t tokenWallet [" + tokenWallet + '] tokens: ' + tokenWalletValue + | |
"\n\t tokens totalSupply: " + totalBalance.totalSupply.toNumber()); | |
res = await PreSale.sendTransaction({ from: earlyBuyer, value: web3.toWei(earlyBuyerDealValue, 'ether') }); | |
transLog = findInLogs(res.logs, 'PurchasedTokens'); | |
assert(transLog); | |
assert.strictEqual(transLog._purchaser, earlyBuyer); | |
assert.strictEqual(transLog._ether.toNumber(), web3.toWei(earlyBuyerDealValue, 'ether').toNumber()); | |
assert.strictEqual(transLog._tokens.toNumber(), web3.toWei(buyerTokensValue, 'ether').toNumber()); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('early buyer: buy again (no discount anymore)', async function(){ | |
totalBalance.wallet = totalBalance.wallet.plus(earlyBuyerDealValue); | |
var percentage = big(1000).minus(tokenWalletPercentage).minus(bountyWalletPercentage); | |
var buyerTokensValue = discount(earlyBuyerDealValue.mul(tokensForOneEther), currentDiscount); | |
var tokenWalletValue = buyerTokensValue.mul(tokenWalletPercentage).div(percentage); | |
var bountyWalletValue = buyerTokensValue.mul(bountyWalletPercentage).div(percentage); | |
totalBalance.bountyWallet = totalBalance.bountyWallet.plus(bountyWalletValue); | |
totalBalance.tokenWallet = totalBalance.tokenWallet.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(buyerTokensValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(bountyWalletValue); | |
advancedLog && console.log("\t + early investor [" + earlyBuyer + '] (paid ' + earlyBuyerDealValue.toNumber() + ' ether), tokens: ' + buyerTokensValue.toNumber() + ' [' + (currentDiscount / 10) + '% discount]' + | |
"\n\t bountyWallet [" + bountyWallet + '] tokens: ' + bountyWalletValue + | |
"\n\t tokenWallet [" + tokenWallet + '] tokens: ' + tokenWalletValue + | |
"\n\t tokens totalSupply: " + totalBalance.totalSupply.toNumber()); | |
res = await PreSale.sendTransaction({ from: earlyBuyer, value: web3.toWei(earlyBuyerDealValue, 'ether') }); | |
transLog = findInLogs(res.logs, 'PurchasedTokens'); | |
assert(transLog); | |
assert.strictEqual(transLog._purchaser, earlyBuyer); | |
assert.strictEqual(transLog._ether.toNumber(), web3.toWei(earlyBuyerDealValue, 'ether').toNumber()); | |
assert.strictEqual(transLog._tokens.toNumber(), web3.toWei(buyerTokensValue, 'ether').toNumber()); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('deals: perform all ' + deals.length + ' deals', async function(){ | |
//DISCOUNT 25% | |
for(var i = 0; i < deals.length; i++){ | |
var percentage = big(1000).minus(tokenWalletPercentage).minus(bountyWalletPercentage); | |
var buyerTokensValue = discount(deals[i].value.mul(tokensForOneEther), currentDiscount); | |
var tokenWalletValue = buyerTokensValue.mul(tokenWalletPercentage).div(percentage); | |
var bountyWalletValue = buyerTokensValue.mul(bountyWalletPercentage).div(percentage); | |
totalBalance.bountyWallet = totalBalance.bountyWallet.plus(bountyWalletValue); | |
totalBalance.tokenWallet = totalBalance.tokenWallet.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(buyerTokensValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(bountyWalletValue); | |
buyersBalances[deals[i].acc] = buyersBalances[deals[i].acc].plus(buyerTokensValue); | |
totalBalance.wallet = totalBalance.wallet.plus(deals[i].value); | |
advancedLog && console.log("\t + investor [" + deals[i].acc + '] (paid ' + deals[i].value.toNumber() + ' ether), tokens: ' + buyerTokensValue.toNumber() + ' [' + (currentDiscount / 10) + '% discount]' + | |
"\n\t bountyWallet [" + bountyWallet + '] tokens: ' + bountyWalletValue + | |
"\n\t tokenWallet [" + tokenWallet + '] tokens: ' + tokenWalletValue + | |
"\n\t tokens totalSupply: " + totalBalance.totalSupply.toNumber()); | |
res = await PreSale.sendTransaction({ from: deals[i].acc, value: web3.toWei(deals[i].value, 'ether') }); | |
transLog = findInLogs(res.logs, 'PurchasedTokens'); | |
assert(transLog); | |
assert.strictEqual(transLog._purchaser, deals[i].acc); | |
assert.strictEqual(transLog._ether.toNumber(), web3.toWei(deals[i].value, 'ether').toNumber()); | |
assert.strictEqual(transLog._tokens.toNumber(), web3.toWei(buyerTokensValue, 'ether').toNumber()); | |
gasLog(res.receipt.gasUsed); | |
} | |
}); | |
it('discount: set 15%', async function(){ | |
currentDiscount = big(150); | |
res = await PreSale.changeDiscount(currentDiscount); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('deals: perform all' + deals.length + ' deals (direct ether transfer)', async function(){ | |
//DISCOUNT 15% | |
for(var i = 0; i < deals.length; i++){ | |
var percentage = big(1000).minus(tokenWalletPercentage).minus(bountyWalletPercentage); | |
var buyerTokensValue = discount(deals[i].value.mul(tokensForOneEther), currentDiscount); | |
var tokenWalletValue = buyerTokensValue.mul(tokenWalletPercentage).div(percentage); | |
var bountyWalletValue = buyerTokensValue.mul(bountyWalletPercentage).div(percentage); | |
totalBalance.bountyWallet = totalBalance.bountyWallet.plus(bountyWalletValue); | |
totalBalance.tokenWallet = totalBalance.tokenWallet.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(buyerTokensValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(bountyWalletValue); | |
buyersBalances[deals[i].acc] = buyersBalances[deals[i].acc].plus(buyerTokensValue); | |
totalBalance.wallet = totalBalance.wallet.plus(deals[i].value); | |
advancedLog && console.log("\t + investor [" + deals[i].acc + '] (paid ' + deals[i].value.toNumber() + ' ether), tokens: ' + buyerTokensValue.toNumber() + ' [' + (currentDiscount / 10) + '% discount]' + | |
"\n\t bountyWallet [" + bountyWallet + '] tokens: ' + bountyWalletValue + | |
"\n\t tokenWallet [" + tokenWallet + '] tokens: ' + tokenWalletValue + | |
"\n\t tokens totalSupply: " + totalBalance.totalSupply.toNumber()); | |
// res = await PreSale.purchaseTokens({ from: deals[i].acc, value: web3.toWei(deals[i].value, 'ether') }); | |
res = await PreSale.sendTransaction({ from: deals[i].acc, value: web3.toWei(deals[i].value, 'ether') }); | |
transLog = findInLogs(res.logs, 'PurchasedTokens'); | |
assert(transLog); | |
assert.strictEqual(transLog._purchaser, deals[i].acc); | |
assert.strictEqual(transLog._ether.toNumber(), web3.toWei(deals[i].value, 'ether').toNumber()); | |
assert.strictEqual(transLog._tokens.toNumber(), web3.toWei(buyerTokensValue, 'ether').toNumber()); | |
gasLog(res.receipt.gasUsed); | |
} | |
}); | |
it('discount: set 10%', async function(){ | |
currentDiscount = big(100); | |
res = await PreSale.changeDiscount(currentDiscount); | |
gasLog(res.receipt.gasUsed); | |
}); | |
it('deals: perform all ' + deals.length + ' deals', async function(){ | |
//DISCOUNT 10% | |
for(var i = 0; i < deals.length; i++){ | |
var percentage = big(1000).minus(tokenWalletPercentage).minus(bountyWalletPercentage); | |
var buyerTokensValue = discount(deals[i].value.mul(tokensForOneEther), currentDiscount); | |
var tokenWalletValue = buyerTokensValue.mul(tokenWalletPercentage).div(percentage); | |
var bountyWalletValue = buyerTokensValue.mul(bountyWalletPercentage).div(percentage); | |
totalBalance.bountyWallet = totalBalance.bountyWallet.plus(bountyWalletValue); | |
totalBalance.tokenWallet = totalBalance.tokenWallet.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(buyerTokensValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(bountyWalletValue); | |
buyersBalances[deals[i].acc] = buyersBalances[deals[i].acc].plus(buyerTokensValue); | |
totalBalance.wallet = totalBalance.wallet.plus(deals[i].value); | |
advancedLog && console.log("\t + investor [" + deals[i].acc + '] (paid ' + deals[i].value.toNumber() + ' ether), tokens: ' + buyerTokensValue.toNumber() + ' [' + (currentDiscount / 10) + '% discount]' + | |
"\n\t bountyWallet [" + bountyWallet + '] tokens: ' + bountyWalletValue + | |
"\n\t tokenWallet [" + tokenWallet + '] tokens: ' + tokenWalletValue + | |
"\n\t tokens totalSupply: " + totalBalance.totalSupply.toNumber()); | |
res = await PreSale.sendTransaction({ from: deals[i].acc, value: web3.toWei(deals[i].value, 'ether') }); | |
transLog = findInLogs(res.logs, 'PurchasedTokens'); | |
assert(transLog); | |
assert.strictEqual(transLog._purchaser, deals[i].acc); | |
assert.strictEqual(transLog._ether.toNumber(), web3.toWei(deals[i].value, 'ether').toNumber()); | |
assert.strictEqual(transLog._tokens.toNumber(), web3.toWei(buyerTokensValue, 'ether').toNumber()); | |
gasLog(res.receipt.gasUsed); | |
} | |
}); | |
it('ether: referrer wallet balance', async function(){ | |
var balance = await web3.eth.getBalance(earlyBuyer); | |
totalBalance.referrerWallet = web3.fromWei(balance, 'ether'); | |
advancedLog && console.log("\t current referrer wallet balance", web3.fromWei(balance, 'ether').toNumber()); | |
}); | |
it('deals: deals with referral', async function(){ | |
for(var i = 0; i < deals.length; i++){ | |
var percentage = big(1000).minus(tokenWalletPercentage).minus(bountyWalletPercentage); | |
var buyerTokensValue = discount(deals[i].value.mul(tokensForOneEther), currentDiscount.plus(referredDiscount)); | |
var tokenWalletValue = buyerTokensValue.mul(tokenWalletPercentage).div(percentage); | |
var bountyWalletValue = buyerTokensValue.mul(bountyWalletPercentage).div(percentage); | |
totalBalance.bountyWallet = totalBalance.bountyWallet.plus(bountyWalletValue); | |
totalBalance.tokenWallet = totalBalance.tokenWallet.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(buyerTokensValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(tokenWalletValue); | |
totalBalance.totalSupply = totalBalance.totalSupply.plus(bountyWalletValue); | |
buyersBalances[deals[i].acc] = buyersBalances[deals[i].acc].plus(buyerTokensValue); | |
totalBalance.referrerWallet = totalBalance.referrerWallet.plus(deals[i].value.mul(referralPercentage).div(1000)); | |
totalBalance.wallet = totalBalance.wallet.plus(deals[i].value.minus(deals[i].value.mul(referralPercentage).div(1000))); | |
advancedLog && console.log("\t + investor [" + deals[i].acc + '] (paid ' + deals[i].value.toNumber() + ' ether), tokens: ' + buyerTokensValue.toNumber() + ' [' + (currentDiscount.plus(referredDiscount).toNumber() / 10) + '% discount]' + | |
"\n\t bountyWallet [" + bountyWallet + '] tokens: ' + bountyWalletValue + | |
"\n\t tokenWallet [" + tokenWallet + '] tokens: ' + tokenWalletValue + | |
"\n\t tokens totalSupply: " + totalBalance.totalSupply.toNumber()); | |
res = await PreSale.purchaseTokens(earlyBuyer, { from: deals[i].acc, value: web3.toWei(deals[i].value, 'ether') }); | |
transLog = findInLogs(res.logs, 'PurchasedTokens'); | |
assert(transLog); | |
assert.strictEqual(transLog._purchaser, deals[i].acc); | |
assert.strictEqual(transLog._ether.toNumber(), web3.toWei(deals[i].value, 'ether').toNumber()); | |
assert.strictEqual(transLog._tokens.toNumber(), web3.toWei(buyerTokensValue, 'ether').toNumber()); | |
gasLog(res.receipt.gasUsed); | |
} | |
}); | |
it('after deals: totalSupply tokens', async function(){ | |
balance = await Contract.totalSupply.call(); | |
assert.strictEqual(balance.toNumber(), web3.toWei(totalBalance.totalSupply, 'ether').toNumber()); | |
advancedLog && console.log("\ttotalSupply " + totalBalance.totalSupply.toNumber() + ' tokens'); | |
}); | |
it('after deals: bounty account tokens balance', async function(){ | |
var balance = await Contract.balanceOf.call(bountyWallet); | |
assert.strictEqual(balance.toNumber(), web3.toWei(totalBalance.bountyWallet, 'ether').toNumber()); | |
advancedLog && console.log("\tbounty balance " + totalBalance.bountyWallet.toNumber() + ' tokens'); | |
}); | |
it('after deals: check buyers token balances from any account', async function(){ | |
for(i in buyersBalances){ | |
balance = await Contract.balanceOf.call(i, {from: 0x0}); | |
assert.strictEqual(balance.toNumber(), web3.toWei(buyersBalances[i], 'ether').toNumber()); | |
} | |
}); | |
it('after deals: wallet ether balance', async function(){ | |
var balance = await web3.eth.getBalance(wallet); | |
assert.strictEqual(balance.toNumber(), web3.toWei(totalBalance.wallet, 'ether').toNumber()); | |
advancedLog && console.log("\twallet balance " + totalBalance.wallet.toNumber() + ' ether'); | |
}); | |
it('after deals: referral wallet ether balance', async function(){ | |
var balance = await web3.eth.getBalance(earlyBuyer); | |
assert.strictEqual(balance.toNumber(), web3.toWei(totalBalance.referrerWallet, 'ether').toNumber()); | |
advancedLog && console.log("\treferral ether balance " + totalBalance.referrerWallet.toNumber() + ' ether'); | |
}); | |
}); |
Author
safead
commented
Jan 30, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment