Ethereum Smartcontracts Monetization and Ownership Transfer

decentralized apps ethereum smart contracts dapps nameledger

In this part 2 of the tutorial we will see how we can monetize our ethereum smart contracts and transfer ownership of our nameledger entry. Take a look at Part 1

First lets look at how we can buy a nameledger entry

function registerFriendlyName(string name, uint futureSellPrice) public payable {
  require(msg.value >= slotValue);
  require(lookup[name] == address(0));
  reverseLookup[msg.sender] = name;
  lookup[name] = msg.sender;
  bidPrice[name] = futureSellPrice;
}

So the difference between this method in part 1 and now is that we set a futureSellPrice. This is to say that we can set a price for this name to be transfered to some other person. So the slotValue is how much its currently costing us which is 0.001 ether to buy a name. You can think of futureSellPrice as the appreciation cost and how much you are willing to give up the name for. Lets say you don’t want to sell the name you could as well set a super high value, something like 10000000000000000 ethers.

So the slotValue which we are asking is one way to monetize the smartcontract. Think of this as buying a domain name.

Lets say you are a NameLedger investor and buy a bunch of names which you think are useful and buy them at a cheap price like what its now 0.001 ether. At some point in future you might want to sell one of your name for 10 ethers to a buyer. Our smart contract which is dealing with the transferring the ownership of the name can take a percentage of the total transaction amount as transaction fee. For the sake of this example its 0.005 %

function transferName(string name, uint futureSellPrice) payable public{
    require(msg.value >= bidPrice[name]);
    address oldOwner = lookup[name];
    lookup[name] = msg.sender;
    reverseLookup[msg.sender] = name;
    oldOwner.transfer(bidPrice[name] - (bidPrice[name] *  transactionFee/1000));
    bidPrice[name] = futureSellPrice;
  }

Lets talk about a few things about this method. The payable keyword is used to say that we can ether to that method. public is just to say that anyone can call this method.

So there are different way to handle this action and we want to keep it simple by letting anyone call the method and transfer the name to themselves as long as the send enough ether to meet the requirement of the seller.

require(msg.value >= _totalTransactionAmount(name)); Is a way to ensure that the code following this line is only executed when the condition is met and not otherwise. So if the seller is interested in selling it for 10 ether and you send 1 ether then the require condition would fail and the name would not be transferred. We do take a cut of the transaction amount and send the remaining amount to the seller.

Lets do a transaction

Complete Ethereum Smart contract code

pragma solidity ^0.4.19;

/**
 * The NameLedger contract does this and that...
 */
contract NameLedger {
  address owner;
  uint slotValue = 0.001 ether;
  uint transactionFee = 5;

  mapping (address => string) reverseLookup;
  mapping (string => uint)  bidPrice;
  mapping (string => address) lookup;

  function NameLedger () {
    owner = msg.sender;
  }

  function contractOwner() public returns(address){
    return owner;
  }
  /* Modifiers */

  modifier onlyOwner(string name) {
    require(lookup[name] == msg.sender);
    _;
  }

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

  function setSlotValue(uint value) public onlyContractOwner{
    slotValue = value;
  }

  function registerFriendlyName(string name, uint futureSellPrice) public payable {
      require(msg.value >= slotValue);
      require(lookup[name] == address(0));
      reverseLookup[msg.sender] = name;
      lookup[name] = msg.sender;
      bidPrice[name] = futureSellPrice;
  }

  function isRegistered(string name) public view returns(bool) {
      if (lookup[name] == address(0)) {
          return false;
      } else {
          return true;
      }
  }

  function transferName(string name, uint futureSellPrice) payable public{
    require(msg.value >= bidPrice[name]);
    address oldOwner = lookup[name];
    lookup[name] = msg.sender;
    reverseLookup[msg.sender] = name;
    oldOwner.transfer(bidPrice[name] - (bidPrice[name] * transactionFee/1000));
    bidPrice[name] = futureSellPrice;
  }

  function ownerOf(string name) public returns(address) {
    return lookup[name];
  }

  function owns(address ownAddr) public returns(string) {
    return reverseLookup[ownAddr];
  }

  function getBalance() constant returns (uint) {
    return this.balance;
  }

  function getPrice(string name) public returns(uint) {
    return bidPrice[name];
  }

}

Let me know if you have any feedback and also leave a comment if you have any idea which you would like to see implemented.

Comments

comments powered by Disqus