1

Quick introduction:I send arrays of strings from a python script to a js script. The data sent are of these type: enter image description here and are stored in a var "data".So I have this object("data") which contains 12 "blocks",evry of which contains ID_utent=...Xacc=..Yacc=...Zacc=..Date=..Time=..(as shown in the image) (here there is the JS code if you want to see:

var net = require('net');

var HOST = '127.0.0.1';
var PORT = 10001;


net.createServer(function(sock) {


console.log('__________');


sock.on('data', function(data) {

    console.log('DATI:\r' + data);


});


sock.on('close', function(data) {
    console.log('__________');
});

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

I want these data saved on blockchain. I thought to use a smart contract like this

pragma solidity ^0.4.0;

contract SimpleStorage {
    uint storedData;

    function set(uint x) {
        storedData = x;
    }

    function get() constant returns (uint) {
        return storedData;
    }
}

So my question is: Could I save these data passing directly "data" as an object parameter?? like this:

pragma solidity ^0.4.0;
contract SimpleStorage {
    object storedData;

    function set(object x) {
        storedData = x;
    }

    function get() constant returns (object) {
        return storedData;
    }
}
1
  • 1
    Solidity is a strongly typed language and will not accept arbitrary data. You can try saving byte arrays Nov 13, 2017 at 3:25

2 Answers 2

1

Take a look at Struct type in solidity, you can use it to model your data, but the only challenge is, you can not return a struct in solidity yet.

pragma solidity ^0.4.0;

contract ItemListContract {
struct item {
    bytes iname;
    uint16 itemid;
    bytes icode;
    uint ivalue;
}

uint itemCount;
mapping(bytes => item) itemList;
item[] itemArray;

function ItemListContract() {
    log0('hi');
}

function addItem(bytes name, uint16 iid, bytes code, uint val) {        
    var itemnew = item(name, iid ,code, val);
    // log0(itemnew);
    itemList[code] = itemnew;
    itemArray.push(itemnew);
    itemCount++;
}

function countItemList() constant returns (uint count) {     
    return itemCount;
}

function removeItem(bytes code) {
    delete itemList[code];
    itemCount--;
}

function getItem(bytes code) constant returns (bytes iname, uint val) {   
    return (itemList[code].iname, itemList[code].ivalue);
}
}

in the sample code above, the struct will be the format for your data

1
  • A code example would improve this answer.
    – carver
    Sep 13, 2017 at 15:40
0

 pragma solidity ^0.4.0;
  contract SimpleStorage {
    struct Obj{
        int info;
        uint[2] exDataArr;
        bytes32[2] exDataStr;
    }
    mapping(int =>Obj) Obj;
    function set(uint x,uint[2] exDataArr,bytes32[2] exDataStr) {
        Obj[x]= Obj({info:info,exDataArr:exDataArr,exDataStr:exDataStr});
    }

function get(int x) public constant returns(int,uint[2],bytes32[2]){ uint[3] memory exDataInt = Obj].exDataArr; bytes32[3] memory exDataStr = Obj[x].exDataStr; return (Obj[x].info,exDataInt,exDataStr); }

} //maybe this can help you~~~~

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.