Repository navigation
feat(swap-service): dispute a claim on a transaction an older quote holds #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b3ecd0c
feat(swap-service): dispute a claim on a transaction an older quote h…
kaladinlight fc8dae9
refactor(swap-service): clarify how a claim's verdict is reached
kaladinlight 3bd525d
fix(swap-service): let the oldest claim settle claims accepted before it
kaladinlight 976cf5c
feat(swap-service): refuse a claim an earlier quote has already settled
kaladinlight cd18dab
refactor: describe attribution in the vocabulary the domain already has
kaladinlight 0a166e3
fix(swap-service): contend only with quotes on the same chain
kaladinlight 6fcd135
Revert "feat(swap-service): refuse a claim an earlier quote has alrea…
kaladinlight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
apps/swap-service/src/swaps/__tests__/attribution-contest.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { SwapsService } from '../swaps.service' | ||
| import type { Swap } from '../types' | ||
|
|
||
| // unchained-client is ESM; ts-jest can't transform it and nothing here uses it. | ||
| jest.mock('@shapeshiftoss/unchained-client', () => ({ TxStatus: {} })) | ||
|
|
||
| // contracts reaches lodash-es, which is ESM for the same reason, and only the block time lookup needs it. | ||
| jest.mock('@shapeshiftoss/contracts', () => ({ viemClientByChainId: {} })) | ||
|
|
||
| process.env.NOTIFICATIONS_SERVICE_URL ??= 'http://notifications.test' | ||
| process.env.USER_SERVICE_URL ??= 'http://user.test' | ||
| process.env.SERVICE_API_KEY ??= 'test-api-key' | ||
|
|
||
| const TX = '0xsharedtx' | ||
| const BLOCK_TIME = Math.floor(Date.UTC(2026, 8, 7, 15, 13, 56) / 1000) | ||
|
|
||
| type Claim = { swapId: string; quotedAt: Date } | ||
|
|
||
| const buildService = (claims: Claim[]) => { | ||
| const updates: { swapId: string; data: Record<string, unknown> }[] = [] | ||
| const sweeps: { where: Record<string, unknown>; data: Record<string, unknown> }[] = [] | ||
|
|
||
| const prisma = { | ||
| swap: { | ||
| // mirrors the real predicate: an older quote, or the same quote time with a lower swapId | ||
| findFirst: (args: { | ||
| where: { | ||
| sellAsset: { path: string[]; equals: string } | ||
| OR: [{ quotedAt: { lt: Date } }, { quotedAt: Date; swapId: { lt: string } }] | ||
| } | ||
| }): Promise<{ swapId: string } | null> => { | ||
| const [older, tied] = args.where.OR | ||
| const claim = claims.find( | ||
| (c) => | ||
| c.quotedAt < older.quotedAt.lt || | ||
| (c.quotedAt.getTime() === tied.quotedAt.getTime() && c.swapId < tied.swapId.lt), | ||
| ) | ||
| return Promise.resolve(claim ? { swapId: claim.swapId } : null) | ||
| }, | ||
| update: (args: { where: { swapId: string }; data: Record<string, unknown> }) => { | ||
| updates.push({ swapId: args.where.swapId, data: args.data }) | ||
| return Promise.resolve({ ...args.data, swapId: args.where.swapId }) | ||
| }, | ||
| updateMany: (args: { where: Record<string, unknown>; data: Record<string, unknown> }) => { | ||
| sweeps.push(args) | ||
| return Promise.resolve({ count: 0 }) | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| const blockTime = { lookup: () => Promise.resolve({ blockTime: BLOCK_TIME }) } | ||
| const stub = {} as never | ||
| const service = new SwapsService( | ||
| prisma as never, | ||
| stub, | ||
| stub, | ||
| stub, | ||
| stub, | ||
| stub, | ||
| stub, | ||
| stub, | ||
| stub, | ||
| stub, | ||
| stub, | ||
| blockTime as never, | ||
| ) | ||
|
|
||
| return { service, updates, sweeps } | ||
| } | ||
|
|
||
| const swapOf = (claim: Claim): Swap => | ||
| ({ | ||
| swapId: claim.swapId, | ||
| sellTxHash: TX, | ||
| sellAsset: { chainId: 'eip155:1' }, | ||
| quotedAt: claim.quotedAt, | ||
| status: 'SUCCESS', | ||
| createdAt: claim.quotedAt, | ||
| attributionStatus: 'PENDING', | ||
| attributionDetails: null, | ||
| }) as unknown as Swap | ||
|
|
||
| describe('resolveAttribution contest', () => { | ||
| const first = { swapId: 'first', quotedAt: new Date(Date.UTC(2026, 8, 7, 14, 14, 57)) } | ||
| const second = { swapId: 'second', quotedAt: new Date(Date.UTC(2026, 8, 7, 14, 26, 12)) } | ||
|
|
||
| // both quotes precede the block, so binding accepts each one on its own | ||
| it('accepts the oldest claim on a transaction', async () => { | ||
| const { service, updates } = buildService([first, second]) | ||
|
|
||
| await service.resolveAttribution(swapOf(first)) | ||
|
|
||
| expect(updates[0]?.data).toMatchObject({ | ||
| attributionStatus: 'ACCEPTED', | ||
| attributionDetails: { reason: 'quote-precedes-tx' }, | ||
| }) | ||
| }) | ||
|
|
||
| it('disputes a later claim on a transaction another quote already holds', async () => { | ||
| const { service, updates } = buildService([first, second]) | ||
|
|
||
| await service.resolveAttribution(swapOf(second)) | ||
|
|
||
| expect(updates[0]?.data).toMatchObject({ | ||
| attributionStatus: 'DISPUTED', | ||
| attributionDetails: { checked: true, reason: 'quote-superseded' }, | ||
| }) | ||
| }) | ||
|
|
||
| // a younger claim can be accepted before its older sibling is even registered | ||
| it('demotes a claim already accepted on the transaction when the oldest arrives', async () => { | ||
| const { service, updates, sweeps } = buildService([first, second]) | ||
|
|
||
| await service.resolveAttribution(swapOf(first)) | ||
|
|
||
| expect(updates[0]?.data).toMatchObject({ attributionStatus: 'ACCEPTED' }) | ||
| expect(sweeps[0]).toMatchObject({ | ||
| where: { sellTxHash: TX, swapId: { not: 'first' }, attributionStatus: 'ACCEPTED' }, | ||
| data: { attributionStatus: 'DISPUTED', attributionDetails: { reason: 'quote-superseded' } }, | ||
| }) | ||
| }) | ||
|
|
||
| it('leaves other claims alone when this one loses', async () => { | ||
| const { service, sweeps } = buildService([first, second]) | ||
|
|
||
| await service.resolveAttribution(swapOf(second)) | ||
|
|
||
| expect(sweeps).toHaveLength(0) | ||
| }) | ||
|
|
||
| it('accepts an uncontested claim', async () => { | ||
| const { service, updates } = buildService([second]) | ||
|
|
||
| await service.resolveAttribution(swapOf(second)) | ||
|
|
||
| expect(updates[0]?.data).toMatchObject({ attributionStatus: 'ACCEPTED' }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.