|
| 1 | +--- |
| 2 | +title: Web2Json for Custom API |
| 3 | +authors: [nikerzetic, filipkoprivec] |
| 4 | +description: Retrieve arbitrary Web2 data. |
| 5 | +tags: [intermediate, ethereum, fdc, hardhat] |
| 6 | +keywords: [ethereum, flare-data-connector, evm, flare-network] |
| 7 | +sidebar_position: 12 |
| 8 | +--- |
| 9 | + |
| 10 | +The [Web2Json guide](/fdc/guides/foundry/web-2-json) demonstrates how the Flare Data Connector can be used to fetch Web2 and store it on the chain. |
| 11 | +The code for this and other examples is available within the [Flare Hardhat starter](https://github.com/flare-foundation/flare-foundry-starter) repository. |
| 12 | +In this guide, we will see how the `Web2Json` example script within the Flare Hardhat starter can be modified to work with custom data and custom contracts. |
| 13 | +That way, the example code can serve as the base building block for a custom project. |
| 14 | + |
| 15 | +## Necessary modifications |
| 16 | + |
| 17 | +In order to run on custom data, the example code needs to be modified in four places only. |
| 18 | +Those are: |
| 19 | + |
| 20 | +1. The contract within the `src/fdcExample/Web2Json.sol` file. |
| 21 | +2. The attestation request parameters at the top of the `script/fdcExample/Web2Json.s.sol`. |
| 22 | + In particular the parameters: |
| 23 | + |
| 24 | +- `apiUrl` |
| 25 | +- `postProcessJq` |
| 26 | +- `abiSignature` |
| 27 | + |
| 28 | +3. The `DeployContract` script within the `script/fdcExample/Web2Json.s.sol` file. |
| 29 | +4. The `InteractWithContract` function within the `script/fdcExample/Web2Json.s.sol` file. |
| 30 | + |
| 31 | +## Contract |
| 32 | + |
| 33 | +The contract within the `src/fdcExample/Web2Json.sol` file should be changed to reflect the project goals. |
| 34 | +It could be omitted entirely and replaced with multiple files. |
| 35 | +The only requirement is that at least one contract - the one interacting with FDC - implements a function that accepts an `IWeb2Json.Proof` struct parameter. |
| 36 | + |
| 37 | +## Attestation request parameters |
| 38 | + |
| 39 | +```solidity title="script/fdcExample/Web2Json.s.sol" |
| 40 | + // Setting request data |
| 41 | + string public apiUrl = "https://swapi.info/api/people/3"; |
| 42 | + string public httpMethod = "GET"; |
| 43 | + string public headers = '{\\"Content-Type\\":\\"text/plain\\"}'; |
| 44 | + string public queryParams = "{}"; |
| 45 | + string public body = "{}"; |
| 46 | + string public postProcessJq = |
| 47 | + '{name: .name, height: .height, mass: .mass, numberOfFilms: .films | length, uid: (.url | split(\\"/\\") | .[-1] | tonumber)}'; |
| 48 | + string public abiSignature = |
| 49 | + '{\\"components\\": [{\\"internalType\\": \\"string\\", \\"name\\": \\"name\\", \\"type\\": \\"string\\"},{\\"internalType\\": \\"uint256\\", \\"name\\": \\"height\\", \\"type\\": \\"uint256\\"},{\\"internalType\\": \\"uint256\\", \\"name\\": \\"mass\\", \\"type\\": \\"uint256\\"},{\\"internalType\\": \\"uint256\\", \\"name\\": \\"numberOfFilms\\", \\"type\\": \\"uint256\\"},{\\"internalType\\": \\"uint256\\", \\"name\\": \\"uid\\", \\"type\\": \\"uint256\\"}],\\"name\\": \\"task\\",\\"type\\": \\"tuple\\"}'; |
| 50 | +``` |
| 51 | + |
| 52 | +The attestation request parameters should describe the new Web2 source. |
| 53 | + |
| 54 | +The `apiUrl` is the URL of the API. |
| 55 | +It can additionally be configured with the `headers`, `queryParams`, and `body` fields. |
| 56 | +A different `httpMethod` can also be selected. |
| 57 | + |
| 58 | +The `postProcessJq` is the jq-filter that will be applied to the JSON data retrieved from the `apiUrl` API. |
| 59 | +It rearranges and modifies the input JSON into a new JSON. |
| 60 | +The `postProcessJq` filter can be workshopped using an online tool, like [The JQ Playground](https://play.jqlang.org). |
| 61 | + |
| 62 | +The `abiSignature` parameter determines how the modified JSON should be represented in Solidity. |
| 63 | +It is the ABI signature of an appropriate struct. |
| 64 | + |
| 65 | +The easiest way to acquire it is to create a `DataTransportObject` struct with the correct fields within some solidity file. |
| 66 | +Then, create a public dummy function that accepts a `DataTransportObject` parameter. |
| 67 | +After running `yarn hardhat compile` or `npx hardhat compile`, the contract artifact will be created within the `artifacts/contracts` directory. |
| 68 | +The dummy function can be searched for within the file of its parent contract, and the `abiSignature` read from its `inputs` field. |
| 69 | + |
| 70 | +## Deploy script |
| 71 | + |
| 72 | +```solidity title="script/fdcExample/Web2Json.s.sol" |
| 73 | +contract DeployContract is Script { |
| 74 | + function run() external { |
| 75 | + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); |
| 76 | + vm.startBroadcast(deployerPrivateKey); |
| 77 | +
|
| 78 | + StarWarsCharacterList characterList = new StarWarsCharacterList(); |
| 79 | + address _address = address(characterList); |
| 80 | +
|
| 81 | + vm.stopBroadcast(); |
| 82 | +
|
| 83 | + Base.writeToFile( |
| 84 | + dirPath, |
| 85 | + string.concat(attestationTypeName, "_address"), |
| 86 | + StringsBase.toHexString(abi.encodePacked(_address)), |
| 87 | + true |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +The `DeployContract` script should be modified to deploy and verify the new contract. |
| 94 | +If the contract is deployed and verified by another script, and thus the script will only interact with an existing contract, the script can be omitted. |
| 95 | + |
| 96 | +## Interact with contract script |
| 97 | + |
| 98 | +```solidity title="script/fdcExample/Web2Json.s.sol" |
| 99 | +contract InteractWithContract is Script { |
| 100 | + function run() external { |
| 101 | + string memory addressString = vm.readLine( |
| 102 | + string.concat(dirPath, attestationTypeName, "_address", ".txt") |
| 103 | + ); |
| 104 | + address _address = vm.parseAddress(addressString); |
| 105 | + string memory proofString = vm.readLine( |
| 106 | + string.concat(dirPath, attestationTypeName, "_proof", ".txt") |
| 107 | + ); |
| 108 | + bytes memory proofBytes = vm.parseBytes(proofString); |
| 109 | + IWeb2Json.Proof memory proof = abi.decode( |
| 110 | + proofBytes, |
| 111 | + (IWeb2Json.Proof) |
| 112 | + ); |
| 113 | + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); |
| 114 | + vm.startBroadcast(deployerPrivateKey); |
| 115 | + IStarWarsCharacterList characterList = IStarWarsCharacterList(_address); |
| 116 | + characterList.addCharacter(proof); |
| 117 | + vm.stopBroadcast(); |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +The `InteractWithContract` script should be modified to interact with the new contract. |
| 123 | +Unless the dApp requires a more intricate interaction with the new contract, only the last few lines should be fundamentally changed. |
| 124 | +Likely, only the parameter type and the contract functions called should change. |
0 commit comments