Skip to content

Commit

Permalink
added sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfantaholic committed Oct 24, 2024
1 parent 466012c commit 3093609
Show file tree
Hide file tree
Showing 24 changed files with 9,135 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Unit Tests

on:
push:
pull_request:

jobs:
tests:
name: Unit tests

runs-on: ubuntu-latest

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

- name: Setup node
uses: actions/setup-node@v2
with:
node-version: 18
registry-url: https://registry.npmjs.org

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Run tests
run: yarn test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.log
.DS_Store
node_modules
dist
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn format & yarn test
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.github
node_modules
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 4,
"printWidth": 180,
"semi": true
}
10 changes: 10 additions & 0 deletions .size-limit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"path": "dist/sdk.cjs.production.min.js",
"limit": "10 KB"
},
{
"path": "dist/sdk.esm.js",
"limit": "10 KB"
}
]
1 change: 1 addition & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignore-scripts true
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# permit2-sdk
# Permit2 sdk

[![npm version](https://img.shields.io/npm/v/@real-wagmi/permit2-sdk/latest.svg)](https://www.npmjs.com/package/@real-wagmi/permit2-sdk/v/latest)
8 changes: 8 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"preset": "ts-jest",
"testEnvironment": "node",
"transform": {
"^.+\\.ts?$": "ts-jest"
},
"transformIgnorePatterns": ["<rootDir>/node_modules/"]
}
49 changes: 49 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@real-wagmi/permit2-sdk",
"version": "1.0.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"module": "dist/index.mjs",
"files": [
"dist"
],
"engines": {
"node": ">=18"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"start": "tsup --watch",
"build": "tsup",
"test": "vitest --run",
"lint": "tsdx lint",
"prepare": "yarn build",
"size": "size-limit",
"analyze": "size-limit --why",
"format": "prettier --write \"*/**/*.ts\""
},
"author": "Wagmi",
"devDependencies": {
"@size-limit/preset-small-lib": "^9.0.0",
"@types/big.js": "^6.2.0",
"@types/jest": "^29.5.12",
"husky": "^8.0.3",
"size-limit": "^9.0.0",
"tsdx": "^0.14.1",
"tslib": "^2.6.2",
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
"repository": "https://github.com/RealWagmi/permit2-sdk.git",
"keywords": [
"wagmi",
"ethereum"
],
"dependencies": {
"@real-wagmi/sdk": "^1.3.9",
"tiny-invariant": "^1.3.1",
"viem": "^2.7.20",
"vitest": "^1.3.1"
}
}
41 changes: 41 additions & 0 deletions src/abis/Permit2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export const permit2Abi = [
{
inputs: [
{
internalType: 'address',
name: '',
type: 'address',
},
{
internalType: 'address',
name: '',
type: 'address',
},
{
internalType: 'address',
name: '',
type: 'address',
},
],
name: 'allowance',
outputs: [
{
internalType: 'uint160',
name: 'amount',
type: 'uint160',
},
{
internalType: 'uint48',
name: 'expiration',
type: 'uint48',
},
{
internalType: 'uint48',
name: 'nonce',
type: 'uint48',
},
],
stateMutability: 'view',
type: 'function',
},
] as const;
142 changes: 142 additions & 0 deletions src/allowanceTransfer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { describe, expect, it } from 'vitest';
import { AllowanceTransfer } from './allowanceTransfer';
import { MaxAllowanceExpiration, MaxAllowanceTransferAmount, MaxOrderedNonce, MaxSigDeadline } from './constants';

describe('AllowanceTransfer', () => {
describe('Max values', () => {
it('max values pass', () => {
expect(() =>
AllowanceTransfer.hash(
{
details: {
token: '0x0000000000000000000000000000000000000000',
amount: MaxAllowanceTransferAmount.toString(),
expiration: MaxAllowanceExpiration.toString(),
nonce: MaxOrderedNonce.toString(),
},
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: MaxSigDeadline.toString(),
},
'0x0000000000000000000000000000000000000000',
1,
),
).not.toThrow();
});

it('nonce out of range', () => {
expect(() =>
AllowanceTransfer.hash(
{
details: {
token: '0x0000000000000000000000000000000000000000',
amount: '0',
expiration: '0',
nonce: MaxOrderedNonce + 1n,
},
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: '0',
},
'0x0000000000000000000000000000000000000000',
1,
),
).toThrow('NONCE_OUT_OF_RANGE');
});

it('amount out of range', () => {
expect(() =>
AllowanceTransfer.hash(
{
details: {
token: '0x0000000000000000000000000000000000000000',
amount: MaxAllowanceTransferAmount + 1n,
expiration: '0',
nonce: 0,
},
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: '0',
},
'0x0000000000000000000000000000000000000000',
1,
),
).toThrow('AMOUNT_OUT_OF_RANGE');
});

it('expiration out of range', () => {
expect(() =>
AllowanceTransfer.hash(
{
details: {
token: '0x0000000000000000000000000000000000000000',
amount: '0',
expiration: MaxAllowanceExpiration + 1n,
nonce: 0,
},
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: '0',
},
'0x0000000000000000000000000000000000000000',
1,
),
).toThrow('EXPIRATION_OUT_OF_RANGE');
});

it('sigDeadline out of range', () => {
expect(() =>
AllowanceTransfer.hash(
{
details: {
token: '0x0000000000000000000000000000000000000000',
amount: '0',
expiration: '0',
nonce: 0,
},
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: MaxSigDeadline + 1n,
},
'0x0000000000000000000000000000000000000000',
1,
),
).toThrow('SIG_DEADLINE_OUT_OF_RANGE');
});
});

it('non-batch', () => {
expect(
AllowanceTransfer.hash(
{
details: {
token: '0x0000000000000000000000000000000000000000',
amount: '0',
expiration: '0',
nonce: 0,
},
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: '0',
},
'0x0000000000000000000000000000000000000000',
1,
),
).toBe('0xd47437bffdbc4d123a2165feb6ca646b8700c038622ce304f84e9048bc744f36');
});

it('batch', () => {
expect(
AllowanceTransfer.hash(
{
details: [
{
token: '0x0000000000000000000000000000000000000000',
amount: '0',
expiration: '0',
nonce: 0,
},
],
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: '0',
},
'0x0000000000000000000000000000000000000000',
1,
),
).toBe('0x49642ada5f77eb9458f8265eb01fed2684c2f25d50534fea3efdf2cf395deb2f');
});
});
Loading

0 comments on commit 3093609

Please sign in to comment.