@pickhooks/sdk 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +309 -3
  3. package/dist/abis/any3.d.ts +2123 -0
  4. package/dist/abis/any3.js +3 -0
  5. package/dist/abis/anytax.d.ts +1786 -0
  6. package/dist/abis/anytax.js +3 -0
  7. package/dist/abis/calls.d.ts +2515 -0
  8. package/dist/abis/calls.js +4 -0
  9. package/dist/abis/common.d.ts +449 -0
  10. package/dist/abis/common.js +91 -0
  11. package/dist/abis/dips.d.ts +897 -0
  12. package/dist/abis/dips.js +2 -0
  13. package/dist/abis/index.d.ts +12 -0
  14. package/dist/abis/index.js +12 -0
  15. package/dist/abis/infra.d.ts +701 -0
  16. package/dist/abis/infra.js +4 -0
  17. package/dist/abis/lever.d.ts +1710 -0
  18. package/dist/abis/lever.js +5 -0
  19. package/dist/abis/lever2.d.ts +2710 -0
  20. package/dist/abis/lever2.js +5 -0
  21. package/dist/abis/pools.d.ts +1898 -0
  22. package/dist/abis/pools.js +4 -0
  23. package/dist/abis/sticky.d.ts +953 -0
  24. package/dist/abis/sticky.js +4 -0
  25. package/dist/abis/v1.d.ts +1627 -0
  26. package/dist/abis/v1.js +5 -0
  27. package/dist/abis/v2.d.ts +3264 -0
  28. package/dist/abis/v2.js +8 -0
  29. package/dist/addresses.d.ts +87 -0
  30. package/dist/addresses.js +62 -0
  31. package/dist/any.d.ts +447 -0
  32. package/dist/any.js +729 -0
  33. package/dist/api.d.ts +62 -0
  34. package/dist/api.js +30 -0
  35. package/dist/blocks.d.ts +107 -0
  36. package/dist/blocks.js +126 -0
  37. package/dist/coin-rules-config.d.ts +8 -0
  38. package/dist/coin-rules-config.js +13 -0
  39. package/dist/coin-rules.d.ts +383 -0
  40. package/dist/coin-rules.js +878 -0
  41. package/dist/coins.d.ts +217 -0
  42. package/dist/coins.js +520 -0
  43. package/dist/dips.d.ts +133 -0
  44. package/dist/dips.js +148 -0
  45. package/dist/errors.d.ts +35 -0
  46. package/dist/errors.js +347 -0
  47. package/dist/index.d.ts +73 -0
  48. package/dist/index.js +58 -0
  49. package/dist/infinity.d.ts +63 -0
  50. package/dist/infinity.js +96 -0
  51. package/dist/infra.d.ts +112 -0
  52. package/dist/infra.js +114 -0
  53. package/dist/lever.d.ts +391 -0
  54. package/dist/lever.js +419 -0
  55. package/dist/pools.d.ts +274 -0
  56. package/dist/pools.js +405 -0
  57. package/dist/sticky-rules.d.ts +179 -0
  58. package/dist/sticky-rules.js +580 -0
  59. package/dist/sticky.d.ts +216 -0
  60. package/dist/sticky.js +292 -0
  61. package/dist/tx.d.ts +70 -0
  62. package/dist/tx.js +69 -0
  63. package/dist/v3.d.ts +42 -0
  64. package/dist/v3.js +98 -0
  65. package/package.json +58 -4
