Chainlink Functions

Dashboard: Chainlink Functions

Doc: https://docs.chain.link/chainlink-functions

Usage in Fury Racing:

Chainlink functions are used twice within Fury Racing. It is interesting to note that we are writing JavaScript code inside a solidity contract.

  • The first function call is used to query the race results API, which will return the time for 10 laps per player. The smallest time wins the race. This calculation is handled by the backend. Using Chainlink Functions allows the contracts to access the results directly and in a decentralized manner, and to conclude the race accordingly.

```solidity
function getRaceResult() internal pure returns (string memory) {
        return (
            "const attributes = args[0].toString();"
            "const raceResultRequest = await Functions.makeHttpRequest({"
            "  url: `https://...amazonaws.com/dev/api/races/data/${attributes}`,"
            "});" 
            "if (raceResultRequest.error) {" 
            "  throw new Error('Request failed');" 
            "}"
            "const combinedResult = BigInt(raceResultRequest.data.combinedResult);"
            "return Functions.encodeUint256(combinedResult);"
        );
    }
```
  • The function call is used to update the weather for each circuit (Currently, only the weather for Monaco is handled). To make is more interesting, this function is called every hour to mimic a weather in real-time. This is accomplished with the help of Chainlink Automation (see next paragraph). To put it otherwise, Chainlink Automation is in charge of calling a Chainlink Functions every hour.

```solidity
function getWeatherScore() internal pure returns (string memory) {
    return (
        "const response = await Functions.makeHttpRequest({"
        "  url: `https://...amazonaws.com/dev/api/races/weather`,"
        "});"
        "if (response.error) {"
        "  throw new Error('Request failed');"
        "}"
        "return Functions.encodeUint256(response.data.weatherScore);"
    );
```

Last updated