This repository contains Solidity contracts and tests for handling pre-confirmation commitments and bids. The system uses two separate registries to manage users and providers, where both parties must stake ETH to participate. Commitments are verified and stored in a contract.
This is the core contract that handles pre-confirmation commitments. It uses EIP-712 for typed data hashing and signing. Commitments are stored in a mapping, accessible via their hash.
getBidHash: Generates a hash of the bid based on the transaction hash, bid amount, and block number.getPreConfHash: Generates a hash of the pre-confirmation commitment.recoverAddress: Recovers the signer's address from a message digest and a signature.verifyBid: Verifies a bid's validity by checking the signer's stake and the bid amount.storeCommitment: Stores a valid commitment in the contract.
This is an interface that must be implemented by the user registry contract. It contains methods for registering, staking, and retrieving funds.
registerAndStake: Registers a user and stakes ETH.checkStake: Checks the staked amount for a given user.depositFunds: Deposits additional funds into the contract.retrieveFunds: Retrieves a specific amount of funds for a user and sends them to a provider.
This is an interface that must be implemented by the provider registry contract. It contains methods for registering, depositing funds, slashing, and rewarding.
registerAndStake: Registers a provider and stakes ETH.checkStake: Checks the staked amount for a given provider.depositFunds: Deposits additional funds into the contract.slash: Slashes a specific amount of staked ETH from a provider and sends it to a user.reward: Rewards a specific amount of ETH to a provider.
Note: In both IProviderRegistry and IUserRegistry - some functions are restrictied to be called exclusively by the preconfimration contract.
The tests in this repository perform the following:
- Deployment of the
ProviderRegistryandUserRegistrycontracts. - Registration and staking of users and providers.
- Verification of bid hashes and pre-confirmation commitment hashes.
- Recovery of signer addresses.
- Storage of valid commitments.
To run the tests, use the following command:
npx hardhat testsequenceDiagram
participant User
participant Provider
participant PreConf
participant UserRegistry
participant ProviderRegistry
participant Oracle
User->>UserRegistry: registerAndStake()
activate UserRegistry
UserRegistry-->>User: UserRegistered event
deactivate UserRegistry
Provider->>ProviderRegistry: registerAndStake()
activate ProviderRegistry
ProviderRegistry-->>Provider: ProviderRegistered event
deactivate ProviderRegistry
Provider->>PreConf: storeCommitment()
activate PreConf
PreConf->>ProviderRegistry: checkStake(Provider)
activate ProviderRegistry
ProviderRegistry-->>PreConf: stake
deactivate ProviderRegistry
PreConf->>UserRegistry: checkStake(User)
activate UserRegistry
UserRegistry-->>PreConf: stake
deactivate UserRegistry
PreConf->>PreConf: verifyBidAndCommitment()
PreConf-->>Provider: SignatureVerified event
deactivate PreConf
alt Reward Case
Oracle->>PreConf: initateReward(commitmentHash)
activate PreConf
PreConf->>UserRegistry: retrieveFunds(User, amt, Provider)
activate UserRegistry
UserRegistry-->>PreConf: FundsRetrieved event
deactivate UserRegistry
PreConf-->>Oracle: CommitmentUsed event
deactivate PreConf
else Slashing Case
Oracle->>PreConf: initiateSlash(commitmentHash)
activate PreConf
PreConf->>ProviderRegistry: slash(amt, Provider, User)
activate ProviderRegistry
ProviderRegistry-->>PreConf: FundsSlashed event
deactivate ProviderRegistry
PreConf-->>Oracle: CommitmentUsed event
deactivate PreConf
end
npx hardhat run scripts/deploy.js
forge test
forge coverage