0

I am using a factory contract for selling and buying goods. how can retrieve how many buyers bought and calculate how much ether earned by selling goods? here is my source code. I am new on smart contracts

contract FactroyContract {
  address[] public contracts;
  address public lastContractAddresses;

  event newPurchaseContract (address contractAddress);

  function getContractcount() public view returns(uint ContracCount) 
  {
    return contracts.length;
  }

  function newPurchase(uint value, string memory bookName, string memory description, string memory imageHash, string memory privHash, string memory dataHash) 
    public 
    payable
    returns(address NewContract)
  {
    Purchase c = new Purchase(address(msg.sender), value, bookName, description, imageHash, privHash, dataHash);
    contracts.push(c);
    lastContractAddresses = address(c);
    emit newPurchaseContract(c);
    return c;
  }

  function seePurchase(uint pos) public view returns(address contractAddress)
  {
    return address(contracts[pos]);
  }
}

contract Purchase
{
  uint public price;
  address public seller;
  address[] public buyers;
  address public buyer;
  string public ipfsHash;
  string public preview;
  string public cover;
  string public name;
  string public text;

  constructor(address contractSeller, uint value, string memory bookName, string memory description, string memory imageHash, string memory privHash, string memory dataHash) public payable
  {
    seller = contractSeller;
    name = bookName; 
    text = description;
    cover = imageHash;
    preview = privHash;
    ipfsHash = dataHash;
    price = value * 1000000000000000000;
  }

  modifier condition(bool _condition) 
  {
    require(_condition);
    _;    
  }

  modifier onlyBuyer()
  {
    (msg.sender == buyer);
    _;
  }

  modifier onlySeller()
  {
    (msg.sender == seller);
    _;
  }

  event ItemReceived();

  function confrimReceived() public 
    condition(msg.value == price)
    payable
  {
    emit ItemReceived();

    buyer = msg.sender;
    seller.transfer(address(this).balance);
  }
  function getBuyerCount() public view returns(address buyerAddresses)
  {
    return buyers.length;
  }

  function getEtherCount() public view return(uint earnedEather)
  {
    return price+= msg.value;
  }
}

closed as too broad by Ismael, shane, alberto, Crissi Mariam Robert, Sanjay S B Oct 17 at 6:25

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Your contract does not compile. Both in 0.4.0 and 0.5.0. – Iftifar Taz Oct 8 at 6:43
0

Inside Purchase smart contract define counters for the total number of buyers and the total earnings like this:

contract Purchase
{
  uint public totalBuyers = 0;
  uint public totalEarnings = 0;
  ...
}

Then, when purchase is confirmed, increase these counters appropriately:

function confrimReceived() public 
  condition(msg.value == price)
  payable
{
  totalBuyers += 1;
  totalEarnings += price;
  ...
}

As long as counters are public, you may query them from other smart contract or from off-chain applications, and even from Etherscan.io.

Not the answer you're looking for? Browse other questions tagged or ask your own question.