-
Notifications
You must be signed in to change notification settings - Fork 22
/
example.js
33 lines (30 loc) · 1.32 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const BigNumber = require('bignumber.js')
const { pay, receive } = require('./src/index')
function normalizeAmount (assetAmount) {
if (assetAmount.assetScale) {
const value = new BigNumber(assetAmount.amount).dividedBy(new BigNumber(10).exponentiatedBy(assetAmount.assetScale))
return `${value.toString()} ${assetAmount.assetCode}`
}
return `${assetAmount.amount} units of unknown asset type and scale`
}
;(async () => {
try {
// 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 ] = await Promise.all([
pay(receiver),
receiver.receivePayment(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)}`)
process.exit(0)
} catch (error) {
console.error(error)
process.exit(1)
}
})()