0
pragma solidity ^0.5.0;

contract reeler{
  uint public id;
  string public name;
  string public addres;
  string public state;
  string public pincode;
  uint public yarncount = 0;
  mapping(uint => Yarn) public yarns;
  
  struct Yarn{
    address payable owner; 
    uint yid;
    string types;
    string weight;
    uint price;
    string colour; 
  }

  event YarnCreated(
    address payable owner,
    uint yid, 
    string types,
    string weight,
    uint price,
    string colour
  );
  event YarnPurchased(
    address payable owner,
    uint yid, 
    string types,
    string weight,
    uint price,
    string colour
  );
    constructor() public {
    id= 1098;
    name="joy";
    addres="mysore";
    state="karnataka";
    pincode="560043";
    /*types="thread";
    weight= 80;
    rate= 1000;
    colour= "lilac";*/
    }

  function  createYarn(string memory types, string memory weight, uint price, string memory colour) public {
        //make sure parameters are correct
        //require valid name
        require(bytes(types).length > 0);
        //require valid weight
        require(bytes(weight).length > 0);
        //require valid price
        require(price > 0);
        //require valid clour
        require(bytes(colour).length > 0);
         //increment product count
        yarncount ++;
        //create product
        yarns[yarncount]= Yarn(msg.sender,yarncount, types, weight, price, colour);
        //trigger event
        emit YarnCreated(msg.sender, yarncount, types, weight, price, colour);
        
  }
  function purchaseYarn(uint yid) public payable {
    // fetch the product
    Yarn memory yarn= yarns[yid];
    //fetch the owner
    address payable seller = Yarn.owner;
    //make sure the yarn is valid
    //purchase it
    Yarn.owner = msg.sender;
    //update the product
    Yarn[yid]= Yarn;
    //pay the seller by ethers
    address(seller).transfer(msg.value);
    //trigger event
    emit YarnPurchased(msg.sender, yarncount, Yarn.types, Yarn.weight, Yarn.price, Yarn.colour);
  }



}
0

The issue comes from the purchaseYarn function where there is a confusion between the Yarn struct type and the yarn struct instance.

This code works :

  function purchaseYarn(uint yid) public payable {
// fetch the product
Yarn memory yarn= yarns[yid];
//fetch the owner
address payable seller = yarn.owner;
//make sure the yarn is valid
//purchase it
yarn.owner = msg.sender;
//update the product
//pay the seller by ethers
address(seller).transfer(msg.value);
//trigger event
emit YarnPurchased(msg.sender, yarncount, yarn.types, yarn.weight, yarn.price, yarn.colour);
}

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.