Skip to main content
Add better example
Source Link
supakaity
  • 1.5k
  • 8
  • 17

Circular creation.

The following is an example of creating a circular group of items.

In this example you would:

  • create a new Factory
  • check factory.head.next == factory.head
  • call factory.add(factory.head)
  • check factory.head.next is a new Item
  • check factory.head.next.next == factory.head

Code:

pragma solidity ^0.4.22;

contract Item {
    Item public nextItem;next;
    address owner;
    
    constructor(Item _next) public {
        owner = msg.sender;
    }
    
     functionif setNext(Item n) public_next returns== (Item(0)) {
        require(n != address(0) && msg.sendernext === owner);this;
        nextItem} =else n;{
        return n;
   next }
}
= _next;
contract Chain {
    Item[3] public items;}
    }
    constructor
    function addNew(Item item) public {
        items[0] =require(msg.sender new== Item(owner);
        items[1]next = newitem;
    }
}

contract Factory {
    Item public head;
    
    constructor(); public {
        items[2]head = new Item(Item(0));
 
    }
    items[0].setNext(items[1])
    function add(Item _to) public {
        Item link = _to.setNextnext(items[2]);
        Item next = new Item(link);
        _to.setNextaddNew(items[0]next);
    }
}

Circular creation.

The following is an example of creating a circular group of items:

pragma solidity ^0.4.22;

contract Item {
    Item public nextItem;
    address owner;
    
    constructor() public {
        owner = msg.sender;
    }
    
     function setNext(Item n) public returns (Item) {
        require(n != address(0) && msg.sender == owner);
        nextItem = n;
        return n;
    }
}

contract Chain {
    Item[3] public items;
    
    constructor() public {
        items[0] = new Item();
        items[1] = new Item();
        items[2] = new Item();
 
        items[0].setNext(items[1])
                .setNext(items[2])
                .setNext(items[0]);
    }
}

Circular creation.

The following is an example of creating a circular group of items.

In this example you would:

  • create a new Factory
  • check factory.head.next == factory.head
  • call factory.add(factory.head)
  • check factory.head.next is a new Item
  • check factory.head.next.next == factory.head

Code:

pragma solidity ^0.4.22;

contract Item {
    Item public next;
    address owner;
    
    constructor(Item _next) public {
        owner = msg.sender;
        if (_next == Item(0)) {
            next = this;
        } else {
            next = _next;
        }
    }
    
    function addNew(Item item) public {
        require(msg.sender == owner);
        next = item;
    }
}

contract Factory {
    Item public head;
    
    constructor() public {
        head = new Item(Item(0));
    }
    
    function add(Item _to) public {
        Item link = _to.next();
        Item next = new Item(link);
        _to.addNew(next);
    }
}
Source Link
supakaity
  • 1.5k
  • 8
  • 17

Circular creation.

The following is an example of creating a circular group of items:

pragma solidity ^0.4.22;

contract Item {
    Item public nextItem;
    address owner;
    
    constructor() public {
        owner = msg.sender;
    }
    
    function setNext(Item n) public returns (Item) {
        require(n != address(0) && msg.sender == owner);
        nextItem = n;
        return n;
    }
}

contract Chain {
    Item[3] public items;
    
    constructor() public {
        items[0] = new Item();
        items[1] = new Item();
        items[2] = new Item();

        items[0].setNext(items[1])
                .setNext(items[2])
                .setNext(items[0]);
    }
}