-
Notifications
You must be signed in to change notification settings - Fork 0
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: sample lib & app #6
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c793f65
feat: sample blockchain lib
0xnigir1 4a8d4da
feat: sample app
0xnigir1 37aecee
Merge remote-tracking branch 'origin/dev' into feat/sample-lib
0xnigir1 fbc80e1
docs: update readme
0xnigir1 b97079a
fix: lint errors
0xnigir1 fc5b9d2
chore: update pnpm-lock.yaml
0xnigir1 2d068a3
fix: eslintrc config
0xnigir1 cfe08df
fix: turbo.json lint config
0xnigir1 fbc3f35
fix: add missing vitest coverage dep
0xnigir1 0ac0f84
docs: wording
0xnigir1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
RPC_URL= # RPC URL. Example: https://eth.llamarpc.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# ts-turborepo-boilerplate: sample-app | ||
|
||
> Note: use this app as reference but preferred way is to re-write app | ||
> from zero instead of refactoring this one. | ||
> When you don't need this anymore, you can delete it | ||
|
||
Sample app that uses [sample-lib](../../packages/sample-lib) Blockchain | ||
provider to fetch Vitalik and Zero address native balance and sums them | ||
|
||
## Setup | ||
|
||
1. Change package name to follow yours project one in [`package.json`](./package.json) | ||
2. Install dependencies running `pnpm install` | ||
|
||
### ⚙️ Setting up env variables | ||
|
||
- Create `.env` file and copy paste `.env.example` content in there. | ||
|
||
``` | ||
$ cp .env.example .env | ||
``` | ||
|
||
Available options: | ||
| Name | Description | Default | Required | Notes | | ||
|-----------------------------|--------------------------------------------------------------------------------------------------------------------------------|-----------|----------------------------------|-----------------------------------------------------------------| | ||
| `RPC_URL` | RPC URL to use for querying balances | N/A | Yes | | | ||
|
||
## Available Scripts | ||
|
||
Available scripts that can be run using `pnpm`: | ||
|
||
| Script | Description | | ||
| ------------- | ------------------------------------------------------- | | ||
| `build` | Build library using tsc | | ||
| `check-types` | Check types issues using tsc | | ||
| `clean` | Remove `dist` folder | | ||
| `lint` | Run ESLint to check for coding standards | | ||
| `lint:fix` | Run linter and automatically fix code formatting issues | | ||
| `format` | Check code formatting and style using Prettier | | ||
| `format:fix` | Run formatter and automatically fix issues | | ||
| `start` | Run the app | | ||
| `test` | Run tests using vitest | | ||
| `test:cov` | Run tests with coverage report | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "@ts-turborepo-boilerplate/sample-app", | ||
"version": "0.0.1", | ||
"type": "module", | ||
"main": "./dist/index.js", | ||
"scripts": { | ||
"build": "tsc -p tsconfig.build.json", | ||
"check-types": "tsc --noEmit -p ./tsconfig.json", | ||
"clean": "rm -rf dist", | ||
"format": "prettier --check \"{src,test}/**/*.{js,ts,json}\"", | ||
"format:fix": "prettier --write \"{src,test}/**/*.{js,ts,json}\"", | ||
"lint": "eslint \"{src,test}/**/*.{js,ts,json}\"", | ||
"lint:fix": "pnpm lint --fix", | ||
"start": "node dist/index.js", | ||
"test": "vitest run --config vitest.config.ts --passWithNoTests", | ||
"test:cov": "vitest run --config vitest.config.ts --coverage" | ||
}, | ||
"dependencies": { | ||
"@ts-turborepo-boilerplate/sample-lib": "workspace:*", | ||
"dotenv": "16.4.5", | ||
"zod": "3.23.8" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import dotenv from "dotenv"; | ||
import { z } from "zod"; | ||
|
||
dotenv.config(); | ||
|
||
const validationSchema = z.object({ | ||
RPC_URL: z.string().url(), | ||
}); | ||
|
||
const env = validationSchema.safeParse(process.env); | ||
|
||
if (!env.success) { | ||
console.error(env.error.issues.map((issue) => JSON.stringify(issue)).join("\n")); | ||
process.exit(1); | ||
} | ||
|
||
export const environment = env.data; | ||
export type Environment = z.infer<typeof validationSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./env.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { inspect } from "util"; | ||
import { BlockchainProvider, IBlockchainProvider } from "@ts-turborepo-boilerplate/sample-lib"; | ||
|
||
import { environment } from "./config/env.js"; | ||
import { BalancesController } from "./stats/index.js"; | ||
|
||
const main = async (): Promise<void> => { | ||
const dataProvider: IBlockchainProvider = new BlockchainProvider(environment.RPC_URL); | ||
|
||
const balanceController = new BalancesController(dataProvider); | ||
|
||
const vitalikAddress = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"; | ||
const zeroAddress = "0x0000000000000000000000000000000000000000"; | ||
|
||
const balance = await balanceController.addBalances(vitalikAddress, zeroAddress); | ||
|
||
console.log(`Vitalik's and Zero summed balance is: ${balance}`); | ||
}; | ||
|
||
process.on("unhandledRejection", (reason, p) => { | ||
console.error(`Unhandled Rejection at: \n${inspect(p, undefined, 100)}, \nreason: ${reason}`); | ||
}); | ||
|
||
process.on("uncaughtException", (error: Error) => { | ||
console.error( | ||
`An uncaught exception occurred: ${error}\n` + `Exception origin: ${error.stack}`, | ||
); | ||
}); | ||
|
||
main().catch((err) => { | ||
console.error(`Caught error in main handler: ${err}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Address, IBlockchainProvider } from "@ts-turborepo-boilerplate/sample-lib"; | ||
|
||
export class BalancesController { | ||
constructor(private readonly blockchainProvider: IBlockchainProvider) {} | ||
|
||
public async addBalances(addressA: Address, addressB: Address): Promise<bigint> { | ||
const balances = await Promise.all([ | ||
this.blockchainProvider.getBalance(addressA), | ||
this.blockchainProvider.getBalance(addressB), | ||
]); | ||
|
||
return balances[0] + balances[1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./balances.controller.js"; |
39 changes: 39 additions & 0 deletions
39
apps/sample-app/test/stats/controller/balances.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { IBlockchainProvider } from "@ts-turborepo-boilerplate/sample-lib"; | ||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { BalancesController } from "../../../src/stats/index.js"; | ||
|
||
describe("BalancesController", () => { | ||
let balancesController: BalancesController; | ||
let blockchainProviderMock: IBlockchainProvider; | ||
|
||
beforeEach(() => { | ||
blockchainProviderMock = { | ||
getBalance: vi.fn(), | ||
}; | ||
balancesController = new BalancesController(blockchainProviderMock); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("addBalances", () => { | ||
it("should return the sum of balances for two addresses", async () => { | ||
const addressA = "0x1234567890abcdef"; | ||
const addressB = "0xabcdef1234567890"; | ||
const balanceA = BigInt(1000000000000000000); | ||
const balanceB = BigInt(2000000000000000000); | ||
|
||
vi.spyOn(blockchainProviderMock, "getBalance").mockResolvedValueOnce(balanceA); | ||
vi.spyOn(blockchainProviderMock, "getBalance").mockResolvedValueOnce(balanceB); | ||
|
||
const result = await balancesController.addBalances(addressA, addressB); | ||
|
||
expect(result).toEqual(balanceA + balanceB); | ||
expect(blockchainProviderMock.getBalance).toHaveBeenCalledTimes(2); | ||
expect(blockchainProviderMock.getBalance).toHaveBeenCalledWith(addressA); | ||
expect(blockchainProviderMock.getBalance).toHaveBeenCalledWith(addressB); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.build.json", | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "dist", "tests"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import path from "path"; | ||
import { configDefaults, defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
globals: true, // Use Vitest's global API without importing it in each file | ||
environment: "node", // Use the Node.js environment | ||
include: ["test/**/*.spec.ts"], // Include test files | ||
exclude: ["node_modules", "dist"], // Exclude certain directories | ||
coverage: { | ||
provider: "v8", | ||
reporter: ["text", "json", "html"], // Coverage reporters | ||
exclude: ["node_modules", "dist", "src/index.ts", ...configDefaults.exclude], // Files to exclude from coverage | ||
}, | ||
}, | ||
resolve: { | ||
alias: { | ||
// Setup path alias based on tsconfig paths | ||
"@": path.resolve(__dirname, "src"), | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# ts-turborepo-boilerplate: sample-lib package | ||
|
||
> Note: use this lib as reference but preferred way is to re-write package | ||
> from zero instead of refactoring this one. | ||
> When you don't need this anymore, you can delete it | ||
|
||
Sample library that exposes a Blockchain provider to query | ||
account balances on Ethereum mainnet or EVM-compatible blockchains | ||
|
||
## Setup | ||
|
||
1. Change package name to follow yours project one in [`package.json`](./package.json) | ||
2. Install dependencies running `pnpm install` | ||
|
||
## Available Scripts | ||
|
||
Available scripts that can be run using `pnpm`: | ||
|
||
| Script | Description | | ||
| ------------- | ------------------------------------------------------- | | ||
| `build` | Build library using tsc | | ||
| `check-types` | Check types issues using tsc | | ||
| `clean` | Remove `dist` folder | | ||
| `lint` | Run ESLint to check for coding standards | | ||
| `lint:fix` | Run linter and automatically fix code formatting issues | | ||
| `format` | Check code formatting and style using Prettier | | ||
| `format:fix` | Run formatter and automatically fix issues | | ||
| `test` | Run tests using vitest | | ||
| `test:cov` | Run tests with coverage report | | ||
|
||
## Usage | ||
|
||
### Importing the Package | ||
|
||
You can import the package in your TypeScript or JavaScript files as follows: | ||
|
||
```typescript | ||
import { BlockchainProvider } from "@ts-turborepo-boilerplate/sample-lib"; | ||
``` | ||
|
||
### Example | ||
|
||
```typescript | ||
// EVM-provider | ||
const rpcUrl = ""; //non-empty valid url | ||
const address = "0x..."; | ||
|
||
const provider = new BlockchainProvider(rpcUrl); | ||
|
||
const balance = await provider.getBalance(address); | ||
|
||
console.log(`Balance of ${address} is ${balance}`); | ||
``` | ||
|
||
## API | ||
|
||
### [IBlockchainProvider](./src/interfaces/blockchainProvider.interface.ts) | ||
|
||
Available methods | ||
|
||
- `getBalance(address: Address)` | ||
|
||
## References | ||
|
||
- [Viem](https://viem.sh/) | ||
- [Offchain docs: Internal module pattern](https://www.notion.so/defi-wonderland/Best-Practices-c08b71f28e59490f8dadef64cf61c9ac?pvs=4#89f99d33053a426285bacc6275d994c0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@ts-turborepo-boilerplate/sample-lib", | ||
"version": "0.0.1", | ||
"private": true, | ||
"description": "", | ||
"license": "MIT", | ||
"author": "Wonderland", | ||
"type": "module", | ||
"main": "./dist/src/index.js", | ||
"scripts": { | ||
"build": "tsc -p tsconfig.build.json", | ||
"check-types": "tsc --noEmit -p ./tsconfig.json", | ||
"clean": "rm -rf dist", | ||
"format": "prettier --check \"{src,test}/**/*.{js,ts,json}\"", | ||
"format:fix": "prettier --write \"{src,test}/**/*.{js,ts,json}\"", | ||
"lint": "eslint \"{src,test}/**/*.{js,ts,json}\"", | ||
"lint:fix": "pnpm lint --fix", | ||
"test": "vitest run --config vitest.config.ts --passWithNoTests", | ||
"test:cov": "vitest run --config vitest.config.ts --coverage" | ||
}, | ||
"dependencies": { | ||
"viem": "2.21.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./invalidRpcUrl.exception.js"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't get "to follow yours project one," maybe you meant "Change package name to your own"