Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate to vitest and merry in gardenfi/core #31

Merged
merged 21 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Run Tests

on:
push:
branches:
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v4
with:
node-version: 18
registry-url: https://registry.npmjs.org/
- name: Install merry
run: curl https://get.merry.dev | bash
Comment on lines +18 to +19
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

Ensure secure installation of external scripts.

Downloading and executing scripts from external sources can introduce security risks. Consider verifying the integrity of the script by adding checksum verification for the downloaded binary.

  • Add a step to verify the checksum of the downloaded binary.
  • Ensure the checksum is fetched from a trusted source.
Analysis chain

Ensure secure installation of external scripts.

Downloading and executing scripts from external sources can introduce security risks. Consider verifying the integrity of the script.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the integrity of the merry installation script.

# Test: Check if the script is being downloaded from a secure and trusted source.
curl -s https://get.merry.dev | sha256sum

Length of output: 120


Script:

#!/bin/bash
# Description: Fetch and display the content of the merry installation script for manual inspection.

# Fetch the script from the given URL
curl -s https://get.merry.dev

Length of output: 3023

- name: Start merry
run: merry go --headless
- name: install dependencies
run: |
corepack enable
yarn set version 4.3.1
yarn install
yarn run build
Comment on lines +22 to +27
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize dependency installation steps.

Combining the installation steps into a single run command can improve readability and performance.

-      - name: install dependencies
-        run: |
-          corepack enable
-          yarn set version 4.3.1
-          yarn install
-          yarn run build
+      - name: Install and build dependencies
+        run: |
+          corepack enable && \
+          yarn set version 4.3.1 && \
+          yarn install && \
+          yarn run build
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- name: install dependencies
run: |
corepack enable
yarn set version 4.3.1
yarn install
yarn run build
- name: Install and build dependencies
run: |
corepack enable && \
yarn set version 4.3.1 && \
yarn install && \
yarn run build

- run: yarn run test
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v4
with:
node-version: 18
registry-url: https://registry.npmjs.org/
- name: install dependencies
run: |
corepack enable
yarn set version 4.3.1
yarn install
- run: yarn run build
58 changes: 38 additions & 20 deletions docs/docs/developers/sdk/api-reference/Core.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@ id: core
---

# Core

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address markdown linting issue: Multiple top-level headings.

The file contains multiple top-level headings, which is a markdown linting issue.

- # Core
- # GardenJS
+ ## Core
+ ## GardenJS

Committable suggestion was skipped due to low confidence.

# GardenJS

The `GardenJS` class simplifies the creation of atomic swap orders and interactions with it.

## Creating a `GardenJS` Instance

```ts
constructor(orderbook: IOrderbook, wallets: Partial<Wallets>)
```

It accepts an `IOrderbook`, and an object with the keys as the `Chain` and the corresponding wallet for that chain. It can be created as follows:

```ts
import {
BitcoinOTA,
EVMWallet,
BitcoinNetwork,
BitcoinProvider,
} from "@catalogfi/wallets";
import { Orderbook, Chains } from "@gardenfi/orderbook";
import { GardenJS } from "@gardenfi/core";
import { BrowserProvider } from "ethers";
import {
BitcoinOTA,
EVMWallet,
BitcoinNetwork,
BitcoinProvider,
} from '@catalogfi/wallets';
import { Orderbook, Chains } from '@gardenfi/orderbook';
import { GardenJS } from '@gardenfi/core';
import { BrowserProvider } from 'ethers';

const bitcoinProvider = new BitcoinProvider(BitcoinNetwork.Testnet);
const provider = new BrowserProvider(window.ethereum);
Expand All @@ -31,19 +35,20 @@ const bitcoinWallet = new BitcoinOTA(bitcoinProvider, signer);
const ethereumWallet = new EVMWallet(signer);

const orderbook = await Orderbook.init({
signer,
opts: {
domain: window.location.host
}
signer,
opts: {
domain: window.location.host,
},
});

const garden = new GardenJS(orderbook, {
"ethereum_sepolia": ethereumWallet,
"bitcoin_testnet": bitcoinWallet
ethereum_sepolia: ethereumWallet,
bitcoin_testnet: bitcoinWallet,
});
```

### Methods

```ts
subscribeOrders(address: string, callback: (orders: Orders) => void): void

Expand All @@ -67,23 +72,28 @@ calculateReceiveAmt(
```

## Subscribe Orders

```ts
subscribeOrders(address: string, callback: (orders: Orders) => void): void
```

The `subscribeOrders` method is a wrapper over `IOrderbook`'s `subscribeOrders`. This method allows you to listen to all order updates where you're the maker or the taker.

### Parameters

- `address`: The address whose orders you want to listen to
- `callback`: a callback function that takes in `Orders` as a parameter.

## Unsubscribe Orders

```ts
unsubscribeOrders(): void
```

