contract SoulToken is ERC20Token{

    // The three letter symbol of token
    string public constant symbol = "SOUL";

    // Name of token
    string public constant name = "Soul Napkins";

    // 6 is a holy number (2*3) so there are 6 decimals
    uint8 public constant decimals = 6;

    // With 6 decimals, a single unit is 10**6
    uint256 public constant unit = 1000000;

    // fee to pay to transfer soul, 10% like the ecclesiastical tithe
    uint8 public constant obol = 10;

    // price per token, 100 napkins per Ether
    uint256 public constant napkinPrice = 10 finney / unit;

    // Total number of napkins available
    // 144,000 (get it?)
    uint256 constant totalSupply_ = 144000 * unit;

    // mapping to keep the reason of the soul sale!
    mapping(address => string) reasons;

    // prices that people put up for their soul
    mapping(address => uint256) soulPrices;

    // who owns a particular soul
    mapping(address => address) ownedBy;

    // number of souls owned by someone
    mapping(address => uint256) soulsOwned;

    // book of souls, listing all souls on sale and sold
    mapping(uint256 => address) soulBook;

    // owner of the contract
    address public owner;

    // Address where soul obol is due to
    address public charonsBoat;

    // small fee to insert soul into soulbook
    uint256 public bookingFee;

    // souls for sale
    uint256 public soulsForSale;

    // souls already sold
    uint256 public soulsSold;

    // total amount of Wei collected by Charon
    uint256 public totalObol;

    // Logs a soul transfer
    event SoulTransfer(address indexed _from, address indexed _to);

    function SoulToken() public{
        owner = msg.sender;
        charonsBoat = msg.sender;
        // fee for inserting into soulbook, unholy 13 finney
        bookingFee = 13 finney;
        soulsForSale = 0;
        soulsSold = 0;
        totalObol = 0;
        // all napkins belong to the contract at first:
        balances[this] = totalSupply_;
        // 1111 napkins for the dev ;-)
        payOutNapkins(1111 * unit);
    }

    // fallback function, Charon sells napkins as merchandise!
    function () public payable {
        uint256 amount;
        uint256 checkedAmount;
        // give away some napkins in return proportional to value
        amount = msg.value / napkinPrice;
        checkedAmount = checkAmount(amount);
        // only payout napkins if there is the appropriate amount available
        // else throw
        require(amount == checkedAmount);
        // forward money to Charon
        payCharon(msg.value);
        // finally payout napkins
        payOutNapkins(checkedAmount);
    }

    // allows changing of the booking fee, note obol and token price are fixed and cannot change
    function changeBookingFee(uint256 fee) public {
        require(msg.sender == owner);
        bookingFee = fee;
    }

    // changes Charon's boat, i.e. the address where the obol is paid to
    function changeBoat(address newBoat) public{
        require(msg.sender == owner);
        charonsBoat = newBoat;
    }

    // total number of napkins distributed by Charon
    function totalSupply() public constant returns (uint256){
        return totalSupply_;
    }

    // returns the reason for the selling of one's soul
    function soldSoulBecause(address noSoulMate) public constant returns(string){
        return reasons[noSoulMate];
    }

    // returns the owner of a soul
    function soulIsOwnedBy(address noSoulMate) public constant returns(address){
        return ownedBy[noSoulMate];
    }

    // returns number of souls owned by someone
    function ownsSouls(address soulOwner) public constant returns(uint256){
        return soulsOwned[soulOwner];
    }

    // returns price of a soul
    function soldSoulFor(address noSoulMate) public constant returns(uint256){
        return soulPrices[noSoulMate];
    }

    // returns the nth entry in the soulbook
    function soulBookPage(uint256 page) public constant returns(address){
        return soulBook[page];
    }

    // sells your soul for a given price and a given reason!
    function sellSoul(string reason, uint256 price) public payable{
        uint256 charonsObol;
        string storage has_reason = reasons[msg.sender];

        // require that user gives a reason
        require(bytes(reason).length > 0);

        // require to pay bookingFee
        require(msg.value >= bookingFee);

        // you cannot give away your soul for free, at least Charon wants some share
        charonsObol = price / obol;
        require(charonsObol > 0);

        // assert has not sold her or his soul, yet
        require(bytes(has_reason).length == 0);
        require(soulPrices[msg.sender] == 0);
        require(ownedBy[msg.sender] == address(0));

        // pay book keeping fee
        payCharon(msg.value);

        // store the reason forever on the blockchain
        reasons[msg.sender] = reason;
        // also the price is forever kept on the blockchain, so do not be too cheap
        soulPrices[msg.sender] = price;
        // and keep the soul in the soul book
        soulBook[soulsForSale + soulsSold] = msg.sender;
        soulsForSale += 1;
    }

    // buys msg.sender a soul and rewards him with tokens!
    function buySoul(address noSoulMate) public payable returns(uint256 amount){
        uint256 charonsObol;
        uint256 price;

        // you cannot buy an owned soul:
        require(ownedBy[noSoulMate] == address(0));
        // get the price of the soul
        price = soulPrices[noSoulMate];
        // Soul must be for sale
        require(price > 0);
        require(bytes(reasons[noSoulMate]).length > 0);
        // Msg sender needs to pay the soul price
        require(msg.value >= price);
        charonsObol = msg.value / obol;

        // check for wrap around
        require(soulsOwned[msg.sender] + 1 > soulsOwned[msg.sender]);

        // pay Charon
        payCharon(charonsObol);
        // pay the soul owner
        noSoulMate.transfer(msg.value - charonsObol);

        // Update the soul stats
        soulsForSale -= 1;
        soulsSold += 1;
        // Increase the sender's balance by the appropriate amount of souls ;-)
        soulsOwned[msg.sender] += 1;
        ownedBy[noSoulMate] = msg.sender;
        // log the transfer
        SoulTransfer(noSoulMate, msg.sender);

        // and give away napkins proportional to obol plus 1 bonus napkin ;-)
        amount = charonsObol / napkinPrice + unit;
        amount = checkAmount(amount);
        if (amount > 0){
            // only payout napkins if they are available
            payOutNapkins(amount);
        }

        return amount;
    }

    // can transfer a soul to a different account, but beware you have to pay Charon again!
    function transferSoul(address _to, address noSoulMate) public payable{
        uint256 charonsObol;

        // require correct ownership
        require(ownedBy[noSoulMate] == msg.sender);
        require(soulsOwned[_to] + 1 > soulsOwned[_to]);
        // require transfer fee is payed again
        charonsObol = soulPrices[noSoulMate] / obol;
        require(msg.value >= charonsObol);
        // pay Charon
        payCharon(msg.value);
        // transfer the soul
        soulsOwned[msg.sender] -= 1;
        soulsOwned[_to] += 1;
        ownedBy[noSoulMate] = _to;

        // Log the soul transfer
        SoulTransfer(msg.sender, _to);
    }

    // logs and pays charon fees
    function payCharon(uint256 obolValue) internal{
        totalObol += obolValue;
        charonsBoat.transfer(obolValue);
    }

    // checks if napkins are still available and adjusts amount accordingly
    function checkAmount(uint256 amount) internal constant returns(uint256 checkedAmount){

        if (amount > balances[this]){
            checkedAmount = balances[this];
        } else {
            checkedAmount = amount;
        }

        return checkedAmount;
    }

    // transfers napkins to people
    function payOutNapkins(uint256 amount) internal{
        // check for amount and wrap around
        require(amount > 0);
        // yeah some sanity check
        require(amount <= balances[this]);

        // send napkins from contract to msg.sender
        balances[this] -= amount;
        balances[msg.sender] += amount;
        // log napkin transfer
        Transfer(this, msg.sender, amount);
    }

}
// Token implementation that allows selling of souls and distribution of napkins
function buySoul(address noSoulMate) public payable returns(uint256 amount){
    uint256 charonsObol;
    uint256 price;

    // you cannot buy an owned soul:
    require(ownedBy[noSoulMate] == address(0));
    // get the price of the soul
    price = soulPrices[noSoulMate];
    // Soul must be for sale
    require(price > 0);
    require(bytes(reasons[noSoulMate]).length > 0);
    // Msg sender needs to pay the soul price
    require(msg.value >= price);
    charonsObol = msg.value / obol;

    // check for wrap around
    require(soulsOwned[msg.sender] + 1 > soulsOwned[msg.sender]);

    // pay Charon
    payCharon(charonsObol);
    // pay the soul owner
    noSoulMate.transfer(msg.value - charonsObol);

    // Update the soul stats
    soulsForSale -= 1;
    soulsSold += 1;
    // Increase the sender's balance by the appropriate amount of souls ;-)
    soulsOwned[msg.sender] += 1;
    ownedBy[noSoulMate] = msg.sender;
    // log the transfer
    SoulTransfer(noSoulMate, msg.sender);

    // and give away napkins proportional to obol plus 1 bonus napkin ;-)
    amount = charonsObol / napkinPrice + unit;
    amount = checkAmount(amount);
    if (amount > 0){
        // only payout napkins if they are available
        payOutNapkins(amount);
    }

    return amount;
}

// buys msg.sender a soul and rewards him with tokens!