0

I am new to blockchain. I am trying to implement a dapp project in my college curriculum. I am trying to implement seller-buyer smart contract on private ethereum node(using solidity, geth console, mist browser). Please help.

Problems that I am facing:

  1. Once the seller contract is executed after entering all the values. I want to capture the updated product quantity. Ex: The seller is selling 10 apples and each apple costs an ether. Once the buyer transfers 1 ether to seller account then the product quantity should be updated to 9 (it means the seller sold 1 apple) based on the ether sent by the buyer the product quantity should get updated in read from contract.

  2. How to display this updated product quantity

  3. How to show that the buyer received the product

I created 2 contracts: buyer and seller. I created 2 accounts: buyer account and seller account.

  1. Buyer contract: I am writing/reading from contract: buyer name, buyer address

  2. seller contract: I am writing to the contract: seller name, seller address, product description, perunitcost.

Here is the code:

pragma solidity ^0.4.15;
contract Seller {
    uint public value;
    address public seller;
    string public sellername;
    string public selleraddress;
    string public productdescription;
    uint public perunitcost; 
    uint public productquantity;
    uint public updatedproductquantity;
    mapping (address => uint) public balances;

function Seller(string tokensellername,string tokenselleraddress, string tokenproductdescription, uint tokenproductquantity,uint tokenperunitcost) public {
        sellername = tokensellername;
        selleraddress = tokenselleraddress;
        productdescription = tokenproductdescription;
        productquantity = tokenproductquantity;
        perunitcost = tokenperunitcost;
    }

  function confirmPurchase(address _seller) payable {
    uint itemstoBuy = value / msg.value; 

  // balances[_seller] holds the number of carrots to sell
    require(balances[_seller] >= itemstoBuy);

    balances[_seller] -= itemstoBuy;
    balances[msg.sender] += itemstoBuy;
    _seller.transfer(msg.value); //transfer the ether to the seller

    //display how much product quantity in left: show this on read from contract
    updatedproductquantity = balances[_seller];
  } 
}

seller smart contract - input the seller details enter image description here output of update product quantity - its always showing 0

0

My opinion on this is that you are making a mistake on the design of this problem. Instead of have a buyer and seller contract and overcomplicating the app, I'd just use one contract that holds the stock of a particular item and manages the balance of the seller. You could later expand this to sell other items as well. Here's a simple take on this:

  pragma solidity 0.4.18;

contract Marketplace {

    uint public applePrice;
    mapping (address => uint) public applesBalance;
    address seller;

    function Marketplace(uint _applePrice, uint _initialAppleStock) {
        applePrice = _applePrice;
        seller = msg.sender; //seller is the person who creates this contract
        applesBalance[seller]= _initialAppleStock;
    }

    function buyApplesFromSeller() payable {
        uint applesToBuy = msg.value / applePrice; 

        require(applesBalance[seller] >= applesToBuy);

        applesBalance[msg.sender] += applesToBuy;
        applesBalance[seller] -= applesToBuy;

        seller.transfer(msg.value);

    } 
}

There would be a seller account who deploys the contract.

Then you have any other account buying by calling buyApplesFromSeller() and passing some ether. The function updates the apples balance for the seller (reducing his stock) and updates the balance the buyer has.

You can retrieve the amount of apples the seller (or the buyer) have by accessing the applesBalance mapping with each address.

You can easily try this on Remix.

  • Thank you, Pablo. I will try this on remix and get back to you if I have any doubts – rupali Nov 30 '17 at 14:23
  • I tried to execute the above code. But, I am still having the issue with displaying/ retrieving: amount of apples the seller has after selling and the amount of apples the buyer has after buying. Here is the scenario: (apple price: 2ethers, apple quantity:20 apples ) 1. seller executed the marketplace contract. 2. buyer executed: buyapplesfromseller function sends 2 ethers to seller account and completes execution. – rupali Nov 30 '17 at 17:04
  • I entered the seller address in the mist (read from contract)and value 20 gets displayed in the apples balances. Ideally, the apple balance should be updated to 19, because buyer sent 2 ethers and bought 1 apple. If I enter buyer1 account address in read from contract, 0 gets displayed all the time. If I give buyer address then it should show no of apples bought. – rupali Nov 30 '17 at 17:05
  • Sorry, I wrote this on a rush and didn't test it. The line that calculates the apples bought should be uint applesToBuy = msg.value / applePrice; and not applePrice / msg.value. Already updated my answer. – pabloruiz55 Nov 30 '17 at 17:14
  • That's also the problem with your original code. You were doing price / msg.value which is incorrect. – pabloruiz55 Nov 30 '17 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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