The `unsubscribeOrders` method stops listening to all orders on all accounts.

## Swap

```ts
swap(
from: Asset,
Expand All @@ -97,37 +107,42 @@ swap(
The swap method is used to create atomic swap orders. The initiator addresses and receiver addresses are filled based on the wallets provided. For example, if your `fromAsset` is testnet Bitcoin then the address chosen as the initiator address is the testnet Bitcoin wallet.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add missing comma for clarity.

There is a possible missing comma in the sentence.

- For example, if your `fromAsset` is testnet Bitcoin then the address chosen as the initiator address is the testnet Bitcoin wallet.
+ For example, if your `fromAsset` is testnet Bitcoin, then the address chosen as the initiator address is the testnet Bitcoin wallet.
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
The swap method is used to create atomic swap orders. The initiator addresses and receiver addresses are filled based on the wallets provided. For example, if your `fromAsset` is testnet Bitcoin then the address chosen as the initiator address is the testnet Bitcoin wallet.
The swap method is used to create atomic swap orders. The initiator addresses and receiver addresses are filled based on the wallets provided. For example, if your `fromAsset` is testnet Bitcoin, then the address chosen as the initiator address is the testnet Bitcoin wallet.
Tools
LanguageTool

[uncategorized] ~107-~107: Possible missing comma found.
Context: ...example, if your fromAsset is testnet Bitcoin then the address chosen as the initiato...

(AI_HYDRA_LEO_MISSING_COMMA)


The secret is the message signed by an `IBitcoinWallet` and the signed message is `garden.js<nonce><EVM wallet's pub key>` where the nonce is the number of orders created plus one (without the angular brackets).

```ts
//after creating a GardenJS instance

import { Assets } from '@gardenfi/orderbook';

const orderId = await garden.swap(
Assets.bitcoin_testnet.BTC,
Assets.ethereum_sepolia.WBTC,
sendAmount,
receiveAmount
Assets.bitcoin_testnet.BTC,
Assets.ethereum_sepolia.WBTC,
sendAmount,
receiveAmount
);
```

### Parameters

- `from`: The asset that you want to swap from
- `to`: The asset you want to receive
- `amt`: The amount that you're sending. Note that this number must be in the lowest denomination of that asset. For example, if you're swapping 1 Bitcoin then the amount must be `100,000,000` SATS
- `receiveAmount`: The amount of the to Asset that you want to receive. Like the send amount, this amount, too, should be in its lowest denomination.
- `opts.btcInputAddress`: The address in which you want to receive your Bitcoin.

### Returns

The order id.

### Get Swap

```ts
getSwap(order: Order): ISwapper
```

The `getSwap` method provides a `Swapper` instance that allows you to initiate, redeem, or refund your order by using the `.next()` method. The `.next()` method makes the appropriate action based on the status of the order and the role of the person performing the action that is, initiator or redeemer.

The `status` of an order is 3 digit number where:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hyphenate "3 digit number" for grammatical correctness.

The phrase "3 digit number" should be hyphenated to "3-digit number" for grammatical correctness.

- The `status` of an order is 3 digit number where:
+ The `status` of an order is a 3-digit number where:
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
The `status` of an order is 3 digit number where:
The `status` of an order is a 3-digit number where:
Tools
LanguageTool

[grammar] ~144-~144: When ‘3-digit’ is used as a modifier, it is usually spelled with a hyphen.
Context: ... redeemer. The status of an order is 3 digit number where: - the first digit indica...

(WORD_ESSAY_HYPHEN)


- the first digit indicates the overall order status, found in `Order.status`
- the second digit indicates the order status of the initiator atomic swap, found in `Order.initiatorAtomicSwap.swapStatus`
- the third digit indicates the order status of the follower (redeemer) atomic swap, found in `Order.followerAtomicSwap.swapStatus`.
Expand All @@ -144,16 +159,19 @@ The `parseStatus` function in `@gardenfi/orderbook` provides you the `status` of
When creating orders, the follower is the backend orderbook fillers.

### Calculate Receive Amount

```ts
calculateReceiveAmt(from: Asset, to: Asset, sendAmt: number): Promise<number>
```

Calculates the optimal amount of fee taken by the fillers in the orderbook which is *currently 3%*.
Calculates the optimal amount of fee taken by the fillers in the orderbook which is _currently 3%_. Note that, this function accepts the `sendAmt` in it's lowest denomination.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add missing comma for clarity.

There is a possible missing comma in the sentence.

- Calculates the optimal amount of fee taken by the fillers in the orderbook which is _currently 3%_. Note that, this function accepts the `sendAmt` in it's lowest denomination.
+ Calculates the optimal amount of fee taken by the fillers in the orderbook, which is _currently 3%_. Note that, this function accepts the `sendAmt` in its lowest denomination.
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Calculates the optimal amount of fee taken by the fillers in the orderbook which is _currently 3%_. Note that, this function accepts the `sendAmt` in it's lowest denomination.
Calculates the optimal amount of fee taken by the fillers in the orderbook, which is _currently 3%_. Note that, this function accepts the `sendAmt` in its lowest denomination.
Tools
LanguageTool

