Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
enhance example flow to include placing an order (#2)
Browse files Browse the repository at this point in the history
* is now placing an order

* poll for order status example

* clarify some steps in polling for status

* update readme with order interaction

* typo
  • Loading branch information
michaelneale authored Nov 8, 2023
1 parent 240a3b8 commit 2a0e514
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
62 changes: 57 additions & 5 deletions src/example/request-for-quote.ts
Original file line number Diff line number Diff line change
@@ -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'

//
Expand Down Expand Up @@ -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: {
Expand All @@ -63,20 +65,70 @@ 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 },
privateKeyJwk,
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
}
}
}
}



0 comments on commit 2a0e514

Please sign in to comment.