Skip to content

Instantly share code, notes, and snippets.

@fewwwww
Last active June 20, 2023 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fewwwww/472a08039680bf6c79d5379c8533a2fd to your computer and use it in GitHub Desktop.
Save fewwwww/472a08039680bf6c79d5379c8533a2fd to your computer and use it in GitHub Desktop.
Raffle with zkGraph

Raffle with zkGraph

Ethereum Smart Contract Code

pragma solidity >=0.8.2 <0.9.0;

contract retwi_rng {
    uint256 number;
    event retwi_number(uint256 indexed num);

    function input(uint256 num) public {
        emit retwi_number(num);
    }

    function retrieve() public view returns (uint256) {
        return number;
    }
}

zkGraph Code

export function handleEvent(esig: Uint8Array, topic1: Uint8Array, topic2: Uint8Array, topic3: Uint8Array, data: Uint8Array): Uint8Array {
    // Get on-chain input data from topic1 of event.
    var input = changetype<Bytes>(topic1);
    input = input.slice(32-8,32);
    var num = input.toU64();

    // Make raffle output random!
    let year = 23;
    let month = 6;
    let day = 19;
    let raffle = num;
    for (let i = 0; i < 4; i++) {
        raffle = (raffle * year - month) % day;
    }
  
    // Output raffle result as state
    let state = Bytes.new(32);
    state.fromU64(raffle);
    return state as Uint8Array;
}

Links in Explorer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment