Skip to content

Commit

Permalink
Hardhat test with brc20 contract (#2927)
Browse files Browse the repository at this point in the history
* feat: P-886 integrate hardhat into dynamic assertions, wrap contract in proxy contract so as to get the return value in unit test, added test for BRC20 TokenHoldingAmount.
    Fixed openzipline import with @ import working with remix too

* fix: formatted all contracts

* fix: update ci install script for Dynamic-contract step

* fix: prevent lockfile update because it may cause check-format fail

* fix: remove frozen-lockfile option from pnpm install, ignore pnpm-lock.yaml for prettier

* fix: use hardhat instead of foundry to compile contracts

* fix: use Strings.equal to simplify string compare, add remixd install step to README

* add remix config file to .gitignore

---------

Co-authored-by: higherordertech <higherordertech>
  • Loading branch information
higherordertech authored Jul 25, 2024
1 parent 93c07d8 commit eabb481
Show file tree
Hide file tree
Showing 98 changed files with 7,031 additions and 2,733 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ jobs:
- name: Tee-worker check ts code format
working-directory: ./tee-worker/ts-tests
run: pnpm run check-format

- name: Dynamic-contract install npm deps
working-directory: ./tee-worker/litentry/core/assertion-build/src/dynamic
run: pnpm install

- name: Dynamic-contract check code format
working-directory: ./tee-worker/litentry/core/assertion-build/src/dynamic
run: pnpm run check-format

- name: Fail early
if: failure()
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ update-ts-dep:

# format

.PHONY: fmt ## (cargo, taplo, ts) fmt
fmt: fmt-cargo fmt-taplo fmt-ts
.PHONY: fmt ## (cargo, taplo, ts, solidity) fmt
fmt: fmt-cargo fmt-taplo fmt-ts fmt-contract

.PHONY: fmt-cargo ## cargo fmt
fmt-cargo:
Expand All @@ -208,6 +208,10 @@ fmt-ts:
@cd ts-tests && pnpm install && pnpm run format
@cd tee-worker/ts-tests && pnpm install && pnpm run format

.PHONY: fmt-contract ## contract fmt
fmt-contract:
@cd tee-worker/litentry/core/assertion-build/src/dynamic && pnpm install && pnpm run format

.PHONY: githooks ## install the githooks
githooks:
git config core.hooksPath .githooks
Expand Down
23 changes: 7 additions & 16 deletions tee-worker/cli/lit_ts_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,14 @@ pnpm install
pnpm run build

if [ "$TEST" = "assertion_contracts.test.ts" ]; then
cd /
ls assertion-contracts/
cp -r assertion-contracts /ts-tests/integration-tests/contracts

cd /ts-tests
curl -L https://foundry.paradigm.xyz | bash
source /root/.bashrc
apt install -y git
foundryup

pnpm install
pnpm --filter integration-tests run compile-contracts

else
cd /ts-tests
cd /assertion-contracts
pnpm install

# compile contracts
pnpm compile
ln -s /assertion-contracts/artifacts/contracts /ts-tests/integration-tests/contracts
fi

cd /ts-tests
pnpm install

NODE_ENV=staging pnpm --filter integration-tests run test $TEST
2 changes: 1 addition & 1 deletion tee-worker/docker/lit-assertion-contracts-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
- ../ts-tests:/ts-tests
- ../client-api:/client-api
- ../cli:/usr/local/worker-cli
- ../litentry/core/assertion-build/src/dynamic/contracts:/assertion-contracts
- ../litentry/core/assertion-build/src/dynamic:/assertion-contracts

build:
context: ..
Expand Down
24 changes: 22 additions & 2 deletions tee-worker/litentry/core/assertion-build/src/dynamic/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
.deps
node_modules
.env

# Hardhat files
cache
artifacts
.prettierrc.json

# TypeChain files
typechain
typechain-types

# solidity-coverage files
coverage
coverage.json

# Hardhat Ignition default folder for deployments against a local node
ignition/deployments/chain-31337

.deps
!*.sol

# Remix
compiler_config.json
remix-compiler.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
artifacts
cache
node_modules
.env
.idea
typechain-types
coverage
pnpm-lock.yaml
22 changes: 22 additions & 0 deletions tee-worker/litentry/core/assertion-build/src/dynamic/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"plugins": ["prettier-plugin-solidity"],
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true,
"useTabs": false,
"printWidth": 80,
"overrides": [
{
"files": "*.sol",
"options": {
"parser": "solidity-parse",
"printWidth": 80,
"tabWidth": 4,
"useTabs": false,
"singleQuote": false,
"bracketSpacing": true
}
}
]
}

This file was deleted.

59 changes: 59 additions & 0 deletions tee-worker/litentry/core/assertion-build/src/dynamic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
## Description

Dynamic VC assertion contract written by solidity, using [Hardhat](https://hardhat.org) for compile and test.

## Environment setup

- Install [nvm](https://github.com/nvm-sh/nvm)
- Inside the repository, run `nvm use` to set the correct Node version.
- If the version is not installed, run `nvm install`.

## Installation

```shell
nvm use
corepack enable pnpm
pnpm install
```

## Usage

### Compile

1. Using hardhat.

```shell
pnpm compile
```

After compiled, the contract bytecode will generate in file `artifacts/contracts/**/{contractName}.sol/{contractName}.json`, e.g. the bytecode of A1 is in the file `artifacts/contracts/A1.sol/A1.json`.

2. Using [Remix IDE](https://remix.ethereum.org).

Should use the `dynamic` as your project root path in Remix IDE as below:

```shell
remixd -s your_repo_path/tee-worker/litentry/core/assertion-build/src/dynamic --remix-ide https://remix.ethereum.org
```

If you have not install remixd before, rub below script to install it.

```shell
npm install -g @remix-project/remixd
```

### Testing

- Test all: `pnpm test`.

```shell
pnpm test
```

- Test single file: `pnpm test {testFilePath}`.

Example:

```shell
pnpm test tests/token-holding-amount.ts
```
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

pragma solidity ^0.8.8;

import "./openzeppelin/Strings.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "./libraries/AssertionLogic.sol";
import "./libraries/Http.sol";
import "./libraries/Identities.sol";
Expand Down Expand Up @@ -123,22 +123,18 @@ contract A6 is DynamicAssertion {
return (description, assertion_type, assertions, schema_url, result);
}

function prepareHeaders(string memory apiKey)
private
pure
returns (HttpHeader[] memory)
{
function prepareHeaders(
string memory apiKey
) private pure returns (HttpHeader[] memory) {
HttpHeader[] memory headers = new HttpHeader[](1);
// we expect first secret to be twitter api key
headers[0] = HttpHeader("authorization", prepareAuthHeader(apiKey));
return headers;
}

function prepareAuthHeader(string memory apiKey)
private
pure
returns (string memory)
{
function prepareAuthHeader(
string memory apiKey
) private pure returns (string memory) {
return string(abi.encodePacked("Bearer ", apiKey));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2020-2024 Trust Computing GmbH.
// This file is part of Litentry.
//
// Litentry is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Litentry is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.

// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity ^0.8.8;

import "./libraries/Identities.sol";

interface IDynamicAssertion {
function execute(
Identity[] memory identities,
string[] memory secrets,
bytes memory params
)
external
returns (
string memory description,
string memory assertionType,
string[] memory assertions,
string memory schemaUrl,
bool result
);
}

// This proxy is for test purpose.
contract ProxyDynamicAssertion {
address private target;

event DynamicAssertionGenerated(
string description,
string assertionType,
string[] assertions,
string schemaUrl,
bool result
);

constructor(address _target) {
target = _target;
}

function execute(
Identity[] memory identities,
string[] memory secrets,
bytes memory params
)
public
returns (
string memory description,
string memory assertionType,
string[] memory assertions,
string memory schemaUrl,
bool result
)
{
IDynamicAssertion dynamicAssertion = IDynamicAssertion(target);
(
description,
assertionType,
assertions,
schemaUrl,
result
) = dynamicAssertion.execute(identities, secrets, params);
emit DynamicAssertionGenerated(
description,
assertionType,
assertions,
schemaUrl,
result
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,9 @@ library AssertionLogic {
return cc;
}

function toString(CompositeCondition memory cc)
internal
pure
returns (string memory)
{
function toString(
CompositeCondition memory cc
) internal pure returns (string memory) {
string memory result = "{";

if (cc.conditions.length > 0) {
Expand All @@ -100,11 +98,9 @@ library AssertionLogic {
return result;
}

function toString(Condition memory condition)
internal
pure
returns (string memory)
{
function toString(
Condition memory condition
) internal pure returns (string memory) {
return
string(
abi.encodePacked(
Expand Down
Loading

0 comments on commit eabb481

Please sign in to comment.