[uncategorized] ~167-~167: Possible missing comma found.
Context: ...ount of fee taken by the fillers in the orderbook which is currently 3%. Note that, thi...

(AI_HYDRA_LEO_MISSING_COMMA)


### Parameters

- `from`: The asset that you want to swap from
- `to`: The asset that you want to swap to
- `sendAmt`: The amount that you're sending in the lowest denomination of the asset that you're swapping from.

### Returns

The amount that you will receive in the lowest denomination of the asset that you're swapping.
18 changes: 18 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import globals from 'globals';
import pluginJs from '@eslint/js';
import tseslint from 'typescript-eslint';
Comment on lines +1 to +3
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

Incompatibility of typescript-eslint with ESLint 9.x

The typescript-eslint version 7.15.0 is not compatible with the current ESLint version 9.x, as it requires eslint version ^8.56.0. Additionally, the compatibility of @eslint/js version 9.6.0 with ESLint 9.x could not be confirmed due to the lack of peer dependency information.

  • typescript-eslint: Requires eslint version ^8.56.0, but the project uses eslint version 9.x.
  • @eslint/js: Compatibility with ESLint 9.x needs to be verified manually or through documentation.

Please address the version incompatibility and verify the compatibility of the plugins.

Analysis chain

Ensure compatibility of ESLint plugins.

Make sure that the plugins @eslint/js and typescript-eslint are compatible with the current ESLint version and the project setup.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify compatibility of ESLint plugins.

# Test: List installed ESLint plugins and versions. Expect: Compatible versions.
npm list @eslint/js typescript-eslint

Length of output: 110


Script:

#!/bin/bash
# Description: Extract versions of ESLint and relevant plugins from package.json

# Extract versions of @eslint/js, typescript-eslint, and eslint
jq '.dependencies["@eslint/js"], .dependencies["typescript-eslint"], .dependencies["eslint"], .devDependencies["@eslint/js"], .devDependencies["typescript-eslint"], .devDependencies["eslint"]' package.json

Length of output: 247


Script:

#!/bin/bash
# Description: Search for the usage of @eslint/js in the codebase

# Search for @eslint/js in the codebase
rg '@eslint/js'

Length of output: 294


Script:

#!/bin/bash
# Description: Verify compatibility of typescript-eslint with ESLint 9.x

# Check typescript-eslint version compatibility with ESLint 9.x
curl -s https://registry.npmjs.org/@typescript-eslint/eslint-plugin | jq '.versions["7.15.0"].peerDependencies'

Length of output: 180


Script:

#!/bin/bash
# Description: Verify compatibility of @eslint/js with ESLint 9.x

# Check @eslint/js version compatibility with ESLint 9.x
curl -s https://registry.npmjs.org/@eslint/js | jq '.versions["9.6.0"].peerDependencies'

Length of output: 95


export default [
{ files: ['**/*.{js,mjs,cjs,ts}'] },
{ languageOptions: { globals: { ...globals.browser, ...globals.node } } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{
rules: {
'@typescript-eslint/no-explicit-any': 'off',
},
},
{
ignores: ['**/dist/*', '**/build/*', 'scripts'],
},
];
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@
"docs"
],
"devDependencies": {
"@eslint/js": "^9.6.0",
"@types/chrome": "^0.0.268",
"@types/node": "^20.11.30",
"eslint": "9.x",
"globals": "^15.8.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.2",
"typescript-eslint": "^7.15.0",
"vite": "^5.1.6",
"vite-plugin-dts": "^3.7.3",
"vite-plugin-eslint": "^1.8.1",
"vitest": "^1.6.0"
}
},
"packageManager": "[email protected]"
}
10 changes: 0 additions & 10 deletions packages/core/jest.config.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"scripts": {
"build": "vite build",
"test": "vitest run",
"dev": "vite build --watch"
},
"main": "./dist/index.cjs",
Expand All @@ -31,14 +32,13 @@
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"dotenv": "^16.3.1",
"jest": "^29.7.0",
"typescript": "^5.2.2",
"vite": "^5.1.6",
"vite-plugin-dts": "^3.7.3",
"vite-plugin-top-level-await": "^1.4.1",
"vite-plugin-wasm": "^3.3.0"
"vite-plugin-wasm": "^3.3.0",
"vitest": "^1.6.0"
},
"sideEffects": false
}
2 changes: 1 addition & 1 deletion packages/core/src/lib/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const CatalogErrors = {
export const GardenErrors = {
WALLET_NOT_FOUND: (from: boolean): string =>
`${
from ? 'from' : 'to'
Expand Down
Loading
Loading