Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/veria-140-charge-default-constraints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'mppx': patch
---

Preserved charge supported modes and split payment defaults in challenges.
69 changes: 69 additions & 0 deletions src/tempo/server/Charge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3654,6 +3654,75 @@ describe('tempo', () => {
})
expect(challenge.request.methodDetails?.supportedModes).toEqual(['pull'])
})

test('challenge contains supportedModes from method defaults', async () => {
const handler = Mppx_server.create({
methods: [
tempo_server.charge({
getClient: () => client,
account: accounts[0].address,
currency: asset,
supportedModes: ['pull'],
}),
],
realm,
secretKey,
})

const result = await handler.charge({ amount: '1' })(new Request('https://example.com'))
expect(result.status).toBe(402)
if (result.status !== 402) throw new Error()

const challenge = Challenge.fromResponse(result.challenge, {
methods: [tempo_client.charge()],
})
expect(challenge.request.methodDetails?.supportedModes).toEqual(['pull'])
})

test('stable binding contains supportedModes', () => {
const method = tempo_server.charge({
getClient: () => client,
account: accounts[0].address,
currency: asset,
})
const request = method.schema.request.parse({
amount: '1',
currency: asset,
decimals: 6,
supportedModes: ['pull'],
})

expect(method.stableBinding?.(request)).toMatchObject({
supportedModes: ['pull'],
})
})

test('challenge contains splits from method defaults', async () => {
const split = { amount: '0.2', recipient: accounts[2].address }
const handler = Mppx_server.create({
methods: [
tempo_server.charge({
getClient: () => client,
account: accounts[0].address,
currency: asset,
splits: [split],
}),
],
realm,
secretKey,
})

const result = await handler.charge({ amount: '1' })(new Request('https://example.com'))
expect(result.status).toBe(402)
if (result.status !== 402) throw new Error()

const challenge = Challenge.fromResponse(result.challenge, {
methods: [tempo_client.charge()],
})
expect(challenge.request.methodDetails?.splits).toEqual([
{ amount: '200000', recipient: split.recipient },
])
})
})

describe('attribution memo', () => {
Expand Down
32 changes: 32 additions & 0 deletions src/tempo/server/Charge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export function charge<const parameters extends charge.Parameters>(
feePayerPolicy,
html,
memo,
splits,
supportedModes,
validateSender,
waitForConfirmation = true,
} = parameters
Expand Down Expand Up @@ -96,8 +98,12 @@ export function charge<const parameters extends charge.Parameters>(
externalId,
memo,
recipient,
splits,
supportedModes,
} as unknown as Defaults,

stableBinding: chargeBinding,

html: html
? {
config: {},
Expand Down Expand Up @@ -579,6 +585,32 @@ export declare namespace charge {
type FeePayerPolicy = Partial<FeePayer.Policy>
}

type ChargeRequest = z.output<typeof Methods.charge.schema.request>

function chargeBinding(request: ChargeRequest) {
// Exhaustively destructure so new charge request fields require an explicit binding decision.
const { amount, currency, description, externalId, methodDetails, recipient, ...requestRest } =
request
requestRest satisfies Record<string, never>

const { chainId, feePayer, memo, splits, supportedModes, ...methodDetailsRest } =
methodDetails ?? {}
methodDetailsRest satisfies Record<string, never>
void feePayer

return {
amount,
chainId,
currency,
description,
externalId,
memo,
recipient,
splits,
supportedModes,
}
}

type ExpectedTransfer = {
amount: string
allowAnyMemo?: boolean | undefined
Expand Down
Loading