package/dist/v3.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ import { type Address, type TransactionReceipt } from "viem";
2
+ import { type Addresses } from "./addresses.js";
3
+ import { type Ctx, type TxRequest } from "./tx.js";
4
+ /** A buy through the V3 SwapRouter, BNB in (the router wraps it). */
5
+ export declare function buildV3Buy(a: Addresses, b: {
6
+ token: Address;
7
+ bnbIn: bigint;
8
+ minOut: bigint;
9
+ recipient: Address;
10
+ deadline?: bigint;
11
+ fee?: number;
12
+ gasPct?: number;
13
+ }): TxRequest;
14
+ /** A sell through the V3 SwapRouter for native BNB: swap to the router, unwrap to the seller. Needs the coin approved to the router. */
15
+ export declare function buildV3Sell(a: Addresses, s: {
16
+ token: Address;
17
+ amount: bigint;
18
+ minOut: bigint;
19
+ recipient: Address;
20
+ deadline?: bigint;
21
+ fee?: number;
22
+ gasPct?: number;
23
+ }): TxRequest;
24
+ /** What a trade gets in the V3 pool, from the QuoterV2 (it runs the swap, so a coin's refusing rule fails it: raw error). */
25
+ export declare function v3Quote(ctx: Ctx, q: {
26
+ token: Address;
27
+ side: "buy" | "sell";
28
+ amount: bigint;
29
+ fee?: number;
30
+ }): Promise<bigint>;
31
+ /** The approval of `spender` (default the V3 router) a sell needs, or null when it is in place. */
32
+ export declare function approvalFor(ctx: Ctx, owner: Address, token: Address, amount: bigint, spender?: Address, label?: string): Promise<TxRequest | null>;
33
+ /** Coins of `token` that reached `to` in a receipt (its Transfer events). */
34
+ export declare function receivedOf(receipt: TransactionReceipt, token: Address, to: Address): bigint;
35
+ /**
36
+ * Coins a buy of `bnbIn` would get from a V3 pool, estimated as a constant product on the pool's active liquidity at its
37
+ * current price, the fee taken off the input. PancakeSwap's QuoterV2 cannot run a buy a coin's rule refuses, and it does
38
+ * not pass the coin's error on ("Unexpected error"), so this estimate is what names the rule then. Never use it as a minOut.
39
+ */
40
+ export declare function v3EstimateBuy(ctx: Ctx, pool: Address, token: Address, bnbIn: bigint, fee?: number): Promise<bigint>;
41
+ /** BNB per coin at a V3 pool's sqrt price (V3 prices token1 per token0). */
42
+ export declare function v3PriceBnb(sqrtPriceX96: bigint, token: Address, wbnb: Address): number;
package/dist/v3.js ADDED
@@ -0,0 +1,98 @@
1
+ import { encodeFunctionData, isAddressEqual, maxUint256, parseAbi, parseEventLogs } from "viem";
2
+ import { erc20Abi, v3QuoterAbi, v3RouterAbi } from "./abis/common.js";
3
+ import { LIMITS } from "./addresses.js";
4
+ import { deadlineIn } from "./tx.js";
5
+ /**
6
+ * PancakeSwap V3, shared by the products whose coins trade in a V3 pool of 1 % (coins with rules in the coin, v1 and v2,
7
+ * and sticky coins): the SwapRouter's buy and sell, the QuoterV2, the sell approval, and what a receipt delivered.
8
+ */
9
+ const transferAbi = [
10
+ {
11
+ type: "event",
12
+ name: "Transfer",
13
+ inputs: [
14
+ { name: "from", type: "address", indexed: true },
15
+ { name: "to", type: "address", indexed: true },
16
+ { name: "value", type: "uint256", indexed: false },
17
+ ],
18
+ anonymous: false,
19
+ },
20
+ ];
21
+ /** A buy through the V3 SwapRouter, BNB in (the router wraps it). */
22
+ export function buildV3Buy(a, b) {
23
+ return {
24
+ to: a.v3Router,
25
+ value: b.bnbIn,
26
+ data: encodeFunctionData({
27
+ abi: v3RouterAbi,
28
+ functionName: "exactInputSingle",
29
+ args: [{ tokenIn: a.wbnb, tokenOut: b.token, fee: b.fee ?? LIMITS.stickyPoolFee, recipient: b.recipient, deadline: b.deadline ?? deadlineIn(), amountIn: b.bnbIn, amountOutMinimum: b.minOut, sqrtPriceLimitX96: 0n }],
30
+ }),
31
+ gasPct: b.gasPct,
32
+ label: "The buy",
33
+ };
34
+ }
35
+ /** A sell through the V3 SwapRouter for native BNB: swap to the router, unwrap to the seller. Needs the coin approved to the router. */
36
+ export function buildV3Sell(a, s) {
37
+ const swap = encodeFunctionData({
38
+ abi: v3RouterAbi,
39
+ functionName: "exactInputSingle",
40
+ args: [{ tokenIn: s.token, tokenOut: a.wbnb, fee: s.fee ?? LIMITS.stickyPoolFee, recipient: a.v3Router, deadline: s.deadline ?? deadlineIn(), amountIn: s.amount, amountOutMinimum: s.minOut, sqrtPriceLimitX96: 0n }],
41
+ });
42
+ const unwrap = encodeFunctionData({ abi: v3RouterAbi, functionName: "unwrapWETH9", args: [s.minOut, s.recipient] });
43
+ return { to: a.v3Router, value: 0n, data: encodeFunctionData({ abi: v3RouterAbi, functionName: "multicall", args: [[swap, unwrap]] }), gasPct: s.gasPct, label: "The sell" };
44
+ }
45
+ /** What a trade gets in the V3 pool, from the QuoterV2 (it runs the swap, so a coin's refusing rule fails it: raw error). */
46
+ export async function v3Quote(ctx, q) {
47
+ const buy = q.side === "buy";
48
+ const r = await ctx.pub.simulateContract({
49
+ address: ctx.a.v3Quoter,
50
+ abi: v3QuoterAbi,
51
+ functionName: "quoteExactInputSingle",
52
+ args: [{ tokenIn: buy ? ctx.a.wbnb : q.token, tokenOut: buy ? q.token : ctx.a.wbnb, amountIn: q.amount, fee: q.fee ?? LIMITS.stickyPoolFee, sqrtPriceLimitX96: 0n }],
53
+ });
54
+ return r.result[0];
55
+ }
56
+ /** The approval of `spender` (default the V3 router) a sell needs, or null when it is in place. */
57
+ export async function approvalFor(ctx, owner, token, amount, spender = ctx.a.v3Router, label = "The approval") {
58
+ const have = await ctx.pub.readContract({ address: token, abi: erc20Abi, functionName: "allowance", args: [owner, spender] });
59
+ if (have >= amount)
60
+ return null;
61
+ return { to: token, value: 0n, data: encodeFunctionData({ abi: erc20Abi, functionName: "approve", args: [spender, maxUint256] }), label };
62
+ }
63
+ /** Coins of `token` that reached `to` in a receipt (its Transfer events). */
64
+ export function receivedOf(receipt, token, to) {
65
+ const logs = parseEventLogs({ abi: transferAbi, logs: receipt.logs.filter((l) => isAddressEqual(l.address, token)), eventName: "Transfer" });
66
+ return logs.filter((l) => isAddressEqual(l.args.to, to)).reduce((x, l) => x + l.args.value, 0n);
67
+ }
68
+ const poolStateAbi = parseAbi([
69
+ "function slot0() view returns (uint160 sqrtPriceX96, int24 tick, uint16, uint16, uint16, uint32, bool)",
70
+ "function liquidity() view returns (uint128)",
71
+ ]);
72
+ /**
73
+ * Coins a buy of `bnbIn` would get from a V3 pool, estimated as a constant product on the pool's active liquidity at its
74
+ * current price, the fee taken off the input. PancakeSwap's QuoterV2 cannot run a buy a coin's rule refuses, and it does
75
+ * not pass the coin's error on ("Unexpected error"), so this estimate is what names the rule then. Never use it as a minOut.
76
+ */
77
+ export async function v3EstimateBuy(ctx, pool, token, bnbIn, fee = LIMITS.stickyPoolFee) {
78
+ const [slot0, liquidity] = await Promise.all([
79
+ ctx.pub.readContract({ address: pool, abi: poolStateAbi, functionName: "slot0" }),
80
+ ctx.pub.readContract({ address: pool, abi: poolStateAbi, functionName: "liquidity" }),
81
+ ]);
82
+ const sqrtP = slot0[0];
83
+ if (sqrtP === 0n || liquidity === 0n)
84
+ return 0n;
85
+ const Q96 = 1n << 96n;
86
+ const r0 = (liquidity * Q96) / sqrtP;
87
+ const r1 = (liquidity * sqrtP) / Q96;
88
+ const [coins, bnb] = token.toLowerCase() < ctx.a.wbnb.toLowerCase() ? [r0, r1] : [r1, r0];
89
+ const dIn = (bnbIn * BigInt(1_000_000 - fee)) / 1000000n;
90
+ return (coins * dIn) / (bnb + dIn);
91
+ }
92
+ /** BNB per coin at a V3 pool's sqrt price (V3 prices token1 per token0). */
93
+ export function v3PriceBnb(sqrtPriceX96, token, wbnb) {
94
+ const p = (Number(sqrtPriceX96) / 2 ** 96) ** 2;
95
+ if (p === 0)
96
+ return 0;
97
+ return token.toLowerCase() < wbnb.toLowerCase() ? p : 1 / p;
98
+ }
package/package.json CHANGED
@@ -1,6 +1,60 @@
1
1
  {
2
2
  "name": "@pickhooks/sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "Read, quote, trade and launch on pickhooks (BNB Chain) from a bot, a launchpad or a trading terminal. Built on viem.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ },
