A decentralized Verifiable Random Function (VRF) implementation for Ethereum, providing cryptographically secure and verifiable random number generation for smart contracts.
ECVRF is a production-ready VRF solution that combines:
- Verifiable Randomness: Cryptographically provable random number generation
- Smart Contract Integration: Seamless integration with Solidity contracts
- Off-chain Worker: Automated VRF request processing and fulfillment
- Dual Framework Support: Built with both Hardhat and Foundry
- π Cryptographic Security: ECVRF-based provable randomness
- π― Request-Response Model: Consumer contracts request, coordinator fulfills
- β‘ Automated Fulfillment: Off-chain worker listens and processes requests
- π Subscription Model: Efficient gas management via VRFCoordinatorV2
- π οΈ Developer Friendly: Comprehensive testing and deployment scripts
- π TypeScript Integration: Type-safe integration services
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β VRF Consumer ββββββββββΆβ VRF Coordinator βββββββββββ VRF Worker β
β (Your Dapp) β β (On-chain) β β (Off-chain) β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β β β
β 1. requestRandomWords() β β
βββββββββββββββββββββββββββββΆβ β
β β 2. RandomWordsRequested β
β βββββββββββββββββββββββββββββββΆβ
β β β
β β 3. fulfillRandomWords() β
β ββββββββββββββββββββββββββββββββ€
β 4. rawFulfillRandomWords() β β
ββββββββββββββββββββββββββββββ€ β
- Node.js >= 18.x
- Yarn or npm
- Foundry (optional, for Forge commands)
# Clone the repository
git clone https://github.com/yourusername/ecvrf.git
cd ecvrf
# Install dependencies
yarn install
# Compile contracts
yarn compileCreate a .env file in the root directory:
# Network Configuration
TESTNET_RPC_URL=https://your-testnet-rpc-url
MAINNET_RPC_URL=https://your-mainnet-rpc-url
# Private Keys
DEPLOYER_PRIVATE_KEY=your_deployer_private_key
ORACLE_PRIVATE_KEY=your_oracle_private_key
# Contract Addresses (populated after deployment)
VRF_COORDINATOR_ADDRESS=
DAPP_CONSUMER_ADDRESS=
# VRF Configuration
VRF_PUBLIC_KEY=
VRF_SECRET_KEY=Deploy the VRF Coordinator and Consumer contracts:
# Deploy to testnet
yarn deploy --testnet
# Deploy to mainnet
yarn deployRun the off-chain worker to process VRF requests:
# Start worker on testnet
yarn start --testnet
# Start worker on mainnet
yarn start
# Development mode with hot reload
yarn dev --testnetSend a randomness request from your consumer contract:
yarn send-random# Run all tests
yarn test
# Run Foundry tests
forge test
# Run with gas reporting
forge test --gas-reportecvrf/
βββ contracts/ # Solidity smart contracts
β βββ VRFCoordinator.sol # Main coordinator contract
β βββ VRFConsumer.sol # Base consumer contract
β βββ KeeperVRFConsumer.sol # Automation-compatible consumer
β βββ core/VRF.sol # Core VRF cryptographic logic
βββ integration-services/ # TypeScript integration layer
β βββ services/
β βββ VRFCoordinator.service.ts
β βββ VRFConsumer.service.ts
βββ jobs/ # Off-chain worker services
β βββ index.ts # Worker entry point
β βββ listeners/vrfListener.ts # Event listener
β βββ handlers/requestRandomness.ts
βββ scripts/ # Deployment and utility scripts
β βββ deploy.ts
β βββ contractDeployment/
βββ utils/ # Cryptographic utilities
β βββ vrfProof.ts # VRF proof generation
β βββ crypto.ts # Cryptographic primitives
β βββ solidityProof.ts # Solidity-compatible proof formatting
βββ tests/ # Test suites
βββ abis/ # Contract ABIs
βββ config/ # Configuration files
| Script | Description |
|---|---|
yarn start |
Start VRF worker (mainnet) |
yarn dev |
Start VRF worker with hot reload |
yarn build |
Compile TypeScript |
yarn compile |
Compile Solidity contracts and export ABIs |
yarn test |
Run Hardhat tests |
yarn deploy |
Deploy contracts |
yarn send-random |
Send a random number request |
yarn lint |
Lint TypeScript files |
yarn prettier |
Format code |
forge build |
Build contracts with Foundry |
forge test |
Run Foundry tests |
# Using Hardhat
yarn compile
# Using Foundry
forge build# Hardhat tests
yarn test
# Foundry tests
forge test
# With verbosity
forge test -vvv# Format TypeScript
yarn prettier
# Format Solidity
forge fmtimport "./VRFConsumer.sol";
contract MyContract is VRFConsumerBaseV2 {
constructor(address vrfCoordinator)
VRFConsumerBaseV2(vrfCoordinator) {}
function requestRandomness() external {
requestRandomWords(
keyHash,
requestConfirmations,
callbackGasLimit,
numWords
);
}
function fulfillRandomWords(
uint256 requestId,
uint256[] memory randomWords
) internal override {
// Use your random numbers here
}
}- Set the VRF Coordinator address
- Configure callback gas limit
- Set number of random words needed
- Deploy your consumer contract
- Request random words
- VRF worker automatically fulfills requests
- Keep private keys secure and never commit them
- Use hardware wallets for mainnet deployments
- Audit callback gas limits to prevent out-of-gas errors
- Validate VRF proofs on-chain before accepting randomness
- Implement access controls for sensitive functions
0xEndale
- Email: endadinh@gmail.com
This project is licensed under the MIT License.
Contributions, issues, and feature requests are welcome!
Built with β€οΈ using Solidity, TypeScript, Hardhat, and Foundry