Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create MockEntropy.sol #1745

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions target_chains/ethereum/entropy_sdk/solidity/MockEntropy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: Apache 2
pragma solidity ^0.8.0;

interface IEntropyConsumer {
danielstevenberger marked this conversation as resolved.
Show resolved Hide resolved
function _entropyCallback(
uint64 sequenceNumber,
address provider,
bytes32 randomNumber
) external;
}

contract MockEntropy {
Copy link
Contributor

@jayantk jayantk Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should inherit from IEntropy and define all of the methods there, so it's a drop-in replacement for testing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(it's ok to throw an error saying "not implemented" for methods like register which are targeted toward providers though)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please document this mock contract and how to use it

Please also add a unit test in https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ethereum/contracts/forge-test to validate that this mock behaves as expected

uint64 public sequenceNumber;
uint128 public constant FEE = 0.000015 ether;
danielstevenberger marked this conversation as resolved.
Show resolved Hide resolved

function getDefaultProvider() external view returns (address) {
return address(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest requiring the caller to pass a provider address as a constructor argument. Using the address of the deployed contract could cause mixups in the tested code (e.g., it won't catch a bug where someone passes the Entropy contract address as the provider address by mistake).

}

function getFee(address provider) external pure returns (uint128) {
require(provider != address(0), "Invalid provider address");
danielstevenberger marked this conversation as resolved.
Show resolved Hide resolved
return FEE;
}

function requestWithCallback(
address provider,
bytes32 userRandomNumber
) external payable returns (uint64) {
require(provider != address(0), "Invalid provider address");
danielstevenberger marked this conversation as resolved.
Show resolved Hide resolved
require(msg.value >= FEE, "Not enough ether sent for fee");
return sequenceNumber++;
}

function triggerCallback(
uint64 _sequenceNumber,
uint256 _randomNumber,
address _callbackAddress
) external {
bytes32 randomNumberBytes = bytes32(_randomNumber);
danielstevenberger marked this conversation as resolved.
Show resolved Hide resolved
IEntropyConsumer(_callbackAddress)._entropyCallback(
_sequenceNumber,
_callbackAddress,
randomNumberBytes
);
}
}