domenica 13 marzo 2016

SOLIDITY - concat string

function strConcat(string _a, string _b, string _c, string _d, string _e) internal returns (string){
    bytes memory _ba = bytes(_a);
    bytes memory _bb = bytes(_b);
    bytes memory _bc = bytes(_c);
    bytes memory _bd = bytes(_d);
    bytes memory _be = bytes(_e);
    string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length);
    bytes memory babcde = bytes(abcde);
    uint k = 0;
    for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i];
    for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i];
    for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i];
    for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i];
    for (i = 0; i < _be.length; i++) babcde[k++] = _be[i];
    return string(babcde);
}

function strConcat(string _a, string _b, string _c, string _d) internal returns (string) {
    return strConcat(_a, _b, _c, _d, "");
}

function strConcat(string _a, string _b, string _c) internal returns (string) {
    return strConcat(_a, _b, _c, "", "");
}

function strConcat(string _a, string _b) internal returns (string) {
    return strConcat(_a, _b, "", "", "");
}

sabato 12 marzo 2016

SOLIDITY - COMPARE STRING


function stringsEqual(string storage _a, string memory _b) internal returns (bool) {
bytes storage a = bytes(_a);
bytes memory b = bytes(_b);
if (a.length != b.length)
return false;
// @todo unroll this loop
for (uint i = 0; i < a.length; i ++)
if (a[i] != b[i])
return false;
return true;
}

SOLIDITY - info getter

contract basicInfoGetter { address creator; function basicInfoGetter() public { creator = msg.sender; } function getMsgSender() constant returns (address) // Returns the address of whomever made this call { // (i.e. not necessarily the creator of the contract) return msg.sender; } function getMsgValue() constant returns (uint) // returns amt of wei sent with this call { return msg.value; } function getContractAddress() constant returns (address) { return this; } function getContractBalance() constant returns (uint) { return this.balance; } /********** Standard kill() function to recover funds **********/ function kill() { if (msg.sender == creator) suicide(creator); // kills this contract and sends remaining funds back to creator } } /* var abi = [{ "constant": true, "inputs": [], "name": "getContractAddress", "outputs": [{ "name": "", "type": "address" }], "type": "function" }, { "constant": false, "inputs": [], "name": "kill", "outputs": [{ "name": "", "type": "bool" }], "type": "function" }, { "constant": true, "inputs": [], "name": "getContractBalance", "outputs": [{ "name": "", "type": "uint256" }], "type": "function" }, { "constant": true, "inputs": [], "name": "getMsgSender", "outputs": [{ "name": "", "type": "address" }], "type": "function" }, { "constant": true, "inputs": [], "name": "getMsgValue", "outputs": [{ "name": "", "type": "uint256" }], "type": "function" }, { "inputs": [], "type": "constructor" }]; */

venerdì 11 marzo 2016

COLIDITY CONTRACT - Check balance

creator = msg.sender; creatorbalance = creator.balance;

SOLIDITY CONTRACT - Iterator loop

contract BasicIterator { address creator; uint8[10] integers; function BasicIterator() { creator = msg.sender; uint8 x = 0; while(x < integers.length) { integers[x] = x; x++; } } }

ETHEREUM SOLIDITY - how Store informations inside contract

contract SimpleStorage {
    uint storedData;
    function set(uint x) {
        storedData = x;
    }
    function get() constant returns (uint retVal) {
        return storedData;
    }
}

BASIC ETHEREUM SOLIDITY CONTRACT WITH KILL FUNCTION

contract HelloSystem {

    address owner;

    // Constructor
    function HelloSystem(){
        owner = msg.sender;
    }

    function remove() {
        if (msg.sender == owner){
            suicide(owner);
        }
    }

}