Skip to content
Open
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
1 change: 0 additions & 1 deletion .eslintrc

This file was deleted.

17 changes: 6 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
/node_modules/
/.nyc_output/
/coverage/
**/*.js
**/*.js.map
**/*.d.ts
!example.js
!typedoc.js
!scripts/*
*.log
/integration-test/
.vscode
node_modules
coverage
.nyc_output
doc
build
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
src/test
lib/test
tsconfig.json
tslint.json
typedoc.js
33 changes: 0 additions & 33 deletions example.js

This file was deleted.

55 changes: 55 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { normalizeAmount } = require('../lib/main/types/asset')
const Client = require('../lib/main/client').Client
const { createBackend, createLogger } = require('ilp-module-loader')

;(async () => {
try {
const log = createLogger('app')
const client = new Client()
const paymentPointer = '$twitter.xrptipbot.com/WietseWind'

log.info('Connecting to payment pointer...')
const { amountRequested, reference, remoteName } = await client.connect({
paymentPointer
})

const remote = remoteName || paymentPointer
if (amountRequested) {
log.info(`${remote} requested payment of ${normalizeAmount({
amount: result.amountRequested,
assetInfo: client.remoteAssetInfo,
})}, reference=${reference}`)

// Get sensible exchange rate
const backend = createBackend()
const marketRate = backend.getRate(client.assetInfo, client.remoteAssetInfo)
log.info(`Market rate is ${marketRate.toString()}`)
const currentRate = client.currentRate
log.info(`Rate on connection is ${currentRate.toString()}`)
const sendMax = amountRequested.dividedBy(marketRate).times(1.05)
log.info(`Setting max send amount to ${sendMax.toString()} (based on 5% allowed slippage from market rate)`)

client.pay()
} else {
log.info(`${remote} did not request a specific amount, reference=${reference}`)
}
// Send to Payment Pointer
// const paymentPointer = '$twitter.xrptipbot.com/WietseWind'
// const receipt1 = await pay({ amount: 100, paymentPointer })
// console.log(`Sent ${normalizeAmount(receipt1.sent)} to ${paymentPointer} (${receipt1.destinationAccount}) ` +
// `who received ${normalizeAmount(receipt1.received)}`)

// Create invoice, pay it and wait for payment
// const receiver = await receive(100, 'test-payment-123')
// const [ senderReceipt, [ receiverReceipt, receiverData ] ] = await Promise.all([
// pay(receiver, Buffer.from(JSON.stringify(serializeInvoice(receiver)), 'utf8')),
// receiver.receive(30 * 1000)
// ])
// console.log(`According to sender, sent ${normalizeAmount(senderReceipt.sent)} and receiver got ${normalizeAmount(senderReceipt.received)}`)
// console.log(`According to receiver, got ${normalizeAmount(receiverReceipt.received)} and the following data: ${receiverData.toString('utf8')}`)
process.exit(0)
} catch (error) {
console.error(error)
process.exit(1)
}
})()
12 changes: 12 additions & 0 deletions example/spsp-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { normalizeAmount, Client } = require('../build/lib')

;(async () => {
// This assumes you are running the spsp-server-express.js sample.
const paymentPointer = 'http://localhost:3000?amount=100&reference=INV001'
const client = new Client()
await client.connect({ paymentPointer })

// const receipt = await client.({ amount: 105, paymentPointer })
// console.log(`Sent ${normalizeAmount(receipt.sent)} to ${paymentPointer} (${receipt.destinationAccount}) ` +
// `who received ${normalizeAmount(receipt.received)}`)
})()
32 changes: 32 additions & 0 deletions example/spsp-server-express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { normalizeAmount, express } = require('.')
const app = require('express')()

;(async () => {
const template = {
receiver_info: {
name: 'Bob Smith'
}
}

const paymentHandler = async (receiver) => {
const reference = receiver.reference

console.log(`Waiting for payment: reference=${reference}, maxAmount=${receiver.amount}, timeout=15minutes`)

try {
const receipt = await receiver.receivePayment(15 * 60 * 1000) // 15 minute timeout
const data = await receiver.receiveData()

console.log(`Received payment: reference=${reference}`)
console.log(` - got ${normalizeAmount(receipt.received)}`)
console.log(` - data:${data.toString('utf8')}`)
} catch (e) {
console.error(`Error receiving payment: reference=${reference}`, e)
}
}

app.get('/.well-known/pay', await express.createMiddleware(template, paymentHandler))
app.listen(3000, () => {
console.log('Listening on port 3000...')
})
})()
Loading