Skip to content

Commit

Permalink
chore: add vrf fallback w3f (#95)
Browse files Browse the repository at this point in the history
* chore: revert storage after reqeust

* chore: add vrf fallback w3f

* chore: update storages, add logs

* chore: use drand round timestamp for log time

* chore: remove unused file

* chore: rpc calls optimization

* chore: add comments

* chore: VRF Fallback audit review (#96)

* chore: add block tip delay to improve performances

* chore: limit multicall requests

* chore: remove unused logCache

* fix: use string representation for requestId

* chore: add blastepolia config

* chore: add fromBlock to VRF fallback

* fix: max number of log filtering

* chore: pick bounded random request

---------

Co-authored-by: Brandon Chuah <songlin.chuah@gmail.com>

* fix: fetch randomness with timeout (#98)

Co-authored-by: Sylvain Goumy <goumsss@protonmail.com>

---------

Co-authored-by: Goums <goumsss@protonmail.com>
  • Loading branch information
brandonchuah and goums authored Mar 28, 2024
1 parent 0536e6c commit ff2d8eb
Show file tree
Hide file tree
Showing 11 changed files with 592 additions and 7 deletions.
2 changes: 2 additions & 0 deletions contracts/GelatoVRFConsumerBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ abstract contract GelatoVRFConsumerBase is IGelatoVRFConsumer {

delete requestedHash[requestId];
}

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

0 comments on commit ff2d8eb

Please sign in to comment.