trying to use this as an example to learn how contracts work.
I believe i've identified the problem in the contract, but i would love to hear from someone who actually knows.
I think the problem is this line:
while (balance > investors[k].amount * 3 / 100 && k < total_inv) //exit condition to avoid infinite loop
total_inv is always going to be 0.
so this basically never runs. the only investor that is paid is at id 0, the first investor. ie: the owner.
Can someone else confirm?
Thats nonsense, you cant read the code, the total_inv is set to the lenght of the investors array, which is scaling after each contract execution by +1.
Read it again:
contract PiggyBank {
struct InvestorArray {
address etherAddress;
uint amount;
}
InvestorArray[] public investors;
uint public k = 0;
uint public fees;
uint public balance = 0;
address public owner;
// simple single-sig function modifier
modifier onlyowner { if (msg.sender == owner) _ }
// this function is executed at initialization and sets the owner of the contract
function PiggyBank() {
owner = msg.sender;
}
// fallback function - simple transactions trigger this
function() {
enter();
}
function enter() {
if (msg.value < 50 finney) {
msg.sender.send(msg.value);
return;
}
uint amount=msg.value;
// add a new participant to array
uint total_inv = investors.length;
investors.length += 1;
investors[total_inv].etherAddress = msg.sender;
investors[total_inv].amount = amount;
// collect fees and update contract balance
fees += amount / 33; // 3% Fee
balance += amount; // balance update
if (fees != 0)
{
if(balance>fees)
{
owner.send(fees);
balance -= fees; //balance update
}
}
// 4% interest distributed to the investors
uint transactionAmount;
while (balance > investors[k].amount * 3/100 && k) //exit condition to avoid infinite loop
{
if(k%25==0 && balance > investors[k].amount * 9/100)
{
transactionAmount = investors[k].amount * 9/100;
investors[k].etherAddress.send(transactionAmount);
balance -= investors[k].amount * 9/100; //balance update
}
else
{
transactionAmount = investors[k].amount *3/100;
investors[k].etherAddress.send(transactionAmount);
balance -= investors[k].amount *3/100; //balance update
}
k += 1;
}
//----------------end enter
}
function setOwner(address new_owner) onlyowner {
owner = new_owner;
}
}