diff --git a/README.md b/README.md index 23895b6..4648c0b 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ Take the customer DID string and the signed VC string (both long) and run: `npm run example-rfq $pfi_server_did $signed_vc` With the values from step 6 and step 5. -You will see the server print out the interaction between the customer and the PFI. +You will see the server print out the interaction between the customer and the PFI, this will look up offers, ask for a quote, place an order and finally check for status. Each interaction happens in the context of an "Exchange" which is a record of the interaction between the customer and the PFI. diff --git a/src/example/request-for-quote.ts b/src/example/request-for-quote.ts index 078e74d..53e012d 100644 --- a/src/example/request-for-quote.ts +++ b/src/example/request-for-quote.ts @@ -1,4 +1,4 @@ -import { TbdexHttpClient, Rfq } from '@tbdex/http-client' +import { TbdexHttpClient, Rfq, Quote, Order, OrderStatus } from '@tbdex/http-client' import { createOrLoadDid } from './utils.js' // @@ -37,6 +37,8 @@ const kid = alice.document.verificationMethod[0].id // // And here we go with tbdex-protocol! // + +// First, Create an RFQ const rfq = Rfq.create({ metadata: { from: alice.did, to: pfiDid }, data: { @@ -63,13 +65,14 @@ const rfq = Rfq.create({ await rfq.sign(privateKeyJwk, kid) -const rasp = await TbdexHttpClient.sendMessage({ message: rfq }) -console.log('send rfq response', JSON.stringify(rasp, null, 2)) +const resp = await TbdexHttpClient.sendMessage({ message: rfq }) +console.log('send rfq response', JSON.stringify(resp, null, 2)) +console.log(resp) // // // All interaction with the PFI happens in the context of an exchange. -// This is where for example a quote would show up in result to an RFQ. +// This is where for example a quote would show up in result to an RFQ: const exchanges = await TbdexHttpClient.getExchanges({ pfiDid: pfiDid, filter: { id: rfq.exchangeId }, @@ -77,6 +80,55 @@ const exchanges = await TbdexHttpClient.getExchanges({ kid }) -console.log('exchanges', JSON.stringify(exchanges, null, 2)) + +// +// Now lets get the quote out of the returned exchange +// +const [ exchange ] = exchanges.data +for (const message of exchange) { + if (message instanceof Quote) { + console.log('we have a quote!') + const quote = message as Quote + + // Place an order against that quote: + const order = Order.create({ + metadata: { from: alice.did, to: pfiDid, exchangeId: quote.exchangeId }, + }) + await order.sign(privateKeyJwk, kid) + const orderResponse = await TbdexHttpClient.sendMessage({ message: order }) + console.log('orderResponse', orderResponse) + + // finally we poll for response. + const finalState = await pollForStatus(order, pfiDid, privateKeyJwk, kid) + console.log('Final status:', finalState) + } +} + +/* + * This is a very simple polling function that will poll for the status of an order. + */ +async function pollForStatus(order, pfiDid, privateKeyJwk, kid) { + const always = true + while (always) { + const exchanges = await TbdexHttpClient.getExchanges({ + pfiDid: pfiDid, + filter: { id: order.exchangeId }, + privateKeyJwk, + kid + }) + + const [ exchange ] = exchanges.data + + for (const message of exchange) { + if (message instanceof OrderStatus) { + console.log('we have an order status') + const orderStatus = message as OrderStatus + console.log('orderStatus', orderStatus) + return orderStatus + } + } + } +} +