This post is the continuation of:

In this example, we can use an Ethereum smart-contract to register products and its value in Ether. Also, we can use the same smart-contract to be called by the people to buy the products. Very easy to implement and to connect to a simple Decentralized Store or Marketplace App (Dapp).

Let’s see the example:


pragma solidity ^0.4.16;

contract owned {
address public owner;

function owned() public {
owner = msg.sender;
}

modifier onlyOwner {
require(msg.sender == owner);
_;
}

function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
}

contract Contrato_contacto_legal is owned{
struct Contrato {
uint256 coste_ether;
bool pagado;
address cuenta_pagador;
}

mapping (uint256 => Contrato) public contratos;

event Alta(uint256 referencia, uint256 coste_en_ether);
event Compra( uint256 referencia, uint256 coste_en_ether, address comprador);

function dar_alta_contrato( uint256 _referencia, uint256 _coste_en_ether ) onlyOwner public {
contratos[ _referencia] = Contrato(_coste_en_ether, false, owner);
Alta( _referencia, _coste_en_ether);
}

function pagar_contrato( uint256 _referencia) payable public {
require ( msg.value >= contratos[ _referencia].coste_ether );
owner.transfer(msg.value);
contratos[ _referencia] = Contrato(msg.value, true, msg.sender);
Compra(_referencia, msg.value, msg.sender);
}
}

In this contract we can see as always the “Owned” contract and functions , and the primary contract or “Contrato_contacto_legal

Captura de pantalla 2018-09-24 a las 20.16.59

Basically there are two functions:

  • OnlyOwner can call “dar_alta_contrato” to register an asset and its value  ( uint256 _referencia, uint256 _coste_en_ether )
  • Everybody can call “pagar_contrato” (but using Ether during the call because is payable). With param reference (and the Ether send during the call).

At the end of the function there is always a call to an Event. We can see a track of the events already done.

Captura de pantalla 2018-09-24 a las 20.14.38

The Events are useful to trigger actions in the Dapp after a transaction/request is done.

Everybody can read the contract in Rinkeby in this address

For example, we can do a simple query to know the status of the product with reference “11

Captura de pantalla 2018-09-24 a las 20.19.08

If we have the Owner Key installed in MetaMask, we can do more things with MetaMask, we can call the Write functions!!

This is an amazing new feature of Etherscan and Rinkeby!

Captura de pantalla 2018-09-24 a las 20.21.00

Now you are ready to start your first Dapp initial concepts.

By MrAddon

Advertisements
Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s