14
+ "./abis": {
15
+ "types": "./dist/abis/index.d.ts",
16
+ "import": "./dist/abis/index.js"
17
+ },
18
+ "./rules": {
19
+ "types": "./dist/sticky-rules.d.ts",
20
+ "import": "./dist/sticky-rules.js"
21
+ },
22
+ "./coin-rules": {
23
+ "types": "./dist/coin-rules.d.ts",
24
+ "import": "./dist/coin-rules.js"
25
+ }
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "README.md"
30
+ ],
31
+ "sideEffects": false,
32
+ "scripts": {
33
+ "abis": "node scripts/gen-abis.mjs",
34
+ "build": "node scripts/gen-abis.mjs && tsc -p tsconfig.json",
35
+ "check": "node scripts/check-rules-copy.mjs",
36
+ "test": "node --test test/unit.test.mjs",
37
+ "fork-proof": "node test/fork-proof.mjs",
38
+ "fork-proof-2": "node test/fork-proof-2.mjs",
39
+ "fork-proof-infra": "node test/fork-proof-infra.mjs",
40
+ "release": "node scripts/publish.mjs"
41
+ },
42
+ "peerDependencies": {
43
+ "viem": "^2.0.0"
44
+ },
45
+ "devDependencies": {
46
+ "typescript": "^5.9.3",
47
+ "viem": "^2.57.2"
48
+ },
49
+ "keywords": [
50
+ "pickhooks",
51
+ "bnb-chain",
52
+ "pancakeswap",
53
+ "infinity",
54
+ "hooks",
55
+ "viem"
56
+ ],
57
+ "engines": {
58
+ "node": ">=20"
59
+ }
60
+ }