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

chore: add vrf fallback w3f #95

Merged
merged 9 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions contracts/GelatoVRFConsumerBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ abstract contract GelatoVRFConsumerBase is IGelatoVRFConsumer {
_fulfillRandomness(randomness, requestId, extraData);
requestPending[requestId] = false;
}

delete requestedHash[requestId];
}

/// @notice Computes and returns the round number of drand to request randomness from.
Expand Down
7 changes: 6 additions & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const config: HardhatUserConfig = {
w3f: {
rootDir: "./web3-functions",
debug: false,
networks: ["polygon", "mumbai", "goerli", "baseGoerli"], //(multiChainProvider) injects provider for these networks
networks: ["polygon", "mumbai", "goerli", "baseGoerli", "blastsepolia"], //(multiChainProvider) injects provider for these networks
},

namedAccounts: {
Expand Down Expand Up @@ -143,6 +143,11 @@ const config: HardhatUserConfig = {
url: `https://polygon-mumbai.g.alchemy.com/v2/${ALCHEMY_ID}`,
accounts: PRIVATE_KEY ? [PRIVATE_KEY] : [],
},
blastsepolia: {
chainId: 168587773,
url: `https://sepolia.blast.io`,
accounts: PRIVATE_KEY ? [PRIVATE_KEY] : [],
},
},

deterministicDeployment,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"dependencies": {
"@chainlink/contracts": "^0.6.1",
"@gelatonetwork/automate-sdk": "^2.19.2-beta",
"@gelatonetwork/web3-functions-sdk": "^2.1.7",
"@gelatonetwork/web3-functions-sdk": "^2.2.1",
"@openzeppelin/contracts": "4.9.2",
"@safe-global/safe-singleton-factory": "^1.0.14",
"drand-client": "^1.2.0",
Expand Down
27 changes: 26 additions & 1 deletion src/drand_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
HttpCachingChain,
HttpChainClient,
fetchBeacon,
roundTime,
} from "drand-client";

import { quicknet } from "./drand_info";
Expand Down Expand Up @@ -68,14 +69,34 @@ async function fetchDrandResponseWithCaching(round: number) {
return { round, randomness };
}

async function fetchBeaconWithTimeout(
client: HttpChainClient,
round: number,
timeout: number = 5_000
) {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => reject(new Error(`Timeout`)), timeout);
fetchBeacon(client, round)
.then((response) => {
clearTimeout(timeoutId);
resolve(response);
})
.catch((err) => {
clearTimeout(timeoutId);
reject(err);
});
});
}

async function fetchDrandResponse(round: number) {
console.log(`Fetching randomness from round ${round}`);
const errors = [];

for (const client of clientCache.getClients()) {
try {
return await fetchBeacon(client, round);
return await fetchBeaconWithTimeout(client, round);
} catch (err) {
console.error(`Failed to fetch randomness: ${(err as Error).message}`);
errors.push(err);
}
}
Expand All @@ -95,3 +116,7 @@ export async function getNextRandomness(round: number) {
}
}
}

export function getRoundTime(round: number) {
return roundTime(quicknet, round);
}
77 changes: 77 additions & 0 deletions web3-functions/vrf-fallback/abis/GelatoVRFConsumerBase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "round",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bytes",
"name": "data",
"type": "bytes"
}
],
"name": "RequestedRandomness",
"type": "event"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "randomness",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "dataWithRound",
"type": "bytes"
}
],
"name": "fulfillRandomness",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "requestPending",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "requestedHash",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "view",
"type": "function"
}
]
Loading
Loading