2

I am new to this smart contract and blockchain. I'm trying to do simple selling goods dapps. But i'm stuck at the buy button.

I have array for the products.

constructor() public {
   goods[0].owner = msg.sender;
   goods[0].name = 'Product 1';
   goods[0].price = 40 ether;
   goods[0].forSale = true;

   goods[1].owner = msg.sender;
   goods[1].name = 'Product 2';
   goods[1].price = 40 ether;
   goods[1].forSale = true;
 }

and I have this javascript

  let add = goods[0];

  let a = add[0]; //address
  let b = add[1]; //name
  let c = add[2]; //available
  let d = add[3]; //price

  let fullhtml = "";

  for(let m = 0; m < a.length; m++)
  {

    //(condition) ? true : false
    var btn = (c[m]) ? "<form id='buying'><button type='submit' class='btn btn-info'>Buy</button></form>" : "<button class='btn btn-danger' disabled>Sold Out</button>";

    fullhtml = fullhtml + "<tr><td>" + a[m] + "</td>" + "<td>" + hex2a(b[m]) + "</td>" + "<td>" + btn + "</form></td>" + "<td>" + d[m] + "</td></tr>";
  }

So now I have table, each row have same index number. How can I add function to the buy button that represent each index.

My buy function is this.

function buyGood(uint index) public payable {
        Good storage good = goods[index];

        require(msg.sender != good.owner && good.forSale && msg.value >= good.price);

        if(good.owner == 0x0) {
            balances[owner] += msg.value;
        }else {
            balances[good.owner] += msg.value;
        }

        good.owner = msg.sender;
        good.forSale = false;

        emit GoodOwnerChanged(index);
    }//

Your Answer

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

Browse other questions tagged or ask your own question.