0

TL;DR: I'm starting with blockchain, ethereum, web3 environment and i'm very excited about it, so I want to know few things that are not clear.

I already built my first solidity contract (some shop contract) but with hardcoded data, so I'd like to know, where should I get data so it is not hardcoded in solidity. For example:

If I want to create a solidity contract for wining X prize, that prize will be always hardcoded inside the contract?

or if i want to craete a smart contract to do something like kickstarter (fund rising), where will live all those campaings?

Thank you.

3
  • Pass it into a function that stores it a variable. – Rob Hitchens Mar 19 at 4:22
  • @RobHitchens that means that any data that I'd like to use inside a solidity contract will be always living inside the solidity contract? (sorry, i keep thinking in databases) – Loui Mar 19 at 14:25
  • @RobHitchens i updated the post with other example – Loui Mar 19 at 15:49
0

It's a very abstract question, so this is an abstract answer.

This contract is not production-ready. Some concerns are set aside to focus on your variable prize concern and help you get started. The game itself isn't carefully thought through. Creators produce guessing games with arbitrary prize money. Contestants try to guess the secret word.

// SPDX-License-Identifier: UNLICENSED

pragma solidity 0.7.6;

contract XPrize {
    
    struct Contest {
        uint prizeMoney;
        bool claimed;
    }
    
    mapping(bytes32 => Contest) public contests;
    
    event NewContest(address sender, bytes32 contestId, uint prizeMoney);
    event PrizeClaimed(address winner,bytes32 secret, bytes32 contestId, uint prizeMoney);
    
    // STEP 2 - contest creator creates a contest with an arbitrary anount of funding. 
    function newContest(bytes32 contestId) public payable { // anyone can create a contest and fund it
        Contest storage c = contests[contestId];
        require(c.prizeMoney == 0, "contest is already defined.");
        require(msg.value > 0, "send money");
        c.prizeMoney = msg.value;
        emit NewContest(msg.sender, contestId, msg.value);
    }
    
    // STEP 3 - guess the secret, claim the money
    function claimPrize(bytes32 secret) public {
        bytes32 key = hashHelper(secret);
        Contest storage c = contests[key];
        require(c.prizeMoney > 0, "there is no prize money there");
        require(!c.claimed, "already paid out");
        c.claimed = true;
        emit PrizeClaimed(msg.sender,secret, key, c.prizeMoney);
        msg.sender.transfer(c.prizeMoney);
    }
    
    // STEP 1 - contest creator generates a key with a secret that is hard to guess
    function hashHelper(bytes32 secret) public view returns(bytes32) {
        return keccak256(abi.encodePacked(secret, address(this)));
    }
    
}

The contract is acting as a vending machine in that it holds the money as well as the mechanism for its release. Notice that it records the prize money liability in a data structure. There is a pile of cash "in the box" - this a protocol-level accounting that Ethereum handles - it knows how much money is parked in the address. The internal accounting requirements, e.g. how much is owed per user or per game, are internal accounting concerns.

Hope it helps.

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.