Skip to content

Commit 4641c36

Browse files
authored
Chore(entropy) update v2 example (#58)
* chore(entropy) Updated contract with v2 interface * minimal app changes
1 parent b6be282 commit 4641c36

File tree

6 files changed

+117
-21
lines changed

6 files changed

+117
-21
lines changed

entropy/coin_flip/app/src/coin_flip_abi.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,54 @@ export const ICoinFlipAbi = [
2323
"outputs": [],
2424
"stateMutability": "nonpayable"
2525
},
26+
{
27+
"type": "function",
28+
"name": "getDefaultProviderGasLimit",
29+
"inputs": [],
30+
"outputs": [{ "name": "", "type": "uint32", "internalType": "uint32" }],
31+
"stateMutability": "view"
32+
},
2633
{
2734
"type": "function",
2835
"name": "getFlipFee",
2936
"inputs": [],
30-
"outputs": [
31-
{ "name": "fee", "type": "uint256", "internalType": "uint256" }
32-
],
37+
"outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }],
3338
"stateMutability": "view"
3439
},
3540
{
3641
"type": "function",
3742
"name": "requestFlip",
43+
"inputs": [],
44+
"outputs": [],
45+
"stateMutability": "payable"
46+
},
47+
{
48+
"type": "function",
49+
"name": "requestFlipWithCustomGasLimit",
50+
"inputs": [
51+
{ "name": "gasLimit", "type": "uint32", "internalType": "uint32" }
52+
],
53+
"outputs": [],
54+
"stateMutability": "payable"
55+
},
56+
{
57+
"type": "function",
58+
"name": "requestFlipWithCustomProviderAndGasLimit",
59+
"inputs": [
60+
{ "name": "provider", "type": "address", "internalType": "address" },
61+
{ "name": "gasLimit", "type": "uint32", "internalType": "uint32" }
62+
],
63+
"outputs": [],
64+
"stateMutability": "payable"
65+
},
66+
{
67+
"type": "function",
68+
"name": "requestFlipWithCustomProviderAndGasLimitAndUserContribution",
3869
"inputs": [
70+
{ "name": "provider", "type": "address", "internalType": "address" },
71+
{ "name": "gasLimit", "type": "uint32", "internalType": "uint32" },
3972
{
40-
"name": "userRandomNumber",
73+
"name": "userContribution",
4174
"type": "bytes32",
4275
"internalType": "bytes32"
4376
}

entropy/coin_flip/app/src/flip_coin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ async function main() {
8787

8888
console.log("\n3. Sending request to flip coin...");
8989

90-
const flipTxHash = await coinFlipContract.write.requestFlip([randomNumber], {
90+
const flipTxHash = await coinFlipContract.write.requestFlip({
9191
value: flipFee,
9292
});
9393
console.log(`Transaction Hash: ${flipTxHash}`);

entropy/coin_flip/contract/package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"@pythnetwork/entropy-sdk-solidity": "^2.0.0"
4+
}
5+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
@pythnetwork/entropy-sdk-solidity/=node_modules/@pythnetwork/entropy-sdk-solidity
12
ds-test/=lib/forge-std/lib/ds-test/src/
23
forge-std/=lib/forge-std/src/
3-
entropy-sdk-solidity/=../../../entropy_sdk/solidity/

entropy/coin_flip/contract/src/CoinFlip.sol

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
pragma solidity ^0.8.0;
33

44
// Import the entropy SDK in order to interact with the entropy contracts
5-
import "entropy-sdk-solidity/IEntropy.sol";
6-
import "entropy-sdk-solidity/IEntropyConsumer.sol";
5+
import "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
6+
import "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol";
7+
// Import the EntropyStructsV2 contract to get the ProviderInfo struct
8+
import "@pythnetwork/entropy-sdk-solidity/EntropyStructsV2.sol";
79

810
library CoinFlipErrors {
911
error IncorrectSender();
@@ -30,39 +32,73 @@ contract CoinFlip is IEntropyConsumer {
3032
// and a specific entropy provider to use for requests. Each provider commits to a sequence of random numbers.
3133
// Providers are then responsible for fulfilling a request on chain by revealing their random number.
3234
// Users should choose a reliable provider who they trust to uphold these commitments.
33-
// (For the moment, the only available provider is 0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344)
34-
IEntropy private entropy;
35+
IEntropyV2 private entropy;
3536
address private entropyProvider;
3637

3738
constructor(address _entropy, address _entropyProvider) {
38-
entropy = IEntropy(_entropy);
39+
entropy = IEntropyV2(_entropy);
3940
entropyProvider = _entropyProvider;
4041
}
4142

42-
// Request to flip a coin. The caller should generate and pass in a random number when calling this method.
43-
function requestFlip(bytes32 userRandomNumber) external payable {
43+
// Request to flip a coin.
44+
function requestFlip() external payable {
4445
// The entropy protocol requires the caller to pay a fee (in native gas tokens) per requested random number.
4546
// This fee can either be paid by the contract itself or passed on to the end user.
4647
// This implementation of the requestFlip method passes on the fee to the end user.
47-
uint256 fee = entropy.getFee(entropyProvider);
48+
uint256 fee = entropy.getFeeV2();
4849
if (msg.value < fee) {
4950
revert CoinFlipErrors.InsufficientFee();
5051
}
5152

5253
// Request the random number from the Entropy protocol. The call returns a sequence number that uniquely
5354
// identifies the generated random number. Callers can use this sequence number to match which request
5455
// is being revealed in the next stage of the protocol.
55-
uint64 sequenceNumber = entropy.requestWithCallback{value: fee}(
56-
entropyProvider,
57-
userRandomNumber
58-
);
56+
// This requestV2 function will trust the provider to draw a random number.
57+
uint64 sequenceNumber = entropy.requestV2{value: fee}();
5958

6059
emit FlipRequest(sequenceNumber);
6160
}
6261

63-
// Get the fee to flip a coin. See the comment above about fees.
64-
function getFlipFee() public view returns (uint256 fee) {
65-
fee = entropy.getFee(entropyProvider);
62+
// Request to flip a coin with a custom gas limit.
63+
function requestFlipWithCustomGasLimit(uint32 gasLimit) external payable {
64+
uint256 fee = entropy.getFeeV2(gasLimit);
65+
if (msg.value < fee) {
66+
revert CoinFlipErrors.InsufficientFee();
67+
}
68+
69+
uint64 sequenceNumber = entropy.requestV2{value: fee}(gasLimit);
70+
71+
emit FlipRequest(sequenceNumber);
72+
}
73+
74+
// Request to flip a coin with a custom provider and custom gas limit.
75+
function requestFlipWithCustomProviderAndGasLimit(address provider, uint32 gasLimit) external payable {
76+
uint256 fee = entropy.getFeeV2(provider, gasLimit);
77+
if (msg.value < fee) {
78+
revert CoinFlipErrors.InsufficientFee();
79+
}
80+
81+
uint64 sequenceNumber = entropy.requestV2{value: fee}(provider, gasLimit);
82+
83+
emit FlipRequest(sequenceNumber);
84+
}
85+
86+
// Request to flip a coin with a custom provider and custom gas limit and userContribution / Random Number.
87+
function requestFlipWithCustomProviderAndGasLimitAndUserContribution(address provider, uint32 gasLimit, bytes32 userContribution) external payable {
88+
uint256 fee = entropy.getFeeV2(provider, gasLimit);
89+
if (msg.value < fee) {
90+
revert CoinFlipErrors.InsufficientFee();
91+
}
92+
93+
uint64 sequenceNumber = entropy.requestV2{value: fee}(provider, userContribution, gasLimit);
94+
95+
emit FlipRequest(sequenceNumber);
96+
}
97+
98+
// Get the default gas limit for the default provider.
99+
function getDefaultProviderGasLimit() public view returns (uint32) {
100+
EntropyStructsV2.ProviderInfo memory providerInfo = entropy.getProviderInfoV2(entropy.getDefaultProvider());
101+
return providerInfo.defaultGasLimit;
66102
}
67103

68104
// This method is required by the IEntropyConsumer interface.
@@ -84,5 +120,9 @@ contract CoinFlip is IEntropyConsumer {
84120
return address(entropy);
85121
}
86122

123+
function getFlipFee() public view returns (uint256) {
124+
return entropy.getFeeV2();
125+
}
126+
87127
receive() external payable {}
88128
}

0 commit comments

Comments
 (0)