mirror of
https://github.com/Permissionless-Software-Foundation/psf-bch-api-base.git
synced 2026-09-21 16:52:00 -07:00
Logging which faciliator was used
This commit is contained in:
@@ -1,6 +1,23 @@
|
|||||||
import { HTTPFacilitatorClient } from '@x402/core/server'
|
import { HTTPFacilitatorClient } from '@x402/core/server'
|
||||||
import { RoundRobinFacilitatorClient } from './round-robin-facilitator-client.js'
|
import { RoundRobinFacilitatorClient } from './round-robin-facilitator-client.js'
|
||||||
|
|
||||||
|
function withSettleLogging (entry) {
|
||||||
|
return {
|
||||||
|
async getSupported () {
|
||||||
|
return entry.client.getSupported()
|
||||||
|
},
|
||||||
|
async verify (paymentPayload, paymentRequirements) {
|
||||||
|
return entry.client.verify(paymentPayload, paymentRequirements)
|
||||||
|
},
|
||||||
|
async settle (paymentPayload, paymentRequirements) {
|
||||||
|
const result = await entry.client.settle(paymentPayload, paymentRequirements)
|
||||||
|
const tx = result?.transaction || 'n/a'
|
||||||
|
console.log(`[x402] payment settled via ${entry.name || entry.key || 'facilitator'} (${entry.url || 'no-url'}) tx=${tx}`)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function createFacilitatorClient (connectionOpts = [], createAuthHeaders, ClientClass = HTTPFacilitatorClient) {
|
export function createFacilitatorClient (connectionOpts = [], createAuthHeaders, ClientClass = HTTPFacilitatorClient) {
|
||||||
const entries = connectionOpts.map(o => ({
|
const entries = connectionOpts.map(o => ({
|
||||||
key: o.key,
|
key: o.key,
|
||||||
@@ -18,7 +35,7 @@ export function createFacilitatorClient (connectionOpts = [], createAuthHeaders,
|
|||||||
|
|
||||||
if (entries.length === 1) {
|
if (entries.length === 1) {
|
||||||
return {
|
return {
|
||||||
client: entries[0].client,
|
client: withSettleLogging(entries[0]),
|
||||||
strategy: 'single'
|
strategy: 'single'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,6 +82,8 @@ export class RoundRobinFacilitatorClient {
|
|||||||
try {
|
try {
|
||||||
const result = await entry.client.settle(paymentPayload, paymentRequirements)
|
const result = await entry.client.settle(paymentPayload, paymentRequirements)
|
||||||
this.cursorByKey.set(key, (idx + 1) % length)
|
this.cursorByKey.set(key, (idx + 1) % length)
|
||||||
|
const tx = result?.transaction || 'n/a'
|
||||||
|
console.log(`[x402] payment settled via ${entry.name || entry.key || `facilitator-${idx}`} (${entry.url || 'no-url'}) tx=${tx}`)
|
||||||
return result
|
return result
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
attempts.push({
|
attempts.push({
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ describe('#facilitator-client-factory', () => {
|
|||||||
const result = createFacilitatorClient(connectionOpts, async () => ({}), FakeFacilitatorClient)
|
const result = createFacilitatorClient(connectionOpts, async () => ({}), FakeFacilitatorClient)
|
||||||
|
|
||||||
assert.equal(result.strategy, 'single')
|
assert.equal(result.strategy, 'single')
|
||||||
assert.instanceOf(result.client, FakeFacilitatorClient)
|
assert.isFunction(result.client.verify)
|
||||||
assert.equal(result.client.opts.url, 'https://cdp.example.com')
|
assert.isFunction(result.client.settle)
|
||||||
|
assert.isFunction(result.client.getSupported)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns round-robin strategy when multiple facilitators are configured', () => {
|
it('returns round-robin strategy when multiple facilitators are configured', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user