|
|
2 周之前 | |
|---|---|---|
| .. | ||
| abis | f9f8dd9db6 feat(entropy): Minor items to prep for testnet deployments (#2673) | 6 月之前 |
| .prettierignore | 6efbe89542 chore: updated prettier ignores to prevent bikeshedding | 2 周之前 |
| EntropyErrors.sol | 9873e61306 feat(entropy): Add on-chain gas limits and mark out-of-gas failures on-chain (#2559) | 7 月之前 |
| EntropyEvents.sol | a4333da2a6 fix(entropy): Fix Event definitions to maintain backward compatibility (#2616) | 7 月之前 |
| EntropyEventsV2.sol | f9f8dd9db6 feat(entropy): Minor items to prep for testnet deployments (#2673) | 6 月之前 |
| EntropyStatusConstants.sol | f841dfc14e feat(entropy): Add callback failure states to the contract (#2546) | 7 月之前 |
| EntropyStructs.sol | a4333da2a6 fix(entropy): Fix Event definitions to maintain backward compatibility (#2616) | 7 月之前 |
| EntropyStructsV2.sol | a4333da2a6 fix(entropy): Fix Event definitions to maintain backward compatibility (#2616) | 7 月之前 |
| IEntropy.sol | da2fd1405f format fix | 2 月之前 |
| IEntropyConsumer.sol | d821e01109 feat(entropy-v2): add provider parameter to entropy callback (#1389) | 1 年之前 |
| IEntropyV2.sol | c02f495ce6 requestv2 comment fix | 3 月之前 |
| MockEntropy.sol | 9e519a995a feat(entropy-sdk): add MockEntropy contract for local testing (#3121) | 1 月之前 |
| PRNG.sol | e6ae23b7de PRNG Library with Pyth Entropy Integration (#1733) | 1 年之前 |
| README.md | 1b5195aa0a fix: run turbo fix | 8 月之前 |
| package.json | 31c364cf41 feat: performed a minor bump all packages, since all were changed in the big dual package update | 2 周之前 |
| prettier.config.js | b1dc4c27f5 chore: move prettier to individual packages & upgrade prettier | 8 月之前 |
| turbo.json | e08052c2c4 feat: set up turborepo | 1 年之前 |
The Pyth Entropy Solidity SDK allows you to generate secure random numbers on the blockchain by interacting with the Pyth Entropy protocol. This SDK can be used for any application that requires random numbers, such as NFT mints, gaming, and more.
###Truffle/Hardhat
If you are using Truffle or Hardhat, simply install the NPM package:
npm install @pythnetwork/entropy-sdk-solidity
###Foundry
If you are using Foundry, you will need to create an NPM project if you don't already have one. From the root directory of your project, run:
npm init -y
npm install @pythnetwork/entropy-sdk-solidity
Then add the following line to your remappings.txt file:
@pythnetwork/entropy-sdk-solidity/=node_modules/@pythnetwork/entropy-sdk-solidity
To use the SDK, you need the address of an Entropy contract on your blockchain and a randomness provider. You can find current deployments on this page.
Choose one of the networks and instantiate an IEntropy contract in your solidity contract:
IEntropy entropy = IEntropy(<address>);
To generate a random number, follow these steps.
Generate a 32-byte random number on the client side, then hash it with keccak256 to create a commitment. You can do this with typescript and web3.js as follows:
const randomNumber = web3.utils.randomHex(32);
const commitment = web3.utils.keccak256(randomNumber);
Invoke the request method of the IEntropy contract.
The request method requires paying a fee in native gas tokens which is configured per-provider.
Use the getFee method to calculate the fee and send it as the value of the request call:
uint fee = entropy.getFee(provider);
uint64 sequenceNumber = entropy.request{value: fee}(provider, commitment, true);
This method returns a sequence number. Store this sequence number for use in later steps.
If you are invoking this off-chain, the method also emits a PythRandomEvents.Requested event that contains the sequence number in it.
Fetch the provider's random number from them.
For the provider 0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344 you can query the webservice at https://fortuna-staging.dourolabs.app :
await axios.get(
`https://fortuna-staging.dourolabs.app/v1/chains/${chainName}/revelations/${sequenceNumber}`,
);
This method returns a JSON object containing the provider's random number.
Invoke the reveal method on the IEntropy contract:
bytes32 randomNumber = entropy.reveal(
provider,
sequenceNumber,
randomNumber,
providerRandomNumber
)
This method will combine the user and provider's random numbers, along with the blockhash, to construct the final secure random number.
The Coin Flip example demonstrates how to build a smart contract that interacts with Pyth Entropy as well as a typescript client for that application.
The PRNG (Pseudorandom Number Generation) Contract is designed to work seamlessly with Pyth Entropy.
randUint() -> uint256: Generate a random uint256 valuerandUint64() -> uint64: Generate a random uint64 valuerandUintRange(uint256 min, uint256 max) -> uint256: Generate a random integer within a specified rangerandomBytes(uint256 length) -> bytes: Generate a sequence of random bytesrandomPermutation(uint256 length) -> uint256[]: Generate a random permutation of a sequenceTo use the PRNG contract in your project:
Create a contract that inherits from PRNG and uses its internal functions with a seed from Pyth Entropy:
contract MyContract is PRNG {
constructor(bytes32 _seed) {
PRNG(_seed);
}
}
Use the contract functions to generate random numbers:
bytes32 pythEntropySeed = ...; // Get this from Pyth Entropy
setSeed(pythEntropySeed)
uint256 randomNumber = randUint();
uint64 randomSmallNumber = randUint64();
uint256 randomInRange = randUintRange(1, 100);