Contract 6: ChainLink Functions to fetch RW Data
function requestFitnessData(uint256 tokenId) external {
require(block.timestamp >= lastUpdateTime[tokenId] + 1 hours, "Update too frequent");
// In production, this would make a Chainlink Functions request
// to fetch data from fitness APIs like Fitbit, Apple Health, etc.
bytes32 requestId = keccak256(abi.encodePacked(tokenId, block.timestamp));
requestToTokenId[requestId] = tokenId;
emit OracleRequest(requestId, tokenId, "fitness");
}
What's happening here?
Prevents spam by limiting updates to once per hour
In real implementation, would call external APIs (Fitbit, Apple Health)
Creates unique request ID and emits event for off-chain oracle to process
function fulfillFitnessData(bytes32 requestId, uint256 steps) external onlyRole(ORACLE_ROLE) {
uint256 tokenId = requestToTokenId[requestId];
gameEngine.updateSteps(tokenId, steps);
lastUpdateTime[tokenId] = block.timestamp;
emit OracleResponse(requestId, tokenId, abi.encode(steps));
}
What's happening here?
Oracle calls this function with real-world data
Updates the game with actual step count
Records timestamp to enforce rate limiting
Last updated