Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoid committed Jul 1, 2024
0 parents commit ccdac75
Show file tree
Hide file tree
Showing 35 changed files with 4,699 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/package-size-report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Package Size Report

on:
pull_request:
branches: [main]

jobs:
pkg-size-report:
name: Package Size Report
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Package size report
id: pkg-size-report
uses: privatenumber/pkg-size-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42 changes: 42 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Test

on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]

jobs:
test:
name: Test
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version-file: .nvmrc

- name: Setup pnpm
uses: pnpm/action-setup@v3
with:
version: 9
run_install: true

- name: Type check
run: pnpm type-check

- name: Build
run: pnpm build

- name: Test
run: pnpm test

- name: Test Node.js v16
run: pnpm --use-node-version=16.20.0 test

- name: Lint
run: pnpm lint
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# macOS
.DS_Store

# Logs
logs
*.log
npm-debug.log*

# Dependency directories
node_modules/

# Output of 'npm pack'
/*.tgz

# dotenv environment variables file
.env
.env.test

# Distribution
dist

# Cache
.eslintcache
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.12
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Ryan Conceicao

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<h1 align="center">
<sup>AnchorES</sup>
<!-- <br>
<a href="https://www.npmjs.com/package/anchores"><img src="https://badgen.net/npm/v/anchores" title="NPM version"></a>
<a href="https://pkg-size.dev/anchores"><img src="https://pkg-size.dev/badge/bundle/2067" title="Minimal Solana transaction parser."></a> -->
</h1>

Minimal library for parsing Solana transaction, instructions and events.

- Tree-shakeable: 9.72 kB -> 4.08 kB (gzip) to parse Jupiter Swap Events
- ESM and commonjs
- No IDL, structs defined in code
- Typescript friendly
- Minimal dependencies

> [!WARNING]
> **This is still a work in progress**. Support for all Borsh types is not yet implemented, and the API is subject to change.
## Install

```bash
npm i anchores
```

## Parsing Jupiter Swap Transactions

We export a `parseTransaction` function that takes a program id, a list of instruction and event parsers, and the transaction to parse.

```typescript
import { parseTransaction } from "anchores";
import { JUPITER_V6_PROGRAM_ID, SwapEvent } from "anchores/parsers/jupiter";

const tx = await connection.getParsedTransaction("txhash");
const events = parseTransaction(
JUPITER_V6_PROGRAM_ID,
{
events: [SwapEvent],
},
tx,
);
```

See tests for other usage examples.

## Declaring your own parser

You can also declare your own parsers. Here is an example of a parser for the Jupiter Swap Event:

```typescript
import * as b from "anchores/binary";

export function parseSwapEvent(data: Uint8Array) {
const reader = b.createReader(data);
return {
amm: b.publicKey(reader),
inputMint: b.publicKey(reader),
inputAmount: b.u64(reader),
outputMint: b.publicKey(reader),
outputAmount: b.u64(reader),
};
}
export type ParsedSwapEvent = ReturnType<typeof parseSwapEvent>;
```

You can use this function directly or form a parser object to pass to `parseTransaction`, `decodeEvents`, `decodeStructs`.

```typescript
import { createSighash } from "anchores/anchor";

export const SwapEvent = {
name: "SwapEvent" as const,
discriminator: createSighash("event", "SwapEvent"),
// declared above
parse: parseSwapEvent,
};

// Usage
const events = parseTransaction(
PROGRAM_ID,
{
events: [SwapEvent],
},
tx,
);
// or
const structs = tx.meta.innerInstructions.flatMap((inner) =>
inner.instructions.filter(isJupiterInstruction).map((ix) => {
const ixData = base58.decode(ix.data);
const instruct = decodeStructs([SwapInstruction], ixData);
if (instruct) {
return instruct;
}

const event = decodeEvents([SwapEvent], ixData);
return event;
}),
);
```
11 changes: 11 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import unjs from "eslint-config-unjs";

export default unjs({
ignores: [
// ignore paths
],
rules: {
// rule overrides
"unicorn/no-array-callback-reference": "off",
},
});
112 changes: 112 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"name": "anchores",
"version": "0.0.1",
"description": "Minimal library for parsing Solana transaction, instructions and events.",
"keywords": [
"solana",
"tx",
"transaction",
"parser",
"anchor",
"borsh",
"jupiter",
"instruction",
"event",
"web3"
],
"author": "Ryan Conceicao",
"license": "MIT",
"repository": "ryoid/anchores",
"type": "module",
"sideEffects": false,
"files": [
"dist"
],
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.cts",
"exports": {
".": {
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
},
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
}
},
"./binary": {
"require": {
"types": "./dist/binary.d.cts",
"default": "./dist/binary.cjs"
},
"import": {
"types": "./dist/binary.d.mts",
"default": "./dist/binary.mjs"
}
},
"./anchor": {
"require": {
"types": "./dist/anchor.d.cts",
"default": "./dist/anchor.cjs"
},
"import": {
"types": "./dist/anchor.d.mts",
"default": "./dist/anchor.mjs"
}
},
"./parsers": {
"require": {
"types": "./dist/parsers/types.d.cts"
},
"import": {
"types": "./dist/parsers/types.d.mts"
}
},
"./parsers/jupiter": {
"require": {
"types": "./dist/parsers/jupiter.d.cts",
"default": "./dist/parsers/jupiter.cjs"
},
"import": {
"types": "./dist/parsers/jupiter.d.mts",
"default": "./dist/parsers/jupiter.mjs"
}
},
"./parsers/meteora-dlmm": {
"require": {
"types": "./dist/parsers/meteora-dlmm.d.cts",
"default": "./dist/parsers/meteora-dlmm.cjs"
},
"import": {
"types": "./dist/parsers/meteora-dlmm.d.mts",
"default": "./dist/parsers/meteora-dlmm.mjs"
}
}
},
"scripts": {
"lint": "eslint --cache . && prettier -c src",
"lint:fix": "eslint --cache . --fix && prettier -w src",
"test": "vitest --typecheck",
"dev": "pkgroll --watch",
"build": "pkgroll --minify",
"type-check": "tsc --noEmit",
"prepack": "pnpm build && clean-pkg-json"
},
"devDependencies": {
"@solana/web3.js": "^1.94.0",
"@types/node": "^20.14.2",
"clean-pkg-json": "^1.2.0",
"eslint": "^9.6.0",
"eslint-config-unjs": "^0.3.2",
"pkgroll": "^2.1.1",
"prettier": "^3.3.2",
"typescript": "^5.4.5",
"vitest": "^1.6.0"
},
"dependencies": {
"@noble/hashes": "^1.4.0",
"@scure/base": "^1.1.7"
}
}
Loading

0 comments on commit ccdac75

Please sign in to comment.