Skip to content

Commit

Permalink
Nix flake to generate contract abi bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
freesig committed Oct 4, 2022
1 parent 6ddf46a commit b6a8916
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 1 deletion.
21 changes: 20 additions & 1 deletion fuel-relayer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,23 @@ The relayer background task is a simple loop that polls the fuel database and DA
If the state determines that the relayer is out of sync with the DA layer then log messages from the DA contract are downloaded and written to the database.
The range of blocks is `(last_downloaded_height + 1)..=current_finalized_height`.

Logs are paginated into sets of blocks to avoid overloading a single rpc call.
Logs are paginated into sets of blocks to avoid overloading a single rpc call.

## Contract Flake
The `flake.nix` in this repo has a tool to fetch, compile and generate the abi for the fuel contracts.
While it is not mandatory to use nix to do this the advantage is the exact versions of the contracts that were used to generate the abi files is pinned in the `flake.lock` file.

### Usage
To generate the api files run the following from the `fuel-relayer` directory.
```bash
nix run .#generate-abi-json abi
```
To update the version of the contracts that is used run:
```bash
nix flake update
nix run .#generate-abi-json abi
```
You can see the versions that are pinned by running:
```bash
nix flake info
```
129 changes: 129 additions & 0 deletions fuel-relayer/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions fuel-relayer/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
description = ''
A Nix flake for compiling and extracting the fuel contract abi.
'';

inputs = {
open-zeppelin = {
url = "github:OpenZeppelin/openzeppelin-contracts";
flake = false;
};
fuel-v1-contracts = {
url = "github:FuelLabs/fuel-merkle-sol";
flake = false;
};
fuel-v2-contracts.url = "git+ssh://git@github.com/fuellabs/fuel-v2-contracts/";
fuel-v2-contracts.flake = false;
nixpkgs = {
url = "github:NixOS/nixpkgs/nixos-unstable";
};
dapptools = {
url = "github:dapphub/dapptools/master";
flake = false;
};
npm.url = "github:/serokell/nix-npm-buildpackage";
};

outputs = inputs: let
system = "x86_64-darwin";
dapp = import inputs.dapptools {inherit system;};
solc = dapp.runCommand "solc" {} "mkdir -p $out/bin; ln -s ${dapp.solc-static-versions.solc_0_8_9}/bin/solc-0.8.9 $out/bin/solc";
overlays = [inputs.npm.overlays.default];
pkgs = import inputs.nixpkgs {inherit overlays system;};
in {
packages."x86_64-darwin" = {
add-folder = pkgs.linkFarm "contracts-with-layer" [
{
name = "merkle-sol";
path = inputs.fuel-v1-contracts;
}
];
build-abi-json =
pkgs.runCommand
"build-abi-json"
{}
''
${solc}/bin/solc --abi --pretty-json ${inputs.fuel-v2-contracts}/contracts/sidechain/FuelSidechain.sol @openzeppelin=${inputs.open-zeppelin} @fuel-contracts=${inputs.self.packages."${system}".add-folder} -o $out/build
'';
generate-abi-json = pkgs.writeShellApplication {
name = "generate-abi-0json";
runtimeInputs = [solc pkgs.jq inputs.self.packages."${system}".build-abi-json];
text = ''
if [[ -z $1 ]]
then
echo "Invalid input. Must provide output directoy" >&2; exit 1;
fi
declare -a arr=("FuelMessagePortal")
for i in "''${arr[@]}"
do
(cat ${inputs.self.packages."${system}".build-abi-json}/build/"$i".abi) | jq '.' > "$1"/"$i".json
done
'';
};


};
formatter.x86_64-darwin = pkgs.alejandra;
};
}

0 comments on commit b6a8916

Please sign in to comment.