iscutils

package
v1.2.0-alpha.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

@iota/iscutils

The iscutils package contains various utility methods to simplify the interaction with the IOTA Magic contract. This utility library is designed to be used with the @iota/iscmagic npm package.

The Magic contract, an EVM contract, is deployed by default on every ISC chain. It has several methods, accessed via different interfaces like ISCSandbox, ISCAccounts, ISCUtil and more. These can be utilized within any Solidity contract by importing the @iota/iscmagic library.

For further information on the Magic contract, check the Wiki.

Installing @iota/iscutils contracts

The @iota/iscutils contracts are installable via NPM with

npm install @iota/iscutils

After installing @iota/iscutils you can use the functions by importing them as you normally would.

Utilities

PRNG

A pseudorandom number generator is available by importing prng.sol. Its required seeding process simply depends on a random 32-byte value, and each invocation of the getRandomNumber() method will generate an algorithmically determined number, based on the initial seed and numbers generated prior. Its deterministic nature makes it ideal for accomplishing predictable outcomes during testing.

The recommended source for the seed value is attained via the getEntropy() method from the ISC sandbox (via the magic contract.) This call yields random bytes drawn from the existing consensus state. Although it is distinctive for each block, it remains the same between immediate calls processed within the same block. Therefore, in order to introduce randomness within a block, this utility comes into play, providing pseudorandom values throughout the block process, buffering the immutable nature of getEntropy() within the same block.

Example PRNG usage
pragma solidity >=0.8.5;

import "@iota/iscmagic/ISC.sol";
import "@iota/iscutils/prng.sol";

contract MyEVMContract {
    using PRNG for PRNG.PRNGState;

    event PseudoRNG(uint256 value);
    event PseudoRNGHash(bytes32 value);
    
    PRNG.PRNGState private prngState;

    function emitValue() public {
        bytes32 e = ISC.sandbox.getEntropy();
        prngState.seed(e);
        bytes32 randomHash = prngState.generateRandomHash();
        emit PseudoRNGHash(randomHash);
        uint256 random = prngState.generateRandomNumber();
        emit PseudoRNG(random);
        uint256 randomRange = prngState.generateRandomNumberInRange(111, 1111111);
        emit PseudoRNG(randomRange);
    }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//go:embed PRNGTest.abi
	PRNGTestContractABI string
	//go:embed PRNGTest.bin
	PRNGTestContractBytecodeHex string
	PRNGTestContractBytecode    = common.FromHex(strings.TrimSpace(PRNGTestContractBytecodeHex))
	//deployed bytecode and runtime bytecode are different, see: https://ethereum.stackexchange.com/questions/13086/whats-the-difference-between-solcs-bin-bytecode-versus-bin-runtime
	//go:embed PRNGTest.bin-runtime
	PRNGTestContractRuntimeBytecodeHex string
	PRNGTestContractRuntimeBytecode    = common.FromHex(strings.TrimSpace(PRNGTestContractRuntimeBytecodeHex))
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL