|
1 | 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
2 | 2 | import { HashLock } from '../hashlock.js'; |
3 | 3 | import { GraphQLError, AuthError, NetworkError } from '../errors.js'; |
| 4 | +import { meetsKycTier, KYC_TIER_RANK } from '../principal.js'; |
| 5 | +import type { PrincipalAttestation } from '../principal.js'; |
4 | 6 |
|
5 | 7 | function mockFetch(data: unknown, status = 200) { |
6 | 8 | return vi.fn().mockResolvedValue({ |
@@ -228,4 +230,168 @@ describe('HashLock SDK', () => { |
228 | 230 | expect(callArgs[1].headers['Authorization']).toBe('Bearer new-token'); |
229 | 231 | }); |
230 | 232 | }); |
| 233 | + |
| 234 | + // ─── Principal / Attestation (EXPERIMENTAL) ───────────── |
| 235 | + |
| 236 | + describe('principal + attestation types', () => { |
| 237 | + const validAttestation: PrincipalAttestation = { |
| 238 | + principalId: 'pr_acme_001', |
| 239 | + principalType: 'INSTITUTION', |
| 240 | + tier: 'INSTITUTIONAL', |
| 241 | + blindId: 'ag_5g7k92bq', |
| 242 | + issuedAt: Math.floor(Date.now() / 1000), |
| 243 | + expiresAt: Math.floor(Date.now() / 1000) + 3600, |
| 244 | + proof: '0xdeadbeef', |
| 245 | + }; |
| 246 | + |
| 247 | + it('KycTier helpers rank tiers correctly', () => { |
| 248 | + expect(KYC_TIER_RANK.NONE).toBe(0); |
| 249 | + expect(KYC_TIER_RANK.INSTITUTIONAL).toBe(4); |
| 250 | + expect(meetsKycTier('ENHANCED', 'STANDARD')).toBe(true); |
| 251 | + expect(meetsKycTier('BASIC', 'ENHANCED')).toBe(false); |
| 252 | + }); |
| 253 | + |
| 254 | + it('createRFQ accepts attestation + agentInstance + tier filters', async () => { |
| 255 | + const rfq = { |
| 256 | + id: 'rfq-agent', |
| 257 | + baseToken: 'ETH', |
| 258 | + quoteToken: 'USDT', |
| 259 | + side: 'SELL', |
| 260 | + amount: '5.0', |
| 261 | + status: 'ACTIVE', |
| 262 | + isBlind: true, |
| 263 | + createdAt: '2026-04-11', |
| 264 | + userId: 'u1', |
| 265 | + expiresAt: null, |
| 266 | + quotesCount: 0, |
| 267 | + }; |
| 268 | + const fetch = mockFetch({ data: { createRFQ: rfq } }); |
| 269 | + const hl = createClient(fetch); |
| 270 | + |
| 271 | + // Type-level acceptance: the SDK compiles and runs with the |
| 272 | + // new fields. GraphQL wire-through is a later release. |
| 273 | + const result = await hl.createRFQ({ |
| 274 | + baseToken: 'ETH', |
| 275 | + quoteToken: 'USDT', |
| 276 | + side: 'SELL', |
| 277 | + amount: '5.0', |
| 278 | + isBlind: true, |
| 279 | + attestation: validAttestation, |
| 280 | + agentInstance: { |
| 281 | + instanceId: 'ag_5g7k92bq', |
| 282 | + strategy: 'mm-eth-usdt', |
| 283 | + version: '1.0.0', |
| 284 | + }, |
| 285 | + minCounterpartyTier: 'STANDARD', |
| 286 | + hideIdentity: true, |
| 287 | + }); |
| 288 | + |
| 289 | + expect(result.id).toBe('rfq-agent'); |
| 290 | + }); |
| 291 | + |
| 292 | + it('submitQuote accepts attestation + agentInstance + hideIdentity', async () => { |
| 293 | + const quote = { |
| 294 | + id: 'q-agent', |
| 295 | + rfqId: 'rfq-1', |
| 296 | + marketMakerId: 'mm-1', |
| 297 | + price: '3450', |
| 298 | + amount: '1.0', |
| 299 | + status: 'PENDING', |
| 300 | + createdAt: '2026-04-11', |
| 301 | + expiresAt: null, |
| 302 | + }; |
| 303 | + const fetch = mockFetch({ data: { submitQuote: quote } }); |
| 304 | + const hl = createClient(fetch); |
| 305 | + |
| 306 | + const result = await hl.submitQuote({ |
| 307 | + rfqId: 'rfq-1', |
| 308 | + price: '3450', |
| 309 | + amount: '1.0', |
| 310 | + attestation: validAttestation, |
| 311 | + agentInstance: { instanceId: 'ag_5g7k92bq' }, |
| 312 | + hideIdentity: true, |
| 313 | + }); |
| 314 | + |
| 315 | + expect(result.id).toBe('q-agent'); |
| 316 | + }); |
| 317 | + |
| 318 | + it('fundHTLC accepts attestation + agentInstance', async () => { |
| 319 | + const fetch = mockFetch({ |
| 320 | + data: { fundHTLC: { tradeId: 't-1', txHash: '0xabc', status: 'PENDING' } }, |
| 321 | + }); |
| 322 | + const hl = createClient(fetch); |
| 323 | + |
| 324 | + const result = await hl.fundHTLC({ |
| 325 | + tradeId: 't-1', |
| 326 | + txHash: '0xabc', |
| 327 | + role: 'INITIATOR', |
| 328 | + chainType: 'evm', |
| 329 | + attestation: validAttestation, |
| 330 | + agentInstance: { instanceId: 'ag_5g7k92bq' }, |
| 331 | + }); |
| 332 | + |
| 333 | + expect(result.status).toBe('PENDING'); |
| 334 | + }); |
| 335 | + |
| 336 | + it('existing calls without attestation still work (backward compat)', async () => { |
| 337 | + const rfq = { |
| 338 | + id: 'rfq-human', |
| 339 | + baseToken: 'ETH', |
| 340 | + quoteToken: 'USDT', |
| 341 | + side: 'BUY', |
| 342 | + amount: '1.0', |
| 343 | + status: 'ACTIVE', |
| 344 | + isBlind: false, |
| 345 | + createdAt: '2026-04-11', |
| 346 | + userId: 'u1', |
| 347 | + expiresAt: null, |
| 348 | + quotesCount: 0, |
| 349 | + }; |
| 350 | + const fetch = mockFetch({ data: { createRFQ: rfq } }); |
| 351 | + const hl = createClient(fetch); |
| 352 | + |
| 353 | + const result = await hl.createRFQ({ |
| 354 | + baseToken: 'ETH', |
| 355 | + quoteToken: 'USDT', |
| 356 | + side: 'BUY', |
| 357 | + amount: '1.0', |
| 358 | + }); |
| 359 | + |
| 360 | + expect(result.id).toBe('rfq-human'); |
| 361 | + }); |
| 362 | + |
| 363 | + it('parses attestation tier fields from RFQ responses', async () => { |
| 364 | + const rfq = { |
| 365 | + id: 'rfq-1', |
| 366 | + userId: 'u1', |
| 367 | + baseToken: 'ETH', |
| 368 | + quoteToken: 'USDT', |
| 369 | + side: 'SELL', |
| 370 | + amount: '5', |
| 371 | + status: 'ACTIVE', |
| 372 | + isBlind: true, |
| 373 | + createdAt: '2026-04-11', |
| 374 | + expiresAt: null, |
| 375 | + quotesCount: 0, |
| 376 | + attestationTier: 'INSTITUTIONAL', |
| 377 | + attestationBlindId: 'ag_5g7k92bq', |
| 378 | + minCounterpartyTier: 'STANDARD', |
| 379 | + }; |
| 380 | + const fetch = mockFetch({ data: { createRFQ: rfq } }); |
| 381 | + const hl = createClient(fetch); |
| 382 | + |
| 383 | + const result = await hl.createRFQ({ |
| 384 | + baseToken: 'ETH', |
| 385 | + quoteToken: 'USDT', |
| 386 | + side: 'SELL', |
| 387 | + amount: '5', |
| 388 | + }); |
| 389 | + |
| 390 | + // Type is `KycTier | null | undefined`; the JSON response is |
| 391 | + // assignable (TS response types are a loose projection). |
| 392 | + expect(result.attestationTier).toBe('INSTITUTIONAL'); |
| 393 | + expect(result.attestationBlindId).toBe('ag_5g7k92bq'); |
| 394 | + expect(result.minCounterpartyTier).toBe('STANDARD'); |
| 395 | + }); |
| 396 | + }); |
231 | 397 | }); |
0 commit comments