@palindromepay/sdk 1.9.4
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.
- package/LICENCE +21 -0
- package/README.md +344 -0
- package/dist/PalindromeEscrowSDK.d.ts +1217 -0
- package/dist/PalindromeEscrowSDK.js +2710 -0
- package/dist/contract/PalindromeCryptoEscrow.json +1180 -0
- package/dist/contract/PalindromeEscrowWallet.json +306 -0
- package/dist/contract/USDT.json +310 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -0
- package/dist/subgraph/queries.d.ts +21 -0
- package/dist/subgraph/queries.js +466 -0
- package/dist/types/escrow.d.ts +48 -0
- package/dist/types/escrow.js +2 -0
- package/package.json +68 -0
package/LICENCE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Palindrome Finance
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
# PalindromeEscrowSDK
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@palindromecryptoescrow/sdk)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://sdk.palindromefinance.com)
|
|
6
|
+
|
|
7
|
+
A TypeScript/Node SDK for interacting with the PalindromeCryptoEscrow smart contract and subgraph, including signature flows, event utilities, token helpers, and robust error handling.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## π Documentation
|
|
12
|
+
|
|
13
|
+
**Full documentation available at: [sdk.palindromefinance.com](https://sdk.palindromefinance.com)**
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## β¨ Features
|
|
18
|
+
|
|
19
|
+
- Interact with on-chain escrow contracts using public & wallet clients
|
|
20
|
+
- Supports ERC20 deposits, fee calculation, and contract state queries
|
|
21
|
+
- Buyer, Seller, Arbiter rolesβplus dispute/resolve logic
|
|
22
|
+
- Meta-transaction signature helpers (EIP-712 compatible)
|
|
23
|
+
- Auto-release and time/maturity helpers
|
|
24
|
+
- Subgraph GraphQL queries to fetch and filter escrows and dispute messages
|
|
25
|
+
- Flexible error classes and codes for wallet/client state
|
|
26
|
+
- Utilities for fee calculation, token formatting, deadlines, and maturity times
|
|
27
|
+
- Gas estimation helpers for transaction planning
|
|
28
|
+
- Comprehensive state and role validation helpers (v1.9.3+)
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## π¦ Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install @palindromecryptoescrow/sdk
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## π Quick Start
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { PalindromeEscrowSDK } from '@palindromecryptoescrow/sdk';
|
|
42
|
+
import { createPublicClient, createWalletClient, http } from 'viem';
|
|
43
|
+
|
|
44
|
+
// Create clients
|
|
45
|
+
const publicClient = createPublicClient({
|
|
46
|
+
chain: yourChain,
|
|
47
|
+
transport: http()
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const walletClient = createWalletClient({
|
|
51
|
+
chain: yourChain,
|
|
52
|
+
transport: http(),
|
|
53
|
+
account: yourAccount
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Initialize SDK
|
|
57
|
+
const sdk = new PalindromeEscrowSDK({
|
|
58
|
+
publicClient,
|
|
59
|
+
walletClient,
|
|
60
|
+
contractAddress: '0x...',
|
|
61
|
+
subgraphUrl: 'https://...'
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Create an escrow
|
|
65
|
+
const escrow = await sdk.createEscrow({
|
|
66
|
+
token: '0x...', // ERC20 token address
|
|
67
|
+
buyer: '0x...', // Buyer address
|
|
68
|
+
amount: 1000000n, // Amount in smallest unit
|
|
69
|
+
maturityDays: 7n, // 7 days until auto-release
|
|
70
|
+
arbiter: '0x...', // Optional arbiter
|
|
71
|
+
title: 'Purchase of Product X',
|
|
72
|
+
ipfsHash: 'Qm...' // Optional IPFS metadata
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## β οΈ Token Compatibility
|
|
79
|
+
|
|
80
|
+
**CRITICAL: Not all ERC20 tokens are compatible with this escrow system.**
|
|
81
|
+
|
|
82
|
+
### β
Supported Tokens
|
|
83
|
+
|
|
84
|
+
The escrow contract works correctly with **standard ERC20 tokens** that:
|
|
85
|
+
- Transfer the exact amount specified (no fees on transfer)
|
|
86
|
+
- Have fixed supply (no rebasing)
|
|
87
|
+
- Cannot be paused by admin once deposited
|
|
88
|
+
- Do not have address blocklists
|
|
89
|
+
|
|
90
|
+
**Examples of Safe Tokens:**
|
|
91
|
+
- DAI
|
|
92
|
+
- WETH
|
|
93
|
+
- Standard ERC20 tokens without special features
|
|
94
|
+
|
|
95
|
+
### β Unsupported Tokens
|
|
96
|
+
|
|
97
|
+
**1. Fee-on-Transfer Tokens**
|
|
98
|
+
- **What:** Tokens that deduct a fee during transfers
|
|
99
|
+
- **Why Unsafe:** Escrow will receive less than the deposit amount, breaking accounting
|
|
100
|
+
- **Examples:** SafeMoon, PAXG, some tax tokens
|
|
101
|
+
- **Risk Level:** π΄ CRITICAL - Will cause fund loss
|
|
102
|
+
|
|
103
|
+
**2. Rebasing Tokens**
|
|
104
|
+
- **What:** Tokens whose balance changes automatically over time
|
|
105
|
+
- **Why Unsafe:** Escrow balance will change unexpectedly, breaking release amounts
|
|
106
|
+
- **Examples:** stETH, AMPL, OHM
|
|
107
|
+
- **Risk Level:** π΄ CRITICAL - Unpredictable outcomes
|
|
108
|
+
|
|
109
|
+
**3. Pausable Tokens**
|
|
110
|
+
- **What:** Tokens that can be paused by admin, freezing all transfers
|
|
111
|
+
- **Why Unsafe:** Escrow may become permanently locked if token is paused
|
|
112
|
+
- **Examples:** USDC, USDT (admin can pause)
|
|
113
|
+
- **Risk Level:** π‘ MEDIUM - Temporary freeze possible
|
|
114
|
+
- **Mitigation:** Only use with trusted token issuers
|
|
115
|
+
|
|
116
|
+
**4. Blocklist Tokens**
|
|
117
|
+
- **What:** Tokens where admin can blocklist specific addresses
|
|
118
|
+
- **Why Unsafe:** Escrow wallet could be blocklisted, freezing funds
|
|
119
|
+
- **Examples:** USDC, USDT (admin can blocklist)
|
|
120
|
+
- **Risk Level:** π‘ MEDIUM - Funds could be frozen
|
|
121
|
+
- **Mitigation:** Only use with trusted token issuers
|
|
122
|
+
|
|
123
|
+
### π How to Check Token Compatibility
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
// Use the SDK to verify token decimals and basic functionality
|
|
127
|
+
const decimals = await sdk.getTokenDecimals(tokenAddress);
|
|
128
|
+
const balance = await sdk.getTokenBalance(userAddress, tokenAddress);
|
|
129
|
+
|
|
130
|
+
// For production, additionally check:
|
|
131
|
+
// 1. Token contract source code for transfer fees
|
|
132
|
+
// 2. Token documentation for rebasing mechanics
|
|
133
|
+
// 3. Token admin capabilities (pause, blocklist)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**Best Practice:** Always test with small amounts first on mainnet or use testnet for new tokens.
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## π Rate Limiting Best Practices
|
|
141
|
+
|
|
142
|
+
### RPC Provider Limits
|
|
143
|
+
|
|
144
|
+
The SDK makes multiple RPC calls for operations like:
|
|
145
|
+
- Creating escrows (3-5 calls)
|
|
146
|
+
- Fetching multiple nonces (1-20+ calls depending on count)
|
|
147
|
+
- Batch operations (N calls per item)
|
|
148
|
+
|
|
149
|
+
**Recommended RPC Providers & Limits:**
|
|
150
|
+
|
|
151
|
+
| Provider | Free Tier | Recommended Plan | Notes |
|
|
152
|
+
|----------|-----------|------------------|-------|
|
|
153
|
+
| Alchemy | 300M CU/month | Growth ($49/mo) | Best for production |
|
|
154
|
+
| Infura | 100k req/day | Developer ($50/mo) | Good reliability |
|
|
155
|
+
| QuickNode | 10M credits | Pro ($49/mo) | Low latency |
|
|
156
|
+
|
|
157
|
+
### SDK Rate Limiting Features
|
|
158
|
+
|
|
159
|
+
The SDK includes built-in optimizations:
|
|
160
|
+
- **Multicall support:** Batches multiple reads into one RPC call (when supported)
|
|
161
|
+
- **Caching:** Fee receiver, decimals, multicall support cached
|
|
162
|
+
- **Efficient nonce fetching:** Automatically uses multicall for bulk nonce queries
|
|
163
|
+
|
|
164
|
+
### Implementing Your Own Rate Limiting
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
// Example: Rate limit with p-queue
|
|
168
|
+
import PQueue from 'p-queue';
|
|
169
|
+
|
|
170
|
+
const queue = new PQueue({
|
|
171
|
+
concurrency: 5, // Max 5 concurrent requests
|
|
172
|
+
interval: 1000, // Per second
|
|
173
|
+
intervalCap: 10 // Max 10 requests per interval
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// Wrap SDK calls
|
|
177
|
+
const escrow = await queue.add(() =>
|
|
178
|
+
sdk.getEscrowByIdParsed(escrowId)
|
|
179
|
+
);
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Monitoring RPC Usage
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
// Enable debug logging to monitor RPC calls
|
|
186
|
+
const sdk = new PalindromeEscrowSDK({
|
|
187
|
+
// ... other config
|
|
188
|
+
logger: customLogger, // Implement custom logger to track calls
|
|
189
|
+
logLevel: 'debug'
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
**Production Tip:** Use a dedicated RPC endpoint with higher limits for user-facing applications.
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## β½ Gas Estimation
|
|
198
|
+
|
|
199
|
+
The SDK provides gas estimation helpers to help users plan transaction costs:
|
|
200
|
+
|
|
201
|
+
### Estimating Gas for Operations
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
// Estimate gas for creating an escrow
|
|
205
|
+
const gasEstimate = await sdk.estimateGasForCreateEscrow({
|
|
206
|
+
token: tokenAddress,
|
|
207
|
+
buyer: buyerAddress,
|
|
208
|
+
amount: 1000000n,
|
|
209
|
+
maturityDays: 7n,
|
|
210
|
+
arbiter: arbiterAddress,
|
|
211
|
+
title: 'Test Escrow',
|
|
212
|
+
ipfsHash: ''
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
console.log(`Estimated gas: ${gasEstimate.gasLimit}`);
|
|
216
|
+
console.log(`Estimated cost: ${gasEstimate.estimatedCostWei} wei`);
|
|
217
|
+
console.log(`Estimated cost: ${gasEstimate.estimatedCostEth} ETH`);
|
|
218
|
+
|
|
219
|
+
// Estimate gas for deposit
|
|
220
|
+
const depositGas = await sdk.estimateGasForDeposit(escrowId);
|
|
221
|
+
|
|
222
|
+
// Estimate gas for confirm delivery
|
|
223
|
+
const confirmGas = await sdk.estimateGasForConfirmDelivery(escrowId);
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### Gas Price Information
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
// Get current gas prices
|
|
230
|
+
const gasPrice = await sdk.getCurrentGasPrice();
|
|
231
|
+
console.log(`Current gas price: ${gasPrice.standard} gwei`);
|
|
232
|
+
console.log(`Fast: ${gasPrice.fast} gwei`);
|
|
233
|
+
console.log(`Instant: ${gasPrice.instant} gwei`);
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### Setting Custom Gas Limits
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
const sdk = new PalindromeEscrowSDK({
|
|
240
|
+
// ... other config
|
|
241
|
+
defaultGasLimit: 500000n // Custom gas limit for all transactions
|
|
242
|
+
});
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## π‘οΈ Security Best Practices
|
|
248
|
+
|
|
249
|
+
1. **Always validate addresses** before creating escrows
|
|
250
|
+
2. **Use arbiter for high-value transactions**
|
|
251
|
+
3. **Set appropriate maturity times** (7-30 days recommended)
|
|
252
|
+
4. **Test with small amounts first**
|
|
253
|
+
5. **Verify token compatibility** before using (see Token Compatibility section)
|
|
254
|
+
6. **Monitor for pausable/blocklist tokens**
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
## π API Reference
|
|
259
|
+
|
|
260
|
+
### Helper Methods (New in v1.9.3)
|
|
261
|
+
|
|
262
|
+
#### State Validation Helpers
|
|
263
|
+
```typescript
|
|
264
|
+
// Check if user can deposit
|
|
265
|
+
const canDeposit = await sdk.canUserDeposit(userAddress, escrowId);
|
|
266
|
+
|
|
267
|
+
// Check if user can accept escrow
|
|
268
|
+
const canAccept = await sdk.canUserAcceptEscrow(userAddress, escrowId);
|
|
269
|
+
|
|
270
|
+
// Check if user can confirm delivery
|
|
271
|
+
const canConfirm = await sdk.canUserConfirmDelivery(userAddress, escrowId);
|
|
272
|
+
|
|
273
|
+
// Check if user can start dispute
|
|
274
|
+
const canDispute = await sdk.canUserStartDispute(userAddress, escrowId);
|
|
275
|
+
|
|
276
|
+
// Check if escrow can be auto-released
|
|
277
|
+
const canWithdraw = await sdk.canUserWithdraw(escrowId);
|
|
278
|
+
|
|
279
|
+
// Check if seller can auto-release
|
|
280
|
+
const canAutoRelease = await sdk.canSellerAutoRelease(userAddress, escrowId);
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
#### Role Validation Helpers
|
|
284
|
+
```typescript
|
|
285
|
+
const escrow = await sdk.getEscrowByIdParsed(escrowId);
|
|
286
|
+
|
|
287
|
+
// Check roles
|
|
288
|
+
const isBuyer = sdk.isBuyer(userAddress, escrow);
|
|
289
|
+
const isSeller = sdk.isSeller(userAddress, escrow);
|
|
290
|
+
const isArbiter = sdk.isArbiter(userAddress, escrow);
|
|
291
|
+
const hasArbiter = sdk.hasArbiter(escrow);
|
|
292
|
+
|
|
293
|
+
// Compare addresses safely
|
|
294
|
+
const areEqual = sdk.addressEquals(address1, address2);
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
---
|
|
298
|
+
|
|
299
|
+
## π§ͺ Testing
|
|
300
|
+
|
|
301
|
+
```bash
|
|
302
|
+
# Run all tests
|
|
303
|
+
npm test
|
|
304
|
+
|
|
305
|
+
# Run core functionality tests only
|
|
306
|
+
npm run test:core
|
|
307
|
+
|
|
308
|
+
# Run security tests only
|
|
309
|
+
npm run test:security
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
---
|
|
313
|
+
|
|
314
|
+
## π Development
|
|
315
|
+
|
|
316
|
+
```bash
|
|
317
|
+
# Install dependencies
|
|
318
|
+
npm install
|
|
319
|
+
|
|
320
|
+
# Build the project
|
|
321
|
+
npm run build
|
|
322
|
+
|
|
323
|
+
# Clean build artifacts
|
|
324
|
+
npm run clean
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
## π License
|
|
330
|
+
|
|
331
|
+
MIT License - see LICENSE file for details
|
|
332
|
+
|
|
333
|
+
---
|
|
334
|
+
|
|
335
|
+
## π€ Contributing
|
|
336
|
+
|
|
337
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
338
|
+
|
|
339
|
+
---
|
|
340
|
+
|
|
341
|
+
## π Support
|
|
342
|
+
|
|
343
|
+
- GitHub Issues: https://github.com/palindrome-finance/escrow-sdk/issues
|
|
344
|
+
- Documentation: https://github.com/palindrome-finance/escrow-sdk#readme
|