3

I'd like to create a contract through which people can set publicly available key:value information in exchange for some Ether sent to me (the owner of the contract). This is to create a public decentralized database on which anyone can read and write, and the Ether cost would be to limit spam.

However, I'd like it to be free for all users to retrieve any key stored in the contract. My understanding of Ethereum is limited, is this possible?

4

I'd like to create a contract through which people can set publicly available key=value information

You can use a mapping to store data and public method to set values

exchange for some Ether sent to me (the owner of the contract)

You can make the set method payable and check for an amount of Ether from sender

I'd like it to be free for all users to retrieve any key stored in the contract

You can create the function a view one so no cost or transaction invlolved in reading values

is this possible?

Yes, This looks possible. It would look like below.

pragma solidity >=0.4.22 <0.6.0;

contract Store {
    mapping(bytes32 => bytes32) public keyValStore;
    address payable public  owner; 
    uint storeFee;

    constructor(uint fee) public {
        owner = msg.sender;  // setting contract creator address as the owner
        storeFee = fee; // setting a store fee for to set values
    }

    function set(bytes32 key, bytes32 value) public payable {
        require(msg.value >= storeFee); // check if Ether value is greater than the store fee
        owner.transfer(msg.value); // transfer Ether to owner account
        keyValStore[key] = value; // setting the key value pair in mapping
    }

    function get(bytes32 key) public view returns (bytes32) {
        bytes32 val = keyValStore[key]; // get the relavant value for the given key
        return val;
    }
}
0

Yes it's possible, here's an example key/value store contract to help you get started that requires 1 ether to set a key/value and anyone can read the data for free:

pragma solidity >=0.4.22 <0.6.0;
contract Store {
    mapping(bytes32 => bytes32) private store;
    mapping(bytes32 => address) private authors;
    address private owner;

    constructor() public {
        owner = msg.sender;
    }

    function set(bytes32 key, bytes32 value) public payable {
        require(msg.value == 1 ether);
        require(store[key] == 0 || authors[key] == msg.sender);
        store[key] = value;
        authors[key] = msg.sender;
    }

    function get(bytes32 key) public view returns(bytes32) {
        return store[key];
    }

    function withdraw(address payable receiver) public {
        require(msg.sender == owner);
        receiver.transfer(address(this).balance);
    }
}

https://rinkeby.etherscan.io/address/0xf7e0caef5cd7a18d31343670b60ff463fa23d5c8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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