divergencetech / ethier

Golang and Solidity SDK to make Ethereum development ethier
MIT License
217 stars 23 forks source link

Add "Quickstart" instructions to README #32

Open zeuslawyer opened 2 years ago

zeuslawyer commented 2 years ago

In addition to Quickstart, may be useful to outline the project structure.

Happy to do this with a little help from you @aschlosberg .

ARR4N commented 2 years ago

Thank you and yes please! I added a Getting Started to the primary README but would love some help with other parts you suggest. Feel free to send PRs (and I'll turn notifications back on so I don't miss them like I did with this Issue, sorry).

caffeinum commented 2 years ago

@aschlosberg should Getting started section also feature npm package?

I would also add sections about how to use some of the libraries. I am having trouble with effectively integrating PRNG and NextShuffler. I can add examples and READMEs if you help me to figure it out, etc: #47

ARR4N commented 2 years ago

Thanks @caffeinum I'll reply directly to #47 re that usage. I'm having coffee with @zeuslawyer this week so will see where things stand with the Quickstart.

caffeinum commented 2 years ago

@aschlosberg

Is this the correct usage of PRNG.Source?

(P.S. Nevermind the predictability issues, the risk was calculated but man I am bad at math)


contract NextShufflerLazyInit is NextShuffler {
    using PRNG for PRNG.Source;

    uint256[2] private _nextShufflerSourceStore;

    bool private _isRandomnessSourceSet;

    constructor () NextShuffler(1337) {}

    function _setRandomnessSource(bytes32 seed) internal {
        require(
            !isRandomnessSourceSet(),
            "Can't change source after seed has been set"
        );

        PRNG.Source src = PRNG.newSource(seed);

        src.store(_nextShufflerSourceStore);

        _isRandomnessSourceSet = true;
    }

    function _load() internal view returns (PRNG.Source) {
        return PRNG.loadSource(_nextShufflerSourceStore);
    }

    function _store(PRNG.Source _src) internal {
        _src.store(_nextShufflerSourceStore);
    }
}

and then:

contract MetaverseBaseNFT is ERC1155, NextShufflerLazyInit {

    function mint() {
        // ...

        PRNG.Source rndSource = _load();

        for (uint256 i = 0; i < amount; i++) {
            tokenOffset = _next(rndSource);

            // token id is fetched from the random offset
            tokenId = _tokenOffset2TokenId(tokenOffset);

            _mint(to, tokenId, 1, "");
        }

        _store(rndSource);

       // ...
    }
}

https://github.com/buildship-dev/nft-contracts/blob/6a133c70e17297643da213f5bbf0e954aa224d86/contracts/MetaverseBaseNFT_ERC1155.sol#L449

ARR4N commented 1 year ago

Sorry @caffeinum, I don't know how I missed these. I'm sure it's too late, but in general yes that's how it works although I still recommend not storing the value (I'm considering deprecating that anyway).

caffeinum commented 1 year ago

@aschlosberg it was late, yeah, we've released that already! https://etherscan.io/address/0xde95471123ce8bd81ad8e7ba553e019da110b654#code

it worked out nice, because of the low gas it was one of the only moments in history where you could have had random ERC1155 mint! But we still got a pushback from a community because the gas cost to mint was 500k+ per token

I needed to store the value to make sure I predictably generate all of IDs, without leaving any of them out

ARR4N commented 1 year ago

I've set up phone notifications so I shouldn't miss tags again!

I'm glad it worked out well! We've also had issues with the high gas and @cxkoda has since implemented a MWC equivalent that's good for "fun" things that don't require a CSPRNG.