This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding in offerings for stored balances * show balances * simplify stored balance * adding in stored balances * add readme on stored balance
- Loading branch information
1 parent
f5cc771
commit 268bf17
Showing
10 changed files
with
428 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,33 @@ | ||
import type { BalancesApi, Quote } from '@tbdex/http-server' | ||
import { Balance } from '@tbdex/http-server' | ||
import { config } from '../config.js' | ||
|
||
let available = '1000' | ||
|
||
export class _BalancesRepository implements BalancesApi { | ||
async getBalances({ requesterDid }): Promise<Balance[]> { | ||
console.log('getBalances for:', requesterDid) | ||
const bal = Balance.create({ | ||
data: { | ||
currencyCode: 'USDC', | ||
available: available, | ||
}, | ||
metadata: { | ||
from: config.pfiDid.uri, | ||
} | ||
}) | ||
return [bal] | ||
} | ||
|
||
withdraw(quote: Quote) { | ||
// substract this from available | ||
available = (parseFloat(available) - parseFloat(quote.data.payout.amount)).toString() | ||
} | ||
|
||
deposit(quote: Quote) { | ||
// add this to available | ||
available = (parseFloat(available) + parseFloat(quote.data.payin.amount)).toString() | ||
} | ||
} | ||
|
||
export const BalancesRepository = new _BalancesRepository() |
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './offering-repository.js' | ||
export * from './exchange-repository.js' | ||
export * from './balances-repository.js' | ||
export * from './postgres.js' |
This file contains 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,140 @@ | ||
import { | ||
TbdexHttpClient, | ||
Rfq, | ||
Quote, | ||
Order, | ||
OrderStatus, | ||
Close, | ||
} from '@tbdex/http-client' | ||
import { createOrLoadDid } from './utils.js' | ||
import { BearerDid } from '@web5/dids' | ||
import fs from 'fs' | ||
|
||
|
||
// load pfiDid from pfiDid.txt | ||
const pfiDid = fs.readFileSync('pfiDid.txt', 'utf-8').trim() | ||
|
||
|
||
const signedCredential = fs.readFileSync('signedCredential.txt', 'utf-8').trim() | ||
|
||
// | ||
// Connect to the PFI and get the list of offerings (offerings are resources - anyone can ask for them) | ||
// | ||
const offerings = await TbdexHttpClient.getOfferings({ pfiDid: pfiDid }) | ||
console.log('got offerings:', JSON.stringify(offerings, null, 2)) | ||
|
||
|
||
// | ||
// Load alice's private key to sign RFQ | ||
// | ||
const alice = await createOrLoadDid('alice.json') | ||
|
||
const [balances] = await TbdexHttpClient.getBalances({ pfiDid: pfiDid, did: alice }) | ||
console.log('got balances:', JSON.stringify(balances, null, 2)) | ||
|
||
// lets make a deposit | ||
let offering = offerings[1] | ||
console.log('deposit offering', offering) | ||
const rfq = Rfq.create({ | ||
metadata: { from: alice.uri, to: pfiDid }, | ||
data: { | ||
offeringId: offerings[1].id, | ||
payin: { | ||
kind: 'WIRE_TRANSFER', | ||
amount: '100.00', | ||
paymentDetails: {}, | ||
}, | ||
payout: { | ||
kind: 'STORED_BALANCE', | ||
paymentDetails: {}, | ||
}, | ||
claims: [], // no claims for now - will add in kcc soon | ||
}, | ||
}) | ||
|
||
await rfq.sign(alice) | ||
|
||
try { | ||
await TbdexHttpClient.createExchange(rfq) | ||
} catch (error) { | ||
console.log('Can\'t create:', error) | ||
} | ||
console.log('sent RFQ: ', JSON.stringify(rfq, null, 2)) | ||
|
||
let quote | ||
|
||
//Wait for Quote message to appear in the exchange | ||
while (!quote) { | ||
const exchange = await TbdexHttpClient.getExchange({ | ||
pfiDid: pfiDid, | ||
did: alice, | ||
exchangeId: rfq.exchangeId | ||
}) | ||
|
||
quote = exchange.find(msg => msg instanceof Quote) | ||
|
||
if (!quote) { | ||
// Wait 2 seconds before making another request | ||
await new Promise(resolve => setTimeout(resolve, 2000)) | ||
} | ||
} | ||
|
||
// | ||
// | ||
// 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: | ||
const exchange = await TbdexHttpClient.getExchange({ | ||
pfiDid: pfiDid, | ||
did: alice, | ||
exchangeId: rfq.exchangeId | ||
}) | ||
|
||
console.log('got exchange:', JSON.stringify(exchange, null, 2)) | ||
|
||
// Place an order against that quote: | ||
const order = Order.create({ | ||
metadata: { | ||
from: alice.uri, | ||
to: pfiDid, | ||
exchangeId: quote.exchangeId | ||
}, | ||
}) | ||
await order.sign(alice) | ||
await TbdexHttpClient.submitOrder(order) | ||
console.log('Sent order: ', JSON.stringify(order, null, 2)) | ||
|
||
await pollForStatus(order, pfiDid, alice) | ||
|
||
|
||
|
||
/* | ||
* This is a very simple polling function that will poll for the status of an order. | ||
*/ | ||
async function pollForStatus(order: Order, pfiDid: string, did: BearerDid) { | ||
let close: Close | ||
while (!close) { | ||
const exchange = await TbdexHttpClient.getExchange({ | ||
pfiDid: pfiDid, | ||
did: did, | ||
exchangeId: order.exchangeId | ||
}) | ||
|
||
for (const message of exchange) { | ||
if (message instanceof OrderStatus) { | ||
console.log('we got a new order status') | ||
const orderStatus = message as OrderStatus | ||
console.log('orderStatus', JSON.stringify(orderStatus, null, 2)) | ||
} else if (message instanceof Close) { | ||
console.log('we have a close message') | ||
close = message as Close | ||
console.log('close', JSON.stringify(close, null, 2)) | ||
return close | ||
} | ||
} | ||
} | ||
} | ||
|
||
const [end_balances] = await TbdexHttpClient.getBalances({ pfiDid: pfiDid, did: alice }) | ||
console.log('starting balances:', JSON.stringify(balances, null, 2)) | ||
console.log('end balances:', JSON.stringify(end_balances, null, 2)) | ||
|
This file contains 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.