Skip to content

Instantly share code, notes, and snippets.

@maxiar
Last active July 5, 2020 18:26
Show Gist options
  • Select an option

  • Save maxiar/24686c63762eaf472fd8c88e6a95a65c to your computer and use it in GitHub Desktop.

Select an option

Save maxiar/24686c63762eaf472fd8c88e6a95a65c to your computer and use it in GitHub Desktop.
Sample Smart Contract & Remix Unit Test - Gitter Question: In the unit test, how to simulate multiples players register (regUserfunction) and buy multiples pools (buyPool1, buyPoll2 functions)! . And check the owner wallet balance and players balances
pragma solidity 0.5.11 - 0.6.4;
contract MySampleContract {
address public ownerWallet;
uint public currUserID = 0;
uint public pool1currUserID = 0;
uint public pool2currUserID = 0;
uint public pool1activeUserID = 0;
uint public pool2activeUserID = 0;
struct UserStruct {
bool isExist;
uint id;
uint referrerID;
uint referredUsers;
mapping(uint => uint) levelExpired;
}
struct PoolUserStruct {
bool isExist;
uint id;
uint payment_received;
uint cycle;
}
mapping (address => UserStruct) public users;
mapping (uint => address) public userList;
mapping (address => PoolUserStruct) public pool1users;
mapping (uint => address) public pool1userList;
mapping (address => PoolUserStruct) public pool2users;
mapping (uint => address) public pool2userList;
mapping(uint => uint) public LEVEL_PRICE;
uint REGESTRATION_FESS=0.1 ether;
uint pool1_price=0.1 ether;
uint pool2_price=0.2 ether ;
event regLevelEvent(address indexed _user, address indexed _referrer, uint _time);
event getMoneyForLevelEvent(address indexed _user, address indexed _referral, uint indexed _level, uint _time);
event regPoolEntry(address indexed _user, uint indexed _level, uint _time);
event getPoolPayment(address indexed _user,address indexed _receiver, uint indexed _level, uint _time);
UserStruct[] public requests;
constructor(address _player) public {
ownerWallet = msg.sender;
UserStruct memory userStruct;
currUserID++;
userStruct = UserStruct({
isExist: true,
id: currUserID,
referrerID: 0,
referredUsers:0
});
users[ownerWallet] = userStruct;
userList[currUserID] = ownerWallet;
currUserID++;
userStruct = UserStruct({
isExist: true,
id: currUserID,
referrerID: 1,
referredUsers:0
});
users[_player] = userStruct;
userList[currUserID] = _player;
PoolUserStruct memory pooluserStruct;
pool1currUserID++;
pooluserStruct = PoolUserStruct({
isExist:true,
id:pool1currUserID,
payment_received:0,
cycle: 1
});
pool1activeUserID=pool1currUserID;
pool1users[msg.sender] = pooluserStruct;
pool1userList[pool1currUserID]=msg.sender;
pool1currUserID++;
pooluserStruct = PoolUserStruct({
isExist:true,
id:pool1currUserID,
payment_received:0,
cycle: 1
});
pool1users[_player] = pooluserStruct;
pool1userList[pool1currUserID]=_player;
pool2currUserID++;
pooluserStruct = PoolUserStruct({
isExist:true,
id:pool2currUserID,
payment_received:0,
cycle: 1
});
pool2activeUserID=pool2currUserID;
pool2users[msg.sender] = pooluserStruct;
pool2userList[pool2currUserID]=msg.sender;
pool2currUserID++;
pooluserStruct = PoolUserStruct({
isExist:true,
id:pool2currUserID,
payment_received:0,
cycle: 1
});
pool2users[_player] = pooluserStruct;
pool2userList[pool2currUserID]=_player;
}
function regUser(uint _referrerID) public payable {
require(!users[msg.sender].isExist, "User Exists");
require(_referrerID > 0 && _referrerID <= currUserID, 'Incorrect referral ID');
require(msg.value == REGESTRATION_FESS, 'Incorrect Value');
UserStruct memory userStruct;
currUserID++;
userStruct = UserStruct({
isExist: true,
id: currUserID,
referrerID: _referrerID,
referredUsers:0
});
users[msg.sender] = userStruct;
userList[currUserID]=msg.sender;
users[userList[users[msg.sender].referrerID]].referredUsers=users[userList[users[msg.sender].referrerID]].referredUsers+1;
payReferral(1,msg.sender);
emit regLevelEvent(msg.sender, userList[_referrerID], now);
}
function payReferral(uint _level, address _user) internal {
address referer;
referer = userList[users[_user].referrerID];
bool sent = false;
uint level_price_local=0;
level_price_local=REGESTRATION_FESS/10;
sent = address(uint160(referer)).send(level_price_local);
if (sent) {
emit getMoneyForLevelEvent(referer, msg.sender, _level, now);
if(_level < 10 && users[referer].referrerID >= 1){
payReferral(_level+1,referer);
}
else {
sendBalance();
}
}
if(!sent) {
payReferral(_level, referer);
}
}
function buyPool1() public payable {
require(users[msg.sender].isExist, "User Not Registered");
require(!pool1users[msg.sender].isExist, "Already in AutoPool");
require(msg.value == pool1_price, 'Incorrect Value');
PoolUserStruct memory userStruct;
address pool1Currentuser=pool1userList[pool1activeUserID];
if(pool1users[pool1Currentuser].payment_received >= 2) {
reinvestPool1(pool1Currentuser);
pool1activeUserID+=1;
pool1Currentuser=pool1userList[pool1activeUserID];
}
pool1currUserID++;
userStruct = PoolUserStruct({
isExist:true,
id:pool1currUserID,
payment_received:0,
cycle: 1
});
pool1users[msg.sender] = userStruct;
pool1userList[pool1currUserID]=msg.sender;
bool sent = false;
sent = address(uint160(pool1Currentuser)).send(pool1_price);
if (sent) {
pool1users[pool1Currentuser].payment_received+=1;
emit getPoolPayment(msg.sender,pool1Currentuser, 1, now);
}
emit regPoolEntry(msg.sender, 1, now);
}
function reinvestPool1(address _pool1CurrentUser) private {
PoolUserStruct memory userStruct;
pool1currUserID++;
userStruct = PoolUserStruct({
isExist:true,
id:pool1currUserID,
payment_received:0,
cycle: pool1users[_pool1CurrentUser].cycle+1
});
pool1users[_pool1CurrentUser] = userStruct;
pool1userList[pool1currUserID]=_pool1CurrentUser;
emit regPoolEntry(_pool1CurrentUser, 1, now);
}
function buyPool2() public payable {
require(users[msg.sender].isExist, "User Not Registered");
require(msg.value == pool2_price, 'Incorrect Value');
require(!pool2users[msg.sender].isExist, "Already in AutoPool");
PoolUserStruct memory userStruct;
address pool2Currentuser=pool2userList[pool2activeUserID];
if(pool2users[pool2Currentuser].payment_received >= 2) {
reinvestPool2(pool2Currentuser);
pool2activeUserID+=1;
pool2Currentuser=pool2userList[pool2activeUserID];
}
pool2currUserID++;
userStruct = PoolUserStruct({
isExist:true,
id:pool2currUserID,
payment_received:0,
cycle: 1
});
pool2users[msg.sender] = userStruct;
pool2userList[pool2currUserID]=msg.sender;
bool sent = false;
sent = address(uint160(pool2Currentuser)).send(pool2_price);
if (sent) {
pool2users[pool2Currentuser].payment_received+=1;
emit getPoolPayment(msg.sender,pool2Currentuser, 2, now);
}
emit regPoolEntry(msg.sender,2, now);
}
function reinvestPool2(address _pool2CurrentUser) private {
PoolUserStruct memory userStruct;
pool2currUserID++;
userStruct = PoolUserStruct({
isExist:true,
id:pool2currUserID,
payment_received:0,
cycle: pool2users[_pool2CurrentUser].cycle+1
});
pool2users[_pool2CurrentUser] = userStruct;
pool2userList[pool2currUserID]=_pool2CurrentUser;
emit regPoolEntry(_pool2CurrentUser, 2, now);
}
function getEthBalance() public view returns(uint) {
return address(this).balance;
}
function sendBalance() private
{
if(getEthBalance() > 0){
if (!address(uint160(ownerWallet)).send(getEthBalance()))
{
}
}
}
}
pragma solidity >=0.4.22 <0.7.0;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "remix_accounts.sol"; // dummy accounts list
import "./MySampleContract.sol";
// File name has to end with '_test.sol', this file can contain more than one testSuite contracts
contract MySampleContract_test is MySampleContract(TestsAccounts.getAccount(0)) {
//MySampleContract sc;
/// Define variables referring to different accounts
address acc0;
address acc1;
address acc2;
constructor() public {}
/// 'beforeAll' runs before all other tests
/// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll'
function beforeAll() public {
acc0 = TestsAccounts.getAccount(0);
acc1 = TestsAccounts.getAccount(1);
acc2 = TestsAccounts.getAccount(2);
// Create an instance of contract to be tested
//sc = new MySampleContract{gas:5000000}(own);
//ownerWallet = TestsAccounts.getAccount(0);
}
/// Test if initial owner is set correctly
function testOwner() public {
// account at zero index (account-0) is default account, so current owner should be acc0
//Assert.equal(sb2.ownerWallet(), own, 'owner should be own');
//Assert.equal(ownerWallet(), own, 'owner should be own');
//{gas: 9000000, value: 100}
// This will pass
//try sb2.ownerWallet() returns (address r) {
Assert.equal(ownerWallet, acc0, 'owner should be acc0');
}
function getBalance() public {
Assert.equal(getEthBalance(), 0, 'owner should be 0');
}
/// #sender: account-0
/// #value: 10
function checkSenderIs0AndValueis10 () public payable {
Assert.equal(msg.sender, TestsAccounts.getAccount(0), "wrong sender in checkSenderIs0AndValueis10");
Assert.equal(msg.value, 10, "wrong value in checkSenderIs0AndValueis10");
}
/// #sender: account-0
/// #value: 0.1
function registerPlayer1() payable public {
//regUser{gas: 10000000, value: 0.1}(1);
regUser(1);
// As per the calculation, check the total balance
Assert.equal(getEthBalance(), 20, 'token balance should be 20');
}
/// #sender: account-0
/// #value: 0.2
function Player1BuyPool1() payable public {
buyPool1();
Assert.equal(getEthBalance(), 20, 'token balance should be 20');
}
/// #sender: account-0
/// #value: 0.5
function Player1BuyPool2() payable public {
buyPool2();
Assert.equal(getEthBalance(), 20, 'token balance should be 20');
}
function getBalance2() public {
Assert.equal(getEthBalance(), 0, 'owner should be 0');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment