Skip to content
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
64 changes: 64 additions & 0 deletions .github/workflows/release-sdk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Release SDK

on:
push:
tags:
- "sdk@*"

jobs:
validate:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- uses: actions/cache@v4
with:
path: ~/.bun/install/cache
key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lockb') }}
restore-keys: ${{ runner.os }}-bun-
- run: bun install --frozen-lockfile
- name: Validate tag matches package version
working-directory: packages/sdk/js
run: |
TAG_VERSION="${GITHUB_REF_NAME#sdk@}"
PKG_VERSION=$(node -p "require('./package.json').version")
if [ "$TAG_VERSION" != "$PKG_VERSION" ]; then
echo "Tag version ($TAG_VERSION) does not match package.json version ($PKG_VERSION)"
exit 1
fi
- run: bun run typecheck
working-directory: packages/sdk/js
- run: bun run build
working-directory: packages/sdk/js

publish:
needs: validate
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- uses: actions/cache@v4
with:
path: ~/.bun/install/cache
key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lockb') }}
restore-keys: ${{ runner.os }}-bun-
- run: bun install --frozen-lockfile
- run: bun run build
working-directory: packages/sdk/js
- uses: actions/setup-node@v4
with:
node-version: "22"
registry-url: "https://registry.npmjs.org"
- run: npm publish --provenance --access public
working-directory: packages/sdk/js
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
141 changes: 141 additions & 0 deletions bun.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"packageManager": "bun@1.3.12",
"workspaces": {
"packages": [
"packages/*"
"packages/*",
"packages/sdk/*"
],
"catalog": {
"@biomejs/biome": "2.4.10",
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk/js/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://biomejs.dev/schemas/2.4.10/schema.json",
"extends": "//"
}
60 changes: 60 additions & 0 deletions packages/sdk/js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "@newt0n/sdk",
"type": "module",
"version": "0.1.0",
"description": "JavaScript SDK for the newt0n protocol",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/newt0n-ai/newt0n.git",
"directory": "packages/sdk/js"
},
"homepage": "https://github.com/newt0n-ai/newt0n#readme",
"bugs": {
"url": "https://github.com/newt0n-ai/newt0n/issues"
},
"keywords": [
"newt0n",
"x402",
"sdk",
"0g",
"micropayments",
"payments",
"web3",
"evm"
],
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsup",
"typecheck": "tsc --noEmit"
},
"exports": {
"./x402/server": {
"import": {
"types": "./dist/esm/x402/server.d.ts",
"default": "./dist/esm/x402/server.js"
},
"require": {
"types": "./dist/cjs/x402/server.d.ts",
"default": "./dist/cjs/x402/server.js"
}
}
},
"files": [
"dist"
],
"devDependencies": {
"@biomejs/biome": "catalog:",
"@tsconfig/node22": "catalog:",
"@types/node": "catalog:",
"typescript": "catalog:"
},
"dependencies": {
"@x402/core": "2.10.0",
"@x402/evm": "2.10.0",
"tsup": "8.5.1"
}
}
1 change: 1 addition & 0 deletions packages/sdk/js/reset.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@total-typescript/ts-reset";
67 changes: 67 additions & 0 deletions packages/sdk/js/src/x402/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ExactEvmScheme as X402ExactEvmScheme } from "@x402/evm/exact/server";

export type Stablecoin = {
address: string;
name: string;
decimal: number;
version: string;
assetTransferMethod?: "permit2";
supportsEip2612?: true;
};
export type DefaultStablecoins = Record<string, Stablecoin>;

export const DEFAULT_STABLECOINS: DefaultStablecoins = {
"eip155:16661": {
address: "0x1f3aa82227281ca364bfb3d253b0f1af1da6473e",
name: "Bridged USDC",
decimal: 6,
version: "2",
},
};

export const convertToTokenAmount = (
decimalAmount: string,
decimals: number
) => {
const amount = parseFloat(decimalAmount);
if (Number.isNaN(amount)) {
throw new Error(`Invalid amount: ${decimalAmount}`);
}
const [intPart, decPart = ""] = String(amount).split(".");
const paddedDec = decPart.padEnd(decimals, "0").slice(0, decimals);
const tokenAmount = (intPart + paddedDec).replace(/^0+/, "") || "0";
return tokenAmount;
};

export class ExactEvmScheme extends X402ExactEvmScheme {
constructor() {
super();

this.registerMoneyParser(async (amount: number, network: string) => {
const assetInfo = DEFAULT_STABLECOINS[network];
if (!assetInfo) return null;

const tokenAmount = convertToTokenAmount(
amount.toString(),
assetInfo.decimal
);

const includeEip712Domain =
!assetInfo.assetTransferMethod || assetInfo.supportsEip2612;

return {
amount: tokenAmount,
asset: assetInfo.address,
extra: {
...(includeEip712Domain && {
name: assetInfo.name,
version: assetInfo.version,
}),
...(assetInfo.assetTransferMethod && {
assetTransferMethod: assetInfo.assetTransferMethod,
}),
},
};
});
}
}
20 changes: 20 additions & 0 deletions packages/sdk/js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "https://json.schemastore.org/tsconfig.json",
"extends": "@tsconfig/node22/tsconfig.json",
"compilerOptions": {
"target": "ES2020",
"module": "ES2020",
"lib": ["ES2022"],
"moduleResolution": "bundler",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"ignoreDeprecations": "6.0"
},
"include": ["src", "reset.d.ts"]
}
27 changes: 27 additions & 0 deletions packages/sdk/js/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { defineConfig } from "tsup";

const baseConfig = {
entry: {
"x402/server": "src/x402/server.ts",
},
dts: {
resolve: true,
},
sourcemap: true,
target: "es2020",
};

export default defineConfig([
{
...baseConfig,
format: "esm",
outDir: "dist/esm",
clean: true,
},
{
...baseConfig,
format: "cjs",
outDir: "dist/cjs",
clean: false,
